openreceive 0.4.26 → 0.4.28
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 +14 -0
- data/lib/openreceive/version.rb +1 -1
- data/skills/integrate-openreceive/SKILL.md +37 -5
- data/skills/integrate-openreceive/references/astro.md +653 -0
- data/skills/integrate-openreceive/references/bolt.md +580 -0
- data/skills/integrate-openreceive/references/btcpay.md +1 -1
- data/skills/integrate-openreceive/references/django.md +23 -2
- data/skills/integrate-openreceive/references/edd.md +407 -0
- data/skills/integrate-openreceive/references/fastapi.md +23 -2
- data/skills/integrate-openreceive/references/fastify.md +31 -6
- data/skills/integrate-openreceive/references/flask.md +23 -2
- data/skills/integrate-openreceive/references/fossbilling.md +433 -0
- data/skills/integrate-openreceive/references/laravel.md +1 -1
- data/skills/integrate-openreceive/references/lovable.md +6 -2
- data/skills/integrate-openreceive/references/medusa.md +468 -0
- data/skills/integrate-openreceive/references/mobile.md +492 -0
- data/skills/integrate-openreceive/references/nestjs.md +41 -16
- data/skills/integrate-openreceive/references/next.md +34 -7
- data/skills/integrate-openreceive/references/node.md +34 -7
- data/skills/integrate-openreceive/references/nuxt.md +606 -0
- data/skills/integrate-openreceive/references/paymenter.md +369 -0
- data/skills/integrate-openreceive/references/php.md +7 -4
- data/skills/integrate-openreceive/references/rails.md +14 -9
- data/skills/integrate-openreceive/references/react-router.md +617 -0
- data/skills/integrate-openreceive/references/sveltekit.md +650 -0
- data/skills/integrate-openreceive/references/tanstack-start.md +734 -0
- data/skills/integrate-openreceive/references/woocommerce.md +8 -6
- metadata +12 -1
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
This is the full file; follow it from Step 0.
|
|
2
|
+
|
|
3
|
+
# OpenReceive agent directions (SvelteKit)
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
# READ THIS FIRST: this file is 35 KB and a summary drops required steps. Download it whole:
|
|
7
|
+
curl -fsSL https://openreceive.org/agent-directions/sveltekit/full.md
|
|
8
|
+
# Skip the download only if you already have all of it: pasted, read from disk or fetched raw.
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Step 0 in brief** (Step 0 below has the details):
|
|
12
|
+
|
|
13
|
+
- Before installing or editing anything, ask the user for a receive-only NWC code. One question per message.
|
|
14
|
+
- Next ask for the LSC code. If the user wants stablecoins, USDT, USDC, ETH, SOL or crypto, do not ask yes/no first.
|
|
15
|
+
- The user only pastes codes into this chat. Never ask them to edit .env or run a command to save one.
|
|
16
|
+
- Write each code into .env yourself, as Step 0 says. Never echo it or put it in a shell command.
|
|
17
|
+
- Do not suggest rotating or revoking a code because it was pasted here.
|
|
18
|
+
- Start the quickstart only once the NWC code is saved, and the LSC code is saved or the user said "Bitcoin only".
|
|
19
|
+
|
|
20
|
+
These directions describe OpenReceive 0.4.28.
|
|
21
|
+
|
|
22
|
+
Add OpenReceive to a SvelteKit application — the app you are already working
|
|
23
|
+
in, on `@sveltejs/adapter-node`. OpenReceive ships no SvelteKit package: the
|
|
24
|
+
integration is one endpoint that hands each request to `@openreceive/http`.
|
|
25
|
+
You do not need a copy of the OpenReceive source: the packages are on npm, and
|
|
26
|
+
the SvelteKit recipe is appended to this file in full as the quickstart, so
|
|
27
|
+
you can do the whole integration without fetching anything. Prefer the
|
|
28
|
+
published packages and the handler they export — do not reimplement wallet
|
|
29
|
+
RPC, settlement, or pricing.
|
|
30
|
+
|
|
31
|
+
Do not clone the OpenReceive repository into this app, and do not copy a demo's
|
|
32
|
+
models (`ShopOrder`, a signed-cookie visitor, an in-memory catalog) over tables
|
|
33
|
+
that already exist. Find this application's order, product, and user models —
|
|
34
|
+
whatever they are actually named — and map the three hooks onto those.
|
|
35
|
+
|
|
36
|
+
Keep this application's frontend framework, authentication and database. Pick
|
|
37
|
+
the UI package that matches the frontend that is already here
|
|
38
|
+
(`@openreceive/react`, `/vue`, `/svelte`, `/angular`, or `/elements` for
|
|
39
|
+
plain HTML) — do not add React to a Vue app. Reuse the app's existing
|
|
40
|
+
session/auth in `authorize` and its existing database handle in `storage.db`.
|
|
41
|
+
|
|
42
|
+
## What OpenReceive is
|
|
43
|
+
|
|
44
|
+
A payment library that runs inside YOUR server. It mounts as ONE catch-all
|
|
45
|
+
endpoint (`src/routes/openreceive/[...path]/+server.ts`, on Node with
|
|
46
|
+
adapter-node) in the application you are editing, issues Lightning
|
|
47
|
+
invoices against a wallet the merchant already controls, and calls back into
|
|
48
|
+
your code when one settles. There is no OpenReceive account and no API key, and
|
|
49
|
+
OpenReceive never holds the funds — the sats land in the wallet the merchant
|
|
50
|
+
connected.
|
|
51
|
+
|
|
52
|
+
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
|
|
53
|
+
a string from the merchant's wallet that can create invoices and read their
|
|
54
|
+
status, and cannot spend. A swap provider (an "LSC" code) optionally lets the
|
|
55
|
+
payer send USDT, USDC, ETH or SOL instead, converted into that same
|
|
56
|
+
Lightning payment. Step 0 collects those credentials from the user; you write three hooks — `authorize`, `amountFor`,
|
|
57
|
+
`onPaid`;
|
|
58
|
+
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
|
|
59
|
+
owns orders, users, prices, or fulfillment.
|
|
60
|
+
|
|
61
|
+
## Step 0 — ask for the two codes, one question at a time
|
|
62
|
+
|
|
63
|
+
Before anything else, check one file: the project's `.env`, if it exists.
|
|
64
|
+
`grep -E '^(NWC_URI|LSC_URI_PRIMARY)=.' .env | cut -d= -f1` names the codes
|
|
65
|
+
it sets without printing them. Check with that, here and after each write,
|
|
66
|
+
not by reading the file, and never print the values. A code that is already
|
|
67
|
+
set is not asked for again; if both are set, skip to step 5 (making the
|
|
68
|
+
server load the file), then the quickstart.
|
|
69
|
+
|
|
70
|
+
On a hosted builder (v0, Vercel, Replit, Lovable), the user may say instead
|
|
71
|
+
that both codes are already set as the project's environment variables or
|
|
72
|
+
secrets. Believe them: do not ask for the codes, do not write `.env`, and
|
|
73
|
+
do not read or print the variables. Skip step 5's file loading: the platform already puts them in `process.env`. Do not add them to a compose file or any other deploy config
|
|
74
|
+
either: they already reach the app, and a line such as `NWC_URI: ${NWC_URI}`
|
|
75
|
+
replaces them with an empty value.
|
|
76
|
+
|
|
77
|
+
Otherwise your next action is a question to the user. Do not install packages,
|
|
78
|
+
edit the app, write `.env.example`, or search anywhere else before asking
|
|
79
|
+
it. Do not read deploy config (compose files, Vercel or platform secrets), Docker containers, or process environments, and never run
|
|
80
|
+
`printenv`. A new shop has neither code yet: hunting for one burns the turn and
|
|
81
|
+
can leak other secrets into the transcript.
|
|
82
|
+
|
|
83
|
+
Two server-only URIs are needed before the quickstart:
|
|
84
|
+
|
|
85
|
+
- `NWC_URI` — a receive-only Nostr Wallet Connect code,
|
|
86
|
+
`nostr+walletconnect://…`. Required for Bitcoin.
|
|
87
|
+
- `LSC_URI_PRIMARY` — a Lightning Swap Connect URI,
|
|
88
|
+
`lightning+swapconnect://…`. Required for USDT, USDC, ETH and SOL. Skip it
|
|
89
|
+
only when the user says they want Bitcoin alone.
|
|
90
|
+
|
|
91
|
+
The user never edits an environment file. They paste each code into the chat;
|
|
92
|
+
you store it. Ask one question per message.
|
|
93
|
+
|
|
94
|
+
1. **First message — the NWC code, and nothing else.** Ask for it and walk them
|
|
95
|
+
through getting it:
|
|
96
|
+
|
|
97
|
+
> To receive payments I need a receive-only wallet code. In Rizful: open
|
|
98
|
+
> the menu, tap NWC, choose Receive-only NWC code, and tap Copy
|
|
99
|
+
> (https://openreceive.org/get_a_nwc_code_to_receive_payments). If you would
|
|
100
|
+
> rather run your own wallet, Alby Hub works too: Connections → Add
|
|
101
|
+
> Connection → Read Only. Paste the code here and I will store it.
|
|
102
|
+
|
|
103
|
+
Do not mention `.env`, exports, or "tell me when it's set".
|
|
104
|
+
2. **When they paste it.** If it does not start with `nostr+walletconnect://`,
|
|
105
|
+
ask them to copy the receive-only code again. Otherwise write
|
|
106
|
+
`NWC_URI=<paste>` into the project's `.env` with your file-editing
|
|
107
|
+
tool, creating the file if needed. Never write it with a shell command
|
|
108
|
+
(`echo`, `printf`, a heredoc, `python -c`): the command line shows the
|
|
109
|
+
code. Make sure `.gitignore` covers `.env` (and `.dockerignore`, if
|
|
110
|
+
the app has one). Never echo the value, commit it, or put it in client
|
|
111
|
+
code. Reply only that it is saved, then ask the next question.
|
|
112
|
+
3. **Second message — swaps.** If the user asked for stablecoins, USDT, USDC,
|
|
113
|
+
ETH, SOL, altcoins or "crypto" (as in "Bitcoin and stablecoin payments"),
|
|
114
|
+
this message IS the walkthrough below: send it as it is, and do not ask yes
|
|
115
|
+
or no first. Otherwise ask whether payers should also be able to pay with
|
|
116
|
+
USDT, USDC, ETH or SOL, then give the walkthrough. The walkthrough:
|
|
117
|
+
|
|
118
|
+
> Go to https://lightning-swap.com, sign in for API keys, create a key, and
|
|
119
|
+
> copy the whole URI (https://openreceive.org/set_up_swap_provider). Paste
|
|
120
|
+
> it here and I will store it — or say "Bitcoin only" and I will continue
|
|
121
|
+
> without it.
|
|
122
|
+
|
|
123
|
+
Mention FixedFloat only if they already use it.
|
|
124
|
+
4. **When they paste it.** If it does not start with
|
|
125
|
+
`lightning+swapconnect://`, ask them to copy it again. Otherwise add
|
|
126
|
+
`LSC_URI_PRIMARY=<paste>` to the same `.env` with the file-editing
|
|
127
|
+
tool, never a shell command. Swaps are now on, so build the refund route
|
|
128
|
+
back (the swap non-negotiable below) as part of this integration. If they
|
|
129
|
+
chose Bitcoin only, leave `LSC_URI_PRIMARY` unset and skip that route.
|
|
130
|
+
5. **Make the server load the file — yourself.** OpenReceive reads
|
|
131
|
+
`process.env`, and a `.env` file on disk is not in it. `vite dev` reads
|
|
132
|
+
`.env` only for the variables the app declares, so start it with
|
|
133
|
+
`node --env-file=.env node_modules/.bin/vite dev`, and start the built
|
|
134
|
+
server with `node --env-file=.env build`. When the app is started with
|
|
135
|
+
Docker Compose, give the service `env_file: .env`. Never make a code
|
|
136
|
+
public (`public: true` in `src/env.ts`, or a `PUBLIC_` prefix): that sends
|
|
137
|
+
it to the browser. Restart the server after writing the file.
|
|
138
|
+
Under Compose, recreate the container with `docker compose up -d`:
|
|
139
|
+
`docker compose restart` keeps its old environment.
|
|
140
|
+
|
|
141
|
+
Do not invent placeholder URIs. Start the quickstart only once `NWC_URI` is
|
|
142
|
+
saved and `LSC_URI_PRIMARY` is saved or explicitly declined. The first boot
|
|
143
|
+
runs the receive-only preflight: if it reports spend methods such as
|
|
144
|
+
`pay_invoice`, remove `NWC_URI` from `.env` and ask for a receive-only code
|
|
145
|
+
again. Never set the spend-capable override to get past it.
|
|
146
|
+
|
|
147
|
+
### Upgrading an existing install
|
|
148
|
+
|
|
149
|
+
This is not the opening move of a new integration. When OpenReceive is ALREADY
|
|
150
|
+
installed here, `npx openreceive doctor` reports every credential as set/unset
|
|
151
|
+
(never the value) and probes the wallet. Check the installed
|
|
152
|
+
`@openreceive/node` and `@openreceive/browser` against the release named at the
|
|
153
|
+
top of this file: the headless display models below do not exist in older
|
|
154
|
+
versions, and the first tile click throws with nothing saying why. Upgrade
|
|
155
|
+
first.
|
|
156
|
+
|
|
157
|
+
Only then start the quickstart.
|
|
158
|
+
|
|
159
|
+
## After the quickstart — hand over, then stop
|
|
160
|
+
|
|
161
|
+
The quickstart is done when
|
|
162
|
+
`npx openreceive doctor --db <file-or-url> --url <app url>` is clean and this
|
|
163
|
+
app serves the checkout page for one of its orders. Doctor names any failed
|
|
164
|
+
check and exits nonzero; fix it before going on.
|
|
165
|
+
Doctor reads `.env` itself. Never source it into a shell: the `&` in a
|
|
166
|
+
code splits the value and prints the pieces.
|
|
167
|
+
|
|
168
|
+
Give the user that checkout link. The browser check of that page
|
|
169
|
+
(payment-method icons, wallet logos, a pay tutorial) is theirs: name it in one
|
|
170
|
+
line of your closing message, and do not run it yourself.
|
|
171
|
+
|
|
172
|
+
You cannot pay the invoice: the code is receive-only. Do not pay, settle or
|
|
173
|
+
mark an order paid, and do not look for a way to (a wallet control port, a
|
|
174
|
+
test endpoint, another wallet). If the user wants a real settlement test, they
|
|
175
|
+
pay on that link from their own wallet, and `onPaid` marks the order paid.
|
|
176
|
+
|
|
177
|
+
Setup ends here. Your last message starts "Setup is finished" and has at most
|
|
178
|
+
five short lines (about 80 characters each): the link, the methods it offers,
|
|
179
|
+
and one line of what to look at. Send nothing after it. Do not list what
|
|
180
|
+
changed, copy out the quickstart's browser checklist, offer more work, or end
|
|
181
|
+
the message on a question.
|
|
182
|
+
|
|
183
|
+
- The link is to a real unpaid order. Keep that order; do not delete it.
|
|
184
|
+
- When this app's orders belong to a session or cookie, the user's browser
|
|
185
|
+
cannot open the order you made. Then the link is the shop's own page:
|
|
186
|
+
"Open <shop url> and click Buy: it opens the checkout." Do not explain order
|
|
187
|
+
ownership or mention your test order.
|
|
188
|
+
- Name the methods it offers: Bitcoin, plus USDT, USDC, ETH and SOL when
|
|
189
|
+
`LSC_URI_PRIMARY` is saved. Do not mention minimums, and never say a coin
|
|
190
|
+
will not work or will not be offered.
|
|
191
|
+
|
|
192
|
+
## Non-negotiables
|
|
193
|
+
|
|
194
|
+
The quickstart below has the code. These are the rules it cannot state for
|
|
195
|
+
itself, and they hold for every integration.
|
|
196
|
+
|
|
197
|
+
- OpenReceive never owns orders, users, prices, or fulfillment. The section
|
|
198
|
+
below is how those tables sit next to the library — not a second order model,
|
|
199
|
+
and not a Prisma/Drizzle relation to `openreceive_payments`.
|
|
200
|
+
- Keep `NWC_URI` / `LSC_URI_*` server-only. Never put them in browser code,
|
|
201
|
+
logs, or assets.
|
|
202
|
+
- Do not suggest rotating, revoking or replacing a code because it was pasted
|
|
203
|
+
into this chat; that is the supported path.
|
|
204
|
+
- Work only in this application. Never read or run anything from another
|
|
205
|
+
project on this machine (its `node_modules`, tools or source), for any
|
|
206
|
+
reason. A browser and Playwright are not part of setup.
|
|
207
|
+
- Restart only this app's server, on the port it already uses: stop the
|
|
208
|
+
process you started, or the one listening on that port, by its pid (or
|
|
209
|
+
restart its Compose service). Never `pkill` or `killall` by name: that
|
|
210
|
+
stops other people's servers too. Start it the way this project already
|
|
211
|
+
does (its README, Procfile, compose file or package script), on its own
|
|
212
|
+
port, not a port you pick.
|
|
213
|
+
- Run commands where the app runs. When a compose file builds it, run its
|
|
214
|
+
package manager, generators, migrations and doctor inside that service
|
|
215
|
+
(`docker compose exec` or `run`), and rebuild the image after adding a
|
|
216
|
+
package. This machine's language version and database path are not the
|
|
217
|
+
app's.
|
|
218
|
+
- To check that the running app sees the codes, run doctor: it reports each
|
|
219
|
+
one as present or missing and never prints a value. Never print the
|
|
220
|
+
environment (`printenv`, `env`, `docker compose config`), even filtered to
|
|
221
|
+
names.
|
|
222
|
+
- The host owns the price. `amountFor` reads it from your own data; reject
|
|
223
|
+
payer-supplied amounts.
|
|
224
|
+
- `authorize` runs on every request, and the `resource` it receives is a CLAIM
|
|
225
|
+
the payer made, not proof. Read a framework session; never trust a body field.
|
|
226
|
+
- `onPaid` must be idempotent. Its database fulfillment commits once per `reference` — your order id, one
|
|
227
|
+
per thing you fulfill, created before checkout, kept across retries, never
|
|
228
|
+
reused. A fresh id per page load lets one order be paid twice.
|
|
229
|
+
- Receive-only NWC is required; a spend-capable code fails closed at boot unless
|
|
230
|
+
explicitly overridden.
|
|
231
|
+
- There is NO merchant-initiated refund of a settled Lightning payment, because
|
|
232
|
+
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
|
|
233
|
+
never converted — are the only refund OpenReceive performs, and only from the
|
|
234
|
+
`refund_required` provider state. Do not build, promise, or imply a Lightning
|
|
235
|
+
refund path.
|
|
236
|
+
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
|
|
237
|
+
late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
|
|
238
|
+
after leaving your page to fetch an address from another wallet. Three things
|
|
239
|
+
must exist or that money is unreachable through your UI: a per-order URL your
|
|
240
|
+
server serves (`/checkout/:reference` — `syncUrl` on `<Checkout>`, `sync-url`
|
|
241
|
+
or `resumable` on `<openreceive-checkout>`), your own order-summary route to
|
|
242
|
+
restore the order from, and the ATTEMPT.
|
|
243
|
+
`/checkouts/prepare` returns no attempts, so a checkout rebuilt from the
|
|
244
|
+
reference alone opens on the method grid. Re-picking the same coin
|
|
245
|
+
(`POST /swaps`) re-serves the committed attempt — but only while it is live,
|
|
246
|
+
and the shadow invoice behind a swap lasts about half an hour, after which the
|
|
247
|
+
same click mints a NEW deposit address and the refund is off-screen. Keep the
|
|
248
|
+
`payment_hash` and reopen the attempt with `POST /swaps/status`, which has no
|
|
249
|
+
such window. On the drop-ins: `resumePaymentHash`, fed from `onState`, on
|
|
250
|
+
`<Checkout>`; the `resume-payment-hash` attribute, fed from the
|
|
251
|
+
`openreceive-state` event (`event.detail.state.payment_hash`), on
|
|
252
|
+
`<openreceive-checkout>`.
|
|
253
|
+
That visit can come hours later, and `authorize` must still know the payer:
|
|
254
|
+
a session that expired in between answers 403 and strands the deposit. On a
|
|
255
|
+
guest shop, tie the order to a long-lived signed cookie, or put a secret
|
|
256
|
+
per-order token in its URL that your page trades for that cookie.
|
|
257
|
+
https://openreceive.org/guides/swap-refunds.md
|
|
258
|
+
- Show the payer WHAT THEY ARE BUYING. Return an optional `description` beside
|
|
259
|
+
the price from `amountFor` and both drop-ins render it above the amount.
|
|
260
|
+
Without it the checkout is a QR and "$1.00" with no sign of what the dollar
|
|
261
|
+
is for.
|
|
262
|
+
- Show the payer the transaction record: `createTransactionDetails(...)` rows,
|
|
263
|
+
collapsed behind a caret, on the live checkout AND on the receipt. A payment
|
|
264
|
+
hash and a deposit txid are the only evidence a payer has that they paid you.
|
|
265
|
+
`<Checkout>` / `<openreceive-checkout>` already render this panel and the
|
|
266
|
+
`description` — these two rules cost you code only on a custom UI or your own
|
|
267
|
+
receipt page, never a reason to replace the drop-in. (It returns no rows
|
|
268
|
+
while the rail is `checkout_lock` — before the payer has chosen anything
|
|
269
|
+
there is no transaction — so render the caret only when the rows are
|
|
270
|
+
non-empty.)
|
|
271
|
+
- The handler lives in a server-only module under `src/lib/server/`, and the
|
|
272
|
+
stack is built on the first request, never at import: `vite build` imports
|
|
273
|
+
server modules to analyse them, and a stack connects to the wallet as it is
|
|
274
|
+
built. The endpoint exports `GET` and `POST`, runs on Node through
|
|
275
|
+
adapter-node — never an edge runtime — and is never prerendered. If you
|
|
276
|
+
declare `NWC_URI` in `src/env.ts`, give it the recipe's schema, which
|
|
277
|
+
returns `""` while `building`: the build checks every declared variable
|
|
278
|
+
without the codes, and a missing one fails it.
|
|
279
|
+
- Install the OpenReceive packages and `pg` as `dependencies`, never
|
|
280
|
+
`devDependencies`: adapter-node bundles devDependencies into the server
|
|
281
|
+
build and leaves dependencies to `node_modules`.
|
|
282
|
+
- Pass the `RequestEvent` as `native`, so `authorize` reads the session the
|
|
283
|
+
app already has, and give `rateLimiting` `ip: ({ native }) =>
|
|
284
|
+
native.getClientAddress()`. Behind your own reverse proxy, set adapter-node's
|
|
285
|
+
`ADDRESS_HEADER` and `XFF_DEPTH`, or every payer shares the proxy's address.
|
|
286
|
+
- `<Checkout>` comes from `@openreceive/svelte/checkout.svelte`, with
|
|
287
|
+
`@openreceive/svelte/styles.css`, on the order's own page: its resumable
|
|
288
|
+
URL. SvelteKit's CSRF check refuses cross-site form posts; the checkout
|
|
289
|
+
posts JSON, which it lets through, so leave the check on.
|
|
290
|
+
- HTTP JSON is snake_case; TypeScript APIs are camelCase.
|
|
291
|
+
- Money is integers or decimal strings — never binary floats.
|
|
292
|
+
|
|
293
|
+
## Your tables, not ours
|
|
294
|
+
|
|
295
|
+
`npx openreceive scaffold payments` emits `openreceive_payments` and
|
|
296
|
+
`openreceive_meta` for THIS application's database. That is the whole
|
|
297
|
+
persistence OpenReceive needs. It does not replace your orders, users, or
|
|
298
|
+
products, and you do not join them.
|
|
299
|
+
|
|
300
|
+
- **Find this app's models first.** They may be named `Order`, `Invoice`,
|
|
301
|
+
`Booking`, `Product`, `Variant`, `User`, `Account` — anything. Wire the hooks
|
|
302
|
+
to those. Do not generate a parallel `ShopOrder` / `ShopProduct` / `ShopUser`
|
|
303
|
+
stack.
|
|
304
|
+
- **The payable row's id is the `reference`.** Create it before checkout, keep
|
|
305
|
+
it across retries, never reuse it. Pass that id to `<Checkout>` /
|
|
306
|
+
`<openreceive-checkout>`. A fresh id per page load lets one order be paid
|
|
307
|
+
twice.
|
|
308
|
+
- **Products (or the catalog) are the price authority.** Order creation reads
|
|
309
|
+
live prices into the order (snapshot line items if this app has them).
|
|
310
|
+
`amountFor` reads only that order — never a payer-supplied amount, never a
|
|
311
|
+
live catalog lookup that could re-price a cart already placed. Return
|
|
312
|
+
`{ currency, value }` as a decimal STRING, plus a `description` of what they
|
|
313
|
+
are buying.
|
|
314
|
+
- **Users own the order; OpenReceive never sees them.** `authorize` uses the
|
|
315
|
+
same ownership check this app already uses on the order show / pay page —
|
|
316
|
+
`event.locals.user`, `event.cookies`, whatever it is. `native` is the request's
|
|
317
|
+
`RequestEvent`.
|
|
318
|
+
`resource.reference` is a claim the payer sent, not proof.
|
|
319
|
+
- **The order is unpaid or paid.** Do not copy `pending` / `expired` / `failed`
|
|
320
|
+
/ `attention` onto it. Those are attempt statuses on `openreceive_payments`. An
|
|
321
|
+
expired invoice does not cancel the order; a later checkout may mint another
|
|
322
|
+
attempt. The library refuses a new checkout under a reference that already
|
|
323
|
+
settled (409).
|
|
324
|
+
- **A paid order's page says it is paid and shows no checkout.** Nothing is left
|
|
325
|
+
to pay, and a checkout cannot start a new payment for a paid order. Mount the
|
|
326
|
+
checkout only while the order is unpaid. When it settles on the page, update
|
|
327
|
+
the page's status line from `onSettled` on `<Checkout>`: `onPaid` has already
|
|
328
|
+
marked the order paid. Keep that checkout mounted, because it is the payer's
|
|
329
|
+
receipt.
|
|
330
|
+
- **Pass this app's `db` handle.** Do not add a Prisma/Drizzle relation from
|
|
331
|
+
Order to `openreceive_payments`, and do not implement `PaymentRepository`
|
|
332
|
+
unless no supported handle can reach this database. `reference` is not unique
|
|
333
|
+
(many attempts per order). Fulfillment is a guarded transition on YOUR order
|
|
334
|
+
row inside `onPaid` — `UPDATE … WHERE state = 'awaiting_payment'` (or this
|
|
335
|
+
app's equivalent) through the `query` the library hands you, on that same
|
|
336
|
+
settlement transaction, not a second connection from your ORM. Database writes
|
|
337
|
+
only in the hook; emails, jobs, and pushes after commit. Placeholder style is
|
|
338
|
+
the dialect you declared: `?` on sqlite and mysql, `$1` on postgres.
|
|
339
|
+
|
|
340
|
+
## If you build your own checkout UI
|
|
341
|
+
|
|
342
|
+
The drop-ins (`<Checkout>`, `<openreceive-checkout>`) already obey all of this.
|
|
343
|
+
This list is the short form of https://openreceive.org/guides/checkout-ux.md, for a
|
|
344
|
+
UI built on `@openreceive/browser/headless`. Read that before writing
|
|
345
|
+
components.
|
|
346
|
+
|
|
347
|
+
- Check Bitcoin → Switch payment method → Bitcoin: the grid must hide the
|
|
348
|
+
Lightning invoice, then restore the same bolt11 without another mint.
|
|
349
|
+
- `createCheckoutController` is the engine. Do not hand-roll a poll loop.
|
|
350
|
+
- `createCheckoutStatusModel` for the status line. Do not draw a
|
|
351
|
+
Cart → Pay → Done stepper. Read the model's `phase`, not the snapshot's.
|
|
352
|
+
- `resolveWizardSelection` decides whether to ask "which network?". A
|
|
353
|
+
one-network asset starts the swap from the tile. Key `selectedAssetByGroup`
|
|
354
|
+
by group (`USDT`), valued by `pay_in_asset` (`USDT_TRON`).
|
|
355
|
+
- `createMethodGridDisplay` for tiles, including `limitMessage` so an
|
|
356
|
+
unavailable method says the minimum in the payer's currency.
|
|
357
|
+
- `createSwapDisplayModel` → `display.copyRows` for deposits: address, memo,
|
|
358
|
+
and the bare amount each get a copy row. Render `swap.networkWarning*` as
|
|
359
|
+
the model gives it.
|
|
360
|
+
- `swap.deposit_amount` is the only amount a payer is told to send. Never put
|
|
361
|
+
a fiat valuation of it (`swap.fee.pay_in_fiat`) next to a stablecoin amount:
|
|
362
|
+
"$50.03" under "50.05 USDC" reads as a typo, and the payer asks which one to
|
|
363
|
+
send. The one fiat figure on a USDT/USDC deposit panel is the cart total
|
|
364
|
+
(`payout_fiat`); express "you send" and the fee in the token. Use
|
|
365
|
+
`createSwapFeeBreakdown(fee, swap)` — with the swap, not the fee alone — and
|
|
366
|
+
it applies this for you; SOL and ETH keep a fiat breakdown.
|
|
367
|
+
- `createCheckoutSession` owns mint and swap start. To start swaps, pass its
|
|
368
|
+
`swap` option (`selection`, `prefix`, `fetch`) together. Without it
|
|
369
|
+
`startSwap` reports through `onError`.
|
|
370
|
+
- `createQrSvg` is async. Use `createQrSvgController` so you do not render
|
|
371
|
+
`[object Promise]`.
|
|
372
|
+
- `checkoutLabels` for every payer-facing string. Only write copy it lacks.
|
|
373
|
+
- `stageSwapRefund` then `confirmSwapRefund` — only the second submits.
|
|
374
|
+
Validate with `getSwapRefundFormError`. Treat `409` as a normal outcome.
|
|
375
|
+
- Pass `{ resumable: true }` to `createSwapDisplayModel` when the payer has
|
|
376
|
+
a URL they can come back to, and render `display.refundReturnLabel`.
|
|
377
|
+
Resume helpers (`createGuestCheckoutResume`, `createGuestOrderFetcher`)
|
|
378
|
+
are on `@openreceive/browser`, not `/headless`.
|
|
379
|
+
- A refund replaces the deposit panel. On `refund_required` also drop
|
|
380
|
+
"switch payment method".
|
|
381
|
+
- No "Open wallet" button on desktop.
|
|
382
|
+
- Wallet suggestions: `getPaymentWizardRoutes()` +
|
|
383
|
+
`createWizardRouteDisplays`. Lightning only. Logos are data URIs; tutorial
|
|
384
|
+
images load from a JavaScript chunk. For a custom headless UI, load it when
|
|
385
|
+
a tutorial opens and look up the returned table by the tutorial's `path`:
|
|
386
|
+
|
|
387
|
+
```js
|
|
388
|
+
import { loadPayTutorialImages } from "@openreceive/browser/headless";
|
|
389
|
+
|
|
390
|
+
const images = await loadPayTutorialImages();
|
|
391
|
+
const src = images[tutorial.path]; // data URI for the selected tutorial
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Render `src` as the image source and update your UI after loading. Existing
|
|
395
|
+
display objects do not update: their `tutorial.image` stays `undefined` if
|
|
396
|
+
created before loading. Alternatively, await the loader, recreate the displays
|
|
397
|
+
with `createWizardRouteDisplays`, and render the new `tutorial.image`.
|
|
398
|
+
Show the caption while loading or if loading fails; never use an empty image
|
|
399
|
+
source. Deploy all JavaScript chunks and allow `data:` in CSP `img-src`.
|
|
400
|
+
For missing images, check CSP errors, failed chunks, and stale displays.
|
|
401
|
+
Registry paths are lookup keys; there is no asset option or image route.
|
|
402
|
+
|
|
403
|
+
## More documentation
|
|
404
|
+
|
|
405
|
+
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
|
|
406
|
+
enough; drop the `.md` for the same page a person would read.
|
|
407
|
+
|
|
408
|
+
- https://openreceive.org/guides/authorization.md — before you write `authorize`
|
|
409
|
+
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
|
|
410
|
+
- https://openreceive.org/guides/storage.md — the payment tables and the attempt state machine
|
|
411
|
+
- https://openreceive.org/guides/supabase.md — when the database is Supabase: its pooler, its certificate, and locking the tables away from its Data API
|
|
412
|
+
- https://openreceive.org/guides/serverless.md — when the app runs serverless (Netlify, AWS Lambda, Cloud Run, Google AI Studio): pooled database URLs, timeouts, and why it needs no cron or worker
|
|
413
|
+
- https://openreceive.org/guides/node-orms.md — recipes for Prisma, Drizzle, Knex, TypeORM, Sequelize
|
|
414
|
+
- https://openreceive.org/guides/frontend-checkout.md — the drop-in's props, attributes and slots
|
|
415
|
+
- https://openreceive.org/guides/mobile-apps.md — when a mobile app takes the payment: the checkout page it opens, and what the App Store and Google Play allow
|
|
416
|
+
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
|
|
417
|
+
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
|
|
418
|
+
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay
|
|
419
|
+
tutorials come from: inside the JavaScript, nothing to serve. This is the page
|
|
420
|
+
that owns the image rule, not the summary in checkout-ux.md
|
|
421
|
+
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
|
|
422
|
+
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
|
|
423
|
+
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
|
|
424
|
+
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
|
|
425
|
+
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
|
|
426
|
+
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
|
|
427
|
+
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
|
|
428
|
+
- https://openreceive.org/guides/api-reference.md — every route, option and error code
|
|
429
|
+
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the shipped adapter's routes with your own
|
|
430
|
+
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
|
|
431
|
+
- https://openreceive.org/guides/quickstart-next.md — each hook explained step by step, on Next.js; they are the same here
|
|
432
|
+
- https://openreceive.org/guides.md — the index, if what you need is not above
|
|
433
|
+
|
|
434
|
+
Questions, or a problem with the library itself:
|
|
435
|
+
https://openreceive.org/contact
|
|
436
|
+
|
|
437
|
+
- https://openreceive.org/guides/payment-safety-upgrade.md — coordinated upgrades and reviewed repair of existing attempts
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
## The quickstart, in full
|
|
442
|
+
|
|
443
|
+
Inlined verbatim so this file needs no network access — follow it once Step 0
|
|
444
|
+
passes. The page it comes from is https://openreceive.org/guides/sveltekit-recipe.
|
|
445
|
+
|
|
446
|
+
## SvelteKit recipe
|
|
447
|
+
|
|
448
|
+
OpenReceive ships no SvelteKit package. A SvelteKit endpoint (`+server.ts`)
|
|
449
|
+
takes a Web-standard `Request` and returns a `Response`, and so does
|
|
450
|
+
`@openreceive/http`, so the integration is the route below. Your server code
|
|
451
|
+
creates each Lightning invoice, the payment goes straight to your wallet, and
|
|
452
|
+
payment attempts are stored in your app's database. There is no OpenReceive
|
|
453
|
+
account and no background worker.
|
|
454
|
+
|
|
455
|
+
Optional swaps let customers pay with **USDT, USDC, SOL, and ETH**. A swap
|
|
456
|
+
provider you configure converts the payment to **BTC over Lightning**, and it
|
|
457
|
+
settles into the same wallet. Available assets and networks depend on the
|
|
458
|
+
provider.
|
|
459
|
+
|
|
460
|
+
This recipe was checked with `@sveltejs/kit` 3.0, `@sveltejs/adapter-node`
|
|
461
|
+
6.0 and `@openreceive/*` 0.4.25. The built app ran on Node in Docker, with
|
|
462
|
+
Postgres and a test wallet on a public NWC relay. In Chromium, the buyer's
|
|
463
|
+
checkout page showed the Lightning invoice. A stranger got 403 from the
|
|
464
|
+
payment routes and 404 from the page, and a request that named its own amount
|
|
465
|
+
got 400. When the wallet marked the invoice paid, the page showed it paid and
|
|
466
|
+
`onPaid` marked the order paid.
|
|
467
|
+
|
|
468
|
+
```sh
|
|
469
|
+
npm install @openreceive/http @openreceive/svelte pg
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Where it runs
|
|
473
|
+
|
|
474
|
+
On Node, with `@sveltejs/adapter-node`. The wallet connection and your
|
|
475
|
+
database driver need Node, so the route cannot run on an edge runtime. The
|
|
476
|
+
Vercel and Netlify adapters deploy Node functions: read
|
|
477
|
+
Bitcoin checkout on serverless hosts first. For
|
|
478
|
+
Cloudflare Workers, the [TanStack Start recipe](https://openreceive.org/guides/tanstack-start-recipe.md#where-it-runs)
|
|
479
|
+
explains what a Worker needs. This recipe was not run on those hosts.
|
|
480
|
+
|
|
481
|
+
### Secrets
|
|
482
|
+
|
|
483
|
+
SvelteKit 3 reads the environment variables you declare in `src/env.ts`. The
|
|
484
|
+
build runs your app to analyse it, without your secrets, so require them only
|
|
485
|
+
when the app starts:
|
|
486
|
+
|
|
487
|
+
```ts
|
|
488
|
+
// src/env.ts
|
|
489
|
+
import { building } from "$app/env";
|
|
490
|
+
import { defineEnvVars } from "@sveltejs/kit/env";
|
|
491
|
+
|
|
492
|
+
const secret = (value: string | undefined): string => {
|
|
493
|
+
if (value) return value;
|
|
494
|
+
if (building) return "";
|
|
495
|
+
throw new Error("is not set");
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
export const variables = defineEnvVars({
|
|
499
|
+
NWC_URI: { schema: secret, description: "Receive-only NWC code from your wallet" },
|
|
500
|
+
DATABASE_URL: { schema: secret, description: "Postgres connection string" },
|
|
501
|
+
});
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
Never mark them `public: true`, which would send them to the browser.
|
|
505
|
+
|
|
506
|
+
### The payment route
|
|
507
|
+
|
|
508
|
+
Keep the handler under `src/lib/server/`. SvelteKit refuses to bundle a
|
|
509
|
+
module from there into browser code:
|
|
510
|
+
|
|
511
|
+
```ts
|
|
512
|
+
// src/lib/server/openreceive.ts
|
|
513
|
+
import { createStack, type Stack } from "@openreceive/http";
|
|
514
|
+
import type { RequestEvent } from "@sveltejs/kit";
|
|
515
|
+
import { NWC_URI } from "$app/env/private";
|
|
516
|
+
import { db } from "./db"; // your existing pg Pool
|
|
517
|
+
import { findOrder } from "./orders"; // your own orders
|
|
518
|
+
|
|
519
|
+
let stack: Stack | undefined;
|
|
520
|
+
|
|
521
|
+
// Built on the first request, not at import: the build imports server
|
|
522
|
+
// modules to analyse them, and a stack connects to the wallet at once.
|
|
523
|
+
function openReceive(): Stack {
|
|
524
|
+
stack ??= createStack({
|
|
525
|
+
wallet: { nwc: NWC_URI },
|
|
526
|
+
storage: {
|
|
527
|
+
db,
|
|
528
|
+
// Runs once, inside the transaction that records the payment.
|
|
529
|
+
onPaid: async ({ reference, paidAt, query }) => {
|
|
530
|
+
await query(
|
|
531
|
+
"UPDATE orders SET state = 'paid', paid_at = $1 WHERE id = $2 AND state = 'awaiting_payment'",
|
|
532
|
+
[paidAt, reference],
|
|
533
|
+
);
|
|
534
|
+
},
|
|
535
|
+
},
|
|
536
|
+
// The price comes from the order row, never from the request.
|
|
537
|
+
amountFor: async (reference) => {
|
|
538
|
+
const order = await findOrder(reference);
|
|
539
|
+
return order?.state === "awaiting_payment"
|
|
540
|
+
? { currency: order.currency, value: order.amount, description: order.title }
|
|
541
|
+
: null;
|
|
542
|
+
},
|
|
543
|
+
// Only the buyer may pay for their order.
|
|
544
|
+
authorize: async ({ native, resource }) => {
|
|
545
|
+
const event = native as RequestEvent;
|
|
546
|
+
const order = resource.reference ? await findOrder(resource.reference) : undefined;
|
|
547
|
+
return Boolean(order && order.visitor === event.cookies.get("visitor"));
|
|
548
|
+
},
|
|
549
|
+
// adapter-node's client address: the socket, or ADDRESS_HEADER behind a proxy.
|
|
550
|
+
rateLimiting: {
|
|
551
|
+
ip: ({ native }) => (native as RequestEvent).getClientAddress(),
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
return stack;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export function handleOpenReceive(event: RequestEvent): Promise<Response> {
|
|
558
|
+
return openReceive().handler(event.request, { native: event });
|
|
559
|
+
}
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
// src/routes/openreceive/[...path]/+server.ts — every route under /openreceive/
|
|
564
|
+
import { handleOpenReceive } from "#lib/server/openreceive.ts";
|
|
565
|
+
import type { RequestHandler } from "./$types";
|
|
566
|
+
|
|
567
|
+
export const GET: RequestHandler = (event) => handleOpenReceive(event);
|
|
568
|
+
export const POST: RequestHandler = (event) => handleOpenReceive(event);
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
`native` is the request's `RequestEvent`, so `authorize` can check the
|
|
572
|
+
session your app already has: its cookies, or the `event.locals.user` your
|
|
573
|
+
`hooks.server.ts` sets. The Next.js quickstart
|
|
574
|
+
explains each hook. They are the same here.
|
|
575
|
+
|
|
576
|
+
One stack serves the whole server process. Behind a reverse proxy, set
|
|
577
|
+
adapter-node's `ADDRESS_HEADER` (and `XFF_DEPTH`), so that
|
|
578
|
+
`getClientAddress()` returns the payer's address and not the proxy's
|
|
579
|
+
(Rate limiting).
|
|
580
|
+
|
|
581
|
+
On SvelteKit 2, import the secret as `env.NWC_URI` from
|
|
582
|
+
`$env/dynamic/private`, and import server modules from `$lib/server/`, with
|
|
583
|
+
no `src/env.ts`. The rest is the same. This recipe was checked on SvelteKit 3
|
|
584
|
+
only.
|
|
585
|
+
|
|
586
|
+
### Your database
|
|
587
|
+
|
|
588
|
+
`db` is a `pg` Pool, `node:sqlite` or `better-sqlite3`, or one of the
|
|
589
|
+
adapters in Node ORM recipes. With Drizzle, pass
|
|
590
|
+
the driver underneath it. `sv add drizzle` sets up Postgres through
|
|
591
|
+
postgres.js or Neon, which OpenReceive does not take directly. Then give
|
|
592
|
+
OpenReceive its own small `pg` Pool on the same `DATABASE_URL`. It is the same
|
|
593
|
+
database, not a second one.
|
|
594
|
+
|
|
595
|
+
Create OpenReceive's two tables with your migrations
|
|
596
|
+
(`npx openreceive scaffold payments --orm drizzle` writes the Drizzle
|
|
597
|
+
schema), or run `paymentsSchemaSql("postgres")` from `@openreceive/http`
|
|
598
|
+
once. It is idempotent. See Payment storage.
|
|
599
|
+
|
|
600
|
+
### The checkout page
|
|
601
|
+
|
|
602
|
+
```ts
|
|
603
|
+
// src/routes/checkout/[reference]/+page.server.ts
|
|
604
|
+
import { error } from "@sveltejs/kit";
|
|
605
|
+
import { findOrder } from "#lib/server/orders.ts";
|
|
606
|
+
import type { PageServerLoad } from "./$types";
|
|
607
|
+
|
|
608
|
+
export const load: PageServerLoad = async ({ params, cookies }) => {
|
|
609
|
+
const order = await findOrder(params.reference);
|
|
610
|
+
if (!order || order.visitor !== cookies.get("visitor")) error(404);
|
|
611
|
+
return { reference: order.id };
|
|
612
|
+
};
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
```svelte
|
|
616
|
+
<!-- src/routes/checkout/[reference]/+page.svelte -->
|
|
617
|
+
<script lang="ts">
|
|
618
|
+
import Checkout from "@openreceive/svelte/checkout.svelte";
|
|
619
|
+
import "@openreceive/svelte/styles.css";
|
|
620
|
+
import type { PageProps } from "./$types";
|
|
621
|
+
|
|
622
|
+
let { data }: PageProps = $props();
|
|
623
|
+
</script>
|
|
624
|
+
|
|
625
|
+
<Checkout reference={data.reference} prefix="/openreceive" />
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
The page renders on the server and starts the checkout in the browser. The
|
|
629
|
+
payer picks Bitcoin and gets the invoice. Keep it at the order's own URL: a
|
|
630
|
+
payer with a swap deposit in flight comes back to it
|
|
631
|
+
(Swap refunds).
|
|
632
|
+
|
|
633
|
+
### Settings
|
|
634
|
+
|
|
635
|
+
- `NWC_URI`: a receive-only code from your wallet
|
|
636
|
+
([get one](https://openreceive.org/get_a_nwc_code_to_receive_payments)).
|
|
637
|
+
- `DATABASE_URL`: your Postgres. On serverless hosts use the pooled URL.
|
|
638
|
+
- Optional: `LSC_URI_PRIMARY` for swaps
|
|
639
|
+
([set one up](https://openreceive.org/set_up_swap_provider)), and
|
|
640
|
+
`LSC_URI_BACKUP`. OpenReceive reads these, and `LOG_LEVEL`, straight from
|
|
641
|
+
`process.env`, not through `src/env.ts`.
|
|
642
|
+
|
|
643
|
+
In production, set them all in the server's environment. In development,
|
|
644
|
+
`vite dev` reads `.env` for the variables in `src/env.ts` but keeps the file
|
|
645
|
+
out of `process.env`. To try swaps locally, start it with
|
|
646
|
+
`node --env-file=.env node_modules/.bin/vite dev`.
|
|
647
|
+
|
|
648
|
+
No worker or cron job is needed. Each request to the payment routes also
|
|
649
|
+
checks the wallet for settled invoices, through a lock in your database. A
|
|
650
|
+
payer who closes the tab is settled on the next request.
|