openreceive 0.4.2 → 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 +24 -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,537 @@
|
|
|
1
|
+
# OpenReceive agent directions (FastAPI)
|
|
2
|
+
|
|
3
|
+
These directions describe OpenReceive 0.4.4.
|
|
4
|
+
|
|
5
|
+
Add OpenReceive to a FastAPI application — the app you are already working in.
|
|
6
|
+
You do not need a copy of the OpenReceive source: the engine is on PyPI
|
|
7
|
+
(`openreceive[fastapi]`), the checkout UI is on npm, and the quickstart is
|
|
8
|
+
appended to this file in full, so you can do the whole integration without
|
|
9
|
+
fetching anything. Prefer the published packages and the routes the router
|
|
10
|
+
serves — do not reimplement wallet RPC, settlement, or 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 or Jinja templates) — do not add React to an HTMX app. Keep
|
|
21
|
+
FastAPI's dependency system: the auth dependency this app already resolves
|
|
22
|
+
its user with is what `authorize` should call on `context.request`, and the
|
|
23
|
+
database OpenReceive gets is a sync SQLAlchemy `Engine` for the SAME
|
|
24
|
+
database this app already uses.
|
|
25
|
+
|
|
26
|
+
## What OpenReceive is
|
|
27
|
+
|
|
28
|
+
A payment library that runs inside YOUR server. It is an `APIRouter`
|
|
29
|
+
(`openreceive_router`, included under `/openreceive`) that serves its HTTP
|
|
30
|
+
routes inside the application you are editing, issues Lightning invoices
|
|
31
|
+
against a wallet the merchant already controls, and calls back into your code
|
|
32
|
+
when one settles. There is no OpenReceive account and no API key, and
|
|
33
|
+
OpenReceive never holds the funds — the sats land in the wallet the merchant
|
|
34
|
+
connected.
|
|
35
|
+
|
|
36
|
+
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
|
|
37
|
+
a string from the merchant's wallet that can create invoices and read their
|
|
38
|
+
status, and cannot spend. A swap provider (an "LSC" code) optionally lets the
|
|
39
|
+
payer send USDT, USDC, ETH or SOL instead, converted into that same
|
|
40
|
+
Lightning payment. You supply those credentials and three hooks — `authorize`, `amountFor`,
|
|
41
|
+
`onPaid`;
|
|
42
|
+
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
|
|
43
|
+
owns orders, users, prices, or fulfillment.
|
|
44
|
+
|
|
45
|
+
## Step 0 — check the environment before you write code
|
|
46
|
+
|
|
47
|
+
Do this before installing packages or editing files.
|
|
48
|
+
|
|
49
|
+
1. Look for `NWC_URI` in this app's server environment — `.env`, the process
|
|
50
|
+
env, the deploy config, whatever this app already uses. If the app runs in a
|
|
51
|
+
container the value is in none of those: ask the running process
|
|
52
|
+
(`docker exec <container> printenv NWC_URI`), because finding the NAME in a
|
|
53
|
+
compose file proves nothing about the value. Never print or echo the value
|
|
54
|
+
itself; only report whether it is set. Check for `LSC_URI_PRIMARY` in the
|
|
55
|
+
same pass.
|
|
56
|
+
2. If BOTH are already set — the common case in an existing app — say so and go
|
|
57
|
+
straight to the quickstart. Steps 3 and 4 are for an environment that is
|
|
58
|
+
missing one; do not stop to ask about altcoins that are already configured.
|
|
59
|
+
If only `NWC_URI` is set, Bitcoin already works: continue, and raise the
|
|
60
|
+
altcoin question at step 4 rather than blocking on it.
|
|
61
|
+
3. If `NWC_URI` is missing or empty, stop and tell the user exactly what to
|
|
62
|
+
create:
|
|
63
|
+
|
|
64
|
+
> OpenReceive cannot issue an invoice without a receive-only NWC code. Get
|
|
65
|
+
> one at https://openreceive.org/get_a_nwc_code_to_receive_payments, then
|
|
66
|
+
> put `NWC_URI=<the code>` in this app's server environment — for most apps
|
|
67
|
+
> that is a `.env` file in the project root — and tell me when it's set.
|
|
68
|
+
|
|
69
|
+
Wait for the user before wiring OpenReceive; do not invent a placeholder
|
|
70
|
+
value. Waiting is not idleness: you may write `.env.example` with the
|
|
71
|
+
variable NAMES only (`NWC_URI=`, `LSC_URI_PRIMARY=`) so the merchant has a
|
|
72
|
+
file to copy, and keep building the parts of the host that do not touch
|
|
73
|
+
OpenReceive — the order model, the cart, the routes. The stop guards the
|
|
74
|
+
credential, not the rest of the app.
|
|
75
|
+
4. If `LSC_URI_PRIMARY` was not already set, ask the user: "Do you want to
|
|
76
|
+
accept altcoins and stablecoins (USDT, USDC, ETH, SOL) as well as
|
|
77
|
+
Bitcoin?"
|
|
78
|
+
|
|
79
|
+
- Yes → send them to https://openreceive.org/set_up_swap_provider for a
|
|
80
|
+
swap-provider (LSC) code, to set as `LSC_URI_PRIMARY` in the same server
|
|
81
|
+
environment. Do NOT wait for it: no application code reads the value, so
|
|
82
|
+
the integration is identical with or without it — the library picks it up
|
|
83
|
+
from the environment and swaps switch on. What a yes DOES change is the
|
|
84
|
+
refund route back (the swap non-negotiable below): build it as part of
|
|
85
|
+
this integration, not when the code arrives.
|
|
86
|
+
- No → skip it. Bitcoin over Lightning works with `NWC_URI` alone, and you
|
|
87
|
+
can add a swap provider later without changing application code.
|
|
88
|
+
5. Check the environment again and confirm `NWC_URI` is present.
|
|
89
|
+
`LSC_URI_PRIMARY` may land later; swaps stay off until it does, and no code
|
|
90
|
+
changes when it arrives.
|
|
91
|
+
6. If OpenReceive is ALREADY installed here, check the installed versions of
|
|
92
|
+
`openreceive` (`pip show openreceive`) and `@openreceive/browser` against
|
|
93
|
+
the release named at the top of this file. The headless display models
|
|
94
|
+
below do not exist in older versions, and the first tile click throws with
|
|
95
|
+
nothing saying why. Upgrade first.
|
|
96
|
+
7. Run `openreceive doctor --app <module>:app` (or `--offline` before the
|
|
97
|
+
router exists). It reports `NWC_URI` presence without printing it, probes
|
|
98
|
+
the wallet, and once the router is wired names any hook still on a
|
|
99
|
+
placeholder and whether the migration ran. Exit code 1 is a finding to
|
|
100
|
+
fix, not a reason to skip the check.
|
|
101
|
+
|
|
102
|
+
Only then start the quickstart.
|
|
103
|
+
|
|
104
|
+
## Non-negotiables
|
|
105
|
+
|
|
106
|
+
The quickstart below has the code. These are the rules it cannot state for
|
|
107
|
+
itself, and they hold for every integration.
|
|
108
|
+
|
|
109
|
+
- OpenReceive never owns orders, users, prices, or fulfillment. The section
|
|
110
|
+
below is how those tables sit next to the library — not a second order model,
|
|
111
|
+
and not a Prisma/Drizzle relation to `openreceive_payments`.
|
|
112
|
+
- Keep `NWC_URI` / `LSC_URI_*` server-only. Never put them in browser code,
|
|
113
|
+
logs, or assets.
|
|
114
|
+
- The host owns the price. `amount_for` reads it from your own data; reject
|
|
115
|
+
payer-supplied amounts.
|
|
116
|
+
- `authorize` runs on every request, and the `resource` it receives is a CLAIM
|
|
117
|
+
the payer made, not proof. Read the Starlette request's session, cookie or
|
|
118
|
+
auth dependency; never trust a body field.
|
|
119
|
+
- `on_paid` must be idempotent. It runs once per `reference` — your order id, one
|
|
120
|
+
per thing you fulfill, created before checkout, kept across retries, never
|
|
121
|
+
reused. A fresh id per page load lets one order be paid twice.
|
|
122
|
+
- Receive-only NWC is required; a spend-capable code fails closed at boot unless
|
|
123
|
+
explicitly overridden.
|
|
124
|
+
- There is NO merchant-initiated refund of a settled Lightning payment, because
|
|
125
|
+
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
|
|
126
|
+
never converted — are the only refund OpenReceive performs, and only from the
|
|
127
|
+
`refund_required` provider state. Do not build, promise, or imply a Lightning
|
|
128
|
+
refund path.
|
|
129
|
+
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
|
|
130
|
+
late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
|
|
131
|
+
after leaving your page to fetch an address from another wallet. Three things
|
|
132
|
+
must exist or that money is unreachable through your UI: a per-order URL your
|
|
133
|
+
server serves (`/checkout/:reference` — `syncUrl` on the drop-ins), your own
|
|
134
|
+
order-summary route to restore the order from, and the ATTEMPT.
|
|
135
|
+
`/checkouts/prepare` returns no attempts, so a checkout rebuilt from the
|
|
136
|
+
reference alone opens on the method grid. Re-picking the same coin
|
|
137
|
+
(`POST /swaps`) re-serves the committed attempt — but only while it is live,
|
|
138
|
+
and the shadow invoice behind a swap lasts about half an hour, after which the
|
|
139
|
+
same click mints a NEW deposit address and the refund is off-screen. Keep the
|
|
140
|
+
`payment_hash` and reopen the attempt with `POST /swaps/status`, which has no
|
|
141
|
+
such window. https://openreceive.org/guides/swap-refunds.md
|
|
142
|
+
- Show the payer WHAT THEY ARE BUYING. Return an optional `description` beside
|
|
143
|
+
the price from `amount_for` and both drop-ins render it above the amount.
|
|
144
|
+
Without it the checkout is a QR and "$1.00" with no sign of what the dollar
|
|
145
|
+
is for.
|
|
146
|
+
- Show the payer the transaction record: `createTransactionDetails(...)` rows,
|
|
147
|
+
collapsed behind a caret, on the live checkout AND on the receipt. A payment
|
|
148
|
+
hash and a deposit txid are the only evidence a payer has that they paid you.
|
|
149
|
+
`<Checkout>` / `<openreceive-checkout>` already render this panel and the
|
|
150
|
+
`description` — these two rules cost you code only on a custom UI or your own
|
|
151
|
+
receipt page, never a reason to replace the drop-in. (It returns no rows
|
|
152
|
+
while the rail is `checkout_lock` — before the payer has chosen anything
|
|
153
|
+
there is no transaction — so render the caret only when the rows are
|
|
154
|
+
non-empty.)
|
|
155
|
+
- Include the router under the prefix — `app.include_router(
|
|
156
|
+
openreceive_router(host, engine=engine), prefix="/openreceive")` — and give
|
|
157
|
+
the app `lifespan=openreceive_lifespan(host, engine=engine)` so a bad wallet
|
|
158
|
+
stops the deploy. Do not add a CSRF layer for these routes: FastAPI has
|
|
159
|
+
none and the engine's `Sec-Fetch-Site: cross-site` refusal is the
|
|
160
|
+
protection. Behind a reverse proxy run uvicorn with `--proxy-headers`, or
|
|
161
|
+
`rate_limiting` counts the proxy as the one payer.
|
|
162
|
+
- The engine is synchronous by design: the router's endpoint runs in
|
|
163
|
+
Starlette's threadpool. Do not wrap it in `async def` code of your own, and
|
|
164
|
+
do not hand OpenReceive an async database session — it wants a sync
|
|
165
|
+
SQLAlchemy `Engine`.
|
|
166
|
+
- HTTP JSON is snake_case, and so is the Python API (`payment_hash`,
|
|
167
|
+
`amount_msats`).
|
|
168
|
+
- Money is integers or decimal strings — never binary floats.
|
|
169
|
+
|
|
170
|
+
## Your tables, not ours
|
|
171
|
+
|
|
172
|
+
`openreceive scaffold payments --alembic` (or `--sql`) emits
|
|
173
|
+
`openreceive_payments` and `openreceive_meta` for THIS application's database.
|
|
174
|
+
That is the whole persistence OpenReceive needs. It does not replace your
|
|
175
|
+
orders, users, or products, and you do not join them.
|
|
176
|
+
|
|
177
|
+
- **Find this app's models first.** They may be named `Order`, `Invoice`,
|
|
178
|
+
`Booking`, `Product`, `Variant`, `User`, `Account` — anything. Wire the hooks
|
|
179
|
+
to those. Do not generate a parallel `ShopOrder` / `ShopProduct` / `ShopUser`
|
|
180
|
+
stack.
|
|
181
|
+
- **The payable row's id is the `reference`.** Create it before checkout, keep
|
|
182
|
+
it across retries, never reuse it. Pass that id to `<Checkout>` /
|
|
183
|
+
`<openreceive-checkout>`. A fresh id per page load lets one order be paid
|
|
184
|
+
twice.
|
|
185
|
+
- **Products (or the catalog) are the price authority.** Order creation reads
|
|
186
|
+
live prices into the order (snapshot line items if this app has them).
|
|
187
|
+
`amount_for` reads only that order — never a payer-supplied amount, never a
|
|
188
|
+
live catalog lookup that could re-price a cart already placed. Return
|
|
189
|
+
`{"currency", "value"}` as a decimal STRING, plus a `description` of what
|
|
190
|
+
they are buying.
|
|
191
|
+
- **Users own the order; OpenReceive never sees them.** `authorize` uses the
|
|
192
|
+
same ownership check this app already uses on the order show / pay page —
|
|
193
|
+
the `current_user` dependency, a session cookie, whatever it is, resolved
|
|
194
|
+
from `context.request`. `context.resource["reference"]` is a claim the
|
|
195
|
+
payer sent, not proof.
|
|
196
|
+
- **The order is unpaid or paid.** Do not copy `pending` / `expired` / `failed`
|
|
197
|
+
/ `attention` onto it. Those are attempt statuses on `openreceive_payments`. An
|
|
198
|
+
expired invoice does not cancel the order; a later checkout may mint another
|
|
199
|
+
attempt. The library refuses a new checkout under a reference that already
|
|
200
|
+
settled (409).
|
|
201
|
+
- **Pass a sync SQLAlchemy `Engine` for this app's database.** Do not add an
|
|
202
|
+
ORM relationship from Order to `openreceive_payments`, and do not implement
|
|
203
|
+
`PaymentRepository` unless no SQLAlchemy engine can reach this database.
|
|
204
|
+
`reference` is not unique (many attempts per order). Fulfillment is a
|
|
205
|
+
guarded transition on YOUR order row inside `on_paid` — `UPDATE … WHERE
|
|
206
|
+
state = 'awaiting_payment'` (or this app's equivalent) through
|
|
207
|
+
`settlement.connection`, the settlement transaction's own connection, not a
|
|
208
|
+
second session from your ORM. Database writes only in the hook; emails,
|
|
209
|
+
jobs, and pushes in `after_paid` or after commit.
|
|
210
|
+
|
|
211
|
+
## If you build your own checkout UI
|
|
212
|
+
|
|
213
|
+
The drop-ins (`<Checkout>`, `<openreceive-checkout>`) already obey all of this.
|
|
214
|
+
This list is the short form of https://openreceive.org/guides/checkout-ux.md, for a
|
|
215
|
+
UI built on `@openreceive/browser/headless`. Read that before writing
|
|
216
|
+
components.
|
|
217
|
+
|
|
218
|
+
- `createCheckoutController` is the engine. Do not hand-roll a poll loop.
|
|
219
|
+
- `createCheckoutStatusModel` for the status line. Do not draw a
|
|
220
|
+
Cart → Pay → Done stepper. Read the model's `phase`, not the snapshot's.
|
|
221
|
+
- `resolveWizardSelection` decides whether to ask "which network?". A
|
|
222
|
+
one-network asset starts the swap from the tile. Key `selectedAssetByGroup`
|
|
223
|
+
by group (`USDT`), valued by `pay_in_asset` (`USDT_TRON`).
|
|
224
|
+
- `createMethodGridDisplay` for tiles, including `limitMessage` so an
|
|
225
|
+
unavailable method says the minimum in the payer's currency.
|
|
226
|
+
- `createSwapDisplayModel` → `display.copyRows` for deposits: address, memo,
|
|
227
|
+
and the bare amount each get a copy row. Render `swap.networkWarning*` as
|
|
228
|
+
the model gives it.
|
|
229
|
+
- `createCheckoutSession` owns mint and swap start. To start swaps, pass its
|
|
230
|
+
`swap` option (`selection`, `prefix`, `fetch`) together. Without it
|
|
231
|
+
`startSwap` reports through `onError`.
|
|
232
|
+
- `createQrSvg` is async. Use `createQrSvgController` so you do not render
|
|
233
|
+
`[object Promise]`.
|
|
234
|
+
- `checkoutLabels` for every payer-facing string. Only write copy it lacks.
|
|
235
|
+
- `stageSwapRefund` then `confirmSwapRefund` — only the second submits.
|
|
236
|
+
Validate with `getSwapRefundFormError`. Treat `409` as a normal outcome.
|
|
237
|
+
- Pass `{ resumable: true }` to `createSwapDisplayModel` when the payer has
|
|
238
|
+
a URL they can come back to, and render `display.refundReturnLabel`.
|
|
239
|
+
Resume helpers (`createGuestCheckoutResume`, `createGuestOrderFetcher`)
|
|
240
|
+
are on `@openreceive/browser`, not `/headless`.
|
|
241
|
+
- A refund replaces the deposit panel. On `refund_required` also drop
|
|
242
|
+
"switch payment method".
|
|
243
|
+
- No "Open wallet" button on desktop.
|
|
244
|
+
- Wallet suggestions: `getPaymentWizardRoutes()` +
|
|
245
|
+
`createWizardRouteDisplays`. Lightning only. Every image ships inside
|
|
246
|
+
the JavaScript — logos as data URIs, tutorials once `loadPayTutorialImages()`
|
|
247
|
+
resolves (`image` is `undefined` until then) — so serve nothing and set no
|
|
248
|
+
asset option. When it works, the logos and payment icons render; a missing
|
|
249
|
+
image means a CSP `img-src` that blocks `data:`, and the console names it.
|
|
250
|
+
|
|
251
|
+
## More documentation
|
|
252
|
+
|
|
253
|
+
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
|
|
254
|
+
enough; drop the `.md` for the same page a person would read.
|
|
255
|
+
|
|
256
|
+
- https://openreceive.org/guides/authorization.md — before you write `authorize`
|
|
257
|
+
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
|
|
258
|
+
- https://openreceive.org/guides/storage.md — the payment tables and the attempt state machine
|
|
259
|
+
- https://openreceive.org/guides/frontend-checkout.md — the drop-in's props, attributes and slots
|
|
260
|
+
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
|
|
261
|
+
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
|
|
262
|
+
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay
|
|
263
|
+
tutorials come from: inside the JavaScript, nothing to serve. This is the page
|
|
264
|
+
that owns the image rule, not the summary in checkout-ux.md
|
|
265
|
+
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
|
|
266
|
+
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
|
|
267
|
+
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
|
|
268
|
+
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
|
|
269
|
+
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
|
|
270
|
+
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
|
|
271
|
+
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
|
|
272
|
+
- https://openreceive.org/guides/api-reference.md — every route, option and error code
|
|
273
|
+
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the shipped adapter's routes with your own
|
|
274
|
+
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
|
|
275
|
+
- https://openreceive.org/guides/flask-recipe.md — the same engine as a Flask Blueprint, if this app is Flask after all
|
|
276
|
+
- https://openreceive.org/guides.md — the index, if what you need is not above
|
|
277
|
+
|
|
278
|
+
Questions, or a problem with the library itself:
|
|
279
|
+
https://openreceive.org/contact
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## The quickstart, in full
|
|
284
|
+
|
|
285
|
+
Inlined verbatim so this file needs no network access — follow it once Step 0
|
|
286
|
+
passes. The page it comes from is https://openreceive.org/guides/quickstart-fastapi.
|
|
287
|
+
|
|
288
|
+
## FastAPI quickstart
|
|
289
|
+
|
|
290
|
+
FastAPI + React. Requires Python ≥ 3.10, FastAPI ≥ 0.115 (Starlette ≥ 0.40)
|
|
291
|
+
and a SQLAlchemy 2 `Engine` for OpenReceive's two tables.
|
|
292
|
+
|
|
293
|
+
### 1. Install
|
|
294
|
+
|
|
295
|
+
```sh
|
|
296
|
+
pip install "openreceive[fastapi]"
|
|
297
|
+
npm install @openreceive/react
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
One Python distribution with framework extras: `openreceive[fastapi]` brings
|
|
301
|
+
FastAPI, Starlette and SQLAlchemy; the wallet client (websockets, coincurve,
|
|
302
|
+
cryptography), the HTTP engine and the `openreceive` CLI come with the base
|
|
303
|
+
package. The npm package is the checkout UI for your frontend. Different
|
|
304
|
+
stack? Swap the two: `openreceive[django]` with the Django quickstart; a
|
|
305
|
+
bundler-less page uses `@openreceive/elements` instead of React.
|
|
306
|
+
|
|
307
|
+
| | Packages |
|
|
308
|
+
| -------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
309
|
+
| Server | `openreceive[fastapi]`, `openreceive[django]`, `openreceive[sqlalchemy]` (Flask and plain WSGI/ASGI: the Flask recipe) |
|
|
310
|
+
| Frontend | `@openreceive/react`, `@openreceive/vue`, `@openreceive/svelte`, `@openreceive/angular`, `@openreceive/elements` (plain HTML) |
|
|
311
|
+
|
|
312
|
+
Use `uv` or a virtualenv on Python 3.10 or newer; a system Python 3.9 cannot
|
|
313
|
+
install the package.
|
|
314
|
+
|
|
315
|
+
### 2. Migrate the payment tables
|
|
316
|
+
|
|
317
|
+
```sh
|
|
318
|
+
openreceive scaffold payments --alembic --dialect postgres # or sqlite | mysql
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
`openreceive scaffold payments --alembic` writes one Alembic revision into
|
|
322
|
+
`alembic/versions/` that creates `openreceive_payments` (the payment attempts)
|
|
323
|
+
and `openreceive_meta` (the reconcile gate) with the DDL frozen in the file.
|
|
324
|
+
Set its `down_revision` to your current head (`alembic heads`), then apply it
|
|
325
|
+
the way you apply your own: `alembic upgrade head`. It never opens a database
|
|
326
|
+
connection.
|
|
327
|
+
→ [openreceive scaffold payments](https://openreceive.org/guides/api-reference.md#openreceive-scaffold-payments-python)
|
|
328
|
+
|
|
329
|
+
No Alembic? `openreceive scaffold payments --sql --dialect postgres` prints
|
|
330
|
+
the same DDL for `psql`, a Flyway file, or whatever runs your migrations; in
|
|
331
|
+
code, `payments_schema_sql(dialect)` from `openreceive.storage.sql` is the
|
|
332
|
+
same string. OpenReceive owns the tables' logic at runtime — locking,
|
|
333
|
+
write-once settlement, the reconciliation state machine — and there is
|
|
334
|
+
nothing else to generate. Details: [Payment storage](https://openreceive.org/guides/storage.md).
|
|
335
|
+
|
|
336
|
+
### 3. Add wallet credentials
|
|
337
|
+
|
|
338
|
+
Put the credentials in the server's environment (a `.env` your process
|
|
339
|
+
manager loads, `uvicorn --env-file .env`, or your secret store):
|
|
340
|
+
|
|
341
|
+
```dotenv
|
|
342
|
+
NWC_URI=
|
|
343
|
+
LSC_URI_PRIMARY=
|
|
344
|
+
LSC_URI_BACKUP=
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
1. Get a receive-only NWC code from a compatible wallet
|
|
348
|
+
([get one here](https://openreceive.org/get_a_nwc_code_to_receive_payments))
|
|
349
|
+
→ `NWC_URI`.
|
|
350
|
+
2. Optionally set up a [swap provider](https://openreceive.org/set_up_swap_provider)
|
|
351
|
+
→ `LSC_URI_PRIMARY` (and `LSC_URI_BACKUP` if you have one).
|
|
352
|
+
|
|
353
|
+
Never put these values in browser code. Your application refuses to start if
|
|
354
|
+
the NWC code also advertises spend methods such as `pay_invoice`; mint a
|
|
355
|
+
receive-only code ([Security](https://openreceive.org/guides/security.md)).
|
|
356
|
+
|
|
357
|
+
OpenReceive reads `os.environ`; a `.env` file on disk is not enough on its
|
|
358
|
+
own. `uvicorn --env-file .env` loads one in development; in production the
|
|
359
|
+
process manager or secret store injects the values. Every variable is on
|
|
360
|
+
[Environment variables](https://openreceive.org/guides/environment-variables.md).
|
|
361
|
+
|
|
362
|
+
### 4. Wire OpenReceive
|
|
363
|
+
|
|
364
|
+
Twelve lines: your three hooks as a `Host`, a router included under a
|
|
365
|
+
prefix, and a lifespan that checks the wallet at startup. The router builds
|
|
366
|
+
the wallet client from `NWC_URI` and mounts the framework-free engine; there
|
|
367
|
+
is no background reconciler — settlement piggybacks on requests through the
|
|
368
|
+
durable gate.
|
|
369
|
+
|
|
370
|
+
```python
|
|
371
|
+
from fastapi import FastAPI
|
|
372
|
+
from sqlalchemy import create_engine
|
|
373
|
+
from openreceive.fastapi import openreceive_lifespan, openreceive_router
|
|
374
|
+
from openreceive.server import Host
|
|
375
|
+
from .app import current_user, orders # your existing models and auth dependency
|
|
376
|
+
|
|
377
|
+
# OpenReceive's own sync Engine for its two tables — the SAME database as
|
|
378
|
+
# your orders, its own connection pool. On SQLite give it a dedicated Engine.
|
|
379
|
+
engine = create_engine("postgresql+psycopg://…")
|
|
380
|
+
|
|
381
|
+
host = Host(
|
|
382
|
+
# The price for a reference — here, your order id — from your own data;
|
|
383
|
+
# OpenReceive converts it into the Lightning invoice. Return None when
|
|
384
|
+
# there is nothing to pay for. `value` is a decimal STRING from the order
|
|
385
|
+
# row, never a float and never a request param. `description` is what the
|
|
386
|
+
# payer is buying, in your own words.
|
|
387
|
+
amount_for=lambda reference: (
|
|
388
|
+
{"currency": "USD", "value": str(order.total), "description": order.summary}
|
|
389
|
+
if (order := orders.find(reference))
|
|
390
|
+
else None
|
|
391
|
+
),
|
|
392
|
+
# Your own access check: may this caller do this action to this reference?
|
|
393
|
+
# `context.request` is the untouched Starlette Request — reuse the same
|
|
394
|
+
# dependency your order page uses. `context.resource["reference"]` is a
|
|
395
|
+
# claim the payer's browser sent, not proof.
|
|
396
|
+
authorize=lambda context: orders.viewer_may(
|
|
397
|
+
current_user(context.request), context.resource["reference"], context.action
|
|
398
|
+
),
|
|
399
|
+
# INSIDE the settlement transaction; runs only for the reference's first
|
|
400
|
+
# settled attempt. Use `settlement.connection` (that transaction) for the
|
|
401
|
+
# order write, never a second session. The WHERE clause is the lock.
|
|
402
|
+
on_paid=lambda settlement: settlement.connection.execute(
|
|
403
|
+
orders.claim_paid(settlement.reference, settlement.paid_at)
|
|
404
|
+
),
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
app = FastAPI(lifespan=openreceive_lifespan(host, engine=engine))
|
|
408
|
+
app.include_router(
|
|
409
|
+
# Recommended for public web shops: `rate_limiting=True` caps invoice
|
|
410
|
+
# creation at 60 per client IP per hour. Leave it off (the default) for
|
|
411
|
+
# point-of-sale deployments, where many payers share the terminal's IP.
|
|
412
|
+
openreceive_router(host, engine=engine, rate_limiting=True),
|
|
413
|
+
prefix="/openreceive",
|
|
414
|
+
)
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
`authorize` receives the Starlette `Request`, so cookies, headers and whatever
|
|
418
|
+
`SessionMiddleware` or auth dependency this app already has are readable
|
|
419
|
+
there — FastAPI ships no session of its own, so the payer's `reference` must
|
|
420
|
+
be bound to something the request proves. Run uvicorn with `--proxy-headers`
|
|
421
|
+
behind a reverse proxy, or `rate_limiting` counts the proxy as the one payer.
|
|
422
|
+
|
|
423
|
+
**There is no CSRF layer to add.** FastAPI has none, and OpenReceive does not
|
|
424
|
+
want one: every mounted route refuses a request whose `Sec-Fetch-Site` header
|
|
425
|
+
says `cross-site`, which is what stops another origin's page from minting or
|
|
426
|
+
refunding on a logged-in payer's behalf — exactly the protection the Express
|
|
427
|
+
and Fastify adapters rely on. Its limit is the header itself: a client that
|
|
428
|
+
does not send it (a non-browser, an old browser) is not refused, so
|
|
429
|
+
`authorize` stays the actual boundary. [Security](https://openreceive.org/guides/security.md) has the full
|
|
430
|
+
account.
|
|
431
|
+
|
|
432
|
+
`openreceive_lifespan` runs the receive-only wallet preflight when uvicorn
|
|
433
|
+
starts and stops the process on a missing, unreachable or spend-capable code —
|
|
434
|
+
the deploy fails instead of the first payer. Pass `lazy=True` for tests and
|
|
435
|
+
secretless build steps; the first request then checks the wallet and answers
|
|
436
|
+
`503 WALLET_UNAVAILABLE` until it passes.
|
|
437
|
+
→ [openreceive_router](https://openreceive.org/guides/api-reference.md#openreceive_router) ·
|
|
438
|
+
[openreceive_lifespan](https://openreceive.org/guides/api-reference.md#openreceive_lifespan) ·
|
|
439
|
+
[the authorize context](https://openreceive.org/guides/api-reference.md#the-authorize-context)
|
|
440
|
+
|
|
441
|
+
`rate_limiting=True` is for public web shops. Leave it off for point-of-sale,
|
|
442
|
+
where many payers share one IP. → [Rate limiting](https://openreceive.org/guides/rate-limiting.md)
|
|
443
|
+
|
|
444
|
+
An optional worker, `openreceive notifications --app main:app`, listens for
|
|
445
|
+
wallet payment notifications so settlement does not wait for the next page
|
|
446
|
+
load. → [openreceive notifications](https://openreceive.org/guides/api-reference.md#openreceive-notifications)
|
|
447
|
+
|
|
448
|
+
Your app also needs an ordinary order-creation route that validates the cart,
|
|
449
|
+
prices with exact decimal math, and returns the order id the page will pass as
|
|
450
|
+
the `reference`. OpenReceive never prices from payer input.
|
|
451
|
+
|
|
452
|
+
The `reference` is a string you choose, and it is the fulfillment identity:
|
|
453
|
+
your order id — one per thing you fulfill, created before checkout, kept
|
|
454
|
+
across retries, never reused. OpenReceive never looks inside it, but `on_paid`
|
|
455
|
+
runs once per reference, a new checkout under a reference that already
|
|
456
|
+
settled is refused with 409, and a fresh id per page load lets one order be
|
|
457
|
+
paid twice.
|
|
458
|
+
|
|
459
|
+
Naming boundary: the Python API is snake_case (`payment_hash`,
|
|
460
|
+
`amount_msats`) and so is everything on the wire — the mounted HTTP routes and
|
|
461
|
+
the browser snapshots use the same names.
|
|
462
|
+
|
|
463
|
+
### 5. Render checkout
|
|
464
|
+
|
|
465
|
+
```tsx
|
|
466
|
+
import { Checkout } from "@openreceive/react";
|
|
467
|
+
import "@openreceive/react/styles.css";
|
|
468
|
+
|
|
469
|
+
<Checkout reference={order.id} prefix="/openreceive" />;
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
The checkout renders, polls, and settles itself. The compiled `styles.css`
|
|
473
|
+
sheets (`@openreceive/react`, `@openreceive/elements`) are self-contained — a
|
|
474
|
+
plain `<link rel="stylesheet">` works with no build step — and scoped: every
|
|
475
|
+
rule applies only inside what OpenReceive renders, so the sheet is safe next
|
|
476
|
+
to any CSS framework (Mantine, Bootstrap, your own reset) in any import order.
|
|
477
|
+
|
|
478
|
+
`<Checkout>` is complete as rendered: it already shows the `description` from
|
|
479
|
+
`amountFor` and the collapsed transaction-details panel. Do not build a custom
|
|
480
|
+
UI to satisfy those rules — they only become your job if you replace the
|
|
481
|
+
drop-in ([Checkout UX](https://openreceive.org/guides/checkout-ux.md)).
|
|
482
|
+
|
|
483
|
+
Match the host page's theme: by default the checkout follows the payer's
|
|
484
|
+
stored choice, then the system scheme. If this page is always one theme, lock
|
|
485
|
+
it — `<Checkout theme="dark" … />` (`theme` attribute on the custom element) —
|
|
486
|
+
so a white card never lands on a dark page. The checkout is styled by CSS
|
|
487
|
+
variables under `data-theme`; [Frontend checkout](https://openreceive.org/guides/frontend-checkout.md) has
|
|
488
|
+
the knobs.
|
|
489
|
+
|
|
490
|
+
Everything the checkout draws ships inside the JavaScript: the payment-method
|
|
491
|
+
icons, the wallet logos and the pay tutorials. There is no image file to copy
|
|
492
|
+
or serve and no asset option to set. Deploy your normal JavaScript and CSS
|
|
493
|
+
build output, including any generated JavaScript chunks. Bundlers with code
|
|
494
|
+
splitting can defer tutorial screenshots until first open; single-file builds
|
|
495
|
+
(including the standalone checkout) include them upfront. If your
|
|
496
|
+
Content-Security-Policy has a strict `img-src`, allow `data:`
|
|
497
|
+
([Provider registry](https://openreceive.org/guides/provider-registry.md#assets)).
|
|
498
|
+
|
|
499
|
+
That is the whole loop: your server owns the price and the order, the payer gets
|
|
500
|
+
an invoice, and `onPaid` runs once inside the settlement transaction.
|
|
501
|
+
|
|
502
|
+
A page without a bundler renders the same checkout as a custom element:
|
|
503
|
+
`<openreceive-checkout reference="…" prefix="/openreceive">` from
|
|
504
|
+
`@openreceive/elements` (or its standalone build, served from your static
|
|
505
|
+
directory). A runnable illustration of the boundary — not a template to copy
|
|
506
|
+
models from — is Buy a Button
|
|
507
|
+
(`examples/buttons/server/fastapi`):
|
|
508
|
+
products, visitors and orders in SQLite, the three hooks as the only bridge,
|
|
509
|
+
and the smallest correct FastAPI integration of the packaged checkout. Map
|
|
510
|
+
that shape onto the models in THIS app.
|
|
511
|
+
|
|
512
|
+
### 6. Verify
|
|
513
|
+
|
|
514
|
+
```sh
|
|
515
|
+
openreceive doctor --app main:app
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
`openreceive doctor` checks Python, `NWC_URI`, and swap-provider
|
|
519
|
+
configuration, and probes the wallet relay to confirm the code is
|
|
520
|
+
receive-only. With `--app module:attr` (your FastAPI app, the router, or an
|
|
521
|
+
`OpenReceiveApp`) it also confirms the migration ran and names any hook still
|
|
522
|
+
on a placeholder; add `--url http://localhost:8000` to confirm the routes are
|
|
523
|
+
mounted. Every failing line states its own fix; exit code 1 when anything
|
|
524
|
+
fails. `openreceive debug-report` prints the same as a redacted support
|
|
525
|
+
report.
|
|
526
|
+
→ [openreceive doctor](https://openreceive.org/guides/api-reference.md#openreceive-doctor-python)
|
|
527
|
+
|
|
528
|
+
Then open the checkout in a browser, confirm the payment-method icons and
|
|
529
|
+
wallet logos render, and open a wallet's pay tutorial to check its screenshots.
|
|
530
|
+
If an image is missing, inspect the console for CSP violations and the Network
|
|
531
|
+
panel for failed JavaScript chunks. Allow `data:` in `img-src` and deploy the
|
|
532
|
+
complete build output. Do not add image routes, copy package source images, or
|
|
533
|
+
use registry `icon_path` / tutorial `path` keys as browser URLs.
|
|
534
|
+
|
|
535
|
+
Swap credentials (`LSC_URI_*`) stay server-side too: the provider order id and
|
|
536
|
+
token live in the attempt's server-only `swap_data` column and never reach a
|
|
537
|
+
response or a log ([Automated swaps](https://openreceive.org/guides/automated-swaps.md)).
|