const body = { event_id: evId, ticket_type_id: currentTicket.id, qty: currentTicket.qty, buyer_name: document.getElementById('buyer_name').value, buyer_email: document.getElementById('buyer_email').value }; const res = await fetch(API+'?action=create_order',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)}); const data = await res.json(); if(!data.ok){ alert(data.error||'Falha'); return; } orderId = data.order.id; document.getElementById('amount').textContent = data.order.amount.toFixed(2); document.getElementById('txid').textContent = data.order.txid; document.getElementById('copia').value = data.order.copia_e_cola; document.getElementById('payment').style.display = ''; document.getElementById('status').textContent = 'pending'; // QR Code const el = document.getElementById('qrcode'); el.innerHTML = ''; new QRCode(el, {text: data.order.payload, width: 180, height: 180}); // inicia polling poll(); }); async function poll(){ if(!orderId) return; try{ const r = await fetch(API+`?action=order_status&order_id=${orderId}`); const d = await r.json(); if(d.ok){ document.getElementById('status').textContent = d.status; if(d.status==='paid'){ alert('Pagamento confirmado!'); return; } } }catch(e){} setTimeout(poll, 5000); } function copy(){ const t = document.getElementById('copia'); t.select(); t.setSelectionRange(0, 99999); document.execCommand('copy'); alert('Código Pix copiado.'); } async function check(){ // Força uma consulta imediata de status const r = await fetch(API+`?action=order_status&order_id=${orderId}`); const d = await r.json(); if(d.ok){ document.getElementById('status').textContent = d.status; if(d.status==='paid'){ alert('Pagamento confirmado!'); } } } load();