# Selling subscriptions to signed-in users

Your app has accounts and a pricing page with tiers — say Basic and Pro, each a subscription product in ShipBill. This is how your server knows which user bought, that they paid, and when their subscription renews, lapses, or changes.

> **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.

## How subscriptions work

A subscription product renews in one of two ways, set on the product as `renewal_mode`:

- **By reminder** (`reminder`, the default, with every provider except those that renew on their own: the merchants of record and Whop). Nobody is charged automatically. Before each renewal ShipBill emails the customer a reminder with a link to pay; you choose when (Settings → Reminders), how long a grace period follows the due date, and whether the link opens ShipBill's renewal page or your own site. A paid renewal continues from the end of the period, so nobody loses days by paying early.
- **Automatically** (`automatic`). A provider with its own subscription engine (Polar, Dodo Payments, Creem, Whop, and Paddle) charges the customer each period, and the customer manages the subscription in that provider's portal. ShipBill still reminds them before each renewal, and turns the provider's notices into the same events. `automatic_or_reminder` renews automatically where the buyer's provider can, and by reminder otherwise.

Your server handles both the same way, from the events below; the subscription's `renewal_mode` says which it is.

## Set up once

- Two subscription products in the dashboard; each has its ID on its page.
- A secret key, a webhook endpoint and its signing secret, and your site in Allowed websites.
- In your database, on the user: the **tier**, **active until**, the **ShipBill subscription ID**, and its **manage link**. And a table of event IDs you've handled. You don't store invoices: ShipBill keeps them.

## 1. Create the checkout for the signed-in user

The browser says which tier; your server maps it to a product, and sets `reference` to the user's ID from its own session — so it's certain who is buying:

*Node · POST /billing/checkout*

```js
const PRODUCTS = { basic: "prod_BASIC…", pro: "prod_PRO…" }; // on the server, never from the page

const session = await shipbill.checkout.sessions.create({
  line_items: [{ product: PRODUCTS[tier], quantity: 1 }],
  reference: user.id,                      // comes back on every event, renewals included
  customer: { email: user.email },         // filled in and fixed on the checkout
  success_url: "https://yourapp.com/billing/done",
  cancel_url: "https://yourapp.com/pricing",
});
return { clientToken: session.client_token };
```

Open it on the page with `ShipBill.checkout.open(clientToken)` ([embedded](https://shipbill.io/docs/embedded-checkout)), or redirect to `session.url` ([hosted](https://shipbill.io/docs/hosted-checkout)).

## 2. Grant access from the webhook

Every subscription event carries the whole subscription, as it is now, in `data.object`:

*Node · POST /webhooks/shipbill*

```js
const event = shipbill.webhooks.verify(rawBody, signatureHeader, WEBHOOK_SECRET); // throws if forged
if (await alreadyHandled(event.id)) return 200;

switch (event.type) {
  case "subscription.created":
  case "subscription.renewed":
  case "subscription.reactivated": {
    const sub = event.data.object;
    await setTier(sub.reference, tierFor(sub.product), {   // tierFor: "prod_PRO…" → "pro"
      activeUntil: sub.access_until,
      subscriptionId: sub.id,
      manageUrl: sub.manage_url,
    });
    break;
  }
  case "subscription.past_due":  await showRenewBanner(event.data.object.reference); break;
  case "subscription.expired":   await setTier(event.data.object.reference, "free"); break;
  case "subscription.cancelled":
    if (!event.data.object.replaced_by) await endAt(event.data.object.reference, event.data.object.current_period_end);
    break;
}
await markHandled(event.id);
return 200;
```

| Event | When | Your app |
| --- | --- | --- |
| `subscription.created` | The first payment succeeded | Grant access until `access_until` |
| `subscription.renewal_upcoming` | The first reminder of a period was sent | Optional: "renews soon" in your app |
| `subscription.renewed` | A renewal was paid | Extend to the new `access_until` |
| `subscription.past_due` | The period ended unpaid; the grace period runs until `grace_until` | Your choice: keep access, or show a banner linking to `manage_url` |
| `subscription.expired` | The grace period ended unpaid | Back to free |
| `subscription.reactivated` | Paid again after expiring: a new period from the payment date | Grant access again |
| `subscription.cancelled` | Cancelled by the customer or by you; or replaced by another subscription product | Keep access until `current_period_end`; ignore it when `replaced_by` is set |

Events can arrive more than once and out of order. Skip IDs you've handled, and since each event carries the whole subscription, apply one only if it's newer than what you have. Your own **active until** date is what ends access if nobody renews, even if an event is missed.

## 3. When the user is back before the webhook

The checkout closes, or the user lands on your `success_url`, a moment before your webhook runs. Ask ShipBill, and apply it if it's paid and it's this user's:

```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.
}
```

For a subscription, the session's `payment` leads to the payment, whose `subscription` you can retrieve. The webhook arriving afterwards finds nothing left to change.

## 4. Switching to another subscription product

Name the subscription being replaced, so the user never has two. When the new product's first payment succeeds, the old subscription is cancelled in the same step and sends no more reminders:

```js
await shipbill.checkout.sessions.create({
  line_items: [{ product: PRODUCTS.pro, quantity: 1 }],
  reference: user.id,
  customer: { email: user.email },
  replace_subscription: user.shipbillSubscriptionId, // the Basic subscription
});
```

You receive `subscription.created` for Pro and `subscription.cancelled` for Basic, with `replaced_by`. The new subscription is charged in full and starts now; there's no automatic credit for unused time. If the buyer abandons the checkout, nothing changes.

## 5. Renewing from your site

For a subscription renewed by reminder, the emails link to ShipBill's renewal page. For a Renew button in your own app:

```js
const session = await shipbill.subscriptions.createRenewalSession(user.shipbillSubscriptionId);
return { clientToken: session.client_token }; // open it like any checkout
```

It covers the next period, or restarts an expired subscription at the product's current price. The subscription already carries your `reference`. To have reminders point at your site instead, choose "My website" in Settings → Reminders.

## 6. Letting users manage it

Link to `manage_url` for a "Manage subscription" button: the customer's own page, where they see their next due date and receipts, pay, or cancel. For a subscription renewed automatically it leads on to the provider's portal, where they also change their card. Your server can make the same link for a customer with `POST /v1/customers/{id}/portal-session`.

## Customers you already have

Subscribers paying you elsewhere can be added without a payment, with the date they've paid to; they're reminded like everyone else from then on. In the dashboard (one at a time or by CSV), or:

```js
await shipbill.subscriptions.create({
  customer_email: "ana@example.com",
  product: "prod_PRO…",
  paid_until: 1796342400,    // unix seconds
  reference: "user_4821",
});
```

## Mistakes to avoid

| Mistake | Instead |
| --- | --- |
| Upgrading in `onComplete` or on the success page alone | Upgrade from the verified webhook, or after your server asks ShipBill |
| Sending a product ID or price from the page to your server | Map the tier's name to a product on the server |
| Trusting `session_id` in the return address | Check the session's `reference` is the signed-in user |
| Parsing the webhook body before verifying it | Verify the raw body |
| Applying a repeated event twice | Record the event IDs you've handled |
| Never ending access | Honour your stored active-until date, and `subscription.expired` |

> **The whole thing, running**
>
> The SDK repository's `examples/pricing-page` is this guide as a small site: two subscription products, the embedded checkout, the webhook, switching between them, and renewals.

Last checked against the product on 12 October 2026.
