Embedded checkout
Your server creates the checkout; your page opens it in a window over itself. The buyer never leaves your site, except for a payment method that has to finish on its provider's own page.
Before you start
- A product, and a test secret key, from the dashboard.
- The Test provider, connected in Settings → Payment providers: it takes test payments without an account.
- Your website in Payments → Website integration → Allowed websites. The checkout opens over your page only on the websites listed there; anywhere else it opens as its own page, after your
onErrorgetsorigin_not_allowed. While you develop, addhttp://localhost:4242: a local address works in test mode only.
Quickstart: two files
A server that creates the checkout, and a page that opens it. Put your product's ID in the server:
// server.mjs. Run: npm install @shipbill/node, then SHIPBILL_SECRET_KEY=sk_test_… node server.mjs
import { readFileSync } from "node:fs";
import { createServer } from "node:http";
import { ShipBill } from "@shipbill/node";
const shipbill = new ShipBill(process.env.SHIPBILL_SECRET_KEY);
createServer(async (request, response) => {
if (request.method === "POST" && request.url === "/checkout") {
// Your server decides what is bought. A product or price sent by the page is never trusted.
const session = await shipbill.checkout.sessions.create({
line_items: [{ product: "prod_…", quantity: 1 }],
reference: "order_1001", // your own ID for this purchase: it comes back on every event
success_url: "http://localhost:4242/thanks",
});
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ clientToken: session.client_token }));
return;
}
const page = request.url?.startsWith("/thanks") ? "<p>Thanks! Your receipt is on its way.</p>" : readFileSync("index.html");
response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
response.end(page);
}).listen(4242, () => console.log("Open http://localhost:4242"));<!-- index.html -->
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Pro plan</title></head>
<body>
<button id="buy">Buy Pro plan</button>
<script src="https://js.shipbill.io/v1/shipbill.js"></script>
<script>
document.getElementById("buy").addEventListener("click", async () => {
const { clientToken } = await fetch("/checkout", { method: "POST" }).then((r) => r.json());
ShipBill.checkout.open(clientToken, {
// The buyer paid. Say thanks; fulfil the order from the webhook, never from here.
onComplete: () => document.body.append("Thanks! Your receipt is on its way."),
});
});
</script>
</body>
</html>Run npm install @shipbill/node, then SHIPBILL_SECRET_KEY=sk_test_… node server.mjs, open http://localhost:4242, and buy with the test provider.
How it works
- The page asks your server for a checkout; the server creates it with your secret key and answers with its
client_token. ShipBill.checkout.open(clientToken)opens the checkout frompay.shipbill.ioin a frame over your page. Your page can't read inside it, and it tells your page only that it's ready, paid, or closed — never the buyer's details.- A method that must finish on its provider's page — a crypto invoice, a bank's confirmation — opens in the whole window; the buyer then comes back to your
success_url. - Your server learns of the payment from the webhook.
Callbacks
| Callback | When |
|---|---|
onReady() | The checkout is showing. |
onComplete({ status: "paid" }) | The buyer paid. A signal for your page, never proof of payment. |
onClose() | The buyer closed the checkout, paid or not. |
onError(error) | It couldn't open. error.code says why, such as origin_not_allowed. |
On a site that isn't in Allowed websites, onError gets origin_not_allowed at once, and the buyer is taken to the checkout page itself, so the sale still happens. The same happens if it can't be shown over your page for another reason within 15 seconds.
Its look
The checkout looks as your profile's Settings → Checkout → Look says: light, dark, or the buyer's device.
To let your page choose instead, say because visitors switch it between light and dark, turn on Let my website choose the embedded checkout's look under Look, and pass your page's current look as theme: "light", "dark", or "system". With the switch off, theme is ignored, and the browser's console says so.
ShipBill.checkout.open(clientToken, {
theme: pageTheme, // your page's look right now: "light" or "dark"
onComplete: () => showThanks(),
});In React and other bundled apps
@shipbill/js loads the same script, typed. It never bundles the checkout into your app, so you always run the current release:
// npm install @shipbill/js
import { useShipBill } from "@shipbill/js/react";
export function BuyButton() {
const shipbill = useShipBill("pk_test_…");
return (
<button disabled={!shipbill} onClick={() => shipbill?.checkout.open({ product: "prod_…" })}>
Buy Pro plan
</button>
);
}Showing the checkout's price on your page
The checkout may show a visitor their local currency. To show the same figure on your pricing page, ask for it with your publishable key:
<script src="https://js.shipbill.io/v1/shipbill.js"></script>
<script>
ShipBill.init("pk_live_…");
ShipBill.prices.get(["prod_…"]).then(([price]) => {
// price.display: "€45.12", as the checkout will show it · price.base: "$49.00", your own price
document.querySelector("#price").textContent = price.display;
});
</script>It's a preview: the amount charged is fixed when the checkout opens. Products that aren't active come back with available: false.