caelum0x a0ee5a54dc feat(mint): confirm IAP deposits by payer, atomic hold expiry, Iyzico refunds, payout webhooks, typed SDK holds
- RevenueCat/Adapty deposits confirmed via subscriber lookup bound to the
  account (confirm_deposit_for), fail closed on refunds, mint from USD price
- hold expiry written in the same transaction as the hold (memory + Postgres)
- Iyzico V3-signed merchant notifications mapped to refund/dispute reversals;
  hosted checkout-form callback resolves the token server-side
- PayPal/Stripe payout webhooks drive ReserveLedger::apply_payout_update
- mint-sdk: typed HoldReceipt/HoldState, validated capture/release,
  capture_hold_in_full

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 11:08:41 +03:00

1310 lines
70 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex" />
<title>Mint Operator Console</title>
<style>
:root {
--bg: #0f1117; --panel: #181b24; --border: #262b38; --fg: #e6e8ee;
--muted: #8b91a1; --accent: #6ea8fe; --danger: #f2668b; --ok: #55c98a;
--warn: #f0b35a; --row: #1e222d;
}
* { box-sizing: border-box; }
body {
margin: 0; background: var(--bg); color: var(--fg);
font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
header {
display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
position: sticky; top: 0; z-index: 10;
}
header h1 { font-size: 16px; margin: 0; font-weight: 600; letter-spacing: .3px; }
header .spacer { flex: 1; }
nav { display: flex; gap: 4px; }
nav button { background: transparent; border-color: transparent; color: var(--muted); }
nav button.active { color: var(--fg); background: var(--row); border-color: var(--border); }
input, button, select {
font: inherit; color: var(--fg); background: var(--row);
border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px;
}
button { cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: #0b1020; border-color: var(--accent); font-weight: 600; }
button.small { padding: 3px 8px; font-size: 12px; }
#status { font-size: 12px; color: var(--muted); }
#status.err { color: var(--danger); }
#status.ok { color: var(--ok); }
main { padding: 20px; max-width: 1200px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; }
.tile .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.tile .value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 4px; word-break: break-all; }
.tile .value.mono { font: 12.5px ui-monospace, SFMono-Regular, Menlo, monospace; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow: auto; margin-bottom: 18px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
td.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; }
td.num { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--row); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 20px; font-size: 12px; border: 1px solid var(--border); background: var(--row); }
.badge.ok, .badge.paid, .badge.approved { color: var(--ok); border-color: #2c5a41; }
.badge.bad, .badge.failed, .badge.declined { color: var(--danger); border-color: #6a2c3d; }
.badge.pending, .badge.unverified, .badge.unavailable { color: var(--warn); border-color: #6a5130; }
.section-title { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; margin: 20px 0 8px; }
.empty, .loading, .error { padding: 24px; color: var(--muted); text-align: center; }
.error { color: var(--danger); }
.gate { max-width: 520px; margin: 60px auto; text-align: center; color: var(--muted); }
.form { display: grid; grid-template-columns: 160px 1fr; gap: 10px; align-items: center; max-width: 640px; padding: 16px; }
.form label { color: var(--muted); font-size: 13px; }
.result { padding: 0 16px 16px; font-size: 13px; }
.result a { color: var(--accent); word-break: break-all; }
code { background: var(--row); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.hidden { display: none !important; }
svg.icon { width: 14px; height: 14px; vertical-align: -2px; stroke: currentColor; fill: none; stroke-width: 2; }
.badge.frozen, .badge.shortfall, .badge.amount_mismatch, .badge.status_mismatch, .badge.reversal_unrecorded { color: var(--warn); border-color: #6a5130; }
.badge.closed, .badge.revoked, .badge.missing_in_mint, .badge.missing_at_provider { color: var(--danger); border-color: #6a2c3d; }
.badge.active, .badge.matched, .badge.clawed_back, .badge.reversal_applied { color: var(--ok); border-color: #2c5a41; }
label.inline { display: flex; align-items: center; gap: .5rem; }
button.danger { color: var(--danger); border-color: #6a2c3d; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 18px; }
.panel h3 { margin: 0; padding: 12px 16px 0; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-weight: 600; }
.two { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 12px; }
.checks { display: flex; gap: 12px; flex-wrap: wrap; }
.checks label { color: var(--fg); display: flex; gap: 4px; align-items: center; }
textarea { font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--fg); background: var(--row); border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; min-height: 60px; resize: vertical; }
.secret { margin: 0 16px 16px; padding: 12px; border: 1px solid #6a5130; border-radius: 6px; background: #221d14; }
.secret code { font-size: 13px; word-break: break-all; }
dl.kv { display: grid; grid-template-columns: 180px 1fr; gap: 6px 12px; margin: 0; padding: 16px; }
dl.kv dt { color: var(--muted); font-size: 12px; }
dl.kv dd { margin: 0; font: 12.5px ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
</style>
</head>
<body>
<header>
<h1>Mint Operator Console</h1>
<nav id="tabs" class="hidden">
<button data-tab="overview" class="active">Overview</button>
<button data-tab="accounts">Accounts</button>
<button data-tab="payouts">Payouts</button>
<button data-tab="funding">Funding</button>
<button data-tab="reconcile">Reconciliation</button>
<button data-tab="reversals">Reversals</button>
<button data-tab="keys">API keys</button>
<button data-tab="payerkeys">Payer keys</button>
</nav>
<div class="spacer"></div>
<input id="key" type="password" placeholder="X-API-Key (MINT_API_KEY)" autocomplete="off" style="min-width:240px" />
<button id="connect" class="primary">Connect</button>
<button id="logout" class="hidden">Sign out</button>
<span id="status">Not connected</span>
</header>
<div id="gate" class="gate">
<p>Enter the Mint API key to open the console. Every request is sent to this
Mint server with the <code>X-API-Key</code> header and is authorized exactly like
any other API call. The key is kept only in this browser tab's session storage.</p>
</div>
<main id="app" class="hidden">
<!-- Overview -->
<section id="tab-overview">
<div class="toolbar"><button id="refresh-overview"><svg class="icon" viewBox="0 0 24 24"><path d="M21 12a9 9 0 1 1-3-6.7L21 8"/><path d="M21 3v5h-5"/></svg> Refresh</button></div>
<div class="grid" id="reserve-tiles"></div>
<div class="section-title">Reconciliation by provider</div>
<div class="card" id="recon"></div>
</section>
<!-- Accounts -->
<section id="tab-accounts" class="hidden">
<div class="panel">
<h3>New account</h3>
<div class="form">
<label for="na-kind">Kind</label>
<select id="na-kind">
<option value="User">User</option>
<option value="Org">Org</option>
<option value="Agent">Agent</option>
<option value="Provider">Provider</option>
<option value="Model">Model</option>
<option value="Treasury">Treasury</option>
</select>
<label for="na-owner">Owner ref (optional)</label><input id="na-owner" placeholder="your system's user / org id" />
<span></span><button id="na-create" class="primary">Create account</button>
</div>
<div class="result" id="na-result"></div>
</div>
<div class="toolbar">
<input id="acct-lookup" placeholder="acct_... (open an account)" style="min-width:320px" />
<button id="acct-open">Open</button>
<input id="obj-lookup" placeholder="hold_... / strm_... / alw_..." style="min-width:260px" />
<button id="obj-open">Look up</button>
<div class="spacer"></div>
<button id="acct-more" class="hidden">Load more</button>
</div>
<div class="card hidden" id="obj-result"></div>
<div class="card" id="accounts"></div>
<div id="account-detail" class="hidden">
<div class="section-title" id="detail-title">Account</div>
<div class="grid" id="detail-tiles"></div>
<div class="toolbar">
<button id="acct-freeze">Freeze</button>
<button id="acct-unfreeze">Unfreeze</button>
<button id="acct-close" class="danger">Close account</button>
<button id="acct-address">New receive address</button>
<button id="kyc-start">Start KYC verification</button>
</div>
<div class="result" id="kyc-result"></div>
<div class="two">
<div class="panel">
<h3>Transfer out (operator adjustment)</h3>
<div class="form">
<label for="tr-to">To (acct_ or ait1 address)</label><input id="tr-to" placeholder="acct_..." />
<label for="tr-amount">Amount (USD)</label><input id="tr-amount" type="number" min="0.000001" step="0.01" placeholder="10.00" />
<label for="tr-ref">Reference (optional)</label><input id="tr-ref" placeholder="adjustment:ticket-123" />
<span></span><button id="tr-send" class="primary">Send transfer</button>
</div>
<div class="result" id="tr-result"></div>
</div>
<div class="panel">
<h3>Redeem (cash out)</h3>
<div class="form">
<label for="rd-amount">Amount (USD)</label><input id="rd-amount" type="number" min="0.01" step="0.01" placeholder="50.00" />
<label for="rd-kind">Destination</label>
<select id="rd-kind">
<option value="stripe">Stripe connected account</option>
<option value="paypal">PayPal email</option>
<option value="crypto">Crypto address</option>
</select>
<label for="rd-dest" id="rd-dest-label">Stripe account</label><input id="rd-dest" placeholder="acct_1Abc..." />
<label for="rd-chain" class="hidden" id="rd-chain-label">Chain</label><input id="rd-chain" class="hidden" placeholder="base" />
<label for="rd-tier">KYC tier presented</label>
<select id="rd-tier"><option value="verified">Verified</option><option value="basic">Basic</option><option value="none">None</option></select>
<span></span><button id="rd-send" class="primary">Burn and pay out</button>
</div>
<div class="result" id="rd-result"></div>
</div>
</div>
<div class="section-title">Open positions (what keeps the held balance up)</div>
<div class="toolbar">
<select id="pos-scope">
<option value="open">Open only</option>
<option value="all">Include settled / closed / revoked</option>
</select>
<button id="pos-refresh">Refresh positions</button>
<span class="result" id="pos-summary"></span>
</div>
<div class="section-title">Holds</div>
<div class="card" id="pos-holds"></div>
<div class="section-title">Streams</div>
<div class="card" id="pos-streams"></div>
<div class="section-title">Allowances granted</div>
<div class="card" id="pos-allowances"></div>
<div class="toolbar">
<label>From <input id="stmt-from" type="date" /></label>
<label>To <input id="stmt-to" type="date" /></label>
<button id="stmt-run">Load statement</button>
<button id="rcpt-run">Signed receipt chain</button>
</div>
<div class="card" id="statement"></div>
</div>
</section>
<!-- Payouts -->
<section id="tab-payouts" class="hidden">
<div class="toolbar">
<select id="payout-status">
<option value="">All statuses</option>
<option value="failed">Failed</option>
<option value="pending">Pending</option>
<option value="paid">Paid</option>
<option value="cancelled">Cancelled</option>
</select>
<input id="payout-account" placeholder="Filter by acct_..." style="min-width:260px" />
<button id="payout-load">Apply</button>
<div class="spacer"></div>
<button id="payout-more" class="hidden">Load more</button>
</div>
<div class="card" id="payouts"></div>
</section>
<!-- Funding -->
<section id="tab-funding" class="hidden">
<div class="card">
<div class="form">
<label for="co-account">Account</label><input id="co-account" placeholder="acct_..." />
<label for="co-amount">Amount (USD)</label><input id="co-amount" type="number" min="0.01" step="0.01" placeholder="25.00" />
<label for="co-provider">Provider</label>
<select id="co-provider" disabled>
<option value="">Loading providers...</option>
</select>
<span></span><button id="co-create" class="primary" disabled>Create hosted checkout</button>
</div>
<div class="result" id="co-providers"></div>
<div class="result" id="co-result"></div>
</div>
<div class="panel">
<h3>Credit a confirmed deposit (manual mint)</h3>
<div class="form">
<label for="mm-account">Account</label><input id="mm-account" placeholder="acct_..." />
<label for="mm-amount">Amount (USD)</label><input id="mm-amount" type="number" min="0.01" step="0.01" placeholder="100.00" />
<label for="mm-ref">Deposit ref</label><input id="mm-ref" placeholder="stripe:pi_... (must be confirmed by the provider)" />
<span></span><button id="mm-create" class="primary">Mint against deposit</button>
</div>
<div class="result" id="mm-result"></div>
</div>
</section>
<!-- Reconciliation against provider reports -->
<section id="tab-reconcile" class="hidden">
<div class="panel">
<h3>Import a provider report</h3>
<div class="form">
<label for="rp-provider">Provider</label>
<select id="rp-provider">
<option value="stripe">Stripe</option>
<option value="paypal">PayPal</option>
<option value="creem">Creem</option>
<option value="polar">Polar</option>
<option value="revenuecat">RevenueCat</option>
<option value="adapty">Adapty</option>
<option value="usdc">USDC</option>
<option value="iyzico">Iyzico</option>
</select>
<label for="rp-file">Report file (CSV or JSON)</label><input id="rp-file" type="file" accept=".csv,.json,text/csv,application/json" />
<label for="rp-unit">Amount column unit</label>
<select id="rp-unit"><option value="major">Dollars (12.34)</option><option value="cents">Cents (1234)</option><option value="micros">Micros</option></select>
<label for="rp-refcol">Reference column (optional)</label><input id="rp-refcol" placeholder="auto: payment_intent_id, transaction_id, tx_hash, id ..." />
<label for="rp-apply">Claw back listed refunds</label>
<label class="inline"><input id="rp-apply" type="checkbox" /> Reverse refunds / chargebacks Mint has not recorded yet</label>
<span></span><button id="rp-import" class="primary">Import and match</button>
</div>
<div class="result" id="rp-result"></div>
</div>
<div class="toolbar"><button id="rp-refresh">Refresh reports</button></div>
<div class="card" id="reports"></div>
<div id="report-detail" class="hidden">
<div class="section-title" id="report-title">Report</div>
<div class="grid" id="report-tiles"></div>
<div class="toolbar">
<label class="checks"><label><input type="checkbox" id="rp-all" /> Show matched lines</label></label>
<div class="spacer"></div>
<button id="rp-delete" class="danger">Delete report</button>
</div>
<div class="section-title">Provider lines</div>
<div class="card" id="report-lines"></div>
<div class="section-title">In Mint, missing from the provider report</div>
<div class="card" id="report-missing"></div>
</div>
</section>
<!-- Deposit reversals -->
<section id="tab-reversals" class="hidden">
<div class="panel">
<h3>Record a refund or chargeback</h3>
<div class="form">
<label for="rv-ref">Deposit ref</label><input id="rv-ref" placeholder="stripe:pi_..." />
<label for="rv-amount">Amount reversed (USD)</label><input id="rv-amount" type="number" min="0.01" step="0.01" placeholder="blank = whole deposit" />
<label for="rv-reason">Reason</label>
<select id="rv-reason"><option value="refund">Refund</option><option value="dispute">Dispute / chargeback</option></select>
<label for="rv-note">Note</label><input id="rv-note" placeholder="optional" />
<span></span><button id="rv-create" class="primary">Record and claw back</button>
</div>
<div class="result" id="rv-result"></div>
</div>
<div class="toolbar">
<select id="rv-status">
<option value="">All</option>
<option value="shortfall">Shortfall</option>
<option value="clawed_back">Clawed back</option>
<option value="written_off">Written off</option>
</select>
<button id="rv-load">Apply</button>
</div>
<div class="card" id="reversals"></div>
</section>
<!-- API keys -->
<section id="tab-keys" class="hidden">
<div class="panel">
<h3>Create an API key</h3>
<div class="form">
<label for="ky-name">Name</label><input id="ky-name" placeholder="ledgerline-prod" />
<label>Scopes</label>
<div class="checks" id="ky-scopes">
<label><input type="checkbox" value="read" checked /> read</label>
<label><input type="checkbox" value="accounts" /> accounts</label>
<label><input type="checkbox" value="spend" /> spend</label>
<label><input type="checkbox" value="funding" /> funding</label>
<label><input type="checkbox" value="payouts" /> payouts</label>
<label><input type="checkbox" value="admin" /> admin</label>
</div>
<label for="ky-accounts">Restrict to accounts</label><textarea id="ky-accounts" placeholder="blank = all accounts; otherwise one acct_... per line"></textarea>
<span></span><button id="ky-create" class="primary">Create key</button>
</div>
<div id="ky-result"></div>
</div>
<div class="section-title">Console-managed keys</div>
<div class="card" id="keys"></div>
<div class="section-title">Environment keys (MINT_API_KEYS_JSON, read-only)</div>
<div class="card" id="env-keys"></div>
</section>
<!-- AITP-402 payer keys -->
<section id="tab-payerkeys" class="hidden">
<div class="panel">
<h3>Register an AITP payer key</h3>
<div class="form">
<label for="pk-payer">Payer account</label><input id="pk-payer" placeholder="acct_..." />
<label for="pk-id">Key id</label><input id="pk-id" placeholder="agent-2026-09 (echoed as pubkey_id)" />
<label for="pk-hex">Ed25519 public key (hex)</label><input id="pk-hex" placeholder="64 hex characters" />
<label for="pk-label">Label (optional)</label><input id="pk-label" placeholder="research agent, prod" />
<span></span><button id="pk-create" class="primary">Register key</button>
</div>
<div class="result" id="pk-result"></div>
</div>
<div class="toolbar">
<input id="pk-filter" placeholder="Filter by payer acct_..." style="min-width:280px" />
<button id="pk-load">Apply</button>
</div>
<div class="card" id="payer-keys"></div>
</section>
</main>
<script>
(() => {
"use strict";
const $ = (id) => document.getElementById(id);
const KEY = "mint.admin.key";
let apiKey = sessionStorage.getItem(KEY) || "";
let acctCursor = null;
let payoutCursor = null;
let currentAccount = null;
const setStatus = (text, cls) => { const s = $("status"); s.textContent = text; s.className = cls || ""; };
const usd = (micros) => {
if (micros === null || micros === undefined) return "-";
const n = Number(micros) / 1e6;
return n.toLocaleString(undefined, { style: "currency", currency: "USD", minimumFractionDigits: 2, maximumFractionDigits: 6 });
};
const when = (ms) => (ms ? new Date(Number(ms)).toLocaleString() : "-");
const el = (tag, props, ...kids) => {
const n = document.createElement(tag);
if (props) for (const [k, v] of Object.entries(props)) {
if (k === "class") n.className = v; else if (k === "onclick") n.onclick = v; else n.setAttribute(k, v);
}
for (const k of kids) n.append(k instanceof Node ? k : document.createTextNode(k === null || k === undefined ? "-" : String(k)));
return n;
};
const badge = (text) => el("span", { class: "badge " + String(text).toLowerCase() }, String(text).replace(/_/g, " "));
const toMicros = (dollars) => Math.round(Number(dollars) * 1e6);
const idem = (prefix) => prefix + ":" + (window.crypto && crypto.randomUUID ? crypto.randomUUID() : Date.now() + "-" + Math.random().toString(16).slice(2));
const enc = encodeURIComponent;
// Run an async action from a button: disable while running, report errors.
async function run(btn, out, busyText, fn) {
if (btn) btn.disabled = true;
if (out && busyText) { out.classList.remove("error"); out.textContent = busyText; }
try { await fn(); }
catch (e) { if (out) out.textContent = e.message; setStatus(e.message, "err"); }
finally { if (btn) btn.disabled = false; }
}
const kv = (obj) => {
const dl = el("dl", { class: "kv" });
for (const [k, v] of Object.entries(obj || {})) {
dl.append(el("dt", null, k), el("dd", null, v !== null && typeof v === "object" ? JSON.stringify(v) : v));
}
return dl;
};
const placeholder = (node, cls, text) => { node.replaceChildren(el("div", { class: cls }, text)); };
async function api(method, path, body) {
const res = await fetch(path, {
method,
headers: Object.assign({ "x-api-key": apiKey }, body ? { "content-type": "application/json" } : {}),
body: body ? JSON.stringify(body) : undefined,
});
let json = null;
try { json = await res.json(); } catch (_) { /* non-JSON */ }
if (res.status === 401) { logout(); throw new Error("API key rejected"); }
if (!res.ok || !json || json.success === false) {
const msg = json && json.error ? json.error.message + " (" + json.error.code + ")" : "HTTP " + res.status;
throw new Error(msg);
}
return json.data;
}
function table(headers, rows) {
const t = el("table");
t.append(el("thead", null, el("tr", null, ...headers.map((h) => el("th", null, h)))));
const tb = el("tbody");
rows.forEach((r) => tb.append(r));
t.append(tb);
return t;
}
// ---- overview --------------------------------------------------------
async function loadOverview() {
const tiles = $("reserve-tiles");
placeholder(tiles, "loading", "Loading reserve...");
placeholder($("recon"), "loading", "Loading reconciliation...");
try {
const [reserve, audit, recon] = await Promise.all([
api("GET", "/v1/reserve"), api("GET", "/v1/receipts/audit"), api("GET", "/v1/reconciliation"),
]);
const tile = (label, value, mono) => el("div", { class: "tile" }, el("div", { class: "label" }, label), el("div", { class: "value" + (mono ? " mono" : "") }, value));
tiles.replaceChildren(
tile("Solvency", reserve.ok ? "Solvent" : "MISMATCH"),
tile("Supply (reserve)", usd(reserve.supply_micros)),
tile("Backing (balances)", usd(reserve.backing_micros)),
tile("Burned, not yet paid", usd(recon.totals.burned_unpaid_micros)),
tile("Chargeback shortfall", usd(recon.totals.reversal_shortfall_micros)),
tile("Journal epoch", audit.epoch),
tile("Receipt Merkle root", audit.merkle_root, true),
);
tiles.children[0].querySelector(".value").style.color = reserve.ok ? "var(--ok)" : "var(--danger)";
if (!recon.providers.length) {
placeholder($("recon"), "empty", "No deposits or payouts yet. Active providers: " + (recon.active_providers.join(", ") || "none"));
} else {
$("recon").replaceChildren(table(
["Provider", "Active", "Deposits minted", "Burned for payout", "Paid", "Pending", "Failed", "Cancelled", "Reversed / clawed", "Burned unpaid"],
recon.providers.map((p) => el("tr", null,
el("td", null, p.provider),
el("td", null, badge(p.active ? "ok" : "inactive")),
el("td", { class: "num" }, usd(p.deposits_minted.micros) + " (" + p.deposits_minted.count + ")"),
el("td", { class: "num" }, usd(p.burned_for_payout.micros) + " (" + p.burned_for_payout.count + ")"),
el("td", { class: "num" }, usd(p.payouts.paid_micros)),
el("td", { class: "num" }, usd(p.payouts.pending_micros) + " (" + p.payouts.pending_count + ")"),
el("td", { class: "num" }, usd(p.payouts.failed_micros) + " (" + p.payouts.failed_count + ")"),
el("td", { class: "num" }, usd(p.payouts.cancelled_micros) + " (" + p.payouts.cancelled_count + ")"),
el("td", { class: "num" }, usd(p.reversals.reversed_micros) + " / " + usd(p.reversals.clawed_back_micros)),
el("td", { class: "num" }, usd(p.burned_unpaid_micros)),
)),
));
}
setStatus("Connected", "ok");
} catch (e) {
placeholder(tiles, "error", "Could not load reserve: " + e.message);
placeholder($("recon"), "error", e.message);
setStatus(e.message, "err");
}
}
// ---- accounts --------------------------------------------------------
async function loadAccounts(append) {
const box = $("accounts");
if (!append) { acctCursor = null; placeholder(box, "loading", "Loading accounts..."); }
try {
const qs = new URLSearchParams({ limit: "50" });
if (append && acctCursor) qs.set("after", acctCursor);
const page = await api("GET", "/v1/accounts?" + qs);
acctCursor = page.next_cursor;
$("acct-more").classList.toggle("hidden", !acctCursor);
const rows = page.items.map((a) => {
const tr = el("tr", { class: "clickable" },
el("td", { class: "mono" }, a.account_id), el("td", null, a.kind), el("td", null, badge(a.status)), el("td", null, a.owner_ref),
el("td", { class: "num" }, usd(a.available_micros)), el("td", { class: "num" }, usd(a.held_micros)),
el("td", null, when(a.created_ms)));
tr.onclick = () => openAccount(a.account_id);
return tr;
});
if (append && box.querySelector("tbody")) {
rows.forEach((r) => box.querySelector("tbody").append(r));
} else if (!rows.length) {
placeholder(box, "empty", "No accounts yet.");
} else {
box.replaceChildren(table(["Account", "Kind", "Status", "Owner ref", "Available", "Held", "Created"], rows));
}
} catch (e) { placeholder(box, "error", "Could not load accounts: " + e.message); }
}
async function openAccount(id) {
id = (id || "").trim();
if (!id) return;
currentAccount = id;
$("account-detail").classList.remove("hidden");
$("detail-title").textContent = "Account " + id;
["kyc-result", "tr-result", "rd-result"].forEach((k) => $(k).replaceChildren());
const tiles = $("detail-tiles");
placeholder(tiles, "loading", "Loading account...");
try {
const [acct, kyc] = await Promise.all([
api("GET", "/v1/accounts/" + enc(id)),
api("GET", "/v1/accounts/" + enc(id) + "/kyc").catch((e) => ({ status: "unavailable", provider: e.message })),
]);
const tile = (label, value) => el("div", { class: "tile" }, el("div", { class: "label" }, label), el("div", { class: "value" }, value));
tiles.replaceChildren(
tile("Status", badge(acct.status)),
tile("Available", usd(acct.available_micros)),
tile("Held", usd(acct.held_micros)),
tile("Kind / owner", acct.kind + (acct.owner_ref ? " / " + acct.owner_ref : "")),
tile("Created", when(acct.created_ms)),
el("div", { class: "tile" }, el("div", { class: "label" }, "KYC " + (kyc.provider || "")), el("div", { class: "value" }, badge(kyc.status))),
);
$("kyc-start").disabled = !kyc.can_start;
$("acct-freeze").disabled = acct.status !== "active";
$("acct-unfreeze").disabled = acct.status !== "frozen";
$("acct-close").disabled = acct.status === "closed";
$("tr-send").disabled = acct.status !== "active";
$("rd-send").disabled = acct.status !== "active";
$("acct-address").disabled = acct.status === "closed";
await Promise.all([loadStatement(), loadPositions()]);
} catch (e) { placeholder(tiles, "error", "Could not open account: " + e.message); }
}
async function createAccount() {
const out = $("na-result");
await run($("na-create"), out, "Creating account...", async () => {
const owner = $("na-owner").value.trim();
const r = await api("POST", "/v1/accounts", { kind: $("na-kind").value, owner_ref: owner || null });
out.replaceChildren("Created ", el("code", null, r.account_id), " ");
const open = el("button", { class: "small" }, "Open");
open.onclick = () => openAccount(r.account_id);
out.append(open);
$("na-owner").value = "";
loadAccounts(false);
});
}
async function accountLifecycle(action) {
if (!currentAccount) return;
const warn = {
freeze: "Freeze this account? Outgoing transfers, holds, streams, allowances and redemptions will be refused until it is unfrozen.",
unfreeze: "Unfreeze this account and allow outgoing movement again?",
close: "Permanently close this account? This cannot be undone. The balance must be zero.",
}[action];
if (!window.confirm(warn)) return;
await run($("acct-" + action), $("kyc-result"), "Updating account...", async () => {
const a = await api("POST", "/v1/accounts/" + enc(currentAccount) + "/" + action, {});
$("kyc-result").textContent = "Account is now " + a.status + ".";
setStatus("Account " + a.account_id + " is " + a.status, "ok");
await openAccount(currentAccount);
loadAccounts(false);
});
}
async function newAddress() {
if (!currentAccount) return;
const purpose = window.prompt("Purpose label for the address (optional)", "") ;
if (purpose === null) return;
const out = $("kyc-result");
await run($("acct-address"), out, "Creating address...", async () => {
const r = await api("POST", "/v1/accounts/" + enc(currentAccount) + "/addresses", { purpose: purpose.trim() || null });
out.replaceChildren("Receive address: ", el("code", null, r.address || JSON.stringify(r)));
});
}
async function sendTransfer() {
if (!currentAccount) return;
const out = $("tr-result");
const to = $("tr-to").value.trim();
const amount = toMicros($("tr-amount").value);
if (!to || !(amount > 0)) { out.textContent = "Enter a recipient and a positive amount."; return; }
if (!window.confirm("Transfer " + usd(amount) + " from " + currentAccount + " to " + to + "?")) return;
await run($("tr-send"), out, "Sending transfer...", async () => {
const ref = $("tr-ref").value.trim();
const r = await api("POST", "/v1/transfers", { from: currentAccount, to, amount_micros: amount, idem_key: idem("console-transfer"), ref: ref || null });
out.textContent = "Transferred " + usd(amount) + ". Sender available now " + usd(r.from_balance && r.from_balance.available_micros) + ".";
$("tr-amount").value = "";
await openAccount(currentAccount);
});
}
function syncRedeemFields() {
const kind = $("rd-kind").value;
$("rd-dest-label").textContent = kind === "stripe" ? "Stripe account" : kind === "paypal" ? "PayPal email" : "Address";
$("rd-dest").placeholder = kind === "stripe" ? "acct_1Abc..." : kind === "paypal" ? "payee@example.com" : "0x...";
["rd-chain", "rd-chain-label"].forEach((k) => $(k).classList.toggle("hidden", kind !== "crypto"));
}
async function redeem() {
if (!currentAccount) return;
const out = $("rd-result");
const amount = toMicros($("rd-amount").value);
const kind = $("rd-kind").value;
const dest = $("rd-dest").value.trim();
if (!(amount > 0) || !dest) { out.textContent = "Enter a positive amount and a destination."; return; }
const payout_dest = kind === "stripe" ? { kind, account: dest } : kind === "paypal" ? { kind, email: dest } : { kind, chain: $("rd-chain").value.trim() || "base", address: dest };
const provider = kind === "crypto" ? "usdc" : kind;
if (!window.confirm("Burn " + usd(amount) + " from " + currentAccount + " and pay it out via " + provider + "?")) return;
await run($("rd-send"), out, "Burning and paying out...", async () => {
const payout_ref = provider + ":" + idem("po").split(":")[1];
const r = await api("POST", "/v1/redeem", { from: currentAccount, amount_micros: amount, payout_ref, kyc_tier: $("rd-tier").value, payout_dest });
out.textContent = "Burned " + usd(amount) + " (" + r.payout_ref + "). Payout status: " + (r.payout ? r.payout.status : "unknown") + ".";
$("rd-amount").value = "";
await openAccount(currentAccount);
});
}
async function loadReceipts() {
if (!currentAccount) return;
const box = $("statement");
placeholder(box, "loading", "Loading signed receipts...");
try {
const chain = await api("GET", "/v1/accounts/" + enc(currentAccount) + "/receipts?limit=200");
if (!chain.length) { placeholder(box, "empty", "No receipts yet: this account has no activity."); return; }
box.replaceChildren(table(["Seq", "Time", "Op", "Amount", "From", "To", "Available after", "Hash", "Prev hash"],
chain.map((r) => el("tr", null,
el("td", { class: "num" }, r.body.account_seq), el("td", null, when(r.body.ts)), el("td", null, r.body.op),
el("td", { class: "num" }, usd(r.body.amount_micros)),
el("td", { class: "mono" }, r.body.from), el("td", { class: "mono" }, r.body.to),
el("td", { class: "num" }, usd(r.body.balance_after_available_micros)),
el("td", { class: "mono" }, String(r.hash).slice(0, 16) + "..."),
el("td", { class: "mono" }, String(r.body.prev_hash).slice(0, 16) + "...")))));
} catch (e) { placeholder(box, "error", "Could not load receipts: " + e.message); }
}
async function lookupObject() {
const id = $("obj-lookup").value.trim();
const box = $("obj-result");
if (!id) return;
box.classList.remove("hidden");
const path = id.startsWith("hold_") ? "/v1/holds/" : id.startsWith("strm_") ? "/v1/streams/" : id.startsWith("alw_") ? "/v1/allowances/" : null;
if (!path) { placeholder(box, "error", "Enter a hold_, strm_ or alw_ id."); return; }
placeholder(box, "loading", "Looking up " + id + "...");
try {
const obj = await api("GET", path + enc(id));
box.replaceChildren(kv(obj));
} catch (e) { placeholder(box, "error", "Not found: " + e.message); }
}
async function loadStatement() {
if (!currentAccount) return;
const box = $("statement");
placeholder(box, "loading", "Loading statement...");
const qs = new URLSearchParams();
if ($("stmt-from").value) qs.set("from_ms", String(new Date($("stmt-from").value + "T00:00:00").getTime()));
if ($("stmt-to").value) qs.set("to_ms", String(new Date($("stmt-to").value + "T00:00:00").getTime() + 86400000));
try {
const s = await api("GET", "/v1/accounts/" + encodeURIComponent(currentAccount) + "/statement?" + qs);
const summary = el("div", { class: "result" },
"Period " + when(s.from_ms) + " to " + when(s.to_ms) + ". Opening " + usd(s.opening.available_micros + s.opening.held_micros) +
", credits " + usd(s.totals.credits_micros) + ", debits " + usd(s.totals.debits_micros) +
", closing " + usd(s.closing.available_micros + s.closing.held_micros) + ".");
if (!s.entries.length) {
box.replaceChildren(summary, el("div", { class: "empty" }, "No activity in this period."));
return;
}
box.replaceChildren(summary, table(["Time", "Op", "Change", "Available after", "Held after", "Counterparty", "Receipt"],
s.entries.map((r) => el("tr", null,
el("td", null, when(r.ts)), el("td", null, r.op),
el("td", { class: "num" }, (r.delta_micros > 0 ? "+" : "") + usd(r.delta_micros)),
el("td", { class: "num" }, usd(r.available_after_micros)), el("td", { class: "num" }, usd(r.held_after_micros)),
el("td", { class: "mono" }, r.from === currentAccount ? r.to : r.from),
el("td", { class: "mono" }, r.receipt_hash.slice(0, 16) + "...")))));
} catch (e) { placeholder(box, "error", "Could not load statement: " + e.message); }
}
async function startKyc() {
if (!currentAccount) return;
const out = $("kyc-result");
out.textContent = "Starting verification...";
try {
const s = await api("POST", "/v1/accounts/" + encodeURIComponent(currentAccount) + "/kyc/session", {});
out.replaceChildren("Verification link (" + s.provider + "): ", el("a", { href: s.url, target: "_blank", rel: "noopener noreferrer" }, s.url));
} catch (e) { out.textContent = "Could not start verification: " + e.message; }
}
// ---- open positions --------------------------------------------------
async function loadPositions() {
if (!currentAccount) return;
const acct = currentAccount;
const all = $("pos-scope").value === "all";
["pos-holds", "pos-streams", "pos-allowances"].forEach((k) => placeholder($(k), "loading", "Loading..."));
$("pos-summary").textContent = "";
const base = "/v1/accounts/" + enc(acct);
const [holds, streams, allowances] = await Promise.allSettled([
api("GET", base + "/holds?limit=200" + (all ? "" : "&status=open")),
api("GET", base + "/streams?limit=200" + (all ? "" : "&status=open")),
api("GET", base + "/allowances?limit=200&status=" + (all ? "all" : "active")),
]);
if (acct !== currentAccount) return;
if (holds.status === "fulfilled") {
const h = holds.value;
$("pos-summary").textContent = "Open holds reserve " + usd(h.open_held_micros) + ".";
if (!h.items.length) placeholder($("pos-holds"), "empty", all ? "No holds on this account." : "No open holds.");
else $("pos-holds").replaceChildren(table(["Hold", "Amount", "Captured", "Status", "Auto-release", "Reference", ""], h.items.map(holdRow)));
} else placeholder($("pos-holds"), "error", "Could not load holds: " + holds.reason.message);
if (streams.status === "fulfilled") {
const list = streams.value.items;
if (!list.length) placeholder($("pos-streams"), "empty", all ? "No streams on this account." : "No open streams.");
else $("pos-streams").replaceChildren(table(["Stream", "Direction", "Counterparty", "Rate", "Cap", "Settled", "Status", ""], list.map(streamRow)));
} else placeholder($("pos-streams"), "error", "Could not load streams: " + streams.reason.message);
if (allowances.status === "fulfilled") {
const list = allowances.value.items;
if (!list.length) placeholder($("pos-allowances"), "empty", all ? "This account never granted an allowance." : "No active allowances.");
else $("pos-allowances").replaceChildren(table(["Allowance", "Delegate key", "Limit", "Spent", "Window", "Status", ""], list.map(allowanceRow)));
} else placeholder($("pos-allowances"), "error", "Could not load allowances: " + allowances.reason.message);
}
// A confirm-then-POST row action that refreshes the account afterwards.
function positionAction(label, question, path, body, cls) {
const b = el("button", { class: "small" + (cls ? " " + cls : "") }, label);
b.onclick = async () => {
if (!window.confirm(question)) return;
b.disabled = true;
try {
await api("POST", path, body);
setStatus(label + " done", "ok");
await openAccount(currentAccount);
} catch (e) { setStatus(e.message, "err"); b.disabled = false; }
};
return b;
}
function holdRow(h) {
const action = el("td");
if (h.status === "Open") {
action.append(positionAction("Release", "Release hold " + h.id + " and return " + usd(h.amount_micros) + " to the account's available balance?",
"/v1/holds/" + enc(h.id) + "/release", { idem_key: idem("console-release"), ref: "operator release" }, "danger"));
}
const expiry = h.expires_at_ms ? (h.expires_at_ms <= Date.now() && h.status === "Open" ? "due " : "") + when(h.expires_at_ms) : "never";
return el("tr", null,
el("td", { class: "mono" }, h.id), el("td", { class: "num" }, usd(h.amount_micros)), el("td", { class: "num" }, usd(h.captured_micros)),
el("td", null, badge(h.status === "Open" ? "pending" : h.status === "Captured" ? "paid" : "closed")), el("td", null, expiry),
el("td", { class: "mono" }, h.ref), action);
}
function streamRow(s) {
const action = el("td");
if (s.status === "Open" && s.direction === "outgoing") {
action.append(positionAction("Close", "Close stream " + s.id + "? Nothing more is settled; the unused " + usd(s.cap_micros - s.settled_micros) + " returns to the payer.",
"/v1/streams/" + enc(s.id) + "/close", { units: 0, seq: 0, idem_key: idem("console-stream-close"), ref: "operator close" }, "danger"));
}
return el("tr", null,
el("td", { class: "mono" }, s.id), el("td", null, s.direction),
el("td", { class: "mono" }, s.direction === "outgoing" ? s.to_id : s.from_id),
el("td", { class: "num" }, usd(s.rate_micros) + " / " + String(s.unit).toLowerCase()), el("td", { class: "num" }, usd(s.cap_micros)),
el("td", { class: "num" }, usd(s.settled_micros)), el("td", null, badge(s.status === "Open" ? "active" : "closed")), action);
}
function windowText(w) {
if (!w) return "-";
if (w === "PerCall") return "lifetime cap";
if (typeof w === "string") return w.toLowerCase();
if (w.Rolling || w.rolling) { const ms = (w.Rolling || w.rolling).window_ms; return "rolling " + Math.round(ms / 3600000 * 10) / 10 + " h"; }
return JSON.stringify(w);
}
function allowanceRow(a) {
const action = el("td");
if (!a.revoked) {
action.append(positionAction("Revoke", "Revoke allowance " + a.id + "? The delegate " + a.to_key + " can no longer spend from this account.",
"/v1/allowances/" + enc(a.id) + "/revoke", { idem_key: idem("console-revoke") }, "danger"));
}
return el("tr", null,
el("td", { class: "mono" }, a.id), el("td", { class: "mono" }, a.to_key), el("td", { class: "num" }, usd(a.limit_micros)),
el("td", { class: "num" }, usd(a.spent_micros)), el("td", null, windowText(a.window)),
el("td", null, badge(a.revoked ? "revoked" : "active")), action);
}
// ---- AITP payer keys -------------------------------------------------
async function loadPayerKeys() {
const box = $("payer-keys");
placeholder(box, "loading", "Loading payer keys...");
const payer = $("pk-filter").value.trim();
try {
const r = await api("GET", "/v1/payer-keys" + (payer ? "?payer=" + enc(payer) : ""));
if (!r.items.length) { placeholder(box, "empty", payer ? "No keys registered for this payer." : "No payer keys registered. Agents cannot pay over AITP-402 until their public key is registered here."); return; }
box.replaceChildren(table(["Payer", "Key id", "Public key", "Label", "Registered", "Status", ""], r.items.map(payerKeyRow)));
} catch (e) { placeholder(box, "error", "Could not load payer keys: " + e.message); }
}
function payerKeyRow(k) {
const action = el("td");
if (k.active) {
const b = el("button", { class: "small danger" }, "Revoke");
b.onclick = async () => {
if (!window.confirm("Revoke key " + k.pubkey_id + " for " + k.payer + "? AITP authorizations signed with it are refused immediately.")) return;
b.disabled = true;
try { const u = await api("POST", "/v1/payer-keys/" + enc(k.payer) + "/" + enc(k.pubkey_id) + "/revoke", {}); tr.replaceWith(payerKeyRow(u)); setStatus("Payer key revoked", "ok"); }
catch (e) { setStatus(e.message, "err"); b.disabled = false; }
};
action.append(b);
}
const tr = el("tr", null,
el("td", { class: "mono" }, k.payer), el("td", { class: "mono" }, k.pubkey_id),
el("td", { class: "mono" }, k.public_key_hex.slice(0, 16) + "..."), el("td", null, k.label),
el("td", null, when(k.created_ms)), el("td", null, badge(k.active ? "active" : "revoked")), action);
return tr;
}
async function createPayerKey() {
const out = $("pk-result");
const payer = $("pk-payer").value.trim();
const pubkey_id = $("pk-id").value.trim();
const public_key_hex = $("pk-hex").value.trim();
if (!payer || !pubkey_id || !/^[0-9a-fA-F]{64}$/.test(public_key_hex)) { out.textContent = "Enter the payer account, a key id and a 64-character hex Ed25519 public key."; return; }
await run($("pk-create"), out, "Registering key...", async () => {
const k = await api("POST", "/v1/payer-keys", { payer, pubkey_id, public_key_hex, label: $("pk-label").value.trim() || null });
out.textContent = "Registered " + k.pubkey_id + " for " + k.payer + ". It verifies AITP authorizations immediately.";
$("pk-id").value = ""; $("pk-hex").value = ""; $("pk-label").value = "";
await loadPayerKeys();
});
}
// ---- payouts ---------------------------------------------------------
async function loadPayouts(append) {
const box = $("payouts");
if (!append) { payoutCursor = null; placeholder(box, "loading", "Loading payouts..."); }
const qs = new URLSearchParams({ limit: "50" });
if ($("payout-status").value) qs.set("status", $("payout-status").value);
if ($("payout-account").value.trim()) qs.set("account_id", $("payout-account").value.trim());
if (append && payoutCursor) qs.set("cursor", payoutCursor);
try {
const page = await api("GET", "/v1/payouts?" + qs);
payoutCursor = page.next_cursor;
$("payout-more").classList.toggle("hidden", !payoutCursor);
const rows = page.items.map(payoutRow);
if (append && box.querySelector("tbody")) rows.forEach((r) => box.querySelector("tbody").append(r));
else if (!rows.length) placeholder(box, "empty", "No payouts match.");
else box.replaceChildren(table(["Created", "Payout ref", "Account", "Amount", "Destination", "Status", "Attempts", "Last error", ""], rows));
} catch (e) { placeholder(box, "error", "Could not load payouts: " + e.message); }
}
function destText(d) {
if (!d) return "-";
if (d.kind === "stripe") return "Stripe " + d.account;
if (d.kind === "paypal") return "PayPal " + d.email;
if (d.kind === "crypto") return d.chain + " " + d.address;
return JSON.stringify(d);
}
function payoutRow(p) {
const action = el("td");
const ref = encodeURIComponent(p.payout_ref);
const act = (label, method, path, body) => {
const b = el("button", { class: "small" }, label);
b.onclick = async (ev) => {
ev.stopPropagation();
b.disabled = true;
try {
const updated = await api(method, path, body);
tr.replaceWith(payoutRow(updated));
setStatus("Payout " + updated.payout_ref + " is " + updated.status, "ok");
} catch (e) { setStatus(e.message, "err"); b.disabled = false; }
};
return b;
};
const confirmAct = (label, method, path, question, body, cls) => {
const b = act(label, method, path, body);
if (cls) b.classList.add(cls);
const inner = b.onclick;
b.onclick = (ev) => { ev.stopPropagation(); if (window.confirm(question)) inner(ev); };
return b;
};
const wrap = el("div", { class: "actions" });
if (p.status === "failed") {
wrap.append(act("Retry", "POST", "/v1/payouts/" + ref + "/retry"));
wrap.append(confirmAct("Cancel and re-credit", "POST", "/v1/payouts/" + ref + "/cancel",
"Cancel this payout and re-credit " + usd(p.amount_micros) + " to " + p.account_id + "?", {}, "danger"));
}
if (p.status === "pending") wrap.append(act("Check status", "GET", "/v1/payouts/" + ref));
if (p.status === "failed" || p.status === "pending") {
const mark = el("button", { class: "small" }, "Mark paid");
mark.onclick = async (ev) => {
ev.stopPropagation();
const pref = window.prompt("Provider reference of the manual settlement (wire id, tx hash). Leave blank if none.", "");
if (pref === null) return;
mark.disabled = true;
try {
const updated = await api("POST", "/v1/payouts/" + ref + "/mark-paid", { provider_ref: pref.trim() || null });
tr.replaceWith(payoutRow(updated));
setStatus("Payout " + updated.payout_ref + " marked paid", "ok");
} catch (e) { setStatus(e.message, "err"); mark.disabled = false; }
};
wrap.append(mark);
}
action.append(wrap);
const tr = el("tr", null,
el("td", null, when(p.created_ms)), el("td", { class: "mono" }, p.payout_ref),
el("td", { class: "mono" }, p.account_id), el("td", { class: "num" }, usd(p.amount_micros)),
el("td", null, destText(p.dest)), el("td", null, badge(p.status)), el("td", { class: "num" }, p.attempts),
el("td", null, p.last_error), action);
return tr;
}
// ---- funding ---------------------------------------------------------
async function loadProviders() {
const select = $("co-provider");
const info = $("co-providers");
const button = $("co-create");
select.disabled = true;
button.disabled = true;
select.replaceChildren(el("option", { value: "" }, "Loading providers..."));
try {
const r = await api("GET", "/v1/providers");
const usable = r.items.filter((p) => p.checkout);
const blocked = r.items.filter((p) => !p.checkout && p.checkout_blocked_reason);
if (!usable.length) {
select.replaceChildren(el("option", { value: "" }, "No checkout provider configured"));
info.textContent = !r.configured
? "Payments are not configured on this deployment. Set a provider's API key (for example STRIPE_API_KEY) and restart Mint."
: blocked.length
? blocked.map((p) => p.provider + ": " + p.checkout_blocked_reason).join("; ")
: "No active provider supports hosted checkout. Configure Stripe, PayPal, Creem, Polar, Coinbase Commerce or Iyzico.";
return;
}
const label = { stripe: "Stripe", paypal: "PayPal", creem: "Creem", polar: "Polar", usdc: "USDC (Coinbase Commerce)", iyzico: "Iyzico" };
select.replaceChildren(...usable.map((p) => el("option", { value: p.provider }, label[p.provider] || p.provider)));
select.disabled = false;
button.disabled = false;
info.textContent = blocked.length ? "Unavailable: " + blocked.map((p) => p.provider + " (" + p.checkout_blocked_reason + ")").join(", ") : "";
} catch (e) {
select.replaceChildren(el("option", { value: "" }, "Providers unavailable"));
info.textContent = "Could not load payment providers: " + e.message;
}
}
async function createCheckout() {
const out = $("co-result");
const dollars = Number($("co-amount").value);
if (!$("co-account").value.trim() || !(dollars > 0)) { out.textContent = "Enter an account and a positive amount."; return; }
const btn = $("co-create");
btn.disabled = true;
out.textContent = "Creating checkout...";
try {
const c = await api("POST", "/v1/checkouts", {
provider: $("co-provider").value,
account_id: $("co-account").value.trim(),
amount_micros: Math.round(dollars * 100) * 10000,
});
out.replaceChildren("Send the buyer to: ", el("a", { href: c.checkout_url, target: "_blank", rel: "noopener noreferrer" }, c.checkout_url),
el("br"), "Credits " + usd(c.amount_micros) + " to " + c.account_id + " once the provider confirms payment.");
} catch (e) { out.textContent = "Could not create checkout: " + e.message; }
finally { btn.disabled = false; }
}
async function manualMint() {
const out = $("mm-result");
const to = $("mm-account").value.trim();
const amount = toMicros($("mm-amount").value);
const deposit_ref = $("mm-ref").value.trim();
if (!to || !(amount > 0) || !deposit_ref.includes(":")) { out.textContent = "Enter an account, a positive amount and a <provider>:<ref> deposit reference."; return; }
await run($("mm-create"), out, "Confirming deposit with the provider and minting...", async () => {
const r = await api("POST", "/v1/mint", { to, amount_micros: amount, deposit_ref, idem_key: deposit_ref });
out.textContent = (r.idempotent_replay ? "Already minted earlier: " : "Minted ") + usd(amount) + " to " + to + ". Reserve now " + usd(r.reserve_after_micros) + ".";
});
}
// ---- reconciliation against provider reports -------------------------
let currentReport = null;
async function loadReports() {
const box = $("reports");
placeholder(box, "loading", "Loading imported reports...");
try {
const r = await api("GET", "/v1/reconciliation/reports");
if (!r.items.length) { placeholder(box, "empty", "No provider reports imported yet. Export a balance / settlement / payouts report from the provider and import it above."); return; }
box.replaceChildren(table(["Imported", "Provider", "Label", "Lines", "Period"],
r.items.map((rep) => {
const tr = el("tr", { class: "clickable" },
el("td", null, when(rep.imported_ms)), el("td", null, rep.provider), el("td", null, rep.label),
el("td", { class: "num" }, rep.line_count),
el("td", null, rep.period_start_ms ? when(rep.period_start_ms) + " to " + when(rep.period_end_ms) : "undated"));
tr.onclick = () => openReport(rep.id);
return tr;
})));
} catch (e) { placeholder(box, "error", "Could not load reports: " + e.message); }
}
async function importReport() {
const out = $("rp-result");
const file = $("rp-file").files[0];
if (!file) { out.textContent = "Choose a report file first."; return; }
if (file.size > 20 * 1024 * 1024) { out.textContent = "The file exceeds 20 MiB; split it by date range."; return; }
await run($("rp-import"), out, "Uploading and matching " + file.name + "...", async () => {
const content = await file.text();
const res = await api("POST", "/v1/reconciliation/reports", {
provider: $("rp-provider").value, label: file.name, content,
amount_unit: $("rp-unit").value, reference_column: $("rp-refcol").value.trim() || null,
apply_reversals: $("rp-apply").checked,
});
const applied = res.summary.reversal_applied ? res.summary.reversal_applied.count : 0;
out.textContent = "Imported " + res.report.line_count + " lines; " + res.discrepancies + " discrepancies" + (applied ? "; " + applied + " refund lines clawed back." : ".");
$("rp-file").value = "";
await loadReports();
renderReport(res);
});
}
async function openReport(id) {
$("report-detail").classList.remove("hidden");
placeholder($("report-lines"), "loading", "Matching against the ledger...");
$("report-missing").replaceChildren();
try { renderReport(await api("GET", "/v1/reconciliation/reports/" + enc(id))); }
catch (e) { placeholder($("report-lines"), "error", "Could not load report: " + e.message); }
}
function renderReport(res) {
currentReport = res;
$("report-detail").classList.remove("hidden");
$("report-title").textContent = res.report.provider + " report " + (res.report.label || res.report.id);
const tile = (label, v) => el("div", { class: "tile" }, el("div", { class: "label" }, label), el("div", { class: "value" }, v.count + " / " + usd(v.micros)));
const s = res.summary;
$("report-tiles").replaceChildren(
tile("Matched", s.matched), tile("Amount mismatch", s.amount_mismatch), tile("Status mismatch", s.status_mismatch),
tile("Missing in Mint", s.missing_in_mint), tile("Refund not clawed back", s.reversal_unrecorded),
tile("Refund clawed back on import", s.reversal_applied || { count: 0, micros: 0 }),
tile("Missing at provider", s.missing_at_provider));
const showAll = $("rp-all").checked;
const lines = res.lines.filter((l) => showAll || (l.status !== "matched" && l.status !== "informational"));
if (!lines.length) {
placeholder($("report-lines"), "empty", showAll ? "The report has no lines." : "Every provider line matches the ledger.");
} else {
$("report-lines").replaceChildren(table(["Line", "Kind", "Reference", "Provider amount", "Mint amount", "Mint account", "Status", ""],
lines.map((l) => {
const action = el("td");
if (l.status === "reversal_unrecorded" && l.mint) {
const b = el("button", { class: "small" }, "Claw back");
b.onclick = async () => {
// Reversal amounts are CUMULATIVE per deposit: sum every refund /
// dispute line the report holds for this deposit (partial refunds
// arrive as separate lines), capped at the minted amount.
const reversedTotal = res.lines
.filter((x) => x.mint && x.mint.reference === l.mint.reference && (x.kind === "refund" || x.kind === "dispute"))
.reduce((sum, x) => sum + Math.abs(Number(x.amount_micros) || 0), 0);
const cumulative = Math.min(reversedTotal || Math.abs(l.amount_micros), l.mint.amount_micros);
if (!window.confirm("Record " + usd(cumulative) + " reversed in total against " + l.mint.reference + " and burn the outstanding part from " + l.mint.account_id + "?")) return;
b.disabled = true;
try {
const r = await api("POST", "/v1/reversals", { deposit_ref: l.mint.reference, amount_micros: cumulative, reason: l.kind === "dispute" ? "dispute" : "refund", note: "from report " + res.report.id });
setStatus("Reversal " + r.status.replace(/_/g, " ") + ": burned " + usd(r.burned_now_micros), "ok");
openReport(res.report.id);
} catch (e) { setStatus(e.message, "err"); b.disabled = false; }
};
action.append(b);
}
return el("tr", null,
el("td", { class: "num" }, l.line_no), el("td", null, l.kind), el("td", { class: "mono" }, l.reference),
el("td", { class: "num" }, usd(l.amount_micros)), el("td", { class: "num" }, l.mint ? usd(l.mint.amount_micros) : "-"),
el("td", { class: "mono" }, l.mint ? l.mint.account_id : "-"), el("td", null, badge(l.status)), action);
})));
}
if (!res.missing_at_provider.length) {
placeholder($("report-missing"), "empty", "Every Mint movement in the report period appears in the report.");
} else {
$("report-missing").replaceChildren(table(["Time", "Kind", "Mint reference", "Amount", "Account"],
res.missing_at_provider.map((m) => el("tr", null,
el("td", null, when(m.ts_ms)), el("td", null, m.kind), el("td", { class: "mono" }, m.reference),
el("td", { class: "num" }, usd(m.amount_micros)), el("td", { class: "mono" }, m.account_id)))));
}
}
async function deleteReport() {
if (!currentReport || !window.confirm("Delete this imported report? The ledger is not affected.")) return;
await run($("rp-delete"), null, null, async () => {
await api("DELETE", "/v1/reconciliation/reports/" + enc(currentReport.report.id));
currentReport = null;
$("report-detail").classList.add("hidden");
setStatus("Report deleted", "ok");
await loadReports();
});
}
// ---- reversals ---------------------------------------------------------
async function loadReversals() {
const box = $("reversals");
placeholder(box, "loading", "Loading reversals...");
const qs = new URLSearchParams({ limit: "200" });
if ($("rv-status").value) qs.set("status", $("rv-status").value);
try {
const r = await api("GET", "/v1/reversals?" + qs);
if (!r.items.length) { placeholder(box, "empty", "No refunds or chargebacks recorded."); return; }
box.replaceChildren(table(["Recorded", "Deposit", "Account", "Reason", "Reversed", "Clawed back", "Outstanding", "Status", "Note", ""],
r.items.map(reversalRow)));
} catch (e) { placeholder(box, "error", "Could not load reversals: " + e.message); }
}
function reversalRow(r) {
const action = el("td");
const ref = enc(r.deposit_ref);
if (r.status === "shortfall") {
const retry = el("button", { class: "small" }, "Retry claw-back");
retry.onclick = async () => {
retry.disabled = true;
try { const u = await api("POST", "/v1/reversals/" + ref + "/retry", {}); tr.replaceWith(reversalRow(u)); setStatus("Burned " + usd(u.burned_now_micros), "ok"); }
catch (e) { setStatus(e.message, "err"); retry.disabled = false; }
};
const wo = el("button", { class: "small danger" }, "Write off");
wo.onclick = async () => {
const note = window.prompt("Write off " + usd(r.outstanding_micros) + " as a loss. Note:", "");
if (note === null) return;
wo.disabled = true;
try { const u = await api("POST", "/v1/reversals/" + ref + "/write-off", { note: note.trim() || null }); tr.replaceWith(reversalRow(u)); }
catch (e) { setStatus(e.message, "err"); wo.disabled = false; }
};
action.append(el("div", { class: "actions" }, retry, wo));
}
const tr = el("tr", null,
el("td", null, when(r.created_ms)), el("td", { class: "mono" }, r.deposit_ref), el("td", { class: "mono" }, r.account_id),
el("td", null, r.reason), el("td", { class: "num" }, usd(r.amount_micros)), el("td", { class: "num" }, usd(r.clawed_micros)),
el("td", { class: "num" }, usd(r.outstanding_micros)), el("td", null, badge(r.status)), el("td", null, r.note), action);
return tr;
}
async function createReversal() {
const out = $("rv-result");
const deposit_ref = $("rv-ref").value.trim();
if (!deposit_ref.includes(":")) { out.textContent = "Enter the <provider>:<ref> of the minted deposit."; return; }
const amountRaw = $("rv-amount").value;
const body = { deposit_ref, reason: $("rv-reason").value, note: $("rv-note").value.trim() || null };
if (amountRaw) body.amount_micros = toMicros(amountRaw);
if (!window.confirm("Record this " + body.reason + " and burn the reversed amount from the credited account?")) return;
await run($("rv-create"), out, "Recording reversal...", async () => {
const r = await api("POST", "/v1/reversals", body);
out.textContent = "Burned " + usd(r.burned_now_micros) + "; status " + r.status.replace(/_/g, " ") + (r.outstanding_micros > 0 ? " (" + usd(r.outstanding_micros) + " outstanding)." : ".");
$("rv-ref").value = ""; $("rv-amount").value = ""; $("rv-note").value = "";
await loadReversals();
});
}
// ---- API keys ----------------------------------------------------------
async function loadKeys() {
const box = $("keys");
placeholder(box, "loading", "Loading keys...");
placeholder($("env-keys"), "loading", "Loading...");
try {
const r = await api("GET", "/v1/keys");
if (!r.items.length) placeholder(box, "empty", "No console-managed keys yet.");
else box.replaceChildren(table(["Name", "Key", "Scopes", "Accounts", "Created", "Status", ""], r.items.map(keyRow)));
if (!r.env_keys.length) placeholder($("env-keys"), "empty", "No MINT_API_KEYS_JSON keys configured.");
else $("env-keys").replaceChildren(table(["Name", "Scopes", "Accounts"], r.env_keys.map((k) => el("tr", null,
el("td", null, k.name), el("td", null, k.scopes.join(", ")), el("td", { class: "mono" }, k.accounts ? k.accounts.join(", ") : "all")))));
} catch (e) {
placeholder(box, "error", "Could not load keys: " + e.message);
$("env-keys").replaceChildren();
}
}
function keyRow(k) {
const action = el("td");
if (k.active) {
const b = el("button", { class: "small danger" }, "Revoke");
b.onclick = async () => {
if (!window.confirm("Revoke key " + k.name + "? Services using it will get 401 immediately.")) return;
b.disabled = true;
try { const u = await api("POST", "/v1/keys/" + enc(k.id) + "/revoke", {}); tr.replaceWith(keyRow(u)); setStatus("Key revoked", "ok"); }
catch (e) { setStatus(e.message, "err"); b.disabled = false; }
};
action.append(b);
}
const tr = el("tr", null,
el("td", null, k.name), el("td", { class: "mono" }, k.prefix + "..."), el("td", null, k.scopes.join(", ")),
el("td", { class: "mono" }, k.accounts ? k.accounts.join(", ") : "all"), el("td", null, when(k.created_ms)),
el("td", null, badge(k.active ? "active" : "revoked")), action);
return tr;
}
async function createKey() {
const out = $("ky-result");
const name = $("ky-name").value.trim();
const scopes = Array.from(document.querySelectorAll("#ky-scopes input:checked")).map((i) => i.value);
const accounts = $("ky-accounts").value.split(/[\s,]+/).map((a) => a.trim()).filter(Boolean);
if (!name || !scopes.length) { out.replaceChildren(el("div", { class: "result" }, "Enter a name and pick at least one scope.")); return; }
await run($("ky-create"), null, null, async () => {
out.replaceChildren(el("div", { class: "result" }, "Creating key..."));
try {
const k = await api("POST", "/v1/keys", { name, scopes, accounts: accounts.length ? accounts : null });
const copy = el("button", { class: "small" }, "Copy");
copy.onclick = () => navigator.clipboard && navigator.clipboard.writeText(k.secret).then(() => setStatus("Key copied", "ok"));
out.replaceChildren(el("div", { class: "secret" },
"Copy this key now. It is shown only once and cannot be recovered: ", el("br"), el("code", null, k.secret), " ", copy));
$("ky-name").value = ""; $("ky-accounts").value = "";
await loadKeys();
} catch (e) { out.replaceChildren(el("div", { class: "result error" }, "Could not create key: " + e.message)); }
});
}
// ---- shell -----------------------------------------------------------
function showTab(name) {
document.querySelectorAll("#tabs button").forEach((b) => b.classList.toggle("active", b.dataset.tab === name));
["overview", "accounts", "payouts", "funding", "reconcile", "reversals", "keys", "payerkeys"].forEach((t) => $("tab-" + t).classList.toggle("hidden", t !== name));
if (name === "overview") loadOverview();
if (name === "accounts") loadAccounts(false);
if (name === "payouts") loadPayouts(false);
if (name === "reconcile") loadReports();
if (name === "funding") loadProviders();
if (name === "reversals") loadReversals();
if (name === "keys") loadKeys();
if (name === "payerkeys") loadPayerKeys();
}
function logout() {
apiKey = "";
sessionStorage.removeItem(KEY);
$("app").classList.add("hidden");
$("tabs").classList.add("hidden");
$("gate").classList.remove("hidden");
$("logout").classList.add("hidden");
$("connect").classList.remove("hidden");
$("key").classList.remove("hidden");
setStatus("Not connected");
}
function connect() {
const k = $("key").value.trim() || apiKey;
if (!k) { setStatus("Enter the API key", "err"); return; }
apiKey = k;
sessionStorage.setItem(KEY, k);
$("key").value = "";
$("gate").classList.add("hidden");
$("app").classList.remove("hidden");
$("tabs").classList.remove("hidden");
$("logout").classList.remove("hidden");
$("connect").classList.add("hidden");
$("key").classList.add("hidden");
showTab("overview");
}
$("connect").onclick = connect;
$("key").addEventListener("keydown", (e) => { if (e.key === "Enter") connect(); });
$("logout").onclick = logout;
document.querySelectorAll("#tabs button").forEach((b) => (b.onclick = () => showTab(b.dataset.tab)));
$("refresh-overview").onclick = loadOverview;
$("acct-open").onclick = () => openAccount($("acct-lookup").value);
$("acct-lookup").addEventListener("keydown", (e) => { if (e.key === "Enter") openAccount($("acct-lookup").value); });
$("acct-more").onclick = () => loadAccounts(true);
$("stmt-run").onclick = loadStatement;
$("kyc-start").onclick = startKyc;
$("payout-load").onclick = () => loadPayouts(false);
$("payout-more").onclick = () => loadPayouts(true);
$("co-create").onclick = createCheckout;
$("na-create").onclick = createAccount;
$("obj-open").onclick = lookupObject;
$("obj-lookup").addEventListener("keydown", (e) => { if (e.key === "Enter") lookupObject(); });
$("acct-freeze").onclick = () => accountLifecycle("freeze");
$("acct-unfreeze").onclick = () => accountLifecycle("unfreeze");
$("acct-close").onclick = () => accountLifecycle("close");
$("acct-address").onclick = newAddress;
$("tr-send").onclick = sendTransfer;
$("rd-kind").onchange = syncRedeemFields;
$("rd-send").onclick = redeem;
$("rcpt-run").onclick = loadReceipts;
$("mm-create").onclick = manualMint;
$("rp-import").onclick = importReport;
// Iyzico merchant-panel refunds may never reach the webhook: default to
// clawing them back on import; other providers apply refunds by webhook.
$("rp-provider").onchange = () => { $("rp-apply").checked = $("rp-provider").value === "iyzico"; };
$("rp-refresh").onclick = loadReports;
$("rp-all").onchange = () => currentReport && renderReport(currentReport);
$("rp-delete").onclick = deleteReport;
$("rv-load").onclick = loadReversals;
$("rv-create").onclick = createReversal;
$("ky-create").onclick = createKey;
$("pos-refresh").onclick = loadPositions;
$("pos-scope").onchange = loadPositions;
$("pk-create").onclick = createPayerKey;
$("pk-load").onclick = loadPayerKeys;
$("pk-filter").addEventListener("keydown", (e) => { if (e.key === "Enter") loadPayerKeys(); });
if (apiKey) connect();
})();
</script>
</body>
</html>