This is the full file; follow it from Step 0.

# OpenReceive agent directions (Vercel Functions)

```sh
# READ THIS FIRST: this file is 37 KB and a summary drops required steps. Download it whole:
curl -fsSL https://openreceive.org/agent-directions/vercel-functions/full.md
# If your tool cuts long output, save it and read it in parts: curl -fsSL -o /tmp/openreceive-directions.md https://openreceive.org/agent-directions/vercel-functions/full.md
# Skip the download only if you already have all of it: pasted, read from disk or fetched raw.
```

**Step 0 in brief** (Step 0 below has the details):

- Before installing or editing anything, ask the user to add a receive-only NWC code in Vercel.
- Next ask for the LSC code. If the user wants stablecoins, USDT, USDC, ETH, SOL or crypto, do not ask yes/no first.
- The user adds NWC_URI and LSC_URI_PRIMARY in Vercel's Environment Variables. Never in .env, vercel.json or code.
- Never run vercel env pull or vercel env ls, and never print the environment.
- Do not suggest rotating or revoking a code because it was pasted here.
- Start Step 1 only once NWC_URI is saved, and LSC_URI_PRIMARY is saved or the user said "Bitcoin only".

These directions describe OpenReceive 0.6.8.

Add OpenReceive to this site on Vercel with no framework: static pages, and
Node functions under `api/`, the site you are already working in. OpenReceive
ships no Vercel package: the integration is one function that hands each
request to `@openreceive/http`, one rewrite in `vercel.json`, and the
`<openreceive-checkout>` element on the order's page. You do not need a copy
of the OpenReceive source: the packages are on npm, and the Vercel Functions
recipe is appended to this file in full, so you can do the whole integration
without fetching anything. Every snippet is plain JavaScript. Prefer the
published packages and the handler they export — do not reimplement wallet
RPC, settlement, or pricing.

A Next.js app on Vercel is not this shape: follow
https://openreceive.org/agent-directions/next.md instead.

Do not clone the OpenReceive repository into this site, and do not copy a
demo's models over tables that already exist. Find this site's orders,
products and buyers, whatever they are named, and map the three hooks onto
those. Keep its pages, its sign-in or guest cookie, and its database.

## What OpenReceive is

A payment library that runs inside THIS site's server code. On Vercel it is
ONE function, `api/openreceive.js`, which serves every route under
`/openreceive/` through a rewrite, issues Lightning invoices against a wallet
the merchant already controls, and calls back into your code when one
settles. There is no OpenReceive account and no API key, and OpenReceive
never holds the funds — the sats land in the wallet the merchant connected.

The one required credential is a receive-only NWC code (Nostr Wallet
Connect): a string from the merchant's wallet that can create invoices and
read their status, and cannot spend. A swap provider (an "LSC" code)
optionally lets the payer send USDT, USDC, ETH or SOL instead, converted into
that same Lightning payment. Step 0 collects those credentials; you write
three hooks — `authorize`, `amountFor`, `onPaid`; OpenReceive supplies
invoices, polling, settlement and the checkout UI. It never owns orders,
users, prices, or fulfillment.

## Step 0 — the codes are environment variables the user adds

If the user says the codes are already set as this project's environment
variables (`NWC_URI`, and `LSC_URI_PRIMARY` unless they want Bitcoin only),
believe them: do not ask for them, and go to the database question (item 4
below). You cannot read a variable's value, and you do not need to. Never run
`vercel env pull` or `vercel env ls`, and never print the environment.

Otherwise, before anything else, your next action is a question to the user.
Do not install packages or edit files before asking it. A new shop has
neither code yet.

Two codes are needed. Only the user can add a secret: in Vercel, the
project's **Settings** → **Environment Variables** → **Add Environment
Variable**, type **Secret**, with **Production**, **Preview** and
**Development** all ticked.

- `NWC_URI` — a receive-only Nostr Wallet Connect code,
  `nostr+walletconnect://…`. Required for Bitcoin.
- `LSC_URI_PRIMARY` — a Lightning Swap Connect URI,
  `lightning+swapconnect://…`. Required for USDT, USDC, ETH and SOL. Skip it
  only when the user says they want Bitcoin alone.

Ask one question per message, and never repeat a code back.

1. **First message — the NWC code, and nothing else.** Ask the user to add
   `NWC_URI` and walk them through getting it:

   > To receive payments I need a receive-only wallet code. In Rizful: open
   > the menu, tap NWC Codes, then tap Copy receive-only code
   > (https://openreceive.org/get_a_nwc_code_to_receive_payments). If you would
   > rather run your own wallet, Alby Hub works too: Connections → Add
   > Connection → Read Only. Then in Vercel, open the project's Settings →
   > Environment Variables, and add NWC_URI with the code as its value, type
   > Secret, for Production, Preview and Development. Tell me when it is saved.

2. **If the user pastes a code into the chat** instead, do not write it into
   any file, code or log: `.env`, `vercel.json` and the repository all travel
   with the project. Thank them, and ask them to add it in Vercel with that
   value. Do not suggest replacing the code.
3. **Second message — swaps.** If the user asked for stablecoins, USDT, USDC,
   ETH, SOL, altcoins or "crypto" (as in "Bitcoin and stablecoin payments"),
   this message IS the walkthrough below: send it as it is, and do not ask yes
   or no first. Otherwise ask whether payers should also be able to pay with
   USDT, USDC, ETH or SOL, then give the walkthrough. The walkthrough:

   > Go to https://lightning-swap.com, sign in for API keys, create a key, and
   > copy the whole URI (https://openreceive.org/set_up_swap_provider). Add it
   > in Vercel as LSC_URI_PRIMARY, the same way — or say "Bitcoin only" and I
   > will continue without it.

   Mention FixedFloat only if they already use it. Swaps on means the refund
   route back is part of this integration (the swap non-negotiable below).
4. **The database.** OpenReceive keeps its payment rows in Postgres that every
   instance of the function shares. Check the code for the database it
   already uses. If the site has no Postgres, ask the user to add Neon from
   the project's **Storage** tab, connected to all three environments: Neon
   sets `DATABASE_URL` (pooled) and `DATABASE_URL_UNPOOLED` (direct). SQLite,
   a KV store and Redis cannot hold the payment rows. If the orders live
   somewhere else (Stripe, Airtable, a JSON file, KV), they stay there: only
   the payment rows go in Postgres.

A variable reaches only the deployments made after it was added, and a site
run locally gets the project's variables from outside its code (`vercel dev`
downloads the Development ones; a compose setup passes them in). Do not add
the codes to a compose file, `vercel.json` or any other config. Do not invent
placeholder codes. Start Step 1 only once the user says `NWC_URI` is saved
and `LSC_URI_PRIMARY` is saved or explicitly declined. The first request runs
the receive-only preflight: if it reports spend methods such as
`pay_invoice`, ask the user for a receive-only code in place of the one in
Vercel. Never set the spend-capable override to get past it.

## Step 1 — the packages

```sh
npm install @openreceive/http@^0.6.8 @openreceive/elements@^0.6.8 pg @vercel/functions
```

The packages are ES modules only: `package.json` needs `"type": "module"`.
If the site's own files are CommonJS (`require`, `module.exports`), name the
new files `.mjs` instead and leave the rest as it is. When the site runs
locally in Docker, install inside its service and rebuild the image.

## Step 2 — the database and the tables

Use the site's own `pg` Pool if it has one, on the pooled `DATABASE_URL`,
with `max` between 1 and 3, and call `attachDatabasePool(pool)` from
`@vercel/functions` on it once, where it is created: Vercel then closes idle
connections before it suspends an instance. Otherwise write the recipe's
`lib/db.js`, which does both. Create OpenReceive's two tables over
`DATABASE_URL_UNPOOLED` with the recipe's `scripts/openreceive-tables.mjs`,
run from the `build` script beside anything already there:
`paymentsSchemaSql("postgres")` is idempotent, so every deploy can run it.

## Step 3 — the order and its buyer

- **The order's id is the `reference`.** Create the order before checkout,
  keep it across retries, never reuse it. A fresh id per page load lets one
  order be paid twice.
- **The price is the order's, never the browser's.** `amountFor` reads the
  order row and returns `{ currency, value, description }`, with `value` a
  decimal string, or `null` once the order is no longer awaiting payment.
- **The buyer is whoever the site already trusts.** `authorize` reads the
  same cookie or session the order page checks, from
  `request.headers.get("cookie")`. A guest shop keeps a cookie signed with
  the site's own secret (such as `SESSION_SECRET`, never `NWC_URI`), lasting
  long enough for a swap refund hours later. If the site has no secret to
  sign with, ask the user to add `SESSION_SECRET` in Vercel the same way, as
  a long random value.
- **`onPaid` marks the order paid** through the `query` it receives, with a
  guard on the unpaid state: `UPDATE … WHERE state = 'awaiting_payment'`. It
  runs inside the transaction that records the payment. For orders outside
  Postgres, call the order store there, after checking the order is not
  already paid.

## Step 4 — the function and the rewrite

Write `api/openreceive.js` as the recipe's "The payment function" shows,
with this site's own `findOrder` and buyer lookup. Then add to
`vercel.json`, beside what it already has:

```json
{
  "rewrites": [{ "source": "/openreceive/:path*", "destination": "/api/openreceive" }],
  "functions": { "api/openreceive.js": { "maxDuration": 30 } }
}
```

## Step 5 — the checkout page

Copy `openreceive-checkout.js` and `openreceive-checkout.css` from
`node_modules/@openreceive/elements/dist/standalone/` into the folder the site
serves static files from (`public/` if it has one), in the `build` script, as
the recipe does, and add the copies' folder to `.gitignore`. On the order's
own page, while the order is unpaid, load both files and render:

```html
<openreceive-checkout reference="ORDER_ID" prefix="/openreceive" resumable></openreceive-checkout>
```

`ORDER_ID` is the order's id, written in by the function that renders the
page, or set from the page's address on a static page. With swaps on, add
the recipe's "The way back to a swap refund": its `api/order-swap.js`, a
`swap_payment_hash` column on the order, and the page's listener and
`resume-payment-hash`. A paid order's page
says it is paid and offers no way to pay. For its receipt, mount the same
checkout with no settled listener: for a paid order it opens on its receipt
and transaction record. When it settles on the page, update the page's status
line from the `openreceive-settled` event: `onPaid` has already marked the
order paid. Keep that checkout mounted: it is the payer's receipt.

## Step 6 — check it

Where the site runs locally (its README, a compose file, or `vercel dev`),
start it the way it already starts, then:

- Run doctor where the site runs, with the database URL expanded there,
  never typed out: for a compose service,
  `docker compose exec -T <service> sh -c 'npx openreceive doctor --db "$DATABASE_URL_UNPOOLED" --url http://127.0.0.1:<port>'`.
  It reports each code as present or missing and never prints one. The
  `openreceive` command comes with `@openreceive/node`, which
  `@openreceive/http` installs. Never source an env file into a shell: the
  `&` in a code splits the value and prints the pieces.
- A stranger asking for a real order gets OpenReceive's JSON refusal:

  ```sh
  curl -s -X POST http://127.0.0.1:<port>/openreceive/checkouts/prepare \
    -H 'content-type: application/json' -d '{"reference":"<an order id>"}'
  ```

  It answers `403` with `"code":"FORBIDDEN"`. A `NOT_FOUND` page means the
  rewrite is missing; `WALLET_UNAVAILABLE` means the function does not see
  `NWC_URI`.

Place one order through the site's own form, as a buyer would, and keep it.

## After setup — hand over, then stop

You cannot pay the invoice: the code is receive-only. Do not pay, settle or
mark an order paid, and do not look for a way to (a wallet control port, a
test endpoint, another wallet). If the user wants a real settlement test,
they pay a small order from their own wallet, and `onPaid` marks the order
paid. Do not deploy unless the user asked you to: a deploy is theirs.

The browser check of that page (payment-method icons, wallet logos, a pay
tutorial) is theirs: name it in one line of your closing message, and do not
run it yourself.

Setup ends here. Your last message starts "Setup is finished" and has at most
five short lines (about 80 characters each): where to buy (the shop's page,
"open it and click Buy"), the methods it offers, that the same curl check runs
against their Vercel deploy, and one line of what to look at. Send nothing
after it. Do not list what changed, copy out the recipe's checks, offer more
work, or end the message on a question. When the swap code is saved, one more
line sends the user to
https://openreceive.org/guides/rate-limiting#if-you-take-coin-payments: limits
per payer on coin payments are their app's to add from it. Do not build them.

- Keep the order you placed; do not delete it. Do not explain order
  ownership or mention your test order.
- Name the methods it offers: Bitcoin, plus USDT, USDC, ETH and SOL when
  `LSC_URI_PRIMARY` is saved. Do not mention minimums, and never say a coin
  will not work or will not be offered.

## Non-negotiables

The recipe below has the code. These are the rules it cannot state for
itself, and they hold for every integration.

- OpenReceive never owns orders, users, prices, or fulfillment. Its two
  tables sit beside the site's own and join nothing.
- Keep `NWC_URI` / `LSC_URI_*` in Vercel's environment variables, read from
  `process.env` in the function. Never in `vercel.json`, a committed file,
  browser code, logs or assets.
- Do not suggest rotating, revoking or replacing a code because it was pasted
  into this chat; ask the user to save it as a secret instead.
- One function, `api/openreceive.js`, behind the rewrite
  `/openreceive/:path*` → `/api/openreceive`. Never
  `api/openreceive/[...path].js`: outside Next.js it matches one path
  segment, so `/openreceive/checkouts/prepare` gets Vercel's 404 page. After
  the rewrite the request still carries the path the browser asked for, so
  pass it to the handler unchanged.
- Build the stack on the first request (`stack ??= createStack({...})`
  inside the handler) and keep it for the instance's later requests. Never
  build it at import: `createStack` connects to the wallet as soon as it is
  built, Vercel loads a function before its first request, and that first
  payer gets a 503.
- The function exports `GET` and `POST`, each taking a `Request` and
  returning the handler's `Response`, even when the site's other functions
  use `(req, res)`: each file picks its own form. Never rebuild a `Request`
  from `req` and `res` by hand.
- It runs on Node, never the Edge runtime: no
  `export const config = { runtime: "edge" }`. `maxDuration: 30` in
  `vercel.json` gives it time to reach the wallet.
- No cron job (`crons` in `vercel.json`) and no notifications worker unless
  the user asks: each request to the payment routes settles payments through
  a lock in the database. A payer who leaves is settled on the next request;
  if orders must not wait on a quiet site, see
  https://openreceive.org/guides/deploying.md#when-the-payer-leaves-the-page
- `rateLimiting.ip` reads the first address of `x-forwarded-for`, which
  Vercel replaces with the client's own on every request. That is true on
  Vercel only: behind another proxy, read the header that proxy sets.
- Work only in this site. Never read or run anything from another project on
  this machine (its `node_modules`, tools or source), for any reason. A
  browser and Playwright are not part of setup.
- The project's own rules (its `AGENTS.md`, contributing guide) win over these
  directions. If one forbids a step here, such as running migrations, ask the
  owner and wait for the answer. Do not move the step somewhere the rule does
  not name, such as the function's first request.
- Restart only this site's local server, on the port it already uses: stop
  the process you started, or the one listening on that port, by its pid (or
  restart its Compose service). Never `pkill` or `killall` by name: that
  stops other people's servers too.
- Run commands where the site runs. When a compose file builds it, run npm,
  the table script and doctor inside that service (`docker compose exec` or
  `run`), and rebuild the image after adding a package.
- The host owns the price. `amountFor` reads it from your own data; reject
  payer-supplied amounts. A donation is no exception: its amount is saved by
  your own function first, on a row made like an order.
- `authorize` runs on every request, and the `resource` it receives is a CLAIM
  the payer made, not proof. Check the cookie or session; never trust a body
  field.
- `onPaid` must be idempotent. Its database fulfillment commits once per
  `reference`. Database writes only in the hook; emails and other calls after
  it.
- Receive-only NWC is required; a spend-capable code fails closed unless
  explicitly overridden. Never set that override.
- There is NO merchant-initiated refund of a settled Lightning payment, because
  the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
  never converted — are the only refund OpenReceive performs, and only from the
  `refund_required` provider state. Do not build, promise, or imply a Lightning
  refund path.
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
  late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
  after leaving your page to fetch an address from another wallet. The order
  needs its own URL that your functions serve (`resumable` on
  `<openreceive-checkout>`), and the ATTEMPT must come back with it: keep the
  swap's `payment_hash` on your order row, fed from the `openreceive-state`
  event (`event.detail.state.payment_hash`) through a function of your own
  (the recipe's `api/order-swap.js`), and render it back as the
  `resume-payment-hash` attribute. The buyer's
  cookie must still be valid on that visit: a session that expired in
  between answers 403 and strands the deposit.
  https://openreceive.org/guides/swap-refunds.md
- Show the payer WHAT THEY ARE BUYING: return a `description` beside the
  price from `amountFor`. The element renders it above the amount, and its
  transaction details on the live checkout and the receipt.
- Plain JavaScript: no TypeScript, no bundler, no framework added for this.
- HTTP JSON is snake_case; JavaScript APIs are camelCase.
- Money is integers or decimal strings — never binary floats.

## If you build your own checkout UI

Do not, on a site like this: the element is the checkout. If the user asks
for one, read https://openreceive.org/guides/checkout-ux.md and
https://openreceive.org/guides/headless-checkout.md first. With no bundler, use
`renderTransactionDetailsHtml` from the standalone file for the transaction
record: it has no `createTransactionDetails`, and one bad import stops the
whole page. No "Open wallet" or "Pay in wallet" button, and no `lightning:`
link: Cash App cancels an invoice opened from a link. Show the QR code and a
Copy invoice button.

## More documentation

Fetch one when the moment comes. Each is raw markdown, so a plain GET is
enough; drop the `.md` for the same page a person would read.

- https://openreceive.org/guides/vercel-functions.md — what the merchant does on Vercel around these steps
- https://openreceive.org/guides/serverless.md — pooled database URLs, timeouts, and when a scheduled pass helps
- https://openreceive.org/guides/storage.md — the payment tables and the attempt state machine
- https://openreceive.org/guides/authorization.md — before you write `authorize`
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
- https://openreceive.org/guides/supabase.md — when the database is Supabase: its pooler and its certificate
- https://openreceive.org/guides/frontend-checkout.md — the element's attributes and events, and the standalone build
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay
  tutorials come from: inside the JavaScript, nothing to serve
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
- https://openreceive.org/guides/currencies.md — supported currencies
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
- https://openreceive.org/guides/api-reference.md — every route, option and error code
- https://openreceive.org/guides/quickstart-next.md — each hook explained step by step, on Next.js; they are the same here
- https://openreceive.org/guides.md — the index, if what you need is not above

Questions, or a problem with the library itself:
https://openreceive.org/contact

---

## The quickstart, in full

Inlined verbatim so this file needs no network access. Steps 0–6 above are the setup on Vercel and this recipe is their reference: its files are the code to write. Where the two differ, the steps win.
The page it comes from is https://openreceive.org/guides/vercel-functions-recipe.

## Vercel Functions recipe

For a site on Vercel with no framework: plain JavaScript functions under
`api/`, and static HTML pages. A Vercel Function can take a Web-standard
`Request` and return a `Response`, and so can `@openreceive/http`, so the
integration is one function, one rewrite and an element on the order page.
Your server code creates each Lightning invoice, the payment goes straight to
your wallet, and payment attempts are stored in your Postgres database. There
is no OpenReceive account, no API key and no background worker.

Optional swaps let customers pay with **USDT, USDC, SOL, and ETH**. A swap
provider you configure converts the payment to **BTC over Lightning**, and it
settles into the same wallet. Available assets and networks depend on the
provider.

This recipe was checked on Vercel with Fluid compute, Node 24, a Neon
database and `@openreceive/*` 0.5.0: a buyer got a real Lightning invoice, a
stranger got 403, three requests at once left one live invoice, and a paid
invoice marked the order paid once. The function below runs in OpenReceive's
CI on every change, through PgBouncer in transaction mode, as Neon's pooler
works.

```sh
npm install @openreceive/http @openreceive/elements pg @vercel/functions
```

### Where it runs

- **On the Node runtime**, Node 22 or newer. Never `runtime: "edge"`: the
  wallet connection and `pg` need Node.
- **As ES modules.** The packages are ESM only: set `"type": "module"` in
  `package.json`, or name the files `.mjs`.
- **Postgres, shared by every instance.** Vercel runs many instances of a
  function, and each has its own short-lived disk, so SQLite cannot work.
  Neither can a KV store or Redis. If the project has no Postgres, add Neon
  from the project's **Storage** tab. Its free plan is enough. Neon sets
  `DATABASE_URL`, a pooled URL for the app, and `DATABASE_URL_UNPOOLED`, a
  direct URL for creating tables.
- **No cron job or worker required.** Each request to the payment routes also
  checks the wallet for settled invoices, through a lock in your database, so
  a payer who closes the tab is settled on the next request from anyone.
  On a quiet site that can be hours later. If fulfillment should not wait,
  see [When the payer leaves the page](https://openreceive.org/guides/deploying.md#when-the-payer-leaves-the-page).
- **Pages in `public/`.** With no `public/` folder, Vercel serves every file
  in the project's root, your server modules in `lib/` included. The codes
  stay in environment variables either way, but keeping the pages in
  `public/` keeps the rest of the code private.

### The routes: one function and a rewrite

Every payment route sits under `/openreceive/`: `/openreceive/checkouts`,
`/openreceive/checkouts/prepare`, `/openreceive/payments/check`,
`/openreceive/swaps/status` and a few more. One function serves them all,
through a rewrite:

```json
{
  "rewrites": [{ "source": "/openreceive/:path*", "destination": "/api/openreceive" }],
  "functions": { "api/openreceive.js": { "maxDuration": 30 } }
}
```

Add both entries to `vercel.json`, beside any it already has. The request the
function receives still carries the path the browser asked for, such as
`/openreceive/checkouts/prepare` (Vercel appends the matched part as a
`path` query parameter, which the routes ignore), so the handler routes it
as is.

Outside Next.js, a file named `api/openreceive/[...path].js` is not a
catch-all: it matches one path segment, so `/checkouts/prepare` would get
Vercel's 404 page. Use the rewrite.

`maxDuration: 30` gives a request time to reach the wallet and, now and
then, to scan it for payments, which OpenReceive cuts off at 9 seconds. With
Fluid compute the default is already longer; without it, the Hobby plan's
default is 10 seconds.

### The payment function

```js
// lib/db.js — one small pool per function instance
import { attachDatabasePool } from "@vercel/functions";
import pg from "pg";

// Neon's pooled URL. Vercel sets DATABASE_URL in every environment.
export const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL, max: 3 });
// Closes idle connections before Vercel suspends the instance.
attachDatabasePool(pool);
```

```js
// api/openreceive.js — every route under /openreceive/, through the rewrite
import { createStack } from "@openreceive/http";
import { pool } from "../lib/db.js";
import { findOrder, visitorFrom } from "../lib/orders.js"; // your own orders

let stack;

// Built on the first request, then kept for the instance's later requests.
// createStack connects to the wallet as soon as it is built, and Vercel can
// load a function well before a request arrives, so never build it at import.
function openReceive() {
  stack ??= createStack({
    wallet: { nwc: process.env.NWC_URI ?? "" },
    storage: {
      db: pool,
      // Runs once, inside the transaction that records the payment. paidAt is
      // Unix seconds: for a timestamptz column, write to_timestamp($1).
      onPaid: async ({ reference, paidAt, query }) => {
        await query(
          "UPDATE orders SET state = 'paid', paid_at = $1 WHERE id = $2 AND state = 'awaiting_payment'",
          [paidAt, reference],
        );
      },
    },
    // The price comes from the order row, never from the request.
    amountFor: async (reference) => {
      const order = await findOrder(reference);
      return order?.state === "awaiting_payment"
        ? { currency: order.currency, value: order.amount, description: order.title }
        : null;
    },
    // Only the buyer may pay for their order.
    authorize: async ({ request, resource }) => {
      const order = resource.reference ? await findOrder(resource.reference) : undefined;
      const visitor = visitorFrom(request);
      return Boolean(order && visitor && order.visitor === visitor);
    },
    // Vercel replaces any x-forwarded-for a client sends, so its first
    // address is the payer's.
    rateLimiting: {
      ip: ({ request }) => request.headers.get("x-forwarded-for")?.split(",")[0]?.trim(),
    },
  });
  return stack;
}

export function GET(request) {
  return openReceive().handler(request);
}

export function POST(request) {
  return openReceive().handler(request);
}
```

`findOrder` and `visitorFrom` are your own code: read the order row, and
the buyer from the request's cookie or session. The function's exports take
a `Request`. If your other functions use the older `(req, res)` form, keep
them as they are: each file picks its own form.

The [Next.js quickstart](https://openreceive.org/guides/quickstart-next.md) explains each hook.
They are the same here.

#### A guest checkout

With no logins, give the buyer a cookie when your own code creates the order,
and keep the same value on the order row. Sign it with your app's own secret
(such as `SESSION_SECRET`), never with `NWC_URI`, and make it last: a payer
with a swap refund comes back hours later, and `authorize` must still know
them ([Swap refunds](https://openreceive.org/guides/swap-refunds.md)).

```js
// lib/orders.js — reading the buyer back from a signed cookie
import { createHmac, timingSafeEqual } from "node:crypto";

const sign = (value) =>
  createHmac("sha256", process.env.SESSION_SECRET).update(value).digest("base64url");

export function visitorCookie(visitor) {
  return `visitor=${visitor}.${sign(visitor)}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=31536000`;
}

export function visitorFrom(request) {
  const pair = (request.headers.get("cookie") ?? "")
    .split(";")
    .map((part) => part.trim())
    .find((part) => part.startsWith("visitor="));
  const [visitor, signature] = (pair?.slice("visitor=".length) ?? "").split(".");
  if (!visitor || !signature) return undefined;
  const expected = Buffer.from(sign(visitor));
  const given = Buffer.from(signature);
  return given.length === expected.length && timingSafeEqual(given, expected) ? visitor : undefined;
}
```

### Your database

Create OpenReceive's two tables, `openreceive_payments` and
`openreceive_meta`, with your migrations, or from the build step over the
direct URL. `paymentsSchemaSql` is idempotent, so it can run on every deploy:

```js
// scripts/openreceive-tables.mjs — run from "build" in package.json
import { paymentsSchemaSql } from "@openreceive/http";
import pg from "pg";

const client = new pg.Client({
  connectionString: process.env.DATABASE_URL_UNPOOLED ?? process.env.DATABASE_URL,
});
await client.connect();
try {
  await client.query(paymentsSchemaSql("postgres"));
} finally {
  await client.end();
}
```

Your orders stay in your own tables. OpenReceive reads the price through
`amountFor` and marks the order paid through `onPaid`, and it never joins
your tables.

#### Orders that are not in Postgres

Some sites keep orders in Stripe, Airtable, a JSON file or a KV store. The
payment rows still need Postgres: add Neon for them. `amountFor` and `onPaid`
can reach your orders wherever they live. In `onPaid`, call your order store
after the database writes; `query` is only for SQL in the same database. Mark
the order paid only once: check its state first, since `onPaid` runs again if
your store failed the first time.

### The checkout page

The page needs no bundler. `@openreceive/elements` ships a standalone build:
copy its two files to where your site serves static files, in the same
build step:

```json
{
  "scripts": {
    "build": "node scripts/openreceive-tables.mjs && mkdir -p public/openreceive-checkout && cp node_modules/@openreceive/elements/dist/standalone/openreceive-checkout.js node_modules/@openreceive/elements/dist/standalone/openreceive-checkout.css public/openreceive-checkout/"
  }
}
```

Use `public/` if the site serves its static files from there, and the
project's root otherwise. Then put the element on the order's own page, with
the order's id as `reference`:

```html
<link rel="stylesheet" href="/openreceive-checkout/openreceive-checkout.css" />
<script type="module" src="/openreceive-checkout/openreceive-checkout.js"></script>

<openreceive-checkout reference="ORDER_ID" prefix="/openreceive" resumable></openreceive-checkout>
```

A function that renders the page writes the id in. A static page can set it
from its own address, such as `/order.html?id=…`:

```html
<script type="module">
  const checkout = document.querySelector("openreceive-checkout");
  checkout.setAttribute("reference", new URLSearchParams(location.search).get("id"));
</script>
```

The payer picks Bitcoin and gets the invoice. Keep the page at the order's
own URL: a payer with a swap deposit in flight comes back to it
([Swap refunds](https://openreceive.org/guides/swap-refunds.md)).

#### The way back to a swap refund

With swaps on, a deposit that arrives short or late is refunded on a later
visit, often from another device. Keep the swap's payment hash on the order
row: add a `swap_payment_hash TEXT` column, and a function that saves it for
the order's own buyer:

```js
// api/order-swap.js — keeps the swap's payment hash on the order
import { pool } from "../lib/db.js";
import { findOrder, visitorFrom } from "../lib/orders.js";

export async function POST(request) {
  const { reference, payment_hash: paymentHash } = await request.json();
  const order = typeof reference === "string" ? await findOrder(reference) : undefined;
  const visitor = visitorFrom(request);
  if (!order || !visitor || order.visitor !== visitor || !/^[0-9a-f]{64}$/.test(paymentHash)) {
    return new Response(null, { status: 403 });
  }
  await pool.query("UPDATE orders SET swap_payment_hash = $1 WHERE id = $2", [
    paymentHash,
    order.id,
  ]);
  return new Response(null, { status: 204 });
}
```

The page sends the hash when a swap starts, and writes a saved one back as
`resume-payment-hash`, which reopens that swap instead of the payment
methods:

```html
<openreceive-checkout reference="ORDER_ID" prefix="/openreceive" resumable
  resume-payment-hash="SAVED_HASH"></openreceive-checkout>
<script type="module">
  const checkout = document.querySelector("openreceive-checkout");
  checkout.addEventListener("openreceive-state", (event) => {
    const { state } = event.detail;
    if (state.rail !== "swap" || !state.payment_hash) return;
    fetch("/api/order-swap", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ reference: checkout.getAttribute("reference"), payment_hash: state.payment_hash }),
    });
  });
</script>
```

Leave `resume-payment-hash` out while the order has none. The checkout ignores
a hash the server will not serve, so a stale one does no harm.

### Settings

In the project's **Settings** → **Environment Variables**, as **Secret**, for
Production, Preview and Development:

- `NWC_URI`: a receive-only code from your wallet
  ([get one](https://openreceive.org/get_a_nwc_code_to_receive_payments)).
- Optional: `LSC_URI_PRIMARY` for swaps
  ([set one up](https://openreceive.org/set_up_swap_provider)), and
  `LSC_URI_BACKUP`.
- `DATABASE_URL` and `DATABASE_URL_UNPOOLED`, which Neon adds itself.

OpenReceive reads `LSC_URI_PRIMARY` and `LSC_URI_BACKUP` from the environment
itself, so `createStack` takes only the wallet code. Keep them all out of
`vercel.json`, the code and the logs. Vercel gives a
deployment the variables of its environment, so a code added later reaches
only the deploys after it: redeploy.

### Check it

On a preview or production deploy, a stranger asking for a real order must
get OpenReceive's JSON refusal, not Vercel's 404 page:

```sh
curl -s -X POST https://<your-deployment>/openreceive/checkouts/prepare \
  -H 'content-type: application/json' -d '{"reference":"<a real order id>"}'
```

It answers `403` with `"code":"FORBIDDEN"`. Vercel's `NOT_FOUND` page means
the rewrite is missing. Then, as the buyer, open the order's page: it shows
the payment methods and, after you pick Bitcoin, a Lightning invoice.

A preview deployment behind Vercel Authentication answers `401` to `curl`.
Check that one in the browser where you are signed in to Vercel, or on the
production domain.
