Custom checkout
Your own payment page, driven by ShipBill's payment intents. You show what is bought and the methods; ShipBill picks the provider, and the buyer pays on its page. Card numbers never reach you or ShipBill.
Before you start
- A product, and a test secret key, from the dashboard. Every call is from your server.
- The Test provider, connected in Settings → Payment providers: it takes test payments without an account.
- While you develop,
return_urlcan be a local address such ashttp://localhost:4242/paid: with test keys only.
1. Create a payment intent
When the buyer opens your payment page, send what they buy (line_items, or a custom amount: amount and currency) and who they are. The email is required: it gets the invoice, issued now with your tax, and the receipt. Give the buyer's country, or their address, so your tax and the provider suit where they are.
import { ShipBill } from "@shipbill/node";
const shipbill = new ShipBill(process.env.SHIPBILL_SECRET_KEY);
// 1. When the buyer opens your payment page: what they buy, and who they are.
const intent = await shipbill.paymentIntents.create({
line_items: [{ product: "prod_…", quantity: 1 }],
customer: { email: user.email, country: "DE" }, // country: for your tax and routing
reference: order.id, // your ID: it comes back on every event
return_url: "https://example.com/paid", // ShipBill adds ?payment_intent=pi_…
});
// Show intent.payment_methods: each one's label ("Card"), and its amount with your tax.
// It's never updated: if the cart, the email, or the country changes, cancel it and create another.
// 2. When the buyer picks a method and presses Pay: hand next_action to your page.
const confirmed = await shipbill.paymentIntents.confirm(intent.id, {
payment_method_type: "card",
});
response.json({ next_action: confirmed.next_action });payment_methods lists the methods it can be paid with, each with its label for the buyer ("Card", "Bank transfer", "QRIS") and what it charges: your price with your tax, perhaps in another currency. A method sold through a merchant of record (merchant_of_record set) adds its own tax on its page instead. An intent lasts 24 hours unless you set expires_in (30 minutes to 24 hours).
An intent is never updated. When the cart, the email, or the country changes, cancel it and create another.
2. Confirm it with the buyer's method
When the buyer presses Pay, your server confirms the intent with their method. ShipBill routes it to one of your providers (your routing rules apply) and starts the payment there, and answers with next_action: where the buyer pays. Hand it to your page as it is.
3. Send the buyer on, or mount the provider's fields
ShipBill.mount(next_action, element), in shipbill.js, does what next_action says, so your page needs nothing per provider. For a redirect it sends the buyer to the provider's page. For a client_component (Stripe today) it mounts the provider's secure card fields in your element; when the buyer presses Pay, confirm() pays, and the provider takes them on (3-D Secure, their bank) and back to your return_url. It needs no ShipBill.init.
<script src="https://js.shipbill.io/v1/shipbill.js"></script>
<div id="fields"></div>
<script>
// next_action, as your server's confirm answered it:
const action = await ShipBill.mount(next_action, "#fields");
// "redirect": the buyer is on their way to the provider's page.
// "fields": the provider's card fields are in #fields. When the buyer presses Pay:
payButton.onclick = async () => {
const { error } = await action.confirm(); // returns only if they stay on this page
message.textContent = error.message; // "Your card was declined."
};
</script>Card details go from the buyer to the provider only. For Stripe, shipbill.js loads its small Stripe file from js.shipbill.io, which loads Stripe.js from js.stripe.com, when you call it and only then: a page that never mounts fields downloads neither. If your page has a Content-Security-Policy, allow https://js.stripe.com in script-src and frame-src, beside https://js.shipbill.io. The fields' return_url is ShipBill's own address, which sends the buyer on to yours with ?payment_intent=pi_… alone, as every provider does.
Your page draws everything around the fields, so their look is yours to set: mount's third argument takes Stripe's Appearance API. On a dark page, pass { appearance: { theme: "night" } }, as Stripe documents.
4. When the buyer returns
The provider sends them to your return_url, to which ShipBill adds ?payment_intent=pi_…. Reading the intent asks the provider about a payment still under way. Check it is this buyer's:
// GET /paid?payment_intent=pi_…
const intent = await shipbill.paymentIntents.retrieve(request.query.payment_intent);
if (intent.reference !== currentOrder.id) return forbidden(); // someone else's payment
if (intent.status === "succeeded") {
// Paid: show it now. Apply the webhook once, whichever comes first.
} else if (intent.status === "requires_payment_method") {
// Not paid: show last_payment_error?.message and the methods, and confirm again.
}
// "processing": the provider is still confirming it; the webhook says when it's done.| Status | What to do |
|---|---|
requires_payment_method | Confirm it. After a failure, last_payment_error says why in words for the buyer, and it can be confirmed again, with the same method or another. |
requires_action | Send the buyer on with ShipBill.mount(next_action, element). |
processing | The provider is confirming it. Wait for the webhook. |
succeeded | Paid. payment and invoice name what it made. |
expired, cancelled | Closed: create a new one if the buyer comes back. |
If the buyer leaves, cancel the intent: its invoice is voided. An intent being paid at the provider can't be cancelled, since the buyer may still be paying there.
5. Fulfil from the webhook
Your endpoint receives payment.succeeded with the payment, its reference, its line_items, and its payment_intent. See Webhooks for checking it.