Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

8.6 KB
Raw Blame
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>x402 Payment Modal — Plain HTML demo</title>

  <!--
    ─────────────────────────────────────────────────────────────────────────
    DEMO ONLY

    This page shows three ways to use @three-ws/x402-payment-modal with no
    build step at all — just an ES module loaded from a CDN.

      (a) Declarative: a button annotated with data-x402-* attributes. The
          library auto-binds these on load — zero JavaScript required.
      (b) Programmatic: a button wired in an inline module that imports pay().
      (c) Events: x402:result / x402:error bubble from the clicked element;
          we listen and render the outcome into #outcome below.

    The endpoint below (https://api.example.com/...) is a PLACEHOLDER. To
    actually take a payment, point data-x402-endpoint / pay({ endpoint }) at a
    REAL x402-enabled HTTP endpoint that responds with HTTP 402 + an x402
    challenge. The modal handles the 402 → connect wallet → sign → settle flow.

    Self-hosting: instead of the unpkg URL you can serve the module yourself,
    e.g. <script type="module" src="/vendor/x402.min.js" ...>. The data-x402-*
    config attributes work the same way on a self-hosted <script> tag.
    ─────────────────────────────────────────────────────────────────────────
  -->

  <style>
    :root {
      --bg: #0b0c10;
      --card: #15171e;
      --border: #262a35;
      --text: #e7e9ee;
      --muted: #9aa1b1;
      --accent: #6c8cff;
      --accent-press: #5476f5;
      --ok: #3ddc97;
      --err: #ff6b6b;
    }
    * { box-sizing: border-box; }
    html, body { height: 100%; }
    body {
      margin: 0;
      background: radial-gradient(1200px 600px at 50% -10%, #1a1d27 0%, var(--bg) 60%);
      color: var(--text);
      font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
      display: grid;
      place-items: center;
      padding: 32px 16px;
    }
    .card {
      width: 100%;
      max-width: 520px;
      background: var(--card);
      border: 1px solid var(--border);
      border-radius: 16px;
      padding: 28px;
      box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
    }
    h1 { font-size: 20px; margin: 0 0 4px; letter-spacing: -0.01em; }
    p.lede { margin: 0 0 22px; color: var(--muted); }
    h2 {
      font-size: 12px;
      text-transform: uppercase;
      letter-spacing: 0.08em;
      color: var(--muted);
      margin: 22px 0 8px;
    }
    .row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
    button {
      appearance: none;
      border: 0;
      border-radius: 10px;
      padding: 11px 18px;
      font: inherit;
      font-weight: 600;
      color: #fff;
      background: var(--accent);
      cursor: pointer;
      transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
    }
    button:hover { background: var(--accent-press); }
    button:active { transform: translateY(1px); }
    button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
    button[disabled] { opacity: 0.55; cursor: progress; }
    .hint { color: var(--muted); font-size: 13px; }
    code {
      background: #0c0e14;
      border: 1px solid var(--border);
      border-radius: 6px;
      padding: 1px 6px;
      font-size: 12px;
    }
    #outcome {
      margin-top: 22px;
      min-height: 44px;
      border: 1px dashed var(--border);
      border-radius: 12px;
      padding: 14px 16px;
      color: var(--muted);
      font-size: 13px;
      white-space: pre-wrap;
      word-break: break-word;
      transition: border-color 0.2s ease;
    }
    #outcome.ok { border-color: var(--ok); color: var(--ok); }
    #outcome.err { border-color: var(--err); color: var(--err); }
    .footer { margin-top: 20px; color: var(--muted); font-size: 12px; }
    a { color: var(--accent); }
  </style>

  <!--
    (a) Declarative usage.

    Loading the module runs init() automatically: any element carrying a
    data-x402-endpoint attribute becomes a pay trigger. Config that applies to
    the whole page (brand, where the Solana checkout endpoint lives) rides on
    THIS script tag as data-x402-* attributes.
  -->
  <script
    type="module"
    src="https://unpkg.com/@three-ws/x402-payment-modal"
    data-x402-brand-name="Acme Summaries"
    data-x402-brand-url="https://example.com"
    data-x402-checkout-origin="https://example.com"
    data-x402-footer-note="Payments settle in USDC. Powered by x402."
  ></script>
</head>

<body>
  <main class="card">
    <h1>x402 Payment Modal</h1>
    <p class="lede">Pay any x402 endpoint in one click — no wallet integration code.</p>

    <!-- (a) Zero-JS declarative button. The attributes describe the request. -->
    <h2>Declarative (no JavaScript)</h2>
    <div class="row">
      <button
        id="declarative-pay"
        data-x402-endpoint="https://api.example.com/paid/summarize"
        data-x402-method="POST"
        data-x402-body='{"url":"https://en.wikipedia.org/wiki/x402"}'
        data-x402-merchant="Acme Summaries"
        data-x402-action="Summarize article"
      >
        Summarize for 0.01 USDC
      </button>
      <span class="hint">Driven entirely by <code>data-x402-*</code> attributes.</span>
    </div>

    <!-- (b) Programmatic button — wired by the inline module below. -->
    <h2>Programmatic (inline module)</h2>
    <div class="row">
      <button id="programmatic-pay">Translate for 0.01 USDC</button>
      <span class="hint">Calls <code>pay()</code> from a <code>&lt;script type="module"&gt;</code>.</span>
    </div>

    <!-- (c) Outcome surface — populated by the x402:result / x402:error listeners. -->
    <h2>Outcome</h2>
    <div id="outcome" role="status" aria-live="polite">No payment attempted yet.</div>

    <p class="footer">
      Endpoints above are placeholders — swap in a real x402 endpoint to take a payment.
      You can also self-host the module instead of unpkg.
    </p>
  </main>

  <script type="module">
    // (b) Programmatic usage: import pay() straight from the CDN module.
    // In a self-hosted setup, import from your own URL instead, e.g.
    //   import { pay } from '/vendor/x402.min.js';
    import { pay } from 'https://unpkg.com/@three-ws/x402-payment-modal';

    const button = document.getElementById('programmatic-pay');

    button.addEventListener('click', async () => {
      button.disabled = true;
      const original = button.textContent;
      button.textContent = 'Processing…';
      try {
        const result = await pay({
          endpoint: 'https://api.example.com/paid/translate',
          method: 'POST',
          body: { text: 'Hello, world', to: 'es' },
          merchant: 'Acme Translate',
          action: 'Translate text',
        });
        // result = { ok, result, payment?, siwx?, response }
        console.log('Payment result:', result);
      } catch (err) {
        // The modal rejects with err.code === 'cancelled' when dismissed.
        if (err && err.code === 'cancelled') {
          console.log('User cancelled the payment.');
        } else {
          console.error('Payment failed:', err);
        }
      } finally {
        button.disabled = false;
        button.textContent = original;
      }
    });

    // (c) Event listeners. x402:result and x402:error bubble from the element
    // that triggered the payment, so a single document-level listener covers
    // both the declarative and programmatic buttons.
    const outcome = document.getElementById('outcome');

    document.addEventListener('x402:result', (event) => {
      const detail = event.detail; // PayResult: { ok, result, payment?, siwx?, response }
      outcome.className = 'ok';
      const payer = detail.payment?.payer ? `\nPayer: ${detail.payment.payer}` : '';
      const tx = detail.payment?.transaction ? `\nTx: ${detail.payment.transaction}` : '';
      outcome.textContent = `Paid ✓  (${detail.payment?.network ?? 'settled'})${payer}${tx}`;
    });

    // detail.error is a string. Note: dismissing the modal does NOT fire this
    // event — the auto-binder swallows the cancellation — so anything here is a
    // genuine failure.
    document.addEventListener('x402:error', (event) => {
      outcome.className = 'err';
      outcome.textContent = `Payment failed: ${event.detail?.error ?? 'unknown error'}`;
    });
  </script>
</body>
</html>