> The full documentation index: https://palbase.studio/llms.txt

# Subscriptions

Manage recurring card subscriptions across **Stripe** and **iyzico** channels through one canonical API — with trials, period tracking, support extensions, self-service cancellation, and `payments.subscription.*` webhook events.

> Looking for mobile app-store subscriptions (StoreKit 2 & Google Play Billing) paired with paywalls and entitlements? See [In-App Purchases](/docs/iap/overview).

## Creating a recurring subscription

Create a subscription from your server via `POST /payments/subscriptions`. If the channel requires an initial card capture or 3D Secure verification, the returned `SubscriptionView` includes a `next_action` you hand off to the customer:

```ts
import { createServer } from "palbase/server";

const palbase = createServer({
  url: process.env.PALBASE_URL!,
  secretKey: process.env.PALBASE_SECRET_KEY!,
});

export async function startSubscription(customerId: string, planRef: string) {
  const { data: sub } = await palbase.payments.createSubscription({
    headers: { "Idempotency-Key": `sub:${customerId}:${planRef}` },
    body: {
      customer_id: customerId,
      product_name: "Pro Monthly",
      amount: 19900,
      currency: "TRY",
      interval: "month",
      interval_count: 1,
      description: "Pro Monthly",
      return_url: "https://app.example.com/billing",
      cancel_url: "https://app.example.com/pricing",
    },
  });
  return sub;
}
```

## Seats

A subscription can be of several of its unit: seats, for an app sold by the seat. `amount` is what one costs a period, `quantity` how many:

```http
POST /payments/subscriptions

{
  "customer_id": "team_42",
  "product_name": "Pro seat",
  "amount": 2000,
  "currency": "USD",
  "interval": "month",
  "quantity": 5,
  "return_url": "https://app.example.com/billing"
}
```

The subscription then says `quantity: 5`, `unit_amount: 2000` and `amount: 10000` — what a period costs — and its first payment is of all five. Without a `quantity` it is of one, and `amount` is its `unit_amount`.

Seats are sold on a channel whose [capabilities](/docs/payments/channels#capabilities-matrix) say `subscription_quantity` (Stripe). Elsewhere a quantity above one is refused with `422 quantity_not_supported`: a subscription is never sold as one seat in silence.

### Changing the quantity

`POST /payments/subscriptions/{subscription_id}/quantity` makes a running subscription of another quantity. It takes your secret key: who sits on a seat, and who may buy one, is your app's own rule.

```http
POST /payments/subscriptions/8d3a…/quantity

{ "quantity": 6 }
```

| You ask for | What happens |
|---|---|
| More seats than the subscription has | The days left of the period are charged at once, and the answer has the new `quantity`. When the charge is not paid nothing is added and nothing is left waiting: see [When the charge is not paid](#when-the-charge-is-not-paid). |
| Fewer | No money comes back. The seats stay until the period's end — `quantity` goes on saying how many — and `renewal_quantity` says how many the next renewal bills. |
| More again, up to what it has | Nothing is charged: those seats are paid for. Above it, only the seats above are charged. |

`proration` asks otherwise:

| `proration` | For seats added | For fewer seats |
|---|---|---|
| `charge_now` | The default: the days left are charged at once. Where the subscription is paid by a method that takes days to pay (a bank debit), the seats are added at once and their charge is in process; if the debit later fails, the provider follows it by its own rules for a failed payment of the subscription, which can end the subscription. Use `next_invoice` there to keep seats and renewal on one invoice. | Refused (`400 invalid_proration`): there is nothing to charge. |
| `next_invoice` | Added at once; the days left are put on the next invoice. | They go at once — `quantity` drops — and the days left are credited on the next invoice. |
| `none` | Refused (`400 invalid_proration`): seats are sold, now or on the next invoice. | The default: they stay until the period's end, and no money comes back. |

The answer is the subscription as it stands after the change, with `charged_now`: what this change charged at once, in minor units — the added seats' days left with `charge_now`, and `0` where it charged nothing then (fewer seats, seats already paid for in the period, `next_invoice`). A call asked again after its answer was lost finds the seats already there and says `0`: what the first charged is on the provider's invoice of the change.

Asked twice, the call sets the quantity it names and charges nothing a second time — a call that failed is asked again the same way, with the same `Idempotency-Key` or another: it reads how the subscription stands and does what is still to do. One change is made at a time (`409 subscription_busy`: ask again), and only of a subscription that is paid for and running (`409 subscription_not_active`).

Seats are changed through this call alone. Leave quantity changes off in the payment provider's own customer portal (Stripe's is off unless you turn it on): a quantity changed on the provider's pages is followed here — more seats at once, fewer from the next renewal — but what it charged or credited is the provider's doing, by its portal's settings, not this API's rules.

A subscription you [extend](#operator--support-actions) keeps its seats through the days given: fewer seats asked for the next renewal are billed when those days end.

A subscription bought at a discount (an [In-App Purchases offer](/docs/iap/offers)) has its seats prorated by the provider's rules for that discount. While a discount of some months lasts, seats added are charged at the discounted price, and the days given back for fewer seats are credited at it. A discount of the first charge alone is over once that charge is paid: the days given back are credited at what was paid for them — the discounted price — and the days left of the new quantity are charged at the full price. Lowering seats at once with `next_invoice` in the first period of such a subscription can therefore put a small charge, not a credit, on the next invoice; lowering them at the period's end (the default) charges and credits nothing.

#### When the charge is not paid

Seats charged at once are charged off session — nobody is on a payment page when your server asks — and added only when that charge is paid. Not paid, nothing is added and nothing is left waiting at the provider: no invoice, no change to take back. `quantity` is as it was:

| Answer | What happened | What to do |
|---|---|---|
| `402 seat_payment_failed` | The card declined the charge. | Ask again once the customer has a card that pays. |
| `402 seat_payment_requires_action` | The card asks for its customer's own confirmation of every charge (3-D Secure), which a call from your server cannot give. A card that was confirmed once, when the customer paid at checkout or saved it, is charged without asking again: this is a card that asks each time. | Ask again once the customer pays with a card that asks for none, or add the seats with `"proration": "next_invoice"`: they are added at once, and their charge is put on the next invoice. |

Every change tells `payments.subscription.changed` (`change: "updated"`) with `quantity`, `unit_amount`, `amount` and, when fewer are billed next, `renewal_quantity`. A renewal (`change: "renewed"`) tells the quantity it billed. `pending_quantity` appears only for a change made at the provider itself that waits there for its invoice — nothing this API is asked leaves one waiting; asking for the quantity the subscription has drops it.

## Subscription states

| Status | Meaning |
|---|---|
| `pending` | Created and awaiting initial payment / 3DS completion. |
| `trialing` | Active in a free trial window (`trial_end`). |
| `active` | Paid and renewing automatically until canceled. |
| `past_due` | Renewal charge failed; provider retry schedule is active. |
| `paused` | Collection is paused at the provider. |
| `canceled` | Ended (`ended_at` says when): it gives nothing more. One only set not to renew stays `active`, with `cancel_at_period_end`. |
| `expired` | Ended without renewal. |

A subscription is `pending` until its first payment is made; from then on its row names the provider's subscription (`provider_sub_id`) and follows it — whoever changes it: your server, support, or the customer in the provider's portal.

## Events

Each change of a subscription is told once, to the project's webhooks and to a `palbase/backend` app's `@On` handlers ([Webhooks](/docs/server/webhooks)):

| Type | Sent when |
|---|---|
| `payments.subscription.changed` | It started, a period was paid for, a renewal was not charged, or something else of it moved: `change` says which. |
| `payments.subscription.canceled` | Its renewal was turned off — it keeps the period paid for (`cancel_at_period_end`) — or it ended (`status` `canceled`, `ended_at`). |

Two things at once are two events — a period's payment, then the cancellation that came with it. Both carry the subscription as it stands: `subscription_id`, `customer_id`, `provider`, `provider_sub_id`, `channel_key`, `livemode`, `status`, `cancel_at_period_end`, `current_period_start`, `current_period_end`, `trial_end`, `amount`, `currency`, `interval` and `interval_count`.

| `change` | What it tells |
|---|---|
| `started` | The first payment was made. The payment itself is `payments.payment.succeeded`, which carries the period it paid for. |
| `renewed` | A period's payment: `transaction` holds the provider's invoice (`id`), `amount_paid`, `tax_amount`, and the period it pays for (`period_start`, `period_end`). The payment is a payment of its own as well — see [A subscription's charges are payments](#a-subscriptions-charges-are-payments). |
| `charged` | A payment within the period — seats added and charged at once: `transaction` holds the provider's invoice (`id`), `amount_paid` and `tax_amount`, with no period (it renews nothing). Told once, when the invoice is paid. Any change the provider charges within a period on its own invoice is told so: a quantity changed here, and at Stripe also a plan changed or a trial ended early from its dashboard or portal. |
| `payment_failed` | A renewal was not charged: `status` is `past_due`, with the `invoice_id` and the `amount_due`. The provider tries again on its own schedule. |
| `canceled` | Its renewal was turned off, or it ended. |
| `updated` | Anything else: the period moved on, a failed charge was made after all, the renewal was turned back on. |

```ts
// src/modules/billing/subscription-events.ts
import { Log, On, type PlatformEvent } from "palbase/backend";

export class SubscriptionEvents {
  @On("payments.subscription.changed")
  changed(event: PlatformEvent<"payments.subscription.changed">): void {
    const { subscription_id, customer_id, status, current_period_end } = event.data;
    Log.info("subscription changed", { subscription_id, customer_id, status, current_period_end });
  }

  @On("payments.subscription.canceled")
  canceled(event: PlatformEvent<"payments.subscription.canceled">): void {
    const { subscription_id, status, cancel_at_period_end, current_period_end } = event.data;
    // status "canceled": it has ended. Otherwise it runs until current_period_end and does not renew.
    Log.info("subscription canceled", { subscription_id, status, cancel_at_period_end, current_period_end });
  }
}
```

A plan bought through [In-App Purchases' web checkout](/docs/iap/web) is a subscription here as well — listed, canceled and extended like any other, the list naming the catalog's `iap_product` and `iap_plan` it was bought as — and its events carry `iap_context` (the catalog's `product_key` and `plan_key`). What the customer has by it is In-App Purchases' to say: read [`CustomerInfo`](/docs/iap/entitlements), not these events.

A subscription a buyer with no account bought that way is nobody's here: its `customer_id` is empty, it is listed without a `customer_id` filter, and its events are told all the same. Whoever redeems the purchase has it in In-App Purchases — their [`CustomerInfo`](/docs/iap/entitlements) — while the subscription here stays without a customer, managed from your server.

When a user is deleted, their subscriptions stop renewing at the provider, at the period's end, before their record is erased; with [`cancel_on_delete`](/docs/payments/channels#project-settings-3ds--velocity-controls) turned off they stay at the provider as they are, yours to end there from its dashboard, and [`payments.customer.left_at_provider`](/docs/payments/server-sdk#webhook-events) names each by the provider's id. One bought through In-App Purchases follows the same setting, whichever module hears of the deletion first, and its subscription stays listed here, without a `customer_id`, until the provider ends it.

A renewal or an end the provider told of and that did not arrive is found again. A subscription whose period is over and of which nothing was heard is asked about at its provider, within the hour. A renewal's payment is read from its invoice: a provider charges a renewal some time after the period turns (Stripe about an hour after), so a renewal whose payment was not told is asked about from an hour and a half after the turn, and its `payments.subscription.changed` (`change: "renewed"`) follows then — hourly for a day, then daily, while the provider still tries the charge. A subscription the provider no longer knows by the channel's keys — the channel was connected to another account, or moved from test to live — stays listed as it was: nothing says it ended, so it is not told as canceled. Canceling it from your server ends it here — the answer carries `provider_unknown: true` — and changes nothing at the provider: end it there if it still renews in the other account. A customer's own cancel of such a subscription is refused (`409 subscription_unknown_at_provider`): only your server ends it here.

## A subscription's charges are payments

Each charge of a subscription after its first — a period's renewal, seats charged within a period (`change: "renewed"`, `"charged"`) — is a payment of its own, of `kind` `subscription_charge`: listed with the customer's payments (`GET /payments/intents`), paid, its `reference` the subscription's `id`. It is made when its invoice is heard of, once however often it is told; a charge paid before charges were payments is made the first time a refund, a dispute or a support refund asks after it. It is never told as `payments.payment.succeeded`: `payments.subscription.changed` told it. A charge of nothing (a free trial's invoice) is no payment. The cash flow ([Ledger, Drift & Audit Export](/docs/payments/audit)) counts these charges from then on, as the money the card paid (tax inside it), with no provider fee: Stripe's fee of an invoice's payment is not read.

So a renewal is refunded like any payment (`POST /payments/refunds` with its `intent_id`), and a refund or a dispute made of it at Stripe — from its dashboard — reaches it: the refund is written on it and told as `payments.refund.succeeded` (with `actor: "provider"`), the dispute as `payments.dispute.opened` and `.closed`. Their events name such a charge by its invoice — `invoice_id`, the `transaction.id` its subscription's event told — beside `provider_subscription_id`; a subscription's first payment keeps the names its own payment's event gave it. Both carry, for any payment of a subscription, `payment_amount` and `refunded_amount`: what the payment took, and how much of it is given back by now. A refund made at the provider's dashboard leaves the subscription as the provider holds it — Stripe keeps it renewing.

Support's refund of a web subscription ([In-App Purchases' support actions](/docs/iap/customers)) gives back the payment of the newest period: the newest paid invoice that paid for a period — a renewal's, the first one's, or the one a trial ended early at Stripe charged its period on —, however many invoices of seats came after it. A period the card was charged nothing for (a free trial, the customer's credit balance at Stripe) has nothing to give back: the refund is refused (`409 nothing_to_refund`), and an older period's payment is not refunded in its place. All that is left of the payment at Stripe goes back, whatever went back of it before. Asked again — the subscription not ended the first time —, it refunds the payment the first asking chose, and no renewal paid since. On iyzico, a subscription's charges after its first are not payments here: refund them at iyzico.

## Operator & support actions

From Studio (**Payments › Subscriptions**) or your backend (`palbase/server`):

- **List subscriptions**: `GET /payments/subscriptions` (filterable by `customer_id`, cursor-paginated).
- **Extend billing period**: `POST /payments/subscriptions/{subscription_id}/extend` shifts the renewal date forward by 1–90 days for free.
- **Cancel subscription**: `POST /payments/subscriptions/{subscription_id}/cancel` with `{ at_period_end: true }` (default, keeps access through `current_period_end`) or `{ at_period_end: false }` (immediate cancellation).

## Customer self-service

Signed-in users manage their own subscriptions from `palbase/web`:

```ts
import { createClient } from "palbase/web";

const pb = createClient({
  url: "https://k3xq81w4m.palbase.studio",
  publishableKey: "pb_k3xq81w4m_cZ8yX7wV6uT5",
});

async function manageMyBilling() {
  const { subscriptions } = await pb.payments.subscriptions.list();
  const active = subscriptions.find((s) => s.status === "active");
  if (active) {
    await pb.payments.subscriptions.cancel(active.id);
  }

  // On Stripe channels, you can also open the hosted Customer Portal:
  const portalUrl = await pb.payments.portal("https://app.example.com/billing");
  return portalUrl;
}

void manageMyBilling;
```

## Related

- [Channels & Routing](/docs/payments/channels)
- [Refunds & Disputes](/docs/payments/refunds-disputes)
- [Server & Backend SDK](/docs/payments/server-sdk)
