/** * Payment API — talks to targo-hub /payments/* endpoints */ const HUB = location.hostname === 'localhost' ? 'http://localhost:3300' : 'https://msg.gigafibre.ca' async function hubGet (path) { const r = await fetch(HUB + path) if (!r.ok) { const data = await r.json().catch(() => ({})) throw new Error(data.error || `Hub ${r.status}`) } return r.json() } async function hubPost (path, body) { const r = await fetch(HUB + path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) const data = await r.json().catch(() => ({})) if (!r.ok) throw new Error(data.error || `Hub ${r.status}`) return data } /** Get customer balance + unpaid invoices */ export const getBalance = (customer) => hubGet(`/payments/balance/${encodeURIComponent(customer)}`) /** Get saved payment methods for customer */ export const getPaymentMethods = (customer) => hubGet(`/payments/methods/${encodeURIComponent(customer)}`) /** Get invoice payment info */ export const getInvoicePaymentInfo = (invoice) => hubGet(`/payments/invoice/${encodeURIComponent(invoice)}`) /** Create checkout session for full balance */ export const checkoutBalance = (customer) => hubPost('/payments/checkout', { customer }) /** Create checkout session for specific invoice */ export const checkoutInvoice = (customer, invoice, { save_card = true, payment_method = 'card' } = {}) => hubPost('/payments/checkout-invoice', { customer, invoice, save_card, payment_method }) /** Save card (setup mode) */ export const setupCard = (customer) => hubPost('/payments/setup', { customer }) /** Open Stripe billing portal */ export const openBillingPortal = (customer) => hubPost('/payments/portal', { customer }) /** Toggle auto-pay (PPA) */ export const togglePPA = (customer, enabled) => hubPost('/payments/toggle-ppa', { customer, enabled })