# Hosted checkout

Send a customer to a payment page we run. It shows their plan, takes the card through Stripe and starts the subscription.

## How it works

1. Your server opens a checkout session for a customer and gets back a URL.
2. You redirect the browser to that URL.
3. The payer confirms. We save the payment method, create the subscription and charge the first invoice.
4. We send `checkout.session.completed`, and the payer is sent to your `success_url`.

The page carries your brand: logo, colour, support email and links come from your branding settings. Totals, tax and discounts are always worked out by the server; the browser never sends a price.

## Open a session

Needs a secret key with the `checkout:write` scope.

**Node**

```ts title="checkout.ts"
const session = await mint.checkout.create({
  customerExternalId: 'ws_123',
  planCode: 'starter_annual',
  seats: 5,
  promotionCode: 'LAUNCH20',
  trialDays: 14,
  metadata: { source: 'pricing_page' },
  successUrl: 'https://app.example.com/billing/done',
  cancelUrl: 'https://app.example.com/billing',
})

redirect(session.url)
```

**curl**

```bash title="terminal"
curl -X POST https://globalymint.com/api/v1/checkout-sessions \
  -H "Authorization: Bearer $MINT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customer_external_id": "ws_123",
    "items": [{ "plan_code": "starter_annual", "quantity": 5 }],
    "promotion_code": "LAUNCH20",
    "trial_days": 14,
    "success_url": "https://app.example.com/billing/done",
    "cancel_url": "https://app.example.com/billing"
  }'
```

The reply has the session `id`, its `url` and `expires_at`. A session is valid for 24 hours.

## Fields

| Field | Notes |
| --- | --- |
| `customer_external_id` or `customer_id` | Exactly one. The customer must already exist. |
| `items` | 1 to 10 plans, each named by `plan_id` or `plan_code`, with an optional `quantity`. |
| `plan_id` or `plan_code`, with `seats` | Shorthand for a single item. |
| `mode` | `subscription` (the default), or `setup` to save a payment method without subscribing. |
| `promotion_code` | Applied up front. It is checked when you open the session, so a bad code fails here and not in front of the payer. |
| `allow_promotion_codes` | Whether the payer may type a code. Default true. |
| `lock_items` | Fix the plan, seats and interval. The page shows them read-only. Default false. |
| `trial_days` | 0 to 730. Without it, the product's own trial length applies. |
| `success_url`, `cancel_url` | Required. https, or http for localhost. |
| `metadata` | Up to 50 string values, copied onto the subscription. |

A metered item names a `meter` and has no `quantity`. It needs a monthly metered plan, and it cannot be the first item.

## After payment

The payer is sent to `success_url` with `?session_id=…` added. Do not grant access on that redirect: a payer can close the tab before it happens.

Grant access when you receive `checkout.session.completed` or `invoice.paid`. For a subscription, the event's `data` has `subscription_id`, `invoice_id`, `amount_due_cents`, `currency` and your `metadata`. For a `setup` session it has `payment_method_id`.

In this event `data.mode` is the kind of session, `subscription` or `setup`. It is not test or live.

A payer who submits twice is not charged twice: the second submit returns the result of the first.

## Refusals

| Code | Status | When |
| --- | --- | --- |
| `not_found` | 404 | The customer or a plan does not exist, or the plan is inactive. |
| `promotion_code_invalid` | 422 | The promotion code does not apply to this customer or plan. |
| `unprocessable` | 422 | An item cannot be billed as asked, such as a meter on a plan that is not metered. |
| `validation_failed` | 422 | A field is missing or malformed. |

## Security

- The session URL holds a signed token that expires. A forged, expired or unknown token answers 404.
- Hosted pages send a Content-Security-Policy that allows only our origin and Stripe, cannot be framed by another site, and are not indexed by search engines.
- A secret key is never needed in the browser.
