# The checkout kit

> A whole checkout as code you copy into your site and own, in our design, re-themed with a few variables

Source: https://tickets.roostertail.com/docs/kit

The kit is the fastest way to a real checkout. It is **code that lives in your repository**, not an embed: React components for every step, and a small server of yours that holds the secret key. Nothing of ours runs on your page, there is no iframe, and the key never reaches a browser. You change it as you like.

```text
the buyer's page  →  your server (holds the key)  →  RCMS Tickets  (holds, orders, tickets)
                     your server  →  your payment processor  →  the card
```

- **Client:** every step, built for a phone first (48 px targets, 17 px text, plain errors that say what to do): tickets, add-ons, code, details, payment, confirmation, and the hold's clock. `Checkout` is the whole flow, `CheckoutSheet` the same in a dialog, and `useCheckout` is the flow as a hook if you want to draw your own steps.
- **Server:** handlers on web-standard `Request` and `Response`, with adapters for Next.js, Express and Firebase. They check the price again before any money moves, and never forward a field a page should not set.
- **Attribution and pixels:** UTM tags and click ids kept for 90 days, first and last touch, and one helper for the Meta, GA4 and TikTok pixels that gives `Purchase` the order's event id.
- **My tickets:** the confirmation links [My tickets](https://tickets.roostertail.com/docs/my-tickets.md), and the storefront carries its address as `my_tickets_url` for your footer or nav.
- **Abandoned checkouts:** the hold carries the buyer's email; when it expires you are told, with a link that puts the same tickets back. The kit sends no email and names no mail vendor.

> **No site at all?** A [hosted link](https://tickets.roostertail.com/docs/hosted-links.md) is a page we host instead. A hold made on one never reaches your site's abandoned-checkout mail.

> **Not published to npm.** The kit is served as files from this site, with a manifest an agent can follow. Nothing is installed from a package.

## Install it

The shortest way is to give an agent the prompt on [the agents page](https://tickets.roostertail.com/docs/agents.md). It fetches the manifest and follows it. By hand:

1. Fetch the manifest, https://rcms-tickets-test.web.app/kit/manifest.json. It lists every file, its checksum, the install tool, a recipe for Next.js and for Vite, and the steps.
2. Run the tool the manifest names from your project's root. It copies the files into `src/rcms-tickets/` and writes `kit.lock`.
3. Apply your framework's recipe: it adds new files only (one server route that holds the key, one page with `<Checkout />`) and says what to merge into files you already have. Call `captureAttribution()` once in your site's root, because the visitor lands somewhere other than checkout.
4. Check that the secret file is ignored by git (`git check-ignore .env.local`), then set `RCMS_TICKETS_KEY` (a test key) and `RCMS_TICKETS_EVENT`, on the server only.
5. Typecheck, build, run it, and buy a ticket. In test mode the payment step has four buttons: Works, Declined, Not enough funds and Processor error. Press Works, then Pay. A button fills the card number (`4242 4242 4242 4242` for Works), and the number in the box is what counts, so typing it does the same; a number that is not a test card is refused, and the expiry and code are not checked.

The kit reads the event's name, date, venue, tickets and add-ons from the API: you write none of them. The order number on its confirmation page is the order's `number` in the API. On Vite, only the tickets server reads `.env.local`. A hand-made Vite project needs `src/vite-env.d.ts` (`/// <reference types="vite/client" />`), or the typecheck fails on the CSS import; with one `tsconfig.json` instead of two, put the recipe's `server` include and `"types": ["node"]` in it.

The whole kit with its notes is one Markdown file: [`https://rcms-tickets-test.web.app/kit/kit.md`](https://rcms-tickets-test.web.app/kit/kit.md). The manifest's `documents` list holds its checksum.

## A whole example

The example site, `https://rcms-tickets-test-yrd.web.app`, is a festival site built this way: the kit copied by its own tool, re-themed in the site's own type and colour, a five-ticket pack, a code, buyer details with a date of birth and a text opt-in, the ad consent banner, an abandoned-checkout email on `hold.expired`, and a server of a few Cloud Functions with the key in Secret Manager. Its source is not public yet.

## On a page that already has a heading

The kit shows the event's name as an `h1`. On a page with its own `h1`, pass `eventHeading="h2"` to make it an `h2`, or `eventHeading={false}` to leave the name out (the dates and venue stay). Every field has a `name` and a `type` (`name`, `email`, `phone`, `code`, `cardnumber`, `q_<question key>`, `ticket_2_name`), so a password manager or autofill knows them; there are no `data-testid` hooks, so a test finds a field by its label.

On a new folder with no git yet, run `git init` before step 4: the check for an ignored `.env.local` needs a repository. A new Vite project is `npm create vite@latest my-site -- --template react-ts`; apply the recipe after it.

## Re-theme it

On a site with a background of its own, set `--rt-canvas: transparent`: the kit's canvas is a light grey that ends where its content ends, so otherwise your background shows as a strip below it, most plainly under the Continue bar on a phone.

A handful of variables at the top of `checkout.css`: `--rt-accent`, `--rt-on-accent`, `--rt-accent-ink`, `--rt-radius`, `--rt-font`, `--rt-canvas`, `--rt-surface`. Or pass one brand colour to `brandStyle('#c8102e')`, which keeps your colour and picks words that stay readable on it. Light and dark follow the buyer's phone.

## Payment

The payment step has one slot.

- **Test mode:** the kit's own card form takes the test cards and sends only a test token. The confirmation's "We sent your tickets" is the same as in live mode, though test mail reaches only members of your organization. [Test mode](https://tickets.roostertail.com/docs/testing.md).
- **Square:** the buyer's card is tokenized in Square's frame and only the token reaches your server. The kit authorizes the hold's total, makes the order, and only then takes the money (delayed capture). A refused order voids the authorization, with no fee and no refund. If the platform charges you a fee, a sale through this slot records `external`, so the fee is owed and invoiced, not taken on the sale: see [status](https://tickets.roostertail.com/docs/status.md).
- **Another processor:** a server slot and a browser slot. A processor that charges at once leaves a gap: if your server dies after the charge and before the order, the buyer has paid for nothing until a person looks. A live key refuses such a processor unless you give it a way to refund and say that you know.
- **A slot of your own that sends the API `provider: 'square'`** can get a `pending` order back. The kit's confirmation says so ("Almost done", no tickets, nothing paid) until the order is paid.

> **On the Square slot the order is `pending` until the capture lands.** The kit sends the payment as held, so the order has no tickets, counts as no money and fires no `order.paid` until the kit tells the API that Square took it. Then it is `paid`, its tickets issue and `order.paid` fires. The buyer's confirmation says "Almost done" until then.
>
> **A capture that does not land at once** is tried again by the same request retried, the `order.paid` and `order.created` webhooks, and a sweep you schedule, so schedule the sweep. The sweep also finds money Square took whose word never reached the API (a crash or an outage in between) and tells it. It keeps nothing in memory: the API and Square are what it reads, so a restart or another server changes nothing.
>
> **Before it takes money from an order that has waited**, the kit asks the API. If the event was cancelled, or the same email has a newer order on this event, it voids the authorization and tells the API `lost`. So while the money has not been taken, the buyer is charged once, and never for a cancelled event. Money Square already took is never voided here: both orders stand, the buyer has paid twice, and each owner gets one email naming the two orders so a person refunds one. The pending page has no "Buy more tickets".
>
> **A capture that can never land** (Square cancels the authorization when its delay runs out) cancels the pending order through the API, once. Its seats go back, no ticket was ever issued, the buyer is told by email that the card was not charged, each owner of your organization gets an email naming the order, and `onError` hears `capture_lost` with it. The sweep counts it in `lost` that once. Nothing is ever voided at a door by this.
>
> **Ended before the capture.** If a person cancels the pending order, the order says the payment is not confirmed, and the buyer is told to contact the organizer if their card shows a charge. The kit's next sweep voids the authorization, and the order then says not charged. If Square had already taken the money, `onError` hears `captured_after_end` and `money_after_end` counts it once, the order says so on every screen, the buyer is told the organizer will refund them, and each owner gets an email with the reference and the amount: refund it in Square, since no order keeps it.
>
> **The gap that is left.** Between Square taking the money and the next sweep, a person looking at the order cannot see that it was taken. So the Cancel dialog says to look in Square first. It closes at the next sweep, not by itself: with no sweep running it stays open, and the only other word is the email to the owners after a held payment has waited 2 hours. A sweep reads cancelled orders made in the last 16 days.
>
> **What the buyer's request can answer.** A cancelled order answers 409, by what is known of its money: `payment_voided` (with `cause: payment_lost`) when the card was not charged, `payment_taken_order_cancelled` when the money was taken and the organizer owes it back, `order_cancelled` when nobody has confirmed it.
>
> **A read that does not answer is never a yes and never a no.** If Square can't say what became of a payment, the kit neither takes it nor says it is lost: the order stays as it is and the next look asks again.
>
> **A refund in Square while the order waits** makes no tickets: the kit reads Square's `refunded_money`, does not confirm the order, and `onError` hears `capture_refunded`. Cancel the order if the refund was meant. This reads Square's docs and is [not proven against Square's sandbox yet](https://tickets.roostertail.com/docs/status.md).
>
> **An order made by kit 0.1.1 or earlier** was `paid` with tickets out before the capture. It cannot be cancelled by a lost capture: the kit only reports it (`capture_lost`), and a person cancels or refunds it. [Update](#updates-and-advisories).
>
> **A lost answer.** If the order's answer never reaches the page, the buyer sees "We can't confirm this payment yet", is told they will not be charged twice, and gets a Check again button. Never charge again from there.

> **Not proven against Square's sandbox yet.** The Square half is tested against a stand-in written from Square's documentation. It has not run against Square's sandbox or live. Treat it as a draft until it has. [What exists and what does not](https://tickets.roostertail.com/docs/status.md).

## Node 26 and your own fetch

On Node 26 the built-in `fetch` offers HTTP/2. Per origin, on a warm connection, it sends requests with a body one at a time, and anything sent behind one waits its turn, with no error. What decides it is whether the request has a body, not its method. A busy on-sale queues behind that connection (four buyers a second took 140 s each). The kit calls the API and Square with `serverFetch`, which speaks HTTP/1.1 through Node's `https` there and is the plain `fetch` everywhere else.

- `NODE_USE_ENV_PROXY=1` or `--use-env-proxy` with `HTTPS_PROXY`: the calls go through your proxy, at once.
- A global dispatcher that is anything but a bare `new Agent()` (`ProxyAgent`, `EnvHttpProxyAgent`, `MockAgent`, `.compose()`, an `Agent` with a private CA or a `lookup`): the platform's `fetch`, so yours is honoured, and on Node 26 the stall comes back. For a CA alone, `NODE_EXTRA_CA_CERTS` keeps both.
- A `fetch` passed to `createCheckout` or `squarePayment` is used as it is. One on `globalThis.fetch`, a test's stub or a tracing wrapper, is called as given: a wrapper keeps working, and on Node 26 it stalls and says so once with a `console.warn`. To keep the wrapper and lose the stall, wrap `http1Fetch` and pass that as `fetch`.
- Next: the kit's routes need nothing. On Node 22 the platform's `fetch` and your cache rules apply (`force-cache` and `only-cache` keep the answer, `next: { revalidate, tags }` is honoured); otherwise the call says `no-store`. On Node 26 Next never sees the call: nothing is kept, `next: { ... }` is dropped, and a page that calls the API as it renders says `export const dynamic = 'force-dynamic'`.
- A write is never sent twice on a redirect: it is an error that says so.

## Before real money

A live key must set `clientIp` for your hosting, or `createCheckout` refuses to start: the buyer's address is what stops one visitor holding a whole sale for free (`KIT.md`, "The buyer's address").

Live mode is open only for an organization we have cleared, so ask us before you follow this list. The manifest's `live` list is the authority, in order: verify the copy against `kit.lock`, update if there is an advisory, set the live key and base URL in production only, switch to the Square slot, register the webhook (`order.paid`, `order.created`, `hold.expired`) and keep its secret, schedule the sweep every 15 minutes, wire the cookie banner, send `onError` to a person, say where the buyer's address is on your hosting, and buy one real ticket and refund it.

## Updates and advisories

Your copy says its version on every API call (`rcms-tickets-kit`), and `kit.lock` records a checksum for every file as installed. That lets us tell you when a version has a problem, and lets you see what you changed.

```bash
node src/rcms-tickets/tools/kit.mjs verify
node src/rcms-tickets/tools/kit.mjs update --manifest https://rcms-tickets-test.web.app/kit/manifest.json
```

- The manifest is **signed** (ed25519, `manifest.json.sig`), and the tool checks it against a public key built into the tool before it writes anything. A changed manifest stops with a plain message. The first install trusts this page and its TLS, which nothing can improve on; every update after it checks the signature.
- The tool refuses an older version without `--allow-downgrade`.
- **To check the signature yourself**, with OpenSSL 3 (macOS's own `openssl` is LibreSSL and cannot): the public key is the `PUBLIC_KEY` line in the tool you ran, and the manifest names the same key under `signing.public_key`. Compare it with a copy you got another way, because a tool and a manifest served together are only as trusted as the page that served them.

```bash
KEY=$(grep -o "PUBLIC_KEY = '[^']*'" src/rcms-tickets/tools/kit.mjs | cut -d"'" -f2)
(echo '-----BEGIN PUBLIC KEY-----'; echo "$KEY"; echo '-----END PUBLIC KEY-----') > key.pem
base64 -d < manifest.json.sig > sig.bin   # macOS: base64 -D
openssl pkeyutl -verify -pubin -inkey key.pem -rawin -in manifest.json -sigfile sig.bin
```

- A file you did not change is replaced; a file you changed is kept, the new copy goes in `.kit-update/` and the diff is printed. Merge by hand, then `update --accept <path>`.
- An **advisory** is printed first and stays until the file it is about has really moved. An agent doing an update should read every advisory and say it to the owner.
