Pay with crypto button
Instead of redirecting the buyer away, put a button or a link on your own checkout page. One script tag and one element: the hosted checkout opens over your page, the buyer pays without leaving it, and your page hears what happened.
Where it goes
The button lives on your website — the page where the buyer would otherwise click "Pay", typically your checkout or order summary page, next to your card option. Nothing is installed on our side. Each button pays exactly one escrow, so your server creates the escrow first and renders its id into the page.
- Your server creates the escrow (as in Accept a payment).
- Your page loads the script and renders a placeholder with the escrow id.
- The buyer pays in the popup; your server books the payment on the
escrow.fundedwebhook.
1. Create the escrow on your server
Exactly the usual create call. The one thing that matters for the button: set successUrl to a page on the same origin (scheme, host and port) as the page that shows the button. The checkout sends its result only to that origin.
// On your server, when the buyer reaches your checkout page.
const res = await fetch(`${process.env.ESCROW_API_BASE}/escrows`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.ESCROW_API_KEY}`, // never in the browser
'Content-Type': 'application/json',
'Idempotency-Key': `order_${order.id}`,
},
body: JSON.stringify({
amount: order.amountAtomic, // "1000000" = 1 USDC
currency: 'USDC',
sellerWalletId: order.sellerWalletId, // or sellerWallet: "0x…"
externalReference: `order-${order.id}`,
description: order.title,
// Same origin as the page that shows the button, or the page never hears
// "funded" or "cancelled":
successUrl: `https://shop.example/orders/${order.id}/paid`,
cancelUrl: `https://shop.example/orders/${order.id}`,
}),
});
const escrow = await res.json(); // escrow.id = "esc_01J8ZQ…"
// Store escrow.id with the order and render it into the page.2. Add the button to your page
<!-- https://shop.example/orders/4471 : your own checkout page -->
<script src="https://<checkout host>/embed/v1.js" async></script>
<div data-escrow-checkout="esc_01J8ZQ0G7H8K3P2R4T6V8X0Z2B" data-label="Pay with crypto"></div>- The script turns every element with
data-escrow-checkoutinto a button — including ones you add later, so it works in single-page apps. data-labelis the button text; it defaults to "Pay with crypto".- The value must be an escrow id (
esc_and 26 characters). Anything else — an empty value,null, an order number — renders nothing and logs a warning in the browser console. - Load the script from the checkout host your escrows'
checkoutUrlpoints to. Do not copy it to your own server, and do not pin it with anintegrityhash:v1receives fixes in place.
Pay without leaving your page
By default the checkout opens over your page, in a frame, when your site is one you have registered as an allowed site (the dashboard's embed settings, or POST /v1/dashboard/embed-origins). Only registered sites can frame the checkout — any other page that tries gets nothing, and the button falls back to a separate window. If the buyer's browser wallet cannot reach the frame, the frame offers "Open secure checkout in a new window" and passes that window's result back to your page.
data-escrow-modeon the element picks the way:modal,popuporredirect. The default tries the frame, then a window, then a redirect.- A link works too. Mark an ordinary payment link with
data-escrow-link: a plain click opens the checkout over your page; a Ctrl/Cmd-click, a middle click, a new tab, or a page with scripts off just follows the link. - Calling it from your own code?
@escrow/sdk/browserhas the same behaviour as one function. - Call it from a click. Browsers only let a click open a window, and the fall back to a window after a blocked frame works best in browsers that keep the click "fresh" for a few seconds.
<!-- A payment link you already have, kept as a link: -->
<a data-escrow-link href="https://<checkout host>/checkout/esc_01J8ZQ0G7H8K3P2R4T6V8X0Z2B">Pay now</a>import { openCheckout } from '@escrow/sdk/browser';
payButton.addEventListener('click', async () => {
const { outcome } = await openCheckout({
escrowId, // esc_…
checkoutOrigin: 'https://<checkout host>', // where your payment links point
});
// outcome: 'success' | 'failed' | 'cancelled' | 'closed' — a hint for your page.
// Book the payment on your server, from the escrow.funded webhook.
});3. Listen for the outcome
The element dispatches an escrowcheckout event that bubbles, so one listener on document covers every button. event.detail is { escrowId, outcome }.
document.addEventListener('escrowcheckout', (event) => {
const { escrowId, outcome } = event.detail;
// outcome: 'opened' | 'redirected' | 'funded' | 'cancelled' | 'closed'
if (outcome === 'funded') {
showMessage('Payment received — confirming…'); // not "paid": wait for the webhook
pollOrderStatus(escrowId); // your server, which asks the API
}
if (outcome === 'cancelled') showMessage('Payment cancelled. You can try again.');
});| outcome | Meaning | Delivered |
|---|---|---|
| opened | The checkout opened over your page, or in a window. | Always |
| redirected | The browser blocked the popup, so the page is going to the checkout instead. | Always |
| funded | The checkout saw the payment confirmed on chain. | A registered site, or the successUrl origin |
| cancelled | The buyer chose “Cancel and close”, or closed the checkout before sending anything. | A registered site, or the successUrl origin |
| failed | The payment window closed, or the payment ended without success. The reason was shown first. | A registered site, or the successUrl origin |
| closed | The checkout closed without any of those — for example after a payment was sent but before it confirmed. | Always |
Each of funded, cancelled, failed and closed arrives at most once per click. On a page that is neither a registered site nor on the successUrl origin, you get only opened and then closed. The SDK reports funded as success.
Every outcome is advisory. funded carries no amount and anyone can fire a fake event in their own browser. Use it to update the page, never to fulfil an order. Your server marks the order paid only on a verified escrow.funded webhook, exactly as with a redirect.
In React or Next.js
'use client';
import Script from 'next/script';
import { useEffect } from 'react';
export function PayWithCrypto({ escrowId, onFunded }: { escrowId: string; onFunded: () => void }) {
useEffect(() => {
const onOutcome = (event: Event) => {
const { escrowId: id, outcome } = (event as CustomEvent).detail;
if (id === escrowId && outcome === 'funded') onFunded();
};
document.addEventListener('escrowcheckout', onOutcome);
return () => document.removeEventListener('escrowcheckout', onOutcome);
}, [escrowId, onFunded]);
return (
<>
<Script src="https://<checkout host>/embed/v1.js" strategy="afterInteractive" />
{/* key: a new escrow id needs a fresh element — a wired element is never rewired */}
<div key={escrowId} data-escrow-checkout={escrowId} data-label="Pay with crypto" />
</>
);
}Styling
The button is a plain <button class="escrow-pay-button"> inside your element, with no styles of its own. Style it from your stylesheet:
.escrow-pay-button {
padding: 12px 20px;
border: 0;
border-radius: 8px;
background: #1a56db;
color: #fff;
font: 600 16px/1 system-ui, sans-serif;
cursor: pointer;
}Things that break it
Cross-Origin-Opener-Policy: same-originon your page cuts it off from the popup, and no outcome arrives. Usesame-origin-allow-popupsor leave the header out.- Popup blockers. The popup opens directly from the click, which browsers allow. If one blocks it anyway, the page goes to the checkout (
redirected) and the buyer returns through yoursuccessUrlorcancelUrllink. - Frames. The checkout can be framed only by a site you registered, enforced by the browser (
frame-ancestors); every other page, including your own dashboard and the wallet verification page, still refuses. Do not frame it yourself with aniframe— use the button, the link or the SDK, which handle the wallet fallback for you. - An unregistered site with a
successUrlon another origin (for examplewww.versus the bare domain, or a different port in development) meansfundedandcancellednever reach the page. Register the site, or keep the two on one origin.
Try it
With a test key, create an escrow whose successUrl is on the origin you will serve the page from, then open that page in a browser with a wallet extension. Click the button: the popup opens and opened fires. Pay with test USDC and, once confirmed, funded fires and the popup offers "Close this window". On a fresh escrow, choose "Cancel and close" to see cancelled.