Pagos SPEI

🏦 Genera referencias de transferencia bancaria para que tus clientes paguen vía SPEI

🏦

Selecciona un cliente para generar una referencia de pago SPEI

document.addEventListener('DOMContentLoaded', () => { // 2. STRIPE SEARCH let selectedStripeClient = null; setupSearch('stripeSearch', 'stripeResults', 'search_stripe', async (partialClient) => { const res = await fetch(`${window.SIIP_STRIPE_PATH || ''}?action=get_stripe_details&id=${partialClient.id}`); const data = await res.json(); selectedStripeClient = data; document.getElementById('stripeClientName').textContent = data.fullName; document.getElementById('stripeClientIdDisplay').textContent = `ID: #${data.id}`; document.getElementById('stripeBalanceBadge').textContent = `Saldo: $${parseFloat(data.accountOutstanding||0).toFixed(2)}`; document.getElementById('stripeCustomerIdDisplay').textContent = data.stripeCustomerId || 'No disponible'; document.getElementById('stripeClabeDisplay').textContent = data.clabeInterbancaria || 'No disponible'; document.getElementById('stripeAmount').value = data.accountOutstanding > 0 ? data.accountOutstanding : ''; document.getElementById('btnVerEnCrm').href = `${store.publicUrl}/client/${data.id}`; document.getElementById('stripeDetailContainer').style.display = 'block'; document.getElementById('stripePlaceholder').style.display = 'none'; // Hide placeholder if (data.stripeCustomerId) loadStripeHistory(data.stripeCustomerId); else document.getElementById('stripeHistoryContainer').style.display = 'none'; }); document.getElementById('btnCreateIntent').onclick = async () => { if (!selectedStripeClient?.stripeCustomerId) return showToast('Error: Cliente sin Stripe ID', true); const amt = parseFloat(document.getElementById('stripeAmount').value); if (!amt || amt < 10) return showToast('Mínimo 10 MXN', true); const btn = document.getElementById('btnCreateIntent'); btn.disabled = true; btn.textContent = 'Procesando...'; const fd = new FormData(); fd.append('action', 'create_intent'); fd.append('clientId', selectedStripeClient.id); fd.append('amount', amt); fd.append('stripeCustomerId', selectedStripeClient.stripeCustomerId); fd.append('adminId', document.getElementById('stripeAdminSelect').value || store.defaultStripeAdminId); try { const res = await fetch(`${window.SIIP_STRIPE_PATH || ''}?`, { method: 'POST', body: fd }); const d = await res.json(); if (d.success) showStripeResult(d); else showToast(d.error, true); } catch (e) { showToast('Error de conexión', true); } btn.disabled = false; btn.textContent = 'Generar Referencia SPEI'; }; function showStripeResult(data) { const c = document.getElementById('stripeResultContent'); c.innerHTML = `

¡Referencia Creada!

Monto: $${data.amount}

`; if (data.next_action?.display_bank_transfer_instructions) { const i = data.next_action.display_bank_transfer_instructions.financial_addresses[0].spei; c.innerHTML += `

Banco: ${i.bank_name}

CLABE: ${i.clabe}

`; } document.getElementById('stripeResultModal').style.display = 'flex'; } async function loadOxxoHistory(stripeCustomerId) { const tbody = document.querySelector('#oxxoHistoryTable tbody'); tbody.innerHTML = 'Cargando historial...'; try { const res = await fetch(`${window.SIIP_STRIPE_PATH || ''}?action=get_oxxo_history&stripeCustomerId=${stripeCustomerId}`); const data = await res.json(); if (data.error) { tbody.innerHTML = `Error: ${data.error}`; return; } if (!data.history || data.history.length === 0) { tbody.innerHTML = 'No hay fichas recientes'; return; } tbody.innerHTML = data.history.map(p => { let statusBadge = ''; switch (p.status) { case 'succeeded': statusBadge = 'Pagado'; break; case 'requires_action': statusBadge = 'Pendiente'; break; case 'canceled': statusBadge = 'Cancelado'; break; default: statusBadge = `${p.status}`; } const date = new Date(p.created * 1000).toLocaleString(); const voucherBtn = p.voucherUrl ? `Ver Ficha` : '-'; return ` ${p.id.slice(-8)} ${date} $${p.amount.toFixed(2)} ${p.currency} ${p.oxxoNumber} ${statusBadge} ${voucherBtn} `; }).join(''); } catch (e) { console.error(e); tbody.innerHTML = 'Error de conexión'; } } });