# Take payments in an App

An App sells a **fixed-price item** by declaring an `output.checkout` step and
starting it from the page. The server owns the price; the page only chooses the
item, the quantity and (for a payment that marks a row) which record it pays for.
Never build a "pay" page yourself: no `mailto:` link, no manual "I paid" toggle,
no pasted Stripe secret key, no amount sent from the client.

Read this before adding money to an App. Read
[security-release.md](security-release.md) before release.

## 1. Declare what is for sale

One `checkout(...)` statement per purchasable item, written in the Flow like
any other step:

```flow
flow 1
let orders = table(
  name: "orders",
  columns: {item: text(), status: enum(options: ["pending", "paid"]), paidAmount: number()}
)
let markPaid = command(inputs: {recordId: reference(table: orders)}, access: {call: owner}) do
  patch(table: orders, recordId: input.recordId, set: {status: "paid"})
end
let buy = checkout(
  name: "buy",
  productName: "Bakery pre-order",
  amount: 2500,
  currency: "usd",
  metadataFields: ["orderId"],
  onPaid: markPaid
)
```

- `name` is the stable handle the page calls. It never changes once shipped.
- `amount` is minor units (`2500` = 25.00) and is server-owned: a caller cannot
  override it or the currency.
- `quantity` is optional; omit it and the item is always 1.
- `metadataFields` names request-body fields a caller may attach (coerced to
  strings). They come back on the Stripe session as metadata.
- `onPaid` names a **declared command** that Stripe's paid event runs as the
  system, with the server-trusted amount. A `patch` command targets the record
  the checkout was started for. `onRefunded` names the command a refund runs.
- `successUrl` / `cancelUrl` are optional https URLs; they default to the App's
  hosted view. Leave them out unless the App has its own return page.

`markPaid` above is a patch command like any other; the checkout names it. It is
not called by the page — it runs after the signed Stripe event, so the row
changes only when Stripe confirms the charge.

## 2. Start the checkout from the page

Use the typed client — `app.checkout("buy").start(...)` — and send the
customer to the returned URL:

```ts
const started = await app.checkout('buy').start({
  quantity: 2,
  metadata: { email },
  // The row this payment is for. Required when the checkout's onPaid patch
  // command changes a record; otherwise the server has nothing to mark paid.
  recordId: order._wf_record_id,
  // Stable per attempt: a retry resumes the same Stripe session.
  idempotencyKey: crypto.randomUUID(),
})
location.assign(started.url)
```

- `idempotencyKey` is required. Keep one key per purchase attempt; a retry with
  the same key returns the same session instead of charging twice. The same key
  with different input is refused with `409`, so use a new key for a new input.
- The result is `{ url, sessionId }` (`url` is the Stripe Checkout page).
- `recordId` is the opaque `_wf_record_id` of the row being paid for. It only
  chooses which record an `onPaid` patch marks — it never changes the price.
- Anonymous visitors get a scoped submitter identity automatically; the client
  singleflights the first bootstrap, so a double click cannot start two
  sessions with different scopes.

## 3. What the owner must have on

Checkout refuses to start, with a message the page can show, when any of these
is missing — check them before telling the person to pay:

- the owner connected **their own** Stripe account in Settings → Payments, and
  Stripe reports card payments active on it (re-checked on every checkout);
- public submissions are enabled for the App (otherwise checkout is absent from
  the Contract entirely);
- the App's plan allows checkout;
- the App is **released**. A draft cannot take money today; verify the paid
  journey on the released App, or wait for the draft test-mode work in #13460.

## 4. Membership

Recurring subscriptions are **not** supported by `output.checkout` today. Do not
invent a subscription: a membership is one fixed-price checkout per period.

- Table `members` with `member_email`, `status`, `period_end` (datetime).
- Item `membership-month` at the monthly price, `onPaid: "mark-member-paid"`.
- The page starts the checkout with the member's row `recordId`, so the paid
  event moves that member's `period_end` forward and sets `status: "active"`.
- Access checks read `status = "active"` and `period_end > now`.
- The page says "renews monthly" only if it starts the next period itself; if
  nothing renews, say "one month at a time" instead.

## 5. Verify it

1. `test` with `kind: "scenario"` for the non-payment path, and a
   `kind: "command"` run for `markPaid` with the value Stripe would send.
2. On the released App, complete a real low-price checkout, then check the row
   the `recordId` named is paid **once** and that a page reload does not charge
   again.
3. Replay the same `idempotencyKey` and confirm no second session or charge.

## 6. Common mistakes

| Symptom | Cause |
| --- | --- |
| `Start this checkout with the recordId…` | The checkout's `onPaid` patch changes a record, but the page did not send `recordId`. |
| `Ask the App owner to connect Stripe…` | The owner has no connected account, or Stripe has not activated card payments. |
| Checkout missing from the Contract | Public submissions are off, or the App is a draft. |
| `409` on retry | The same `idempotencyKey` was reused with different input; keep one key per input. |
| Row still unpaid | `onPaid` names a command that does not exist, or the event has not arrived yet — the page must not mark it paid itself. |
