# Hosted checkout end to end

The hosted checkout keeps card data off your servers. You open a **checkout session** for what the buyer is buying, send the buyer to its URL, and Regoria does the rest: the page, the address and tax quote, the card form, the capture, the receipt. You learn the outcome from a webhook and fulfil.

## 1. Open a session

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/checkout-sessions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: cart-8842" \
  -d '{
    "lines": [{"price_id": "prc_01HZX5Y3V8N2K7QJ4R9T6B3M1C", "quantity": 2}],
    "customer_ref": "cus_A1B2",
    "buyer_email": "buyer@example.com",
    "reference": "INV-1001",
    "success_url": "https://shop.example.com/thanks?order=Regoria",
    "cancel_url": "https://shop.example.com/cart",
    "metadata": {"cart_id": "8842"}
  }'
```

| Field | Meaning |
|---|---|
| `lines[]` | `price_id` and `quantity`; a plan product's price starts a subscription |
| `customer_ref` | optional: your own id for the buyer (1 to 255 characters); it ties orders, cards and subscriptions to one customer. Without it, the customer is the one holding the email the buyer pays with, created then if none does |
| `customer_id` | optional, in place of `customer_ref`: the `cus_` id of one of your customers, who owns the session whatever email the buyer types; both answer `422` `validation` on `customer_id`. A `setup` session needs one of the two |
| `buyer_email`, `buyer_name`, `billing_address` | prefill the page; the buyer can change them |
| `reference` | your own order reference, copied onto the order (searchable, not unique) |
| `save_payment_method` | ask the buyer to keep the card on file for later server-side orders |
| `success_url`, `cancel_url` | where the buyer lands after paying, or after leaving |
| `metadata` | up to 4 KB of your own JSON, copied onto the order |

The answer (`201`) is the session: `id` (`cks_…`), `state: "open"`, `url`, `expires_at` (30 minutes). Store the id against your cart.

## 2. Send the buyer

Redirect the browser to the session's `url`. The page is on Regoria's domain, branded for your store; the buyer enters an address, sees the sales tax, pays. Do not embed the page in an iframe.

Tax is never guessed. If the tax provider does not answer and no correct rate is known for the buyer's ZIP, the page says "Tax could not be calculated, try again in a moment" and the buyer retries; the session stays open and nothing is charged. A renewal in that case waits for the next retry and is not declined.

## 3. Learn the outcome

Your `success_url` tells you the buyer came back, not that money moved. The source of truth is the **`order.paid`** webhook:

```json
{
  "id": "evt_…",
  "type": "order.paid",
  "version": 1,
  "occurred_at": "2026-10-21T14:05:00.000Z",
  "merchant_id": "mer_…",
  "data": {
    "order_id": "ord_…",
    "session_id": "cks_…",
    "charge_id": "chg_…",
    "customer_ref": "cus_A1B2",
    "reference": "INV-1001",
    "buyer_email": "buyer@example.com",
    "subtotal_cents": 5800, "tax_cents": 479, "total_cents": 6279, "currency": "USD",
    "lines": [{"price_id": "prc_…", "product_id": "prd_…", "quantity": 2, "unit_price_cents": 2900, "tax_cents": 479}],
    "source": "hosted",
    "metadata": {"cart_id": "8842"}
  }
}
```

Match it to your cart by `session_id`, `reference` or `metadata`. If you need the order before the webhook arrives (the buyer is on your thank-you page), the return link carries `session_id` beside the `status` hint, keeping any query and fragment of your `success_url`; read `GET /v1/checkout-sessions/{session}`: `state` is `paid` and `order_id` is set once captured, and on a plan session `subscription_id` names the subscription it starts (a read of that subscription can answer `404` for a moment, until it is created); `open`, `paying` or `pending` means wait for the webhook.

To get the session, order, subscription and customer in one event, subscribe to **`checkout.completed`**: it carries `session_id`, `order_id`, `subscription_id` (`null` without a plan) and `customer_id`, and is sent before `subscription.started`.

Other outcomes: `checkout.abandoned` when the session expires unpaid; `order.in_review` when the order is held for a risk review, followed by `order.review_closed`; `charge.declined` on a declined card (the buyer can try another one on the page).

## 4. Fulfil

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/orders/ord_01HZX5Y3V8N2K7QJ4R9T6B3M1C/fulfil \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"evidence": {"type": "tracking", "url": "https://carrier.example.com/track/1Z999", "note": "Shipped 2026-10-21"}}'
```

The order moves to `fulfilled` with `fulfilled_at`. Evidence (a tracking link, a download log) is what defends a dispute later. `GET /v1/orders/{order}/receipt` gives the receipt payload with a fresh signed link you can send the buyer.

## Currencies

A price is set in one currency (its `currency`), and a buyer may pay in another: the session's `currency` is what the buyer is charged in.

- **Which currency a session opens in.** Without a `currency` on `POST /v1/checkout-sessions`, the page opens in the currency of the buyer's country when your business offers it at checkout, otherwise in the price currency. The buyer can switch between the two on the page until they pay.
- **Forcing one.** Send `currency` (a three-letter ISO 4217 code) and the session stays in it: the buyer gets no switch. A currency your business does not offer answers `422 currency_not_supported`; one your payment account cannot charge answers `422 currency_not_accepted`.
- **The amount in the buyer's currency.** When the price has a fixed amount in that currency, the buyer pays exactly that. Otherwise the price is converted with the day's rate, and the rate is locked on the session: the amount does not move while the buyer fills the form.
- **What you get back.** Sessions, orders and the `order.paid` event carry `currency` and the totals in it, plus `price_currency` and `price_amount_cents` when the two differ. Amounts are integers of the currency's smallest unit: `1235` is JPY 1,235 and USD 12.35.
- **Showing a price on your own site.** `GET /v1/prices/{price}/preview?country=DE` answers the currency the checkout would open in for a buyer in that country and the price's amount in it (`mode`: `same`, `fixed` or `converted`). It creates and locks nothing; the session locks its own rate.

Your balance is kept per currency: a sale charged in EUR is pending in EUR.

## Sessions for other things

- **Subscriptions**: a line whose product is a `plan` starts one; the order is the first period. See [Subscriptions](http://docs.staging.regoria.com/guides/subscriptions).
- **Save a card without a payment**: `mode: "setup"` with no lines; the buyer saves a card for later server-side orders or for an existing subscription (`subscription_id`).
- **Expire a session** you no longer want paid: `POST /v1/checkout-sessions/{session}/expire`.

## Server-side orders on a saved card

With a card on file (`payment_method.created` tells you the `pm_` id, or list `GET /v1/payment-methods?customer_ref=…`), take a payment without the buyer present:

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/orders \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: reorder-9001" \
  -d '{
    "lines": [{"price_id": "prc_…", "quantity": 1}],
    "payment_method_id": "pm_01HZX5Y3V8N2K7QJ4R9T6B3M1C",
    "customer_ref": "cus_A1B2",
    "buyer_email": "buyer@example.com",
    "billing_address": {"line1": "1 Main St", "city": "Austin", "state": "TX", "postal_code": "73301"},
    "merchant_initiated": true,
    "agreement": "unscheduled",
    "reference": "INV-1002"
  }'
```

`merchant_initiated: true` with `agreement` (`recurring` or `unscheduled`) states the card-on-file agreement the buyer gave you; a buyer-present payment omits them. The answer is `201` with `status: "paid"` and the order, or `202` when the payment is held for review (`order.in_review` follows). A decline is `402` with the decline category.

## Orders at a glance

`GET /v1/orders` lists them (`status[]`, `customer_ref`, `reference`, `range`); `status` is `paid`, `fulfilled`, `refunded`, `partially_refunded` or `access_revoked`. Each order carries its charge, card summary (brand, last 4), tax, fee and reserve figures, lines and your metadata. `tax_rate_source` says where the tax came from: `provider` (a live quote), `learned` (the provider's earlier answer for the ZIP, used while it was unreachable), `seed_zip4` or `seed_zip` (the official-file rate table).
