openreceive-server 0.3.1 → 0.3.3
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 +29 -0
- data/lib/openreceive/server/errors.rb +10 -6
- data/lib/openreceive/server/request_handler.rb +27 -5
- data/lib/openreceive/server/swap/fixedfloat.rb +11 -6
- data/lib/openreceive/server/version.rb +1 -1
- data/skills/debug-openreceive-payment/SKILL.md +88 -0
- data/skills/integrate-openreceive/SKILL.md +116 -0
- data/skills/integrate-openreceive/references/node.md +480 -0
- data/skills/integrate-openreceive/references/rails.md +568 -0
- metadata +7 -3
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
# OpenReceive agent directions (Rails)
|
|
2
|
+
|
|
3
|
+
These directions describe OpenReceive 0.3.3.
|
|
4
|
+
|
|
5
|
+
Add OpenReceive to a Rails application — the app you are already working in. You
|
|
6
|
+
do not need a copy of the OpenReceive source: the gem is on RubyGems, the
|
|
7
|
+
frontend packages are on npm, and the quickstart is appended to this file in
|
|
8
|
+
full, so you can do the whole integration without fetching anything. Prefer the
|
|
9
|
+
published gem and the mounted engine routes — do not reimplement wallet RPC,
|
|
10
|
+
settlement, or pricing.
|
|
11
|
+
|
|
12
|
+
Do not clone the OpenReceive repository into this app, and do not copy a demo's
|
|
13
|
+
models (`ShopOrder`, `ShopUser`, a signed-cookie visitor) over tables that
|
|
14
|
+
already exist. Find this application's order, product, and user models — whatever
|
|
15
|
+
they are actually named — and map the three hooks onto those.
|
|
16
|
+
|
|
17
|
+
## What OpenReceive is
|
|
18
|
+
|
|
19
|
+
A payment library that runs inside YOUR server. It mounts HTTP routes in the
|
|
20
|
+
application you are editing, issues Lightning invoices against a wallet the
|
|
21
|
+
merchant already controls, and calls back into your code when one settles. There
|
|
22
|
+
is no OpenReceive account and no API key, and OpenReceive never holds the funds —
|
|
23
|
+
the sats land in the wallet the merchant connected.
|
|
24
|
+
|
|
25
|
+
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
|
|
26
|
+
a string from the merchant's wallet that can create invoices and read their
|
|
27
|
+
status, and cannot spend. A swap provider (an "LSC" code) optionally lets the
|
|
28
|
+
payer send USDT, USDC, ETH or SOL instead, converted into that same
|
|
29
|
+
Lightning payment. You supply those credentials and three hooks — `config.authorize`,
|
|
30
|
+
`config.amount_for`, `config.on_paid`;
|
|
31
|
+
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
|
|
32
|
+
owns orders, users, prices, or fulfillment.
|
|
33
|
+
|
|
34
|
+
## Step 0 — check the environment before you write code
|
|
35
|
+
|
|
36
|
+
Do this before installing the gem or editing files.
|
|
37
|
+
|
|
38
|
+
1. Look for `NWC_URI` in this app's server environment — `.env`, Rails
|
|
39
|
+
credentials, the deploy config, whatever this app already uses. If the app
|
|
40
|
+
runs in a container the value is in none of those: ask the running process
|
|
41
|
+
(`docker exec <container> printenv NWC_URI`), because finding the NAME in a
|
|
42
|
+
compose file or `.kamal/secrets` proves nothing about the value. Never print
|
|
43
|
+
or echo the value itself; only report whether it is set. Check for
|
|
44
|
+
`LSC_URI_PRIMARY` in the same pass.
|
|
45
|
+
|
|
46
|
+
If OpenReceive is already installed here, `bin/rails openreceive:doctor`
|
|
47
|
+
answers this whole step in one command — every credential as set/unset, the
|
|
48
|
+
engine mount, the three hooks, and the wallet preflight. It never prints a
|
|
49
|
+
value.
|
|
50
|
+
2. If BOTH are already set — the common case in an existing app — say so and go
|
|
51
|
+
straight to the quickstart. Steps 3 and 4 are for an environment that is
|
|
52
|
+
missing one; do not stop to ask about altcoins that are already configured.
|
|
53
|
+
If only `NWC_URI` is set, Bitcoin already works: continue, and raise the
|
|
54
|
+
altcoin question at step 4 rather than blocking on it.
|
|
55
|
+
3. If `NWC_URI` is missing or empty, stop and tell the user exactly what to
|
|
56
|
+
create:
|
|
57
|
+
|
|
58
|
+
> OpenReceive cannot issue an invoice without a receive-only NWC code. Get
|
|
59
|
+
> one at https://openreceive.org/get_a_nwc_code_to_receive_payments, then
|
|
60
|
+
> put `NWC_URI=<the code>` in this app's server environment — for most apps
|
|
61
|
+
> that is a `.env` file in the project root — and tell me when it's set.
|
|
62
|
+
|
|
63
|
+
Wait for the user before wiring OpenReceive; do not invent a placeholder
|
|
64
|
+
value. Waiting is not idleness: you may write `.env.example` with the
|
|
65
|
+
variable NAMES only (`NWC_URI=`, `LSC_URI_PRIMARY=`) so the merchant has a
|
|
66
|
+
file to copy, and keep building the parts of the host that do not touch
|
|
67
|
+
OpenReceive — the order model, the cart, the routes. The stop guards the
|
|
68
|
+
credential, not the rest of the app.
|
|
69
|
+
4. If `LSC_URI_PRIMARY` was not already set, ask the user: "Do you want to
|
|
70
|
+
accept altcoins and stablecoins (USDT, USDC, ETH, SOL) as well as
|
|
71
|
+
Bitcoin?"
|
|
72
|
+
|
|
73
|
+
- Yes → send them to https://openreceive.org/set_up_swap_provider for a
|
|
74
|
+
swap-provider (LSC) code, to set as `LSC_URI_PRIMARY` in the same server
|
|
75
|
+
environment. Do NOT wait for it: no application code reads the value, so
|
|
76
|
+
the integration is identical with or without it — the engine picks it up
|
|
77
|
+
from the environment and swaps switch on. What a yes DOES change is the
|
|
78
|
+
refund route back (the swap non-negotiable below): build it as part of
|
|
79
|
+
this integration, not when the code arrives.
|
|
80
|
+
- No → skip it. Bitcoin over Lightning works with `NWC_URI` alone, and you
|
|
81
|
+
can add a swap provider later without changing application code.
|
|
82
|
+
5. Check the environment again and confirm `NWC_URI` is present.
|
|
83
|
+
`LSC_URI_PRIMARY` may land later; swaps stay off until it does, and no code
|
|
84
|
+
changes when it arrives.
|
|
85
|
+
6. If OpenReceive is ALREADY installed here, check the installed versions of
|
|
86
|
+
`openreceive-rails` and `@openreceive/browser` against the release named at
|
|
87
|
+
the top of this file. The headless display models below do not exist in
|
|
88
|
+
older versions, and the first tile click throws with nothing saying why.
|
|
89
|
+
Upgrade first — and if this app runs in containers, rebuild the images: the
|
|
90
|
+
gems are baked into the image, so an in-place `bundle update` is undone by
|
|
91
|
+
the next `compose up`.
|
|
92
|
+
|
|
93
|
+
Only then start the quickstart.
|
|
94
|
+
|
|
95
|
+
## Non-negotiables
|
|
96
|
+
|
|
97
|
+
The quickstart below has the code. These are the rules it cannot state for
|
|
98
|
+
itself, and they hold for every integration.
|
|
99
|
+
|
|
100
|
+
- OpenReceive never owns orders, users, prices, or fulfillment. The section
|
|
101
|
+
below is how those tables sit next to the engine — not a second order model,
|
|
102
|
+
and not an association to `OpenReceivePayment`.
|
|
103
|
+
- Keep `NWC_URI` / `LSC_URI_*` server-only. Never put them in browser code,
|
|
104
|
+
logs, or assets.
|
|
105
|
+
- The host owns the price. `config.amount_for` reads it from your own data;
|
|
106
|
+
reject payer-supplied amounts.
|
|
107
|
+
- `config.authorize` runs on every request, and the `resource` it receives is a
|
|
108
|
+
CLAIM the payer made, not proof. Read the framework session; never trust a
|
|
109
|
+
body field.
|
|
110
|
+
- `config.on_paid` must be idempotent. It runs once per `reference` — your order
|
|
111
|
+
id, one per thing you fulfill, created before checkout, kept across retries,
|
|
112
|
+
never reused. A fresh id per page load lets one order be paid twice.
|
|
113
|
+
- Receive-only NWC is required; a spend-capable code fails closed at boot unless
|
|
114
|
+
explicitly overridden.
|
|
115
|
+
- There is NO merchant-initiated refund of a settled Lightning payment, because
|
|
116
|
+
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
|
|
117
|
+
never converted — are the only refund OpenReceive performs, and only from the
|
|
118
|
+
`refund_required` provider state. Do not build, promise, or imply a Lightning
|
|
119
|
+
refund path.
|
|
120
|
+
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
|
|
121
|
+
late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
|
|
122
|
+
after leaving your page to fetch an address from another wallet. Three things
|
|
123
|
+
must exist or that money is unreachable through your UI: a per-order URL your
|
|
124
|
+
server serves (`/checkout/:reference` — `syncUrl` on the drop-ins), your own
|
|
125
|
+
order-summary route to restore the order from, and the ATTEMPT.
|
|
126
|
+
`/checkouts/prepare` returns no attempts, so a checkout rebuilt from the
|
|
127
|
+
reference alone opens on the method grid. Re-picking the same coin
|
|
128
|
+
(`POST /swaps`) re-serves the committed attempt — but only while it is live,
|
|
129
|
+
and the shadow invoice behind a swap lasts about half an hour, after which the
|
|
130
|
+
same click mints a NEW deposit address and the refund is off-screen. Keep the
|
|
131
|
+
`payment_hash` and reopen the attempt with `POST /swaps/status`, which has no
|
|
132
|
+
such window. https://openreceive.org/guides/swap-refunds.md
|
|
133
|
+
- Show the payer WHAT THEY ARE BUYING. Return an optional `description` beside
|
|
134
|
+
the price from `config.amount_for` and both drop-ins render it above the
|
|
135
|
+
amount. Without it the checkout is a QR and "$1.00" with no sign of what the
|
|
136
|
+
dollar is for.
|
|
137
|
+
- Show the payer the transaction record: `createTransactionDetails(...)` rows,
|
|
138
|
+
collapsed behind a caret, on the live checkout AND on the receipt. A payment
|
|
139
|
+
hash and a deposit txid are the only evidence a payer has that they paid you.
|
|
140
|
+
`<openreceive-checkout>` / React's `<Checkout>` already render this panel and
|
|
141
|
+
the `description` — these two rules cost you code only on a custom UI or your
|
|
142
|
+
own receipt page, never a reason to replace the drop-in. (It returns no rows
|
|
143
|
+
while the rail is `checkout_lock` — before the payer has chosen anything
|
|
144
|
+
there is no transaction — so render the caret only when the rows are
|
|
145
|
+
non-empty.)
|
|
146
|
+
- HTTP JSON is snake_case; the browser packages' TypeScript APIs are camelCase.
|
|
147
|
+
- Money is integers or decimal strings — never binary floats.
|
|
148
|
+
|
|
149
|
+
## Your tables, not ours
|
|
150
|
+
|
|
151
|
+
The install migration adds `openreceive_payments` and `openreceive_meta` to THIS
|
|
152
|
+
application's database. That is the whole persistence OpenReceive needs. It does
|
|
153
|
+
not replace your orders, users, or products, and you do not join them.
|
|
154
|
+
|
|
155
|
+
- **Find this app's models first.** They may be named `Order`, `Invoice`,
|
|
156
|
+
`Booking`, `Product`, `Variant`, `User`, `Account` — anything. Wire the hooks
|
|
157
|
+
to those. Do not generate a parallel `ShopOrder` / `ShopProduct` / `ShopUser`
|
|
158
|
+
stack.
|
|
159
|
+
- **The payable row's id is the `reference`.** Create it before checkout, keep
|
|
160
|
+
it across retries, never reuse it. Pass that id to `<openreceive-checkout>`. A
|
|
161
|
+
fresh id per page load lets one order be paid twice.
|
|
162
|
+
- **Products (or the catalog) are the price authority.** Order creation reads
|
|
163
|
+
live prices into the order (snapshot line items if this app has them).
|
|
164
|
+
`config.amount_for` reads only that order — never a payer-supplied amount,
|
|
165
|
+
never a live catalog lookup that could re-price a cart already placed. Return
|
|
166
|
+
`{ currency:, value: }` as a decimal STRING, plus a `description` of what they
|
|
167
|
+
are buying.
|
|
168
|
+
- **Users own the order; OpenReceive never sees them.** `config.authorize` uses
|
|
169
|
+
the same ownership check this app already uses on the order show / pay page —
|
|
170
|
+
`session[:user_id]`, Devise's `current_user`, a signed cookie, whatever it is.
|
|
171
|
+
`context[:resource][:reference]` is a claim the payer sent, not proof.
|
|
172
|
+
- **The order is unpaid or paid.** Do not copy `pending` / `expired` / `failed`
|
|
173
|
+
/ `attention` onto it. Those are attempt statuses on `openreceive_payments`. An
|
|
174
|
+
expired invoice does not cancel the order; a later checkout may mint another
|
|
175
|
+
attempt. The engine refuses a new checkout under a reference that already
|
|
176
|
+
settled (409).
|
|
177
|
+
- **Do not associate `OpenReceivePayment`.** No `has_many`, no `belongs_to`, no
|
|
178
|
+
foreign key either direction. `reference` is not unique (many attempts per
|
|
179
|
+
order). Fulfillment is a guarded transition on YOUR order row inside
|
|
180
|
+
`config.on_paid` — `UPDATE … WHERE state = awaiting_payment` (or this app's
|
|
181
|
+
equivalent). Database writes only in the hook; emails, jobs, and broadcasts
|
|
182
|
+
after commit.
|
|
183
|
+
|
|
184
|
+
## If you build your own checkout UI
|
|
185
|
+
|
|
186
|
+
The engine serves JSON only, so the view is yours — but the drop-ins
|
|
187
|
+
(`<openreceive-checkout>`, React's `<Checkout>`) already obey all of this. This
|
|
188
|
+
list is the short form of https://openreceive.org/guides/checkout-ux.md, for a UI
|
|
189
|
+
built on `@openreceive/browser/headless`. Read that before writing components.
|
|
190
|
+
|
|
191
|
+
- `createCheckoutController` is the engine. Do not hand-roll a poll loop.
|
|
192
|
+
- `createCheckoutStatusModel` for the status line. Do not draw a
|
|
193
|
+
Cart → Pay → Done stepper. Read the model's `phase`, not the snapshot's.
|
|
194
|
+
- `resolveWizardSelection` decides whether to ask "which network?". A
|
|
195
|
+
one-network asset starts the swap from the tile. Key `selectedAssetByGroup`
|
|
196
|
+
by group (`USDT`), valued by `pay_in_asset` (`USDT_TRON`).
|
|
197
|
+
- `createMethodGridDisplay` for tiles, including `limitMessage` so an
|
|
198
|
+
unavailable method says the minimum in the payer's currency.
|
|
199
|
+
- `createSwapDisplayModel` → `display.copyRows` for deposits: address, memo,
|
|
200
|
+
and the bare amount each get a copy row. Render `swap.networkWarning*` as
|
|
201
|
+
the model gives it.
|
|
202
|
+
- `createCheckoutSession` owns mint and swap start. To start swaps, pass its
|
|
203
|
+
`swap` option (`selection`, `prefix`, `fetch`) together. Without it
|
|
204
|
+
`startSwap` reports through `onError`.
|
|
205
|
+
- `createQrSvg` is async. Use `createQrSvgController` so you do not render
|
|
206
|
+
`[object Promise]`.
|
|
207
|
+
- `checkoutLabels` for every payer-facing string. Only write copy it lacks.
|
|
208
|
+
- `stageSwapRefund` then `confirmSwapRefund` — only the second submits.
|
|
209
|
+
Validate with `getSwapRefundFormError`. Treat `409` as a normal outcome.
|
|
210
|
+
- Pass `{ resumable: true }` to `createSwapDisplayModel` when the payer has
|
|
211
|
+
a URL they can come back to, and render `display.refundReturnLabel`.
|
|
212
|
+
Resume helpers (`createGuestCheckoutResume`, `createGuestOrderFetcher`)
|
|
213
|
+
are on `@openreceive/browser`, not `/headless`.
|
|
214
|
+
- A refund replaces the deposit panel. On `refund_required` also drop
|
|
215
|
+
"switch payment method".
|
|
216
|
+
- No "Open wallet" button on desktop.
|
|
217
|
+
- Wallet suggestions: `getPaymentWizardRoutes()` +
|
|
218
|
+
`createWizardRouteDisplays`. Lightning only. Host the icons with
|
|
219
|
+
`asset-base-url`. The registry answers ~37 wallets: pass
|
|
220
|
+
`providerPreviewLimit` and build "show all" from `display.providerCount`,
|
|
221
|
+
or they push the QR off the screen.
|
|
222
|
+
|
|
223
|
+
## More documentation
|
|
224
|
+
|
|
225
|
+
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
|
|
226
|
+
enough; drop the `.md` for the same page a person would read.
|
|
227
|
+
|
|
228
|
+
- https://openreceive.org/guides/authorization.md — before you write `config.authorize`
|
|
229
|
+
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
|
|
230
|
+
- https://openreceive.org/guides/storage.md — the engine tables and the attempt state machine
|
|
231
|
+
- https://openreceive.org/guides/frontend-checkout.md — the drop-in's props, attributes and slots
|
|
232
|
+
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
|
|
233
|
+
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
|
|
234
|
+
- https://openreceive.org/guides/provider-registry.md — where the packaged icons and pay
|
|
235
|
+
tutorials come from, and how to serve them. The asset rule is the one a custom
|
|
236
|
+
UI is most likely to get wrong; this is the page that owns it, not the summary
|
|
237
|
+
in checkout-ux.md
|
|
238
|
+
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
|
|
239
|
+
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
|
|
240
|
+
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
|
|
241
|
+
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
|
|
242
|
+
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
|
|
243
|
+
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
|
|
244
|
+
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
|
|
245
|
+
- https://openreceive.org/guides/api-reference.md — every route, option and error code
|
|
246
|
+
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the mounted engine's routes with your own
|
|
247
|
+
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
|
|
248
|
+
- https://openreceive.org/guides.md — the index, if what you need is not above
|
|
249
|
+
|
|
250
|
+
Questions, or a problem with the library itself:
|
|
251
|
+
https://openreceive.org/contact
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## The quickstart, in full
|
|
256
|
+
|
|
257
|
+
Inlined verbatim so this file needs no network access — follow it once Step 0
|
|
258
|
+
passes. The page it comes from is https://openreceive.org/guides/quickstart-rails.
|
|
259
|
+
|
|
260
|
+
## Rails quickstart
|
|
261
|
+
|
|
262
|
+
Requires Ruby ≥ 3.2.
|
|
263
|
+
|
|
264
|
+
Add the Rails engine gem to your `Gemfile`:
|
|
265
|
+
|
|
266
|
+
```ruby
|
|
267
|
+
gem "openreceive-rails"
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
That is the whole install: `openreceive-rails` depends on `openreceive`,
|
|
271
|
+
`openreceive-server` and `nwc-ruby`, so the default wallet client — built from
|
|
272
|
+
`NWC_URI` — works with nothing else added. Hosts that bring their own NWC
|
|
273
|
+
client set `config.nwc_client` instead.
|
|
274
|
+
|
|
275
|
+
One native prerequisite: `nwc-ruby`'s `rbsecp256k1` builds libsecp256k1 from
|
|
276
|
+
source, so minimal images (`ruby:3.3-slim`, fresh Docker builds) need the
|
|
277
|
+
autotools or `bundle install` dies at `autoreconf: not found`. Before
|
|
278
|
+
bundling:
|
|
279
|
+
|
|
280
|
+
```sh
|
|
281
|
+
apt-get install -y autoconf automake libtool build-essential pkg-config
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Full Ruby images and typical developer machines already have these.
|
|
285
|
+
|
|
286
|
+
Then run:
|
|
287
|
+
|
|
288
|
+
```sh
|
|
289
|
+
bin/rails generate openreceive:install
|
|
290
|
+
bin/rails db:migrate
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
`openreceive:install` emits one migration for both engine tables, the
|
|
294
|
+
initializer, and the engine mount. The migration adapts to the app's configured
|
|
295
|
+
database adapter — PostgreSQL, SQLite, and MySQL (`mysql2`/`trilogy`) are
|
|
296
|
+
supported.
|
|
297
|
+
→ [openreceive:install](https://openreceive.org/guides/api-reference.md#openreceiveinstall)
|
|
298
|
+
|
|
299
|
+
The generator emits three things:
|
|
300
|
+
|
|
301
|
+
- `db/migrate/*_create_openreceive_tables.rb` — one migration creating both
|
|
302
|
+
engine tables (`openreceive_payments` and `openreceive_meta`);
|
|
303
|
+
- a simplified `config/initializers/openreceive.rb`;
|
|
304
|
+
- the `OpenReceive::Engine` route mount at `/openreceive`.
|
|
305
|
+
|
|
306
|
+
The `OpenReceivePayment` model is engine-owned — no model file is generated.
|
|
307
|
+
The engine owns the table's commit locking, write-once settlement, and
|
|
308
|
+
reconciliation state machine. `reference` is indexed but not unique (a
|
|
309
|
+
reference may have many historical attempts); `payment_hash` is globally unique.
|
|
310
|
+
|
|
311
|
+
#### Fulfill exactly once
|
|
312
|
+
|
|
313
|
+
Within OpenReceive's own settlement paths, `on_paid` runs at most once per
|
|
314
|
+
reference: a second payment to a second invoice is recorded with
|
|
315
|
+
`status_reason = "duplicate_settlement"` and never fulfills again.
|
|
316
|
+
|
|
317
|
+
The one thing you own: **if anything other than OpenReceive can also fulfill
|
|
318
|
+
an order** — an admin action, a second payment processor, a replayed job —
|
|
319
|
+
those paths race each other, and `on_paid` must be idempotent. The generated
|
|
320
|
+
initializer spells this out and shows the guarded transition:
|
|
321
|
+
|
|
322
|
+
```ruby
|
|
323
|
+
config.on_paid = lambda do |settlement|
|
|
324
|
+
claimed = Order
|
|
325
|
+
.where(id: settlement.reference, state: "awaiting_payment")
|
|
326
|
+
.update_all(state: "paid", paid_at: Time.at(settlement.paid_at).utc)
|
|
327
|
+
next if claimed.zero? # someone else already fulfilled it
|
|
328
|
+
|
|
329
|
+
# FulfillOrder — like Order — is your own application code: ship the goods,
|
|
330
|
+
# enqueue the confirmation email. OpenReceive provides neither.
|
|
331
|
+
FulfillOrder.call(Order.find(settlement.reference), payment_hash: settlement.payment_hash)
|
|
332
|
+
end
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Delivery is at-least-once: `on_paid` runs inside the settlement transaction,
|
|
336
|
+
and a raise rolls it back for the next pass to retry. Keep it to database
|
|
337
|
+
writes on the order — an email or webhook sent from here would survive the
|
|
338
|
+
rollback and go out again. The `state: "paid"` transition above is the flag;
|
|
339
|
+
let your own job drain it after commit.
|
|
340
|
+
|
|
341
|
+
**`update_all` fires no Active Record callbacks.** That is the point — it is one
|
|
342
|
+
conditional `UPDATE`, so the claim is atomic and there is no model code between
|
|
343
|
+
the check and the write. It also means there is no `after_commit` to hang a
|
|
344
|
+
post-commit side effect on, which is fine for a background job draining the flag
|
|
345
|
+
and useless for a page that wants to know *now*. If you push settlement over
|
|
346
|
+
Action Cable, or your model owns the transition through callbacks, take a row
|
|
347
|
+
lock for the duration instead:
|
|
348
|
+
|
|
349
|
+
```ruby
|
|
350
|
+
config.on_paid = lambda do |settlement|
|
|
351
|
+
order = Order.lock.find_by(id: settlement.reference) # SELECT … FOR UPDATE
|
|
352
|
+
next unless order && order.state == "awaiting_payment"
|
|
353
|
+
order.update!(state: "paid", paid_at: Time.at(settlement.paid_at).utc) # callbacks fire
|
|
354
|
+
end
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Both shapes are idempotent, and both are correct. They differ only in whether
|
|
358
|
+
your model layer gets to run: `update_all` skips it and is the right default;
|
|
359
|
+
the row lock holds the row for the duration of the block and is what you want
|
|
360
|
+
when the transition has to go through your model. The generated fulfillment note
|
|
361
|
+
says the same thing — if your fulfillment is a read-modify-write that cannot be
|
|
362
|
+
expressed as one conditional `UPDATE`, take the lock.
|
|
363
|
+
|
|
364
|
+
Either way the rule above still holds: whatever the callback does must be
|
|
365
|
+
database writes on the order. `after_commit` on the settlement transaction runs
|
|
366
|
+
after OpenReceive's own commit, so an email enqueued there is as safe as one
|
|
367
|
+
enqueued from a job draining the flag — and an email sent *inline* from
|
|
368
|
+
`on_paid` is not, in either shape.
|
|
369
|
+
|
|
370
|
+
A runnable illustration of this boundary — not a template to copy models from —
|
|
371
|
+
is Buy a Button
|
|
372
|
+
(`examples/buttons/server/rails`).
|
|
373
|
+
It has products, visitors, and orders, with the three hooks as the only bridge.
|
|
374
|
+
Map that shape onto the models in THIS app.
|
|
375
|
+
|
|
376
|
+
Supply the receive-only wallet connection as `ENV["NWC_URI"]`. Never put it in
|
|
377
|
+
browser code, logs, or assets. Your application refuses to start when the code
|
|
378
|
+
advertises spend methods such as `pay_invoice`; the explicit override is
|
|
379
|
+
`config.allow_spend_capable_wallet = true` or
|
|
380
|
+
`OPENRECEIVE_ALLOW_SPEND_CAPABLE_NWC=true` ([Security](https://openreceive.org/guides/security.md)).
|
|
381
|
+
|
|
382
|
+
OpenReceive reads `ENV`; Rails does not load a `.env` file on its own.
|
|
383
|
+
`dotenv-rails`, an exported shell environment, or your production secret
|
|
384
|
+
manager has to put the values there first.
|
|
385
|
+
→ [Environment variables](https://openreceive.org/guides/environment-variables.md).
|
|
386
|
+
|
|
387
|
+
### Configure the host hooks
|
|
388
|
+
|
|
389
|
+
The initializer needs three things: authorization, the trusted price, and
|
|
390
|
+
fulfillment. All three receive the `reference` — a string you choose, and the
|
|
391
|
+
fulfillment identity: your order id, one per thing you fulfill, created before
|
|
392
|
+
checkout, kept across retries, never reused. OpenReceive never looks inside
|
|
393
|
+
it, but `on_paid` runs once per reference, a new checkout under a reference
|
|
394
|
+
that already settled is refused with 409, and a fresh id per page load lets
|
|
395
|
+
one order be paid twice.
|
|
396
|
+
|
|
397
|
+
```ruby
|
|
398
|
+
OpenReceive.configure do |config|
|
|
399
|
+
# `Order` throughout is YOUR model — it could be named anything. OpenReceive
|
|
400
|
+
# never sees it or touches its table; these hooks are the only bridge
|
|
401
|
+
# between the engine and your data.
|
|
402
|
+
#
|
|
403
|
+
# Your policy, called before every checkout/payment/swap request. `context`
|
|
404
|
+
# is a Hash with three symbol keys:
|
|
405
|
+
# context[:action] — which route: "checkout.prepare", "checkout.create",
|
|
406
|
+
# "payment.check", "swap.quote", "swap.create",
|
|
407
|
+
# "swap.read", or "swap.refund"
|
|
408
|
+
# context[:request] — the ActionDispatch::Request; read your session,
|
|
409
|
+
# cookies, or headers from it, as in a controller
|
|
410
|
+
# context[:resource] — { reference:, payment_hash: } copied from the
|
|
411
|
+
# payer's JSON body. It names an order; it does not
|
|
412
|
+
# prove this caller owns it.
|
|
413
|
+
# Return true to allow, false for a 403. Here: only the signed-in customer
|
|
414
|
+
# who placed the order may act on it.
|
|
415
|
+
config.authorize = lambda do |context|
|
|
416
|
+
order = Order.find_by(id: context[:resource][:reference])
|
|
417
|
+
order && order.user_id == context[:request].session[:user_id]
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# The price for a reference — here, your order id — from your own data;
|
|
421
|
+
# nil when there is nothing to pay for (a 404). `value` is a decimal STRING
|
|
422
|
+
# from the order row, never a float and never a request param. `description`
|
|
423
|
+
# is what the payer is buying, in your own words.
|
|
424
|
+
config.amount_for = lambda do |reference|
|
|
425
|
+
order = Order.find_by(id: reference)
|
|
426
|
+
order && { currency: "USD", value: order.total.to_s,
|
|
427
|
+
description: "#{order.line_items.size} items" }
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Runs inside the settlement transaction, only for the order's first settled
|
|
431
|
+
# attempt. The WHERE clause is the lock: a second fulfillment path of yours
|
|
432
|
+
# (admin action, replayed job) updates zero rows and does nothing. Plain
|
|
433
|
+
# ActiveRecord, because the engine WRAPS this block in the transaction.
|
|
434
|
+
# (The JS engine instead hands onPaid a `query` handle, since nothing wraps
|
|
435
|
+
# it there; that is the one shape difference between the two stacks.)
|
|
436
|
+
config.on_paid = lambda do |settlement|
|
|
437
|
+
claimed = Order
|
|
438
|
+
.where(id: settlement.reference, state: "awaiting_payment")
|
|
439
|
+
.update_all(state: "paid", paid_at: Time.at(settlement.paid_at).utc)
|
|
440
|
+
next if claimed.zero?
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
`OpenReceive.configure` sets the three host hooks; `on_paid` runs inside the
|
|
446
|
+
settlement transaction, only for the first settled attempt for a reference.
|
|
447
|
+
→ [OpenReceive.configure](https://openreceive.org/guides/api-reference.md#openreceiveconfigure)
|
|
448
|
+
|
|
449
|
+
The engine inherits your application's `protect_from_forgery`. Keep
|
|
450
|
+
`csrf_meta_tags` in the layout that renders the checkout; the checkout client
|
|
451
|
+
sends `X-CSRF-Token` from it automatically.
|
|
452
|
+
|
|
453
|
+
The generated initializer ships
|
|
454
|
+
`config.on_paid = OpenReceive::LOGGING_ON_PAID` — a placeholder that only logs
|
|
455
|
+
the settlement and fulfills nothing. Replace it with your real fulfillment (as
|
|
456
|
+
above); the engine warns every time your application boots while the
|
|
457
|
+
placeholder is still configured, because orders would otherwise be recorded as settled without ever
|
|
458
|
+
being fulfilled.
|
|
459
|
+
|
|
460
|
+
The amount always comes from your own order record; payer-supplied amounts are
|
|
461
|
+
rejected. Advanced hooks (`resolve_checkout`, `on_checkout_created`) remain as
|
|
462
|
+
overrides for custom-repository applications and are not part of the quickstart.
|
|
463
|
+
|
|
464
|
+
For public web shops, opt into the per-IP invoice cap with
|
|
465
|
+
`config.rate_limiting = true`; leave it off (the default) when many payers
|
|
466
|
+
share one IP. → [Rate limiting](https://openreceive.org/guides/rate-limiting.md#rails)
|
|
467
|
+
|
|
468
|
+
In production the engine builds the wallet client — and runs its receive-only
|
|
469
|
+
preflight — eagerly when your application boots, so a missing `NWC_URI`, a
|
|
470
|
+
dead relay, or a spend-capable wallet stops the deploy instead of surfacing as
|
|
471
|
+
customer-facing 500s on the first checkout. Outside production (tests,
|
|
472
|
+
consoles) the client is built lazily so no live wallet is needed.
|
|
473
|
+
|
|
474
|
+
### Render the checkout
|
|
475
|
+
|
|
476
|
+
The engine serves JSON checkout routes only — rendering is your view. Any
|
|
477
|
+
OpenReceive frontend package works against the `/openreceive` mount; the
|
|
478
|
+
smallest is the custom element (its default `prefix` is already
|
|
479
|
+
`/openreceive`, and the package ships a self-contained `styles.css` a plain
|
|
480
|
+
stylesheet link can serve):
|
|
481
|
+
|
|
482
|
+
```erb
|
|
483
|
+
<%# app/views/orders/pay.html.erb %>
|
|
484
|
+
<openreceive-checkout reference="<%= @order.id %>"></openreceive-checkout>
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
```js
|
|
488
|
+
// In your JS bundle (importmap/esbuild/webpacker):
|
|
489
|
+
import { defineElements } from "@openreceive/elements";
|
|
490
|
+
import "@openreceive/elements/styles.css"; // or link the compiled styles.css
|
|
491
|
+
|
|
492
|
+
// Registers the <openreceive-checkout> tag with the browser. Without this,
|
|
493
|
+
// the tag in the ERB above is unknown markup and renders as nothing; with it,
|
|
494
|
+
// the element wakes up wherever the tag appears. Call once per page — order
|
|
495
|
+
// relative to the markup does not matter.
|
|
496
|
+
defineElements();
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
Bundling with esbuild (jsbundling-rails)? Two things:
|
|
500
|
+
|
|
501
|
+
1. Build ESM and load it as a module. esbuild's default IIFE output evaluates
|
|
502
|
+
a dependency's Node fallback in the browser and throws
|
|
503
|
+
`ReferenceError: __filename is not defined`:
|
|
504
|
+
|
|
505
|
+
```sh
|
|
506
|
+
esbuild app/javascript/application.js --bundle --format=esm --outdir=app/assets/builds
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
```erb
|
|
510
|
+
<%= javascript_include_tag "application", type: "module" %>
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
2. Serve the payment-method icons. They are files shipped in
|
|
514
|
+
`@openreceive/browser` and `@openreceive/provider-data` — not in
|
|
515
|
+
`@openreceive/elements` — and only Vite-style bundlers resolve them from
|
|
516
|
+
the import. Merge both packages' `dist/assets` trees into
|
|
517
|
+
`public/openreceive-assets/assets/` and set
|
|
518
|
+
`asset-base-url="/openreceive-assets"` on the element
|
|
519
|
+
([Provider registry](https://openreceive.org/guides/provider-registry.md#assets-are-files-your-host-serves)).
|
|
520
|
+
|
|
521
|
+
The element creates the checkout for `reference`, then renders and polls
|
|
522
|
+
itself. React/Vue/Svelte/Angular apps use the matching wrapper package
|
|
523
|
+
instead — same props and defaults ([Frontend checkout](https://openreceive.org/guides/frontend-checkout.md)).
|
|
524
|
+
Build a custom checkout only if this app cannot use a drop-in; then
|
|
525
|
+
`@openreceive/browser/headless` is the API
|
|
526
|
+
([Headless checkout](https://openreceive.org/guides/headless-checkout.md)).
|
|
527
|
+
|
|
528
|
+
### Reconciliation
|
|
529
|
+
|
|
530
|
+
Settlement runs on the request path. You do not need a cron job. Disable or
|
|
531
|
+
tune it with `config.opportunistic_reconcile` (`false`, or
|
|
532
|
+
`{ min_interval_seconds: … }`).
|
|
533
|
+
|
|
534
|
+
Optionally, run one worker so settlement does not wait for the next page
|
|
535
|
+
load:
|
|
536
|
+
|
|
537
|
+
```sh
|
|
538
|
+
bin/rails openreceive:notifications
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
→ [rake openreceive:notifications](https://openreceive.org/guides/api-reference.md#rake-openreceivenotifications)
|
|
542
|
+
|
|
543
|
+
`OpenReceive.reconcile!` and `bin/rails openreceive:reconcile` are one-shot
|
|
544
|
+
primitives if you want to drive a pass yourself.
|
|
545
|
+
→ [OpenReceive.reconcile!](https://openreceive.org/guides/api-reference.md#openreceivereconcile)
|
|
546
|
+
|
|
547
|
+
### Swap secrets
|
|
548
|
+
|
|
549
|
+
The Ruby server recognizes `LSC_URI_PRIMARY` and `LSC_URI_BACKUP` using the
|
|
550
|
+
shared [Lightning Swap Connect](https://openreceive.org/guides/lightning-swap-connect.md) vectors: setting either one
|
|
551
|
+
auto-builds the matching provider, so an app that wants swaps only supplies the
|
|
552
|
+
connection strings ([Environment variables](https://openreceive.org/guides/environment-variables.md)).
|
|
553
|
+
`config.swap_providers` is the override knob — pass your own adapters to
|
|
554
|
+
replace the auto-built set, or an empty array to disable swaps.
|
|
555
|
+
|
|
556
|
+
One `openreceive_payments` row holds at most one provider order in its
|
|
557
|
+
server-only `swap_data`. The engine filters `swap_data` from Active Record
|
|
558
|
+
inspection and ordinary serialization. Do not explicitly serialize it, log it,
|
|
559
|
+
or return it from your own API; it may contain a provider credential.
|
|
560
|
+
|
|
561
|
+
**Setting either connection string commits you to refunds.** A swap deposit can
|
|
562
|
+
arrive short or late, which leaves it `refund_required` at the provider with
|
|
563
|
+
only your UI able to claim it — and the payer claims it on a second visit,
|
|
564
|
+
after leaving your page for an address in another wallet. That needs a
|
|
565
|
+
per-order URL your app serves, a route that restores the order behind it, and
|
|
566
|
+
something that restores the ATTEMPT, since `/checkouts/prepare` returns none.
|
|
567
|
+
[Swap refunds](https://openreceive.org/guides/swap-refunds.md) is the whole of it; read it before you set
|
|
568
|
+
`LSC_URI_PRIMARY`.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: openreceive-server
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- OpenReceive
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - '='
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.3.
|
|
18
|
+
version: 0.3.3
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - '='
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.3.
|
|
25
|
+
version: 0.3.3
|
|
26
26
|
description: 'Server building blocks for OpenReceive: a storage-free Service that
|
|
27
27
|
mirrors the Node engine and a framework-agnostic Rack app implementing the shipped
|
|
28
28
|
HTTP routes while the host owns order and payment persistence. Receive-only: it
|
|
@@ -53,6 +53,10 @@ files:
|
|
|
53
53
|
- lib/openreceive/server/swap/weight_budget.rb
|
|
54
54
|
- lib/openreceive/server/version.rb
|
|
55
55
|
- lib/openreceive/server/wallet_info.rb
|
|
56
|
+
- skills/debug-openreceive-payment/SKILL.md
|
|
57
|
+
- skills/integrate-openreceive/SKILL.md
|
|
58
|
+
- skills/integrate-openreceive/references/node.md
|
|
59
|
+
- skills/integrate-openreceive/references/rails.md
|
|
56
60
|
homepage: https://openreceive.org
|
|
57
61
|
licenses:
|
|
58
62
|
- MIT
|