openreceive 0.4.3 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/lib/openreceive/core.rb +14 -14
- data/lib/openreceive/generated/tables.rb +290 -0
- data/lib/openreceive/version.rb +1 -1
- data/lib/openreceive.rb +3 -0
- data/skills/integrate-openreceive/SKILL.md +15 -5
- data/skills/integrate-openreceive/references/btcpay.md +193 -0
- data/skills/integrate-openreceive/references/django.md +667 -0
- data/skills/integrate-openreceive/references/fastapi.md +537 -0
- data/skills/integrate-openreceive/references/fastify.md +529 -0
- data/skills/integrate-openreceive/references/laravel.md +658 -0
- data/skills/integrate-openreceive/references/next.md +580 -0
- data/skills/integrate-openreceive/references/node.md +33 -14
- data/skills/integrate-openreceive/references/php.md +598 -0
- data/skills/integrate-openreceive/references/rails.md +72 -27
- metadata +9 -1
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
# OpenReceive agent directions (Next.js)
|
|
2
|
+
|
|
3
|
+
These directions describe OpenReceive 0.4.4.
|
|
4
|
+
|
|
5
|
+
Add OpenReceive to a Next.js App Router application — the app you are already
|
|
6
|
+
working in. You do not need a copy of the OpenReceive source: the packages are
|
|
7
|
+
on npm, and the quickstart is appended to this file in full, so you can do the
|
|
8
|
+
whole integration without fetching anything. Prefer the published packages and
|
|
9
|
+
the route handlers they export — do not reimplement wallet RPC, settlement, or
|
|
10
|
+
pricing.
|
|
11
|
+
|
|
12
|
+
Do not clone the OpenReceive repository into this app, and do not copy a demo's
|
|
13
|
+
models (`ShopOrder`, a signed-cookie visitor, an in-memory catalog) over tables
|
|
14
|
+
that already exist. Find this application's order, product, and user models —
|
|
15
|
+
whatever they are actually named — and map the three hooks onto those.
|
|
16
|
+
|
|
17
|
+
Keep this application's frontend framework, authentication and database. Pick
|
|
18
|
+
the UI package that matches the frontend that is already here
|
|
19
|
+
(`@openreceive/react`, `/vue`, `/svelte`, `/angular`, or `/elements` for
|
|
20
|
+
plain HTML) — do not add React to a Vue app. Reuse the app's existing
|
|
21
|
+
session/auth in `authorize` and its existing database handle in `storage.db`.
|
|
22
|
+
|
|
23
|
+
## What OpenReceive is
|
|
24
|
+
|
|
25
|
+
A payment library that runs inside YOUR server. It mounts as ONE catch-all
|
|
26
|
+
route handler (`app/openreceive/[...openreceive]/route.ts`, on the Node
|
|
27
|
+
runtime, `force-dynamic`) in the application you are editing, issues Lightning
|
|
28
|
+
invoices against a wallet the merchant already controls, and calls back into
|
|
29
|
+
your code when one settles. There is no OpenReceive account and no API key, and
|
|
30
|
+
OpenReceive never holds the funds — the sats land in the wallet the merchant
|
|
31
|
+
connected.
|
|
32
|
+
|
|
33
|
+
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
|
|
34
|
+
a string from the merchant's wallet that can create invoices and read their
|
|
35
|
+
status, and cannot spend. A swap provider (an "LSC" code) optionally lets the
|
|
36
|
+
payer send USDT, USDC, ETH or SOL instead, converted into that same
|
|
37
|
+
Lightning payment. You supply those credentials and three hooks — `authorize`, `amountFor`,
|
|
38
|
+
`onPaid`;
|
|
39
|
+
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
|
|
40
|
+
owns orders, users, prices, or fulfillment.
|
|
41
|
+
|
|
42
|
+
## Step 0 — check the environment before you write code
|
|
43
|
+
|
|
44
|
+
Do this before installing packages or editing files.
|
|
45
|
+
|
|
46
|
+
1. Look for `NWC_URI` in this app's server environment — `.env.local`, the
|
|
47
|
+
process env, the deploy config, whatever this app already uses. Next loads
|
|
48
|
+
`.env.local` itself: do not add `dotenv`, and never give a credential a
|
|
49
|
+
`NEXT_PUBLIC_` prefix, which inlines it into the browser bundle. If the app runs in a
|
|
50
|
+
container the value is in none of those: ask the running process
|
|
51
|
+
(`docker exec <container> printenv NWC_URI`), because finding the NAME in a
|
|
52
|
+
compose file proves nothing about the value. Never print or echo the value
|
|
53
|
+
itself; only report whether it is set. Check for `LSC_URI_PRIMARY` in the
|
|
54
|
+
same pass.
|
|
55
|
+
2. If BOTH are already set — the common case in an existing app — say so and go
|
|
56
|
+
straight to the quickstart. Steps 3 and 4 are for an environment that is
|
|
57
|
+
missing one; do not stop to ask about altcoins that are already configured.
|
|
58
|
+
If only `NWC_URI` is set, Bitcoin already works: continue, and raise the
|
|
59
|
+
altcoin question at step 4 rather than blocking on it.
|
|
60
|
+
3. If `NWC_URI` is missing or empty, stop and tell the user exactly what to
|
|
61
|
+
create:
|
|
62
|
+
|
|
63
|
+
> OpenReceive cannot issue an invoice without a receive-only NWC code. Get
|
|
64
|
+
> one at https://openreceive.org/get_a_nwc_code_to_receive_payments, then
|
|
65
|
+
> put `NWC_URI=<the code>` in this app's server environment — for a Next
|
|
66
|
+
> app that is `.env.local` in the project root — and tell me when it's set.
|
|
67
|
+
|
|
68
|
+
Wait for the user before wiring OpenReceive; do not invent a placeholder
|
|
69
|
+
value. Waiting is not idleness: you may write `.env.example` with the
|
|
70
|
+
variable NAMES only (`NWC_URI=`, `LSC_URI_PRIMARY=`) so the merchant has a
|
|
71
|
+
file to copy, and keep building the parts of the host that do not touch
|
|
72
|
+
OpenReceive — the order model, the cart, the routes. The stop guards the
|
|
73
|
+
credential, not the rest of the app.
|
|
74
|
+
4. If `LSC_URI_PRIMARY` was not already set, ask the user: "Do you want to
|
|
75
|
+
accept altcoins and stablecoins (USDT, USDC, ETH, SOL) as well as
|
|
76
|
+
Bitcoin?"
|
|
77
|
+
|
|
78
|
+
- Yes → send them to https://openreceive.org/set_up_swap_provider for a
|
|
79
|
+
swap-provider (LSC) code, to set as `LSC_URI_PRIMARY` in the same server
|
|
80
|
+
environment. Do NOT wait for it: no application code reads the value, so
|
|
81
|
+
the integration is identical with or without it — the library picks it up
|
|
82
|
+
from the environment and swaps switch on. What a yes DOES change is the
|
|
83
|
+
refund route back (the swap non-negotiable below): build it as part of
|
|
84
|
+
this integration, not when the code arrives.
|
|
85
|
+
- No → skip it. Bitcoin over Lightning works with `NWC_URI` alone, and you
|
|
86
|
+
can add a swap provider later without changing application code.
|
|
87
|
+
5. Check the environment again and confirm `NWC_URI` is present.
|
|
88
|
+
`LSC_URI_PRIMARY` may land later; swaps stay off until it does, and no code
|
|
89
|
+
changes when it arrives.
|
|
90
|
+
6. If OpenReceive is ALREADY installed here, check the installed versions of
|
|
91
|
+
`@openreceive/node` and `@openreceive/browser` against the release named at
|
|
92
|
+
the top of this file. The headless display models below do not exist in
|
|
93
|
+
older versions, and the first tile click throws with nothing saying why.
|
|
94
|
+
Upgrade first.
|
|
95
|
+
|
|
96
|
+
Only then start the quickstart.
|
|
97
|
+
|
|
98
|
+
## Non-negotiables
|
|
99
|
+
|
|
100
|
+
The quickstart below has the code. These are the rules it cannot state for
|
|
101
|
+
itself, and they hold for every integration.
|
|
102
|
+
|
|
103
|
+
- OpenReceive never owns orders, users, prices, or fulfillment. The section
|
|
104
|
+
below is how those tables sit next to the library — not a second order model,
|
|
105
|
+
and not a Prisma/Drizzle relation to `openreceive_payments`.
|
|
106
|
+
- Keep `NWC_URI` / `LSC_URI_*` server-only. Never put them in browser code,
|
|
107
|
+
logs, or assets.
|
|
108
|
+
- The host owns the price. `amountFor` reads it from your own data; reject
|
|
109
|
+
payer-supplied amounts.
|
|
110
|
+
- `authorize` runs on every request, and the `resource` it receives is a CLAIM
|
|
111
|
+
the payer made, not proof. Read a framework session; never trust a body field.
|
|
112
|
+
- `onPaid` must be idempotent. It runs once per `reference` — your order id, one
|
|
113
|
+
per thing you fulfill, created before checkout, kept across retries, never
|
|
114
|
+
reused. A fresh id per page load lets one order be paid twice.
|
|
115
|
+
- Receive-only NWC is required; a spend-capable code fails closed at boot unless
|
|
116
|
+
explicitly overridden.
|
|
117
|
+
- There is NO merchant-initiated refund of a settled Lightning payment, because
|
|
118
|
+
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
|
|
119
|
+
never converted — are the only refund OpenReceive performs, and only from the
|
|
120
|
+
`refund_required` provider state. Do not build, promise, or imply a Lightning
|
|
121
|
+
refund path.
|
|
122
|
+
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
|
|
123
|
+
late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
|
|
124
|
+
after leaving your page to fetch an address from another wallet. Three things
|
|
125
|
+
must exist or that money is unreachable through your UI: a per-order URL your
|
|
126
|
+
server serves (`/checkout/:reference` — `syncUrl` on the drop-ins), your own
|
|
127
|
+
order-summary route to restore the order from, and the ATTEMPT.
|
|
128
|
+
`/checkouts/prepare` returns no attempts, so a checkout rebuilt from the
|
|
129
|
+
reference alone opens on the method grid. Re-picking the same coin
|
|
130
|
+
(`POST /swaps`) re-serves the committed attempt — but only while it is live,
|
|
131
|
+
and the shadow invoice behind a swap lasts about half an hour, after which the
|
|
132
|
+
same click mints a NEW deposit address and the refund is off-screen. Keep the
|
|
133
|
+
`payment_hash` and reopen the attempt with `POST /swaps/status`, which has no
|
|
134
|
+
such window. https://openreceive.org/guides/swap-refunds.md
|
|
135
|
+
- Show the payer WHAT THEY ARE BUYING. Return an optional `description` beside
|
|
136
|
+
the price from `amountFor` and both drop-ins render it above the amount.
|
|
137
|
+
Without it the checkout is a QR and "$1.00" with no sign of what the dollar
|
|
138
|
+
is for.
|
|
139
|
+
- Show the payer the transaction record: `createTransactionDetails(...)` rows,
|
|
140
|
+
collapsed behind a caret, on the live checkout AND on the receipt. A payment
|
|
141
|
+
hash and a deposit txid are the only evidence a payer has that they paid you.
|
|
142
|
+
`<Checkout>` / `<openreceive-checkout>` already render this panel and the
|
|
143
|
+
`description` — these two rules cost you code only on a custom UI or your own
|
|
144
|
+
receipt page, never a reason to replace the drop-in. (It returns no rows
|
|
145
|
+
while the rail is `checkout_lock` — before the payer has chosen anything
|
|
146
|
+
there is no transaction — so render the caret only when the rows are
|
|
147
|
+
non-empty.)
|
|
148
|
+
- The route file is a SERVER module and stays on the Node runtime with
|
|
149
|
+
`export const runtime = "nodejs"` and `export const dynamic =
|
|
150
|
+
"force-dynamic"` — never the Edge runtime, never cached. Nothing in it may be
|
|
151
|
+
imported from a client component. `<Checkout>` is a CLIENT component: it
|
|
152
|
+
lives in a `"use client"` file, which also imports `styles.css`. Render it
|
|
153
|
+
at `app/checkout/[reference]/page.tsx`, the order's own resumable URL.
|
|
154
|
+
- `rateLimiting` on Next needs an IP source (`trustProxyIpHeader: true` behind
|
|
155
|
+
your own proxy or platform); a web Request has no socket IP and the adapter
|
|
156
|
+
refuses to construct without one.
|
|
157
|
+
- HTTP JSON is snake_case; TypeScript APIs are camelCase.
|
|
158
|
+
- Money is integers or decimal strings — never binary floats.
|
|
159
|
+
|
|
160
|
+
## Your tables, not ours
|
|
161
|
+
|
|
162
|
+
`npx openreceive scaffold payments` emits `openreceive_payments` and
|
|
163
|
+
`openreceive_meta` for THIS application's database. That is the whole
|
|
164
|
+
persistence OpenReceive needs. It does not replace your orders, users, or
|
|
165
|
+
products, and you do not join them.
|
|
166
|
+
|
|
167
|
+
- **Find this app's models first.** They may be named `Order`, `Invoice`,
|
|
168
|
+
`Booking`, `Product`, `Variant`, `User`, `Account` — anything. Wire the hooks
|
|
169
|
+
to those. Do not generate a parallel `ShopOrder` / `ShopProduct` / `ShopUser`
|
|
170
|
+
stack.
|
|
171
|
+
- **The payable row's id is the `reference`.** Create it before checkout, keep
|
|
172
|
+
it across retries, never reuse it. Pass that id to `<Checkout>` /
|
|
173
|
+
`<openreceive-checkout>`. A fresh id per page load lets one order be paid
|
|
174
|
+
twice.
|
|
175
|
+
- **Products (or the catalog) are the price authority.** Order creation reads
|
|
176
|
+
live prices into the order (snapshot line items if this app has them).
|
|
177
|
+
`amountFor` reads only that order — never a payer-supplied amount, never a
|
|
178
|
+
live catalog lookup that could re-price a cart already placed. Return
|
|
179
|
+
`{ currency, value }` as a decimal STRING, plus a `description` of what they
|
|
180
|
+
are buying.
|
|
181
|
+
- **Users own the order; OpenReceive never sees them.** `authorize` uses the
|
|
182
|
+
same ownership check this app already uses on the order show / pay page —
|
|
183
|
+
`sessions.currentUser(request)`, a cookie, whatever it is.
|
|
184
|
+
`resource.reference` is a claim the payer sent, not proof.
|
|
185
|
+
- **The order is unpaid or paid.** Do not copy `pending` / `expired` / `failed`
|
|
186
|
+
/ `attention` onto it. Those are attempt statuses on `openreceive_payments`. An
|
|
187
|
+
expired invoice does not cancel the order; a later checkout may mint another
|
|
188
|
+
attempt. The library refuses a new checkout under a reference that already
|
|
189
|
+
settled (409).
|
|
190
|
+
- **Pass this app's `db` handle.** Do not add a Prisma/Drizzle relation from
|
|
191
|
+
Order to `openreceive_payments`, and do not implement `PaymentRepository`
|
|
192
|
+
unless no supported handle can reach this database. `reference` is not unique
|
|
193
|
+
(many attempts per order). Fulfillment is a guarded transition on YOUR order
|
|
194
|
+
row inside `onPaid` — `UPDATE … WHERE state = 'awaiting_payment'` (or this
|
|
195
|
+
app's equivalent) through the `query` the library hands you, on that same
|
|
196
|
+
settlement transaction, not a second connection from your ORM. Database writes
|
|
197
|
+
only in the hook; emails, jobs, and pushes after commit. Placeholder style is
|
|
198
|
+
the dialect you declared: `?` on sqlite, `$1` on postgres.
|
|
199
|
+
|
|
200
|
+
## If you build your own checkout UI
|
|
201
|
+
|
|
202
|
+
The drop-ins (`<Checkout>`, `<openreceive-checkout>`) already obey all of this.
|
|
203
|
+
This list is the short form of https://openreceive.org/guides/checkout-ux.md, for a
|
|
204
|
+
UI built on `@openreceive/browser/headless`. Read that before writing
|
|
205
|
+
components.
|
|
206
|
+
|
|
207
|
+
- `createCheckoutController` is the engine. Do not hand-roll a poll loop.
|
|
208
|
+
- `createCheckoutStatusModel` for the status line. Do not draw a
|
|
209
|
+
Cart → Pay → Done stepper. Read the model's `phase`, not the snapshot's.
|
|
210
|
+
- `resolveWizardSelection` decides whether to ask "which network?". A
|
|
211
|
+
one-network asset starts the swap from the tile. Key `selectedAssetByGroup`
|
|
212
|
+
by group (`USDT`), valued by `pay_in_asset` (`USDT_TRON`).
|
|
213
|
+
- `createMethodGridDisplay` for tiles, including `limitMessage` so an
|
|
214
|
+
unavailable method says the minimum in the payer's currency.
|
|
215
|
+
- `createSwapDisplayModel` → `display.copyRows` for deposits: address, memo,
|
|
216
|
+
and the bare amount each get a copy row. Render `swap.networkWarning*` as
|
|
217
|
+
the model gives it.
|
|
218
|
+
- `createCheckoutSession` owns mint and swap start. To start swaps, pass its
|
|
219
|
+
`swap` option (`selection`, `prefix`, `fetch`) together. Without it
|
|
220
|
+
`startSwap` reports through `onError`.
|
|
221
|
+
- `createQrSvg` is async. Use `createQrSvgController` so you do not render
|
|
222
|
+
`[object Promise]`.
|
|
223
|
+
- `checkoutLabels` for every payer-facing string. Only write copy it lacks.
|
|
224
|
+
- `stageSwapRefund` then `confirmSwapRefund` — only the second submits.
|
|
225
|
+
Validate with `getSwapRefundFormError`. Treat `409` as a normal outcome.
|
|
226
|
+
- Pass `{ resumable: true }` to `createSwapDisplayModel` when the payer has
|
|
227
|
+
a URL they can come back to, and render `display.refundReturnLabel`.
|
|
228
|
+
Resume helpers (`createGuestCheckoutResume`, `createGuestOrderFetcher`)
|
|
229
|
+
are on `@openreceive/browser`, not `/headless`.
|
|
230
|
+
- A refund replaces the deposit panel. On `refund_required` also drop
|
|
231
|
+
"switch payment method".
|
|
232
|
+
- No "Open wallet" button on desktop.
|
|
233
|
+
- Wallet suggestions: `getPaymentWizardRoutes()` +
|
|
234
|
+
`createWizardRouteDisplays`. Lightning only. Every image ships inside
|
|
235
|
+
the JavaScript — logos as data URIs, tutorials once `loadPayTutorialImages()`
|
|
236
|
+
resolves (`image` is `undefined` until then) — so serve nothing and set no
|
|
237
|
+
asset option. When it works, the logos and payment icons render; a missing
|
|
238
|
+
image means a CSP `img-src` that blocks `data:`, and the console names it.
|
|
239
|
+
|
|
240
|
+
## More documentation
|
|
241
|
+
|
|
242
|
+
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
|
|
243
|
+
enough; drop the `.md` for the same page a person would read.
|
|
244
|
+
|
|
245
|
+
- https://openreceive.org/guides/authorization.md — before you write `authorize`
|
|
246
|
+
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
|
|
247
|
+
- https://openreceive.org/guides/storage.md — the payment tables and the attempt state machine
|
|
248
|
+
- https://openreceive.org/guides/node-orms.md — recipes for Prisma, Drizzle, Knex, TypeORM, Sequelize
|
|
249
|
+
- https://openreceive.org/guides/frontend-checkout.md — the drop-in's props, attributes and slots
|
|
250
|
+
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
|
|
251
|
+
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
|
|
252
|
+
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay
|
|
253
|
+
tutorials come from: inside the JavaScript, nothing to serve. This is the page
|
|
254
|
+
that owns the image rule, not the summary in checkout-ux.md
|
|
255
|
+
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
|
|
256
|
+
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
|
|
257
|
+
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
|
|
258
|
+
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
|
|
259
|
+
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
|
|
260
|
+
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
|
|
261
|
+
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
|
|
262
|
+
- https://openreceive.org/guides/api-reference.md — every route, option and error code
|
|
263
|
+
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the shipped adapter's routes with your own
|
|
264
|
+
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
|
|
265
|
+
- https://openreceive.org/guides.md — the index, if what you need is not above
|
|
266
|
+
|
|
267
|
+
Questions, or a problem with the library itself:
|
|
268
|
+
https://openreceive.org/contact
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## The quickstart, in full
|
|
273
|
+
|
|
274
|
+
Inlined verbatim so this file needs no network access — follow it once Step 0
|
|
275
|
+
passes. The page it comes from is https://openreceive.org/guides/quickstart-next.
|
|
276
|
+
|
|
277
|
+
## Next.js quickstart
|
|
278
|
+
|
|
279
|
+
Next.js App Router + React. Requires Node ≥ 22 and Next ≥ 15 (App Router).
|
|
280
|
+
|
|
281
|
+
### 1. Install
|
|
282
|
+
|
|
283
|
+
```sh
|
|
284
|
+
npm install @openreceive/next @openreceive/react
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Install the adapter for your server and the UI package for your frontend; the
|
|
288
|
+
wallet client, HTTP handler, and contracts come along as dependencies. The
|
|
289
|
+
`openreceive` package is the CLI only — `npx openreceive …` below needs no
|
|
290
|
+
install. Different stack? Swap the two packages; the rest of this guide is
|
|
291
|
+
identical.
|
|
292
|
+
|
|
293
|
+
| | Packages |
|
|
294
|
+
| -------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
295
|
+
| Server | `@openreceive/next`, `@openreceive/express`, `@openreceive/fastify` |
|
|
296
|
+
| Frontend | `@openreceive/react`, `@openreceive/vue`, `@openreceive/svelte`, `@openreceive/angular`, `@openreceive/elements` (plain HTML) |
|
|
297
|
+
|
|
298
|
+
Express: [quickstart-node.md](https://openreceive.org/guides/quickstart-node.md) · Fastify:
|
|
299
|
+
[quickstart-fastify.md](https://openreceive.org/guides/quickstart-fastify.md). This page is the Next.js one,
|
|
300
|
+
and it is where an agent handed the Express guide goes wrong: no `dotenv`, no
|
|
301
|
+
`app.use`, a client component for the checkout.
|
|
302
|
+
|
|
303
|
+
On a fresh project, the App Router is what `create-next-app` scaffolds; no
|
|
304
|
+
env loader is needed (Next loads `.env.local` itself). Install your ORM
|
|
305
|
+
before step 2 (`npm install prisma @prisma/client` on the Prisma path) —
|
|
306
|
+
`openreceive scaffold` emits files for the ORM you name but never installs it.
|
|
307
|
+
|
|
308
|
+
npm environments that run with `ignore-scripts` (some editor sandboxes) skip
|
|
309
|
+
Prisma's engine download and esbuild's binary postinstall, so a typecheck or
|
|
310
|
+
build that fails only there is environmental, not a code problem.
|
|
311
|
+
|
|
312
|
+
### 2. Migrate the payment tables
|
|
313
|
+
|
|
314
|
+
```sh
|
|
315
|
+
npx openreceive scaffold payments --orm prisma # or drizzle | typeorm | sequelize | knex
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
`openreceive scaffold payments` emits one schema/migration file for your ORM
|
|
319
|
+
and a wiring guide. It never touches a database.
|
|
320
|
+
→ [openreceive scaffold payments](https://openreceive.org/guides/api-reference.md#openreceive-scaffold-payments)
|
|
321
|
+
|
|
322
|
+
Then run the emitted migration through your normal workflow (for example
|
|
323
|
+
`npx prisma migrate dev`). OpenReceive owns the tables' logic at runtime; there
|
|
324
|
+
is nothing else to generate. Details:
|
|
325
|
+
[Payment storage](https://openreceive.org/guides/storage.md), [Node ORM recipes](https://openreceive.org/guides/node-orms.md).
|
|
326
|
+
|
|
327
|
+
No ORM? A bare driver handle (`pg`, `node:sqlite`, `better-sqlite3`) is a
|
|
328
|
+
supported `db` in step 4, and there is no scaffold flavor for it — execute the
|
|
329
|
+
same DDL once yourself with `paymentsSchemaSql(dialect)` from
|
|
330
|
+
`@openreceive/http` instead of scaffolding.
|
|
331
|
+
|
|
332
|
+
### 3. Add wallet credentials
|
|
333
|
+
|
|
334
|
+
Create a server-only `.env.local` (Next loads it into `process.env` on its
|
|
335
|
+
own — do **not** add `dotenv`, and never prefix these with `NEXT_PUBLIC_`,
|
|
336
|
+
which would inline them into the browser bundle):
|
|
337
|
+
|
|
338
|
+
```dotenv
|
|
339
|
+
NWC_URI=
|
|
340
|
+
LSC_URI_PRIMARY=
|
|
341
|
+
LSC_URI_BACKUP=
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
1. Get a receive-only NWC code from a compatible wallet
|
|
345
|
+
([get one here](https://openreceive.org/get_a_nwc_code_to_receive_payments))
|
|
346
|
+
→ `NWC_URI`.
|
|
347
|
+
2. Optionally set up a [swap provider](https://openreceive.org/set_up_swap_provider)
|
|
348
|
+
→ `LSC_URI_PRIMARY` (and `LSC_URI_BACKUP` if you have one).
|
|
349
|
+
|
|
350
|
+
Never put these values in browser code. Your application refuses to start if
|
|
351
|
+
the NWC code also advertises spend methods such as `pay_invoice`; mint a
|
|
352
|
+
receive-only code ([Security](https://openreceive.org/guides/security.md)).
|
|
353
|
+
|
|
354
|
+
In production supply the same variables through your host's secret manager or
|
|
355
|
+
process environment; `.env.local` is for development and is gitignored by
|
|
356
|
+
`create-next-app`. → [Environment variables](https://openreceive.org/guides/environment-variables.md).
|
|
357
|
+
|
|
358
|
+
### 4. Wire OpenReceive
|
|
359
|
+
|
|
360
|
+
One catch-all route file: your hooks plus a database handle. The adapter
|
|
361
|
+
builds the wallet client and the host and returns the `GET`/`POST` exports the
|
|
362
|
+
App Router expects; there is no background reconciler — settlement piggybacks
|
|
363
|
+
on requests through the durable gate, which is what makes it serverless-safe.
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
// app/openreceive/[...openreceive]/route.ts
|
|
367
|
+
import { openReceiveNextHandlers } from "@openreceive/next";
|
|
368
|
+
import { db, orders, sessions } from "@/lib/app"; // your existing database handle and models
|
|
369
|
+
|
|
370
|
+
// The wallet relay and your database driver need Node, never the Edge runtime,
|
|
371
|
+
// and a payment route must never be cached or statically rendered.
|
|
372
|
+
export const runtime = "nodejs";
|
|
373
|
+
export const dynamic = "force-dynamic";
|
|
374
|
+
|
|
375
|
+
export const { GET, POST } = openReceiveNextHandlers({
|
|
376
|
+
wallet: { nwc: process.env.NWC_URI! }, // receive-only NWC code; your app refuses to start otherwise
|
|
377
|
+
storage: {
|
|
378
|
+
db, // pg Pool/Client, node:sqlite, better-sqlite3, or a custom adapter
|
|
379
|
+
onPaid: async ({ reference, paidAt, query }) => {
|
|
380
|
+
// Settlement transaction; runs only for the first settled attempt for a
|
|
381
|
+
// reference. The WHERE clause is the lock: a second fulfillment path of
|
|
382
|
+
// yours (admin action, replayed job) updates zero rows and does nothing.
|
|
383
|
+
// Use `query` here, not your ORM's other connection. `?` on sqlite, `$1`
|
|
384
|
+
// on postgres.
|
|
385
|
+
const claimed = await query(
|
|
386
|
+
"UPDATE orders SET state = 'paid', paid_at = ? WHERE id = ? AND state = 'awaiting_payment' RETURNING id",
|
|
387
|
+
[paidAt, reference],
|
|
388
|
+
);
|
|
389
|
+
if (claimed.length === 0) return;
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
// The price for a reference — here, your order id — from your own data;
|
|
393
|
+
// OpenReceive converts it into the Lightning invoice. Return null when
|
|
394
|
+
// there is nothing to pay for. `value` is a decimal STRING from the order
|
|
395
|
+
// row, never a float and never a request param. `description` is what the
|
|
396
|
+
// payer is buying, in your own words.
|
|
397
|
+
amountFor: async (reference) => {
|
|
398
|
+
const order = await orders.find(reference);
|
|
399
|
+
return order
|
|
400
|
+
? {
|
|
401
|
+
currency: "USD",
|
|
402
|
+
value: order.total.toString(),
|
|
403
|
+
description: `${order.lines.length} items`,
|
|
404
|
+
}
|
|
405
|
+
: null;
|
|
406
|
+
},
|
|
407
|
+
// Your own access check: may this caller do this action to this reference?
|
|
408
|
+
// `resource.reference` is your own order id, sent back by the payer's
|
|
409
|
+
// browser — a claim, not proof — already validated as a non-empty string.
|
|
410
|
+
// `request` is the Web Request; read cookies or headers from it the way you
|
|
411
|
+
// would in any route handler (`native` is the same NextRequest).
|
|
412
|
+
authorize: async ({ action, request, resource }) =>
|
|
413
|
+
orders.viewerMay(
|
|
414
|
+
await sessions.currentUser(request),
|
|
415
|
+
resource.reference,
|
|
416
|
+
action,
|
|
417
|
+
),
|
|
418
|
+
// Recommended for public web shops: caps invoice creation at 60 per client IP
|
|
419
|
+
// per hour. A web Request has no socket IP, so on Next this ALSO needs an
|
|
420
|
+
// IP source: `trustProxyIpHeader: true` reads the first hop of
|
|
421
|
+
// x-forwarded-for, which is safe only when YOUR reverse proxy or hosting
|
|
422
|
+
// platform sets it (Vercel, Cloudflare and most load balancers do). Without
|
|
423
|
+
// an IP source the adapter refuses to construct rather than run an
|
|
424
|
+
// inactive limiter. Leave both off for point-of-sale deployments, where
|
|
425
|
+
// many payers share the terminal's IP.
|
|
426
|
+
rateLimiting: true,
|
|
427
|
+
trustProxyIpHeader: true,
|
|
428
|
+
});
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
The route file is a **server module** — it imports your database handle and
|
|
432
|
+
reads `process.env`, and nothing in it may be imported from a client
|
|
433
|
+
component. The default prefix is `/openreceive`, which is the directory the
|
|
434
|
+
file sits in; put the catch-all under another directory and pass `prefix` to
|
|
435
|
+
match.
|
|
436
|
+
|
|
437
|
+
When the options are themselves async (a database opened lazily, a wallet
|
|
438
|
+
client shared with a worker), build the handlers per request instead of at
|
|
439
|
+
module load — the Buy a Button example does exactly this:
|
|
440
|
+
|
|
441
|
+
```ts
|
|
442
|
+
async function handle(request: Request): Promise<Response> {
|
|
443
|
+
const { GET, POST } = openReceiveNextHandlers(await httpOptions());
|
|
444
|
+
return request.method === "GET" ? GET(request) : POST(request);
|
|
445
|
+
}
|
|
446
|
+
export { handle as GET, handle as POST };
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
The first request checks the wallet. Later OpenReceive requests also settle
|
|
450
|
+
pending invoices, so a payer who closes the tab is still covered.
|
|
451
|
+
`authorize` runs on every request.
|
|
452
|
+
→ [openReceiveNextHandlers](https://openreceive.org/guides/api-reference.md#openreceivenexthandlers) ·
|
|
453
|
+
[authorize context](https://openreceive.org/guides/api-reference.md#the-authorize-context)
|
|
454
|
+
|
|
455
|
+
`rateLimiting: true` is for public web shops. Leave it off for point-of-sale,
|
|
456
|
+
where many payers share one IP. → [Rate limiting](https://openreceive.org/guides/rate-limiting.md)
|
|
457
|
+
|
|
458
|
+
An optional worker, `startNotificationWorker({ service, host })`, listens for
|
|
459
|
+
wallet payment notifications so settlement does not wait for the next page
|
|
460
|
+
load. It is a separate long-lived Node process, not a route — on a serverless
|
|
461
|
+
host, skip it and rely on the request-path settlement above.
|
|
462
|
+
→ [startNotificationWorker](https://openreceive.org/guides/api-reference.md#startnotificationworker)
|
|
463
|
+
|
|
464
|
+
Composing the pieces yourself (`createOpenReceive` + `createHost`) is
|
|
465
|
+
supported when you need a shared wallet client or a custom repository.
|
|
466
|
+
→ [createOpenReceive](https://openreceive.org/guides/api-reference.md#createopenreceive) ·
|
|
467
|
+
[createHost](https://openreceive.org/guides/api-reference.md#createhost)
|
|
468
|
+
|
|
469
|
+
Your app also needs an ordinary order-creation route that validates the cart,
|
|
470
|
+
prices with exact decimal math, and returns the order id the page will pass as
|
|
471
|
+
the `reference`. OpenReceive never prices from payer input.
|
|
472
|
+
|
|
473
|
+
The `reference` is a string you choose, and it is the fulfillment identity:
|
|
474
|
+
your order id — one per thing you fulfill, created before checkout, kept
|
|
475
|
+
across retries, never reused. OpenReceive never looks inside it, but `onPaid`
|
|
476
|
+
runs once per reference, a new checkout under a reference that already
|
|
477
|
+
settled is refused with 409, and a fresh id per page load lets one order be
|
|
478
|
+
paid twice.
|
|
479
|
+
|
|
480
|
+
Naming boundary: TypeScript APIs use camelCase fields (`paymentHash`,
|
|
481
|
+
`amountMsats`); everything on the wire — the mounted HTTP routes and the
|
|
482
|
+
browser snapshots — is snake_case (`payment_hash`, `amount_msats`).
|
|
483
|
+
|
|
484
|
+
### 5. Render checkout
|
|
485
|
+
|
|
486
|
+
`<Checkout>` polls and holds state, so it is a **client component**. Put it in
|
|
487
|
+
a file that starts with `"use client"` and import the stylesheet from that
|
|
488
|
+
same module; the page that renders it can stay a server component.
|
|
489
|
+
|
|
490
|
+
```tsx
|
|
491
|
+
// app/checkout/[reference]/order-checkout.tsx
|
|
492
|
+
"use client";
|
|
493
|
+
|
|
494
|
+
import { Checkout } from "@openreceive/react";
|
|
495
|
+
import "@openreceive/react/styles.css";
|
|
496
|
+
|
|
497
|
+
export function OrderCheckout({ reference }: { reference: string }) {
|
|
498
|
+
return <Checkout reference={reference} prefix="/openreceive" />;
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
```tsx
|
|
503
|
+
// app/checkout/[reference]/page.tsx — the order's own, resumable URL
|
|
504
|
+
import { OrderCheckout } from "./order-checkout";
|
|
505
|
+
|
|
506
|
+
export default async function CheckoutPage({
|
|
507
|
+
params,
|
|
508
|
+
}: {
|
|
509
|
+
params: Promise<{ reference: string }>;
|
|
510
|
+
}) {
|
|
511
|
+
const { reference } = await params;
|
|
512
|
+
return <OrderCheckout reference={reference} />;
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
`/checkout/[reference]` is deliberately a page of its own rather than a modal
|
|
517
|
+
on the cart: a payer with a swap deposit in flight has no account and no email
|
|
518
|
+
from you, so this URL is the only thing that brings them back to their payment
|
|
519
|
+
— it has to survive a reload and a bookmark
|
|
520
|
+
([Swap refunds](https://openreceive.org/guides/swap-refunds.md)). Do not render the checkout for a
|
|
521
|
+
reference the current session may not see; the page can read the session and
|
|
522
|
+
404 before it renders, and `authorize` refuses the routes regardless.
|
|
523
|
+
|
|
524
|
+
The checkout renders, polls, and settles itself. The compiled `styles.css`
|
|
525
|
+
sheets (`@openreceive/react`, `@openreceive/elements`) are self-contained and
|
|
526
|
+
scoped: every rule applies only inside what OpenReceive renders, so the sheet
|
|
527
|
+
is safe next to any CSS framework (Tailwind, Mantine, your own reset) in any
|
|
528
|
+
import order. No `transpilePackages` entry is needed; the packages ship plain
|
|
529
|
+
ESM.
|
|
530
|
+
|
|
531
|
+
`<Checkout>` is complete as rendered: it already shows the `description` from
|
|
532
|
+
`amountFor` and the collapsed transaction-details panel. Do not build a custom
|
|
533
|
+
UI to satisfy those rules — they only become your job if you replace the
|
|
534
|
+
drop-in ([Checkout UX](https://openreceive.org/guides/checkout-ux.md)).
|
|
535
|
+
|
|
536
|
+
Match the host page's theme: by default the checkout follows the payer's
|
|
537
|
+
stored choice, then the system scheme. If this page is always one theme, lock
|
|
538
|
+
it — `<Checkout theme="dark" … />` (`theme` attribute on the custom element) —
|
|
539
|
+
so a white card never lands on a dark page. The checkout is styled by CSS
|
|
540
|
+
variables under `data-theme`; [Frontend checkout](https://openreceive.org/guides/frontend-checkout.md) has
|
|
541
|
+
the knobs.
|
|
542
|
+
|
|
543
|
+
Everything the checkout draws ships inside the JavaScript: the payment-method
|
|
544
|
+
icons, the wallet logos and the pay tutorials. There is no image file to copy
|
|
545
|
+
or serve and no asset option to set. Deploy your normal JavaScript and CSS
|
|
546
|
+
build output, including any generated JavaScript chunks. Bundlers with code
|
|
547
|
+
splitting can defer tutorial screenshots until first open; single-file builds
|
|
548
|
+
(including the standalone checkout) include them upfront. If your
|
|
549
|
+
Content-Security-Policy has a strict `img-src`, allow `data:`
|
|
550
|
+
([Provider registry](https://openreceive.org/guides/provider-registry.md#assets)).
|
|
551
|
+
|
|
552
|
+
That is the whole loop: your server owns the price and the order, the payer gets
|
|
553
|
+
an invoice, and `onPaid` runs once inside the settlement transaction.
|
|
554
|
+
|
|
555
|
+
A runnable illustration of this boundary — not a template to copy models from —
|
|
556
|
+
is Buy a Button
|
|
557
|
+
(`examples/buttons/server/nextjs-fullstack`).
|
|
558
|
+
It has products, visitors, and orders, with the three hooks as the only bridge,
|
|
559
|
+
its shop routes as three-line App Router wrappers, and `/checkout/[reference]`
|
|
560
|
+
as the resumable page. Map that shape onto the models in THIS app.
|
|
561
|
+
|
|
562
|
+
### 6. Verify
|
|
563
|
+
|
|
564
|
+
```sh
|
|
565
|
+
npx openreceive doctor
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
`openreceive doctor` checks Node, `NWC_URI`, and swap-provider configuration,
|
|
569
|
+
and probes the wallet relay to confirm the code is receive-only. Add
|
|
570
|
+
`--db <file-or-url>` to confirm the migration ran, and
|
|
571
|
+
`--url http://localhost:3000` to confirm the routes are mounted; every failing
|
|
572
|
+
line states its own fix.
|
|
573
|
+
→ [openreceive doctor](https://openreceive.org/guides/api-reference.md#openreceive-doctor)
|
|
574
|
+
|
|
575
|
+
Then open the checkout in a browser, confirm the payment-method icons and
|
|
576
|
+
wallet logos render, and open a wallet's pay tutorial to check its screenshots.
|
|
577
|
+
If an image is missing, inspect the console for CSP violations and the Network
|
|
578
|
+
panel for failed JavaScript chunks. Allow `data:` in `img-src` and deploy the
|
|
579
|
+
complete build output. Do not add image routes, copy package source images, or
|
|
580
|
+
use registry `icon_path` / tutorial `path` keys as browser URLs.
|