# Hosted checkout

Your server creates a checkout for exactly what is being bought, then sends the buyer to ShipBill's checkout page. When they've paid, they come back to your site.

> **Fulfil on the webhook, not on the redirect**
>
> The buyer coming back to your page, or the checkout's `onComplete`, only says they were there. Deliver the order when your server receives a verified [webhook](https://shipbill.io/docs/webhooks), or after your server asks ShipBill for the checkout and finds it paid.

## 1. Create the checkout on your server

Name the products in `line_items` (or a [custom amount](https://shipbill.io/docs/hosted-checkout#custom-amount): `amount` and `currency`), your own ID in `reference`, and where the buyer goes afterwards:

**Node**

```js
import { ShipBill } from "@shipbill/node";
const shipbill = new ShipBill(process.env.SHIPBILL_SECRET_KEY);

// In your "Buy" route, after your own checks:
const session = await shipbill.checkout.sessions.create({
  line_items: [{ product: "prod_…", quantity: 1 }],
  reference: order.id,                 // your ID: it comes back on every event
  customer: { email: user.email },     // optional: filled in, and fixed on the checkout
  success_url: "https://example.com/thanks",  // ShipBill adds ?session_id=cs_…
  cancel_url: "https://example.com/pricing",
});
// Send the buyer there: an HTTP 303 to session.url.
response.redirect(303, session.url);
```

**curl**

```bash
curl https://api.shipbill.io/v1/checkout/sessions \
  -H "Authorization: Bearer $SHIPBILL_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order_1001-checkout" \
  -d '{
    "line_items": [{ "product": "prod_…", "quantity": 1 }],
    "reference": "order_1001",
    "success_url": "https://example.com/thanks"
  }'
```

**Python**

```python
import os, requests

response = requests.post(
    "https://api.shipbill.io/v1/checkout/sessions",
    headers={
        "Authorization": f"Bearer {os.environ['SHIPBILL_SECRET_KEY']}",
        "Idempotency-Key": "order_1001-checkout",
    },
    json={
        "line_items": [{"product": "prod_…", "quantity": 1}],
        "reference": "order_1001",
        "success_url": "https://example.com/thanks",
    },
    timeout=30,
)
response.raise_for_status()
session = response.json()  # session["url"], session["client_token"]
```

**PHP**

```php
<?php
$ch = curl_init("https://api.shipbill.io/v1/checkout/sessions");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("SHIPBILL_SECRET_KEY"),
        "Content-Type: application/json",
        "Idempotency-Key: order_1001-checkout",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "line_items" => [["product" => "prod_…", "quantity" => 1]],
        "reference" => "order_1001",
        "success_url" => "https://example.com/thanks",
    ]),
]);
$session = json_decode(curl_exec($ch), true); // $session["url"], $session["client_token"]
```

A checkout lasts 24 hours unless you set `expires_in` (30 minutes to 24 hours). Supplying the customer's email fills it in and fixes it on the checkout.

> **Attribution**
>
> Analytics → Sources shows the site the buyer came to the checkout from, which is usually your own page. A referrer is only the page before, so a buyer who clicked around your site shows your site. To keep where they first arrived from, pass `attribution` when your server creates the checkout: `page_url`, `referrer`, and `utm_source`, `utm_medium`, and `utm_campaign`, as your site recorded them. ShipBill keeps the addresses' origin and path only.

## 2. Send the buyer to it

Redirect to the session's `url`. The buyer chooses a method, pays, and sees ShipBill's confirmation with a button back to your `success_url`, to which ShipBill adds `?session_id=cs_…`.

> **Not in an iframe**
>
> A payment link or the hosted checkout page can't be shown in an iframe on your site; it comes out blank. To show a checkout over your page, create it from your server and open its `client_token` with [`checkout.open`](https://shipbill.io/docs/embedded-checkout).

## 3. When the buyer returns

The return can arrive a moment before your webhook. To show "paid" straight away, ask ShipBill — and check that the checkout is this buyer's, so nobody can paste someone else's session ID:

```js
// GET /thanks?session_id=cs_…
const session = await shipbill.checkout.sessions.retrieve(request.query.session_id);
if (session.reference !== currentOrder.id) return forbidden(); // someone else's checkout
if (session.payment_status === "paid") {
  // Paid: show it now. The webhook may still be on its way; apply it once, whichever comes first.
}
```

## 4. Fulfil from the webhook

Your endpoint receives `payment.succeeded` with the payment, its `reference`, and its `line_items`. See [Webhooks](https://shipbill.io/docs/webhooks) for checking it.

## Custom amounts

Your server, with a secret key, can send `amount`, `currency`, and `description` instead of `line_items`. It's one line and a one-time payment, and no coupon applies to it. Each provider takes it within its own currencies and limits.

| Takes a custom amount | Never takes one |
| --- | --- |
| Stripe, Whop, Cryptomus, OxaPay, NOWPayments, Midtrans, Xendit, DOKU, Square, SumUp, and the test provider, in test mode | Polar, Dodo Payments, Creem, Paddle |

A merchant of record is the legal seller, so it charges the tax by the kind of product and sells only what it has reviewed. ShipBill makes its copy of a product at the first sale, and needs a product to copy.

When an amount repeats, such as top-ups of 5, 25, and 100, make a product for each and send `line_items`. Every provider takes a product, merchants of record included, and each sale shows the product's name in the dashboard.

If every provider enabled in the mode is a merchant of record, the API refuses a custom amount with `custom_amount_unavailable`. If none suits the buyer, by their currency or country, the checkout says "Payments aren't available for this checkout right now."

## Several products, or several of one

`line_items` takes up to 20 of your products, each with a `quantity` from 1 to 99, all in one currency. A subscription is the first line, with only its own add-ons after it: it renews at its quantity, and its one-time add-ons are charged with the first payment only. Polar and Creem can't take a subscription with add-ons. Your invoice and the buyer's receipt list every line. Each provider is sent the order as it takes one:

| Providers | Several products | Several of one |
| --- | --- | --- |
| Stripe, Cryptomus, OxaPay, NOWPayments, Midtrans, Xendit, DOKU, Square, SumUp, and the test provider | The total, with each line on ShipBill's checkout and invoice | The total, with each line on ShipBill's checkout and invoice |
| Polar, Creem | A product of its own named for the set, such as "Product A + Add-on X" | Its own units: "3 × Product A" |
| Dodo Payments, Paddle | Each line, at its price and quantity | Each line, at its price and quantity |
| Whop | One price: the total | One price: the total |

A copy named for a set is made once and reused for the same set, at most 50 at each provider; past that, a set goes to your other providers. When a line's price after a coupon doesn't divide by its quantity, or one unit is below a provider's least amount, it goes as one unit at its total, named "Product A × 3".

If no provider enabled in the mode can take the products together, because each is limited to providers the others aren't, or a set needs a copy where the only providers that could take it already keep 50, the API refuses the checkout with `items_unavailable`, saying which.

## Add-ons

A product's **add-ons** are other products of yours sold with it. The buyer gets them in one of two ways:

- **Your own selection.** Your page or server picks them: a checkout's `line_items`, after the product; a buy button's `data-shipbill-addons`. The checkout shows those items and no boxes. ShipBill checks each is one of the product's add-ons, so a page can't attach another product to a cheaper one, and prices it.
- **ShipBill's selection.** An add-on marked **Offer at the checkout** is a box with its price, unticked, beside the items already chosen. Ticking prices the order again: its tax, its currency, and the payment methods that can take it. A buy button offers the product's unless its page sends its own selection; a checkout your server creates offers them with `offer_addons: true`; a payment link offers those it was made to.

On a subscription, a one-time product is charged with the first payment only. A subscription product with the subscription's period and currency is a **recurring add-on**: it renews with it, each period at its own price. The subscriber, on their page, or you, on the subscription's page or with `POST /v1/subscriptions/{id}/addons/{product}`, can remove one at the end of the period or add one from the next renewal, with nothing charged or refunded at the change. What each provider takes on a subscription:

| Providers | Add-ons on a subscription |
| --- | --- |
| Stripe, Dodo Payments, Paddle, Cryptomus, OxaPay, NOWPayments, Midtrans, Xendit, DOKU, Square, SumUp, and the test provider | One-time and recurring add-ons; a recurring one can be removed or added later |
| Polar, Creem | Neither: its bundles are one-time, so a renewal would charge the add-ons again |
| Whop | One-time add-ons, with the first payment; no recurring ones, since a membership's price can't change |

A subscription with add-ons that none of your enabled providers can take is refused with `items_unavailable`, naming them.

Last checked against the product on 31 October 2026.
