async function loadBalance() { const r = await api("/wallet/balance?currency=usd"); $("balance").textContent = r.ok ? fmt(r.body.balance, r.body.currency) : "Unavailable"; } async function loadTxs() { const r = await api("/wallet/transactions"); const ul = $("txs"); if (!r.ok) { ul.innerHTML = '
  • Could not load transactions
  • '; return; } if (!r.body.length) { ul.innerHTML = '
  • No transactions yet — add funds or pay an invoice.
  • '; return; } ul.innerHTML = r.body.slice(0, 10).map((t) => { const amt = Math.abs(t.postings[0]?.amount ?? 0); const pos = t.kind === "funding"; return `
  • ${esc(t.memo || t.kind)}${esc(t.kind)} · ${new Date(t.created_at).toLocaleString()}
    ${pos ? "+" : "−"}${fmt(amt)}
  • `; }).join(""); } $("example").addEventListener("click", () => { $("qr").value = JSON.stringify({ v: 1, invoice_id: "INV-" + Date.now().toString(36), merchant_id: "demo-merchant", merchant_name: "Demo Merchant", amount: 500, currency: "usd" }); }); $("payForm").addEventListener("submit", async (e) => { e.preventDefault(); const qr = $("qr").value.trim(); const m = $("payMsg"); if (!qr) return msg(m, "err", "Paste an invoice QR payload first (or click “Fill example”)."); const r = await api("/invoices/pay", { method: "POST", body: { qr } }); if (r.ok) { msg(m, "ok", r.body.status === "already_paid" ? "This invoice was already paid." : `Paid ✓ — new balance ${fmt(r.body.balance)}`); $("qr").value = ""; refresh(); } else msg(m, "err", r.body.error || `Payment failed (${r.status})`); }); $("fund").addEventListener("click", async () => { const m = $("fundMsg"); const amount = Math.round(Number($("fundAmt").value) * 100); if (!(amount >= 50)) return msg(m, "err", "Enter an amount of at least $0.50."); $("fund").disabled = true; const r = await api("/funding/checkout", { method: "POST", body: { amount, currency: "usd" } }); $("fund").disabled = false; if (r.status === 503) { $("testCredit").hidden = false; return msg(m, "warn", "Stripe isn't configured yet."); } if (r.ok && r.body.url) { location.href = r.body.url; return; } msg(m, "err", r.body.error || `Could not start checkout (${r.status})`); }); $("testCreditBtn").addEventListener("click", async () => { const m = $("fundMsg"); const amount = Math.round(Number($("fundAmt").value) * 100); const r = await api("/wallet/test-credit", { method: "POST", body: { amount } }); if (r.status === 401 || r.status === 503) return msg(m, "warn", "Sign in first (Settings page) to add simulated credit."); if (r.ok) { msg(m, "ok", `Added simulated credit ✓ — balance ${fmt(r.body.balance)}`); refresh(); } else msg(m, "err", r.body.error || `Failed (${r.status})`); }); async function loadApps() { const r = await api("/links.json"); const el = $("apps"); if (!r.ok) { el.innerHTML = '

    No apps configured.

    '; return; } el.innerHTML = r.body.apps.map((a) => { const ic = esc(a.name.slice(0, 1)); if (!a.url) return `
    ${ic}
    ${esc(a.name)}Coming soon
    `; const priv = (u) => (u.private ? 'private' : ""); const sec = a.secondary ? `${esc(a.secondary.label)} ↗${priv(a.secondary)}` : ""; return `
    ${ic}
    ${esc(a.name)} ↗${priv(a)}${esc(a.description || "")}
    ${sec}
    `; }).join(""); } async function loadTestCreditHint() { const r = await api("/healthz"); if (r.ok && !r.body.stripe) $("testCredit").hidden = false; } function refresh() { loadBalance(); loadTxs(); loadStatus(); } refresh(); loadApps(); loadTestCreditHint();