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'; }; async function loadStripeHistory(stripeCustomerId) { const container = document.getElementById('stripeHistoryContainer'); const tbody = document.querySelector('#stripeHistoryTable tbody'); const cashBadge = document.getElementById('stripeCashBalanceBadge'); const cashText = document.getElementById('stripeCashBalanceText'); try { container.style.display = 'block'; tbody.innerHTML = 'Cargando historial...'; const res = await fetch(`${window.SIIP_STRIPE_PATH || ''}?action=get_stripe_history&customerId=${stripeCustomerId}`); const d = await res.json(); if (d.cashBalance !== undefined) { cashBadge.style.display = 'flex'; cashText.textContent = `Saldo Stripe: $${(d.cashBalance/100).toFixed(2)} MXN`; } if (!d.payments || d.payments.length === 0) { tbody.innerHTML = 'No hay intenciones de pago recientes'; return; } tbody.innerHTML = d.payments.map(p => { let statusColor = 'var(--text-muted)'; let statusLabel = p.status.toUpperCase(); if (p.status === 'succeeded') { statusColor = 'var(--success)'; statusLabel = 'PAGADO'; } if (p.status === 'processing') { statusColor = 'var(--warning)'; statusLabel = 'PENDIENTE'; } return ` ${statusLabel} $${p.amount.toFixed(2)} ${p.description} ${p.date} ${p.id} `; }).join(''); } catch (e) { console.error(e); tbody.innerHTML = 'Error de conexi贸n'; } } function showStripeResult(data) { const c = document.getElementById('stripeResultContent'); let html = `

隆Referencia Creada!

Monto: $${data.amount} MXN

`; if (data.next_action?.display_bank_transfer_instructions) { const instr = data.next_action.display_bank_transfer_instructions; const spei = instr.financial_addresses[0]?.spei; if (spei) { html += `
Instituci贸n Bancaria ${spei.bank_name}
CLABE Interbancaria ${spei.clabe}

Realiza tu transferencia v铆a SPEI con estos datos. El pago se registrar谩 autom谩ticamente al recibirse.

`; } } else { html += `
No se encontr贸 informaci贸n de transferencia. Por favor, revisa el historial.
`; } c.innerHTML = html; document.getElementById('stripeResultModal').style.display = 'flex'; } });