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,658 @@
|
|
|
1
|
+
# OpenReceive agent directions (Laravel)
|
|
2
|
+
|
|
3
|
+
These directions describe OpenReceive 0.4.4.
|
|
4
|
+
|
|
5
|
+
Add OpenReceive to a Laravel application — the app you are already working in.
|
|
6
|
+
You do not need a copy of the OpenReceive source: the package is on Packagist
|
|
7
|
+
(`openreceive/laravel`), the frontend packages are 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 package and the routes its service
|
|
10
|
+
provider mounts — 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`, `ShopUser`, an encrypted-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
|
+
Keep this application's view layer, its authentication (Breeze, Fortify,
|
|
18
|
+
Jetstream, Sanctum, a plain session guard — whatever `Auth` already uses), its
|
|
19
|
+
Eloquent models and its database. Pick the frontend package that matches what
|
|
20
|
+
already renders here (`@openreceive/elements` for Blade/Livewire/Inertia-less
|
|
21
|
+
pages; `/react`, `/vue`, `/svelte` or `/angular` for an existing SPA or Inertia
|
|
22
|
+
app) — do not add React to a Blade app. Reuse the app's existing
|
|
23
|
+
`$request->user()` or session in `Host::authorize`; the engine's migration adds
|
|
24
|
+
only its own two tables to the app's database, through `php artisan migrate`.
|
|
25
|
+
|
|
26
|
+
## What OpenReceive is
|
|
27
|
+
|
|
28
|
+
A payment library that runs inside YOUR server. It mounts HTTP routes in the
|
|
29
|
+
application you are editing, issues Lightning invoices against a wallet the
|
|
30
|
+
merchant already controls, and calls back into your code when one settles. There
|
|
31
|
+
is no OpenReceive account and no API key, and OpenReceive never holds the funds —
|
|
32
|
+
the sats land in the wallet the merchant connected.
|
|
33
|
+
|
|
34
|
+
The one required credential is a receive-only NWC code (Nostr Wallet Connect):
|
|
35
|
+
a string from the merchant's wallet that can create invoices and read their
|
|
36
|
+
status, and cannot spend. A swap provider (an "LSC" code) optionally lets the
|
|
37
|
+
payer send USDT, USDC, ETH or SOL instead, converted into that same
|
|
38
|
+
Lightning payment. You supply those credentials and three hooks — `authorize`,
|
|
39
|
+
`amountFor`, `onPaid` on one `App\OpenReceive\Host` class;
|
|
40
|
+
OpenReceive supplies invoices, polling, settlement and the checkout UI. It never
|
|
41
|
+
owns orders, users, prices, or fulfillment.
|
|
42
|
+
|
|
43
|
+
## Step 0 — check the environment before you write code
|
|
44
|
+
|
|
45
|
+
Do this before installing the package or editing files.
|
|
46
|
+
|
|
47
|
+
1. Look for `NWC_URI` in this app's server environment — `.env`, the deploy
|
|
48
|
+
config, Forge/Vapor/Envoyer environment panels, whatever this app already
|
|
49
|
+
uses. If the app runs in a container the value is in none of those: ask the
|
|
50
|
+
running process (`docker exec <container> printenv NWC_URI`), because finding
|
|
51
|
+
the NAME in a compose file or `.env.example` proves nothing about the value.
|
|
52
|
+
Never print or echo the value itself; only report whether it is set. Check
|
|
53
|
+
for `LSC_URI_PRIMARY` in the same pass. Remember `php artisan config:cache`:
|
|
54
|
+
a cached config captured the values at cache time, and editing `.env`
|
|
55
|
+
afterwards changes nothing until the cache is rebuilt.
|
|
56
|
+
|
|
57
|
+
If OpenReceive is already installed here, `php artisan openreceive:doctor`
|
|
58
|
+
answers this whole step in one command — every credential as set/unset, the
|
|
59
|
+
host class and which hooks are still placeholders, the route mount, and the
|
|
60
|
+
wallet preflight. It never prints a value.
|
|
61
|
+
2. If BOTH are already set — the common case in an existing app — say so and go
|
|
62
|
+
straight to the quickstart. Steps 3 and 4 are for an environment that is
|
|
63
|
+
missing one; do not stop to ask about altcoins that are already configured.
|
|
64
|
+
If only `NWC_URI` is set, Bitcoin already works: continue, and raise the
|
|
65
|
+
altcoin question at step 4 rather than blocking on it.
|
|
66
|
+
3. If `NWC_URI` is missing or empty, stop and tell the user exactly what to
|
|
67
|
+
create:
|
|
68
|
+
|
|
69
|
+
> OpenReceive cannot issue an invoice without a receive-only NWC code. Get
|
|
70
|
+
> one at https://openreceive.org/get_a_nwc_code_to_receive_payments, then
|
|
71
|
+
> put `NWC_URI=<the code>` in this app's server environment — for most apps
|
|
72
|
+
> that is a `.env` file in the project root — and tell me when it's set.
|
|
73
|
+
|
|
74
|
+
Wait for the user before wiring OpenReceive; do not invent a placeholder
|
|
75
|
+
value. Waiting is not idleness: you may write `.env.example` with the
|
|
76
|
+
variable NAMES only (`NWC_URI=`, `LSC_URI_PRIMARY=`) so the merchant has a
|
|
77
|
+
file to copy, and keep building the parts of the host that do not touch
|
|
78
|
+
OpenReceive — the order model, the cart, the routes. The stop guards the
|
|
79
|
+
credential, not the rest of the app.
|
|
80
|
+
4. If `LSC_URI_PRIMARY` was not already set, ask the user: "Do you want to
|
|
81
|
+
accept altcoins and stablecoins (USDT, USDC, ETH, SOL) as well as
|
|
82
|
+
Bitcoin?"
|
|
83
|
+
|
|
84
|
+
- Yes → send them to https://openreceive.org/set_up_swap_provider for a
|
|
85
|
+
swap-provider (LSC) code, to set as `LSC_URI_PRIMARY` in the same server
|
|
86
|
+
environment. Do NOT wait for it: no application code reads the value, so
|
|
87
|
+
the integration is identical with or without it — the engine picks it up
|
|
88
|
+
from the environment and swaps switch on. What a yes DOES change is the
|
|
89
|
+
refund route back (the swap non-negotiable below): build it as part of
|
|
90
|
+
this integration, not when the code arrives.
|
|
91
|
+
- No → skip it. Bitcoin over Lightning works with `NWC_URI` alone, and you
|
|
92
|
+
can add a swap provider later without changing application code.
|
|
93
|
+
5. Check the environment again and confirm `NWC_URI` is present.
|
|
94
|
+
`LSC_URI_PRIMARY` may land later; swaps stay off until it does, and no code
|
|
95
|
+
changes when it arrives.
|
|
96
|
+
6. If OpenReceive is ALREADY installed here, check the installed versions of
|
|
97
|
+
`openreceive/laravel` (`composer show openreceive/laravel`) and
|
|
98
|
+
`@openreceive/browser` against the release named at the top of this file.
|
|
99
|
+
The headless display models below do not exist in older versions, and the
|
|
100
|
+
first tile click throws with nothing saying why. Upgrade first — and if this
|
|
101
|
+
app runs in containers, rebuild the images: `vendor/` is baked into the
|
|
102
|
+
image, so an in-place `composer update` is undone by the next `compose up`.
|
|
103
|
+
|
|
104
|
+
Only then start the quickstart.
|
|
105
|
+
|
|
106
|
+
## Non-negotiables
|
|
107
|
+
|
|
108
|
+
The quickstart below has the code. These are the rules it cannot state for
|
|
109
|
+
itself, and they hold for every integration.
|
|
110
|
+
|
|
111
|
+
- OpenReceive never owns orders, users, prices, or fulfillment. The section
|
|
112
|
+
below is how those tables sit next to the engine — not a second order model,
|
|
113
|
+
and not an Eloquent model over `openreceive_payments`.
|
|
114
|
+
- Keep `NWC_URI` / `LSC_URI_*` server-only. Never put them in browser code,
|
|
115
|
+
logs, or assets.
|
|
116
|
+
- The host owns the price. `amountFor` reads it from your own data; reject
|
|
117
|
+
payer-supplied amounts.
|
|
118
|
+
- `authorize` runs on every request, and the `resource` it receives is a CLAIM
|
|
119
|
+
the payer made, not proof. Read the Illuminate request it is handed — the
|
|
120
|
+
session, `$request->user()`; never trust a body field. `openreceive:install`
|
|
121
|
+
scaffolds `use AllowAllAuthorize;`, a placeholder trait that allows
|
|
122
|
+
everything (the engine warns at boot while it is there) — replace it with
|
|
123
|
+
this app's real ownership check, same as `onPaid`.
|
|
124
|
+
- `onPaid` must be idempotent. It runs once per `reference` — your order
|
|
125
|
+
id, one per thing you fulfill, created before checkout, kept across retries,
|
|
126
|
+
never reused. A fresh id per page load lets one order be paid twice.
|
|
127
|
+
- Receive-only NWC is required; a spend-capable code fails closed at boot unless
|
|
128
|
+
explicitly overridden.
|
|
129
|
+
- There is NO merchant-initiated refund of a settled Lightning payment, because
|
|
130
|
+
the wallet cannot spend. Swap refunds — a payer reclaiming a deposit that
|
|
131
|
+
never converted — are the only refund OpenReceive performs, and only from the
|
|
132
|
+
`refund_required` provider state. Do not build, promise, or imply a Lightning
|
|
133
|
+
refund path.
|
|
134
|
+
- IF YOU TURN SWAPS ON, BUILD THE ROUTE BACK. A deposit that arrives short or
|
|
135
|
+
late becomes `refund_required`, and the payer claims it on a SECOND VISIT,
|
|
136
|
+
after leaving your page to fetch an address from another wallet. Three things
|
|
137
|
+
must exist or that money is unreachable through your UI: a per-order URL your
|
|
138
|
+
server serves (`/checkout/:reference` — `syncUrl` on the drop-ins), your own
|
|
139
|
+
order-summary route to restore the order from, and the ATTEMPT.
|
|
140
|
+
`/checkouts/prepare` returns no attempts, so a checkout rebuilt from the
|
|
141
|
+
reference alone opens on the method grid. Re-picking the same coin
|
|
142
|
+
(`POST /swaps`) re-serves the committed attempt — but only while it is live,
|
|
143
|
+
and the shadow invoice behind a swap lasts about half an hour, after which the
|
|
144
|
+
same click mints a NEW deposit address and the refund is off-screen. Keep the
|
|
145
|
+
`payment_hash` and reopen the attempt with `POST /swaps/status`, which has no
|
|
146
|
+
such window. https://openreceive.org/guides/swap-refunds.md
|
|
147
|
+
- Show the payer WHAT THEY ARE BUYING. Return an optional `description` beside
|
|
148
|
+
the price from `amountFor` and both drop-ins render it above the
|
|
149
|
+
amount. Without it the checkout is a QR and "$1.00" with no sign of what the
|
|
150
|
+
dollar is for.
|
|
151
|
+
- Show the payer the transaction record: `createTransactionDetails(...)` rows,
|
|
152
|
+
collapsed behind a caret, on the live checkout AND on the receipt. A payment
|
|
153
|
+
hash and a deposit txid are the only evidence a payer has that they paid you.
|
|
154
|
+
`<openreceive-checkout>` / React's `<Checkout>` already render this panel and
|
|
155
|
+
the `description` — these two rules cost you code only on a custom UI or your
|
|
156
|
+
own receipt page, never a reason to replace the drop-in. (It returns no rows
|
|
157
|
+
while the rail is `checkout_lock` — before the payer has chosen anything
|
|
158
|
+
there is no transaction — so render the caret only when the rows are
|
|
159
|
+
non-empty.)
|
|
160
|
+
- HTTP JSON is snake_case; the browser packages' TypeScript APIs are camelCase.
|
|
161
|
+
- Money is integers or decimal strings — never binary floats.
|
|
162
|
+
|
|
163
|
+
## Your tables, not ours
|
|
164
|
+
|
|
165
|
+
The install migration adds `openreceive_payments` and `openreceive_meta` to THIS
|
|
166
|
+
application's database. That is the whole persistence OpenReceive needs. It does
|
|
167
|
+
not replace your orders, users, or products, and you do not join them.
|
|
168
|
+
|
|
169
|
+
- **Find this app's models first.** They may be named `Order`, `Invoice`,
|
|
170
|
+
`Booking`, `Product`, `Variant`, `User`, `Account` — anything. Wire the hooks
|
|
171
|
+
to those. Do not generate a parallel `ShopOrder` / `ShopProduct` / `ShopUser`
|
|
172
|
+
stack.
|
|
173
|
+
- **The payable row's id is the `reference`.** Create it before checkout, keep
|
|
174
|
+
it across retries, never reuse it. Pass that id to `<openreceive-checkout>`. A
|
|
175
|
+
fresh id per page load lets one order be paid twice.
|
|
176
|
+
- **Products (or the catalog) are the price authority.** Order creation reads
|
|
177
|
+
live prices into the order (snapshot line items if this app has them).
|
|
178
|
+
`amountFor` reads only that order — never a payer-supplied amount, never a
|
|
179
|
+
live catalog lookup that could re-price a cart already placed. Return
|
|
180
|
+
`['currency' => …, 'value' => …]` with `value` a decimal STRING, plus a
|
|
181
|
+
`description` of what they are buying.
|
|
182
|
+
- **Users own the order; OpenReceive never sees them.** `authorize` uses the
|
|
183
|
+
same ownership check this app already uses on the order show / pay page —
|
|
184
|
+
`$request->user()`, a policy, a session key, an encrypted cookie, whatever
|
|
185
|
+
it is. `$context->reference()` is a claim the payer sent, not proof.
|
|
186
|
+
- **The order is unpaid or paid.** Do not copy `pending` / `expired` / `failed`
|
|
187
|
+
/ `attention` onto it. Those are attempt statuses on `openreceive_payments`. An
|
|
188
|
+
expired invoice does not cancel the order; a later checkout may mint another
|
|
189
|
+
attempt. The engine refuses a new checkout under a reference that already
|
|
190
|
+
settled (409).
|
|
191
|
+
- **Do not model `openreceive_payments`.** No Eloquent model, no `hasMany`, no
|
|
192
|
+
`belongsTo`, no foreign key either direction. `reference` is not unique (many
|
|
193
|
+
attempts per order). Fulfillment is a guarded transition on YOUR order row
|
|
194
|
+
inside `onPaid` — `Order::where(...)->where('state', 'awaiting_payment')->update(...)`
|
|
195
|
+
(or this app's equivalent), with no `DB::transaction()` of your own around it.
|
|
196
|
+
Database writes only in the hook; emails, jobs, and broadcasts after commit
|
|
197
|
+
(implement `OpenReceive\Hosts\AfterPaid` for those).
|
|
198
|
+
|
|
199
|
+
## If you build your own checkout UI
|
|
200
|
+
|
|
201
|
+
The engine serves JSON only, so the view is yours — but the drop-ins
|
|
202
|
+
(`<openreceive-checkout>`, React's `<Checkout>`) already obey all of this. This
|
|
203
|
+
list is the short form of https://openreceive.org/guides/checkout-ux.md, for a UI
|
|
204
|
+
built on `@openreceive/browser/headless`. Read that before writing components.
|
|
205
|
+
|
|
206
|
+
- `createCheckoutController` is the engine. Do not hand-roll a poll loop.
|
|
207
|
+
- `createCheckoutStatusModel` for the status line. Do not draw a
|
|
208
|
+
Cart → Pay → Done stepper. Read the model's `phase`, not the snapshot's.
|
|
209
|
+
- `resolveWizardSelection` decides whether to ask "which network?". A
|
|
210
|
+
one-network asset starts the swap from the tile. Key `selectedAssetByGroup`
|
|
211
|
+
by group (`USDT`), valued by `pay_in_asset` (`USDT_TRON`).
|
|
212
|
+
- `createMethodGridDisplay` for tiles, including `limitMessage` so an
|
|
213
|
+
unavailable method says the minimum in the payer's currency.
|
|
214
|
+
- `createSwapDisplayModel` → `display.copyRows` for deposits: address, memo,
|
|
215
|
+
and the bare amount each get a copy row. Render `swap.networkWarning*` as
|
|
216
|
+
the model gives it.
|
|
217
|
+
- `createCheckoutSession` owns mint and swap start. To start swaps, pass its
|
|
218
|
+
`swap` option (`selection`, `prefix`, `fetch`) together. Without it
|
|
219
|
+
`startSwap` reports through `onError`.
|
|
220
|
+
- `createQrSvg` is async. Use `createQrSvgController` so you do not render
|
|
221
|
+
`[object Promise]`.
|
|
222
|
+
- `checkoutLabels` for every payer-facing string. Only write copy it lacks.
|
|
223
|
+
- `stageSwapRefund` then `confirmSwapRefund` — only the second submits.
|
|
224
|
+
Validate with `getSwapRefundFormError`. Treat `409` as a normal outcome.
|
|
225
|
+
- Pass `{ resumable: true }` to `createSwapDisplayModel` when the payer has
|
|
226
|
+
a URL they can come back to, and render `display.refundReturnLabel`.
|
|
227
|
+
Resume helpers (`createGuestCheckoutResume`, `createGuestOrderFetcher`)
|
|
228
|
+
are on `@openreceive/browser`, not `/headless`.
|
|
229
|
+
- A refund replaces the deposit panel. On `refund_required` also drop
|
|
230
|
+
"switch payment method".
|
|
231
|
+
- No "Open wallet" button on desktop.
|
|
232
|
+
- Wallet suggestions: `getPaymentWizardRoutes()` +
|
|
233
|
+
`createWizardRouteDisplays`. Lightning only. Every image ships inside
|
|
234
|
+
the JavaScript — logos as data URIs, tutorials once `loadPayTutorialImages()`
|
|
235
|
+
resolves (`image` is `undefined` until then) — so serve nothing and set no
|
|
236
|
+
asset option. When it works, the logos and payment icons render; a missing
|
|
237
|
+
image means a CSP `img-src` that blocks `data:`, and the console names it.
|
|
238
|
+
The registry answers ~37 wallets: pass
|
|
239
|
+
`providerPreviewLimit` and build "show all" from `display.providerCount`,
|
|
240
|
+
or they push the QR off the screen.
|
|
241
|
+
|
|
242
|
+
## More documentation
|
|
243
|
+
|
|
244
|
+
Fetch one when the moment comes. Each is raw markdown, so a plain GET is
|
|
245
|
+
enough; drop the `.md` for the same page a person would read.
|
|
246
|
+
|
|
247
|
+
- https://openreceive.org/guides/authorization.md — before you write `authorize`
|
|
248
|
+
- https://openreceive.org/guides/environment-variables.md — every variable, and what is deliberately not one
|
|
249
|
+
- https://openreceive.org/guides/storage.md — the engine tables and the attempt state machine
|
|
250
|
+
- https://openreceive.org/guides/frontend-checkout.md — the drop-in's props, attributes and slots
|
|
251
|
+
- https://openreceive.org/guides/checkout-ux.md — read before building any custom UI
|
|
252
|
+
- https://openreceive.org/guides/headless-checkout.md — the controller, the display models, refunds
|
|
253
|
+
- https://openreceive.org/guides/provider-registry.md — where the wallet logos and pay
|
|
254
|
+
tutorials come from: inside the JavaScript, nothing to serve. This is the page
|
|
255
|
+
that owns the image rule, not the summary in checkout-ux.md
|
|
256
|
+
- https://openreceive.org/guides/automated-swaps.md — only if `LSC_URI_PRIMARY` is set
|
|
257
|
+
- https://openreceive.org/guides/swap-refunds.md — the refund flow, and the route back to it. Read it before you turn swaps on
|
|
258
|
+
- https://openreceive.org/guides/lightning-swap-connect.md — what an `LSC_URI_*` code actually is
|
|
259
|
+
- https://openreceive.org/guides/price-feeds.md — where the fiat→sats rate comes from, and how to replace it
|
|
260
|
+
- https://openreceive.org/guides/host-testing.md — testing your three hooks without a live wallet or provider
|
|
261
|
+
- https://openreceive.org/guides/rate-limiting.md — before a public shop goes live
|
|
262
|
+
- https://openreceive.org/guides/security.md and https://openreceive.org/guides/deploying.md — before this goes anywhere real
|
|
263
|
+
- https://openreceive.org/guides/api-reference.md — every route, option and error code
|
|
264
|
+
- https://openreceive.org/guides/custom-checkout-route.md — advanced: replacing the mounted engine's routes with your own
|
|
265
|
+
- https://openreceive.org/guides/react-material-ui-recipe.md — a worked custom UI on a component library
|
|
266
|
+
- https://openreceive.org/guides.md — the index, if what you need is not above
|
|
267
|
+
|
|
268
|
+
Questions, or a problem with the library itself:
|
|
269
|
+
https://openreceive.org/contact
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## The quickstart, in full
|
|
274
|
+
|
|
275
|
+
Inlined verbatim so this file needs no network access — follow it once Step 0
|
|
276
|
+
passes. The page it comes from is https://openreceive.org/guides/quickstart-laravel.
|
|
277
|
+
|
|
278
|
+
## Laravel quickstart
|
|
279
|
+
|
|
280
|
+
Requires PHP ≥ 8.2 (64-bit) and Laravel ≥ 11 (12 current). Extensions:
|
|
281
|
+
`ext-gmp` is REQUIRED — the NWC transport's elliptic-curve math depends on it
|
|
282
|
+
— plus `sodium`, `mbstring` and the `pdo_*` driver for your database
|
|
283
|
+
(`pdo_pgsql`, `pdo_mysql` or `pdo_sqlite`). `php -m` lists what your build
|
|
284
|
+
has; on Debian/Ubuntu it is `apt-get install php8.2-gmp`, in the official
|
|
285
|
+
Docker image `docker-php-ext-install gmp`.
|
|
286
|
+
|
|
287
|
+
Add the Laravel package:
|
|
288
|
+
|
|
289
|
+
```sh
|
|
290
|
+
composer require openreceive/laravel
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
That is the whole install: `openreceive/laravel` depends on
|
|
294
|
+
`openreceive/openreceive`, the engine, so the default wallet client — built from
|
|
295
|
+
`NWC_URI` — works with nothing else added, and package discovery registers the
|
|
296
|
+
service provider. Hosts that bring their own NWC client bind
|
|
297
|
+
`OpenReceive\Nwc\ReceiveNwcClient` in the container instead.
|
|
298
|
+
|
|
299
|
+
Then run:
|
|
300
|
+
|
|
301
|
+
```sh
|
|
302
|
+
php artisan openreceive:install
|
|
303
|
+
php artisan migrate
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
`openreceive:install` writes three files:
|
|
307
|
+
|
|
308
|
+
- `config/openreceive.php` — the settings. The host hook is a CLASS NAME, not
|
|
309
|
+
a closure, because `php artisan config:cache` serializes this file and a
|
|
310
|
+
closure anywhere in it fails the cache;
|
|
311
|
+
- `app/OpenReceive/Host.php` — the three hooks (`authorize`, `amountFor`,
|
|
312
|
+
`onPaid`), with the two generated placeholders wired and the fulfillment
|
|
313
|
+
note as comments;
|
|
314
|
+
- `database/migrations/*_create_openreceive_tables.php` — one migration for
|
|
315
|
+
both engine tables (`openreceive_payments` and `openreceive_meta`). Its DDL
|
|
316
|
+
comes from the engine's `PaymentsSchema::statements()` for your connection's
|
|
317
|
+
driver — PostgreSQL, MySQL/MariaDB and SQLite — and `php artisan migrate` runs
|
|
318
|
+
it like any migration of your own; there is no second runner.
|
|
319
|
+
|
|
320
|
+
→ [OpenReceive\Storage](https://openreceive.org/guides/api-reference.md#openreceivestorage)
|
|
321
|
+
|
|
322
|
+
There is no Eloquent model for the engine's tables, and you do not write one.
|
|
323
|
+
The engine owns the table's commit locking, write-once settlement, and
|
|
324
|
+
reconciliation state machine. `reference` is indexed but not unique (a
|
|
325
|
+
reference may have many historical attempts); `payment_hash` is globally unique.
|
|
326
|
+
|
|
327
|
+
#### Fulfill exactly once
|
|
328
|
+
|
|
329
|
+
Within OpenReceive's own settlement paths, `onPaid` runs at most once per
|
|
330
|
+
reference: a second payment to a second invoice is recorded with
|
|
331
|
+
`status_reason = "duplicate_settlement"` and never fulfills again.
|
|
332
|
+
|
|
333
|
+
The one thing you own: **if anything other than OpenReceive can also fulfill
|
|
334
|
+
an order** — an admin action, a second payment processor, a replayed job —
|
|
335
|
+
those paths race each other, and `onPaid` must be idempotent. The generated
|
|
336
|
+
`Host.php` spells this out and shows the guarded transition:
|
|
337
|
+
|
|
338
|
+
```php
|
|
339
|
+
public function onPaid(PaymentSettlement $settlement): void
|
|
340
|
+
{
|
|
341
|
+
$claimed = Order::where('id', $settlement->reference)
|
|
342
|
+
->where('state', 'awaiting_payment')
|
|
343
|
+
->update(['state' => 'paid', 'paid_at' => Carbon::createFromTimestampUTC($settlement->paidAt)]);
|
|
344
|
+
if ($claimed === 0) {
|
|
345
|
+
return; // someone else already fulfilled it
|
|
346
|
+
}
|
|
347
|
+
// The 'paid' state IS the flag. Ship the goods and send the mail from a job
|
|
348
|
+
// that drains it after commit, or from afterPaid() below — never from here.
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Delivery is at-least-once: `onPaid` runs inside the settlement transaction,
|
|
353
|
+
and an exception rolls it back for the next pass to retry. Keep it to database
|
|
354
|
+
writes on the order — an email or webhook sent from here would survive the
|
|
355
|
+
rollback and go out again. The `'paid'` transition above is the flag; let your
|
|
356
|
+
own job drain it after commit, or implement `OpenReceive\Hosts\AfterPaid`:
|
|
357
|
+
its `afterPaid(PaymentSettlement $settlement)` runs after COMMIT, best-effort,
|
|
358
|
+
for the same first settled attempt — the place for `FulfillOrder::dispatch(…)`
|
|
359
|
+
or an event. And open no `DB::transaction()` of your own inside `onPaid`: the
|
|
360
|
+
engine already holds the transaction on this connection, and PDO refuses a
|
|
361
|
+
nested `BEGIN`.
|
|
362
|
+
|
|
363
|
+
**A query-builder `update()` fires no model events.** That is the point — it is
|
|
364
|
+
one conditional `UPDATE`, so the claim is atomic and there is no model code
|
|
365
|
+
between the check and the write. It also means no observer, no `saved` event
|
|
366
|
+
and no `Model::updated` listener runs, which is fine for a job draining the
|
|
367
|
+
flag and useless for a model that owns the transition through events. If yours
|
|
368
|
+
does, take a row lock for the duration instead:
|
|
369
|
+
|
|
370
|
+
```php
|
|
371
|
+
public function onPaid(PaymentSettlement $settlement): void
|
|
372
|
+
{
|
|
373
|
+
$order = Order::lockForUpdate()->find($settlement->reference); // SELECT … FOR UPDATE
|
|
374
|
+
if ($order === null || $order->state !== 'awaiting_payment') {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
$order->update(['state' => 'paid', 'paid_at' => Carbon::createFromTimestampUTC($settlement->paidAt)]); // events fire
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
**Unlocking a download works the same way.** If what the payer bought is a
|
|
382
|
+
file, do not unlock it in the browser: gate the download route on the paid
|
|
383
|
+
order row — `Order::where('id', $id)->where('user_id', $request->user()->id)->where('state', 'paid')->firstOrFail()`
|
|
384
|
+
or a 404 — and serve the file only then. The `'paid'` written above is the
|
|
385
|
+
unlock; the client never decides an order was fulfilled, it re-reads the row.
|
|
386
|
+
Buy a Button's `ShopController::download` is this in twenty lines.
|
|
387
|
+
|
|
388
|
+
Both shapes are idempotent, and both are correct. They differ only in whether
|
|
389
|
+
your model layer gets to run: the query-builder `update()` skips it and is the
|
|
390
|
+
right default; the row lock holds the row for the duration of the method and
|
|
391
|
+
is what you want when the transition has to go through your model. The
|
|
392
|
+
generated fulfillment note says the same thing — if your fulfillment is a
|
|
393
|
+
read-modify-write that cannot be expressed as one conditional `UPDATE`, take
|
|
394
|
+
the lock.
|
|
395
|
+
|
|
396
|
+
A runnable illustration of this boundary — not a template to copy models from —
|
|
397
|
+
is Buy a Button
|
|
398
|
+
(`examples/buttons/server/laravel`).
|
|
399
|
+
It has products, visitors, and orders, with the three hooks as the only bridge.
|
|
400
|
+
Map that shape onto the models in THIS app.
|
|
401
|
+
|
|
402
|
+
### Add wallet credentials
|
|
403
|
+
|
|
404
|
+
Laravel loads `.env` itself, and `config/openreceive.php` reads the values
|
|
405
|
+
through `env()`:
|
|
406
|
+
|
|
407
|
+
```dotenv
|
|
408
|
+
NWC_URI=
|
|
409
|
+
LSC_URI_PRIMARY=
|
|
410
|
+
LSC_URI_BACKUP=
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
1. Get a receive-only NWC code from a compatible wallet
|
|
414
|
+
([get one here](https://openreceive.org/get_a_nwc_code_to_receive_payments))
|
|
415
|
+
→ `NWC_URI`.
|
|
416
|
+
2. Optionally set up a [swap provider](https://openreceive.org/set_up_swap_provider)
|
|
417
|
+
→ `LSC_URI_PRIMARY` (and `LSC_URI_BACKUP` if you have one).
|
|
418
|
+
|
|
419
|
+
Never put these values in browser code. Your application refuses to start if
|
|
420
|
+
the NWC code also advertises spend methods such as `pay_invoice`; mint a
|
|
421
|
+
receive-only code ([Security](https://openreceive.org/guides/security.md)).
|
|
422
|
+
|
|
423
|
+
Under `php artisan config:cache` Laravel never reads `.env` at runtime — the
|
|
424
|
+
values are captured when you cache, as for every other Laravel secret — so
|
|
425
|
+
re-run `config:cache` after changing one. The engine redacts connection
|
|
426
|
+
strings in every log line, and `php artisan openreceive:doctor` prints each
|
|
427
|
+
variable as set/unset only.
|
|
428
|
+
→ [Environment variables](https://openreceive.org/guides/environment-variables.md).
|
|
429
|
+
|
|
430
|
+
### Configure the host hooks
|
|
431
|
+
|
|
432
|
+
`app/OpenReceive/Host.php` needs three things: authorization, the trusted
|
|
433
|
+
price, and fulfillment. All three receive the `reference` — a string you
|
|
434
|
+
choose, and the fulfillment identity: your order id, one per thing you
|
|
435
|
+
fulfill, created before checkout, kept across retries, never reused.
|
|
436
|
+
OpenReceive never looks inside it, but `onPaid` runs once per reference, a new
|
|
437
|
+
checkout under a reference that already settled is refused with 409, and a
|
|
438
|
+
fresh id per page load lets one order be paid twice.
|
|
439
|
+
|
|
440
|
+
```php
|
|
441
|
+
<?php
|
|
442
|
+
|
|
443
|
+
namespace App\OpenReceive;
|
|
444
|
+
|
|
445
|
+
use App\Models\Order; // YOUR model — it could be named anything.
|
|
446
|
+
use Carbon\Carbon;
|
|
447
|
+
use OpenReceive\Host as OpenReceiveHost;
|
|
448
|
+
use OpenReceive\PaymentSettlement;
|
|
449
|
+
use OpenReceive\Server\AuthorizeContext;
|
|
450
|
+
|
|
451
|
+
final class Host implements OpenReceiveHost
|
|
452
|
+
{
|
|
453
|
+
// Your policy, called before every checkout/payment/swap request.
|
|
454
|
+
// $context->action — "checkout.prepare", "checkout.create", "payment.check",
|
|
455
|
+
// "swap.quote", "swap.create", "swap.read" or "swap.refund"
|
|
456
|
+
// $context->request — the Illuminate\Http\Request: read the session, the
|
|
457
|
+
// authenticated user, cookies or headers, as in a controller
|
|
458
|
+
// $context->resource — ['reference' => …, 'payment_hash' => ?] copied from the
|
|
459
|
+
// payer's JSON body. It names an order; it does not prove
|
|
460
|
+
// this caller owns it. reference is always a validated
|
|
461
|
+
// non-empty string (≤200 chars); payment_hash is null except
|
|
462
|
+
// on payment.check / swap.read / swap.refund.
|
|
463
|
+
// Return true to allow, false for a 403. Here: only the signed-in customer
|
|
464
|
+
// who placed the order may act on it.
|
|
465
|
+
public function authorize(AuthorizeContext $context): bool
|
|
466
|
+
{
|
|
467
|
+
$order = Order::find($context->reference());
|
|
468
|
+
return $order !== null && $order->user_id === $context->request->user()?->id;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// The price for a reference — here, your order id — from your own data; null
|
|
472
|
+
// when there is nothing to pay for (a 404). `value` is a decimal STRING from
|
|
473
|
+
// the order row, never a float and never a request param. `description` is
|
|
474
|
+
// what the payer is buying, in your own words.
|
|
475
|
+
public function amountFor(string $reference): ?array
|
|
476
|
+
{
|
|
477
|
+
$order = Order::find($reference);
|
|
478
|
+
return $order === null ? null : [
|
|
479
|
+
'currency' => 'USD',
|
|
480
|
+
'value' => $order->total, // a decimal string column
|
|
481
|
+
'description' => $order->items()->count().' items',
|
|
482
|
+
];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// Runs inside the settlement transaction, only for the order's first settled
|
|
486
|
+
// attempt. The WHERE clause is the lock: a second fulfillment path of yours
|
|
487
|
+
// (admin action, replayed job) updates zero rows and does nothing. Plain
|
|
488
|
+
// Eloquent on the default connection, because the engine holds the
|
|
489
|
+
// transaction on that connection's PDO — the query joins it.
|
|
490
|
+
public function onPaid(PaymentSettlement $settlement): void
|
|
491
|
+
{
|
|
492
|
+
Order::where('id', $settlement->reference)
|
|
493
|
+
->where('state', 'awaiting_payment')
|
|
494
|
+
->update(['state' => 'paid', 'paid_at' => Carbon::createFromTimestampUTC($settlement->paidAt)]);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
`config/openreceive.php` names that class (`'host' => App\OpenReceive\Host::class`)
|
|
500
|
+
and the provider resolves it from the container, so it may take constructor
|
|
501
|
+
dependencies. `onPaid` runs inside the settlement transaction, only for the
|
|
502
|
+
first settled attempt for a reference.
|
|
503
|
+
→ [OpenReceive\Host](https://openreceive.org/guides/api-reference.md#openreceivehost) ·
|
|
504
|
+
[the authorize context](https://openreceive.org/guides/api-reference.md#the-authorize-context-php)
|
|
505
|
+
|
|
506
|
+
The routes mount under `config('openreceive.route_prefix')` (`openreceive`) and
|
|
507
|
+
`config('openreceive.middleware')` (`['web']`). The `web` group is how the
|
|
508
|
+
engine picks up your application's session and CSRF protection: Laravel's
|
|
509
|
+
`VerifyCsrfToken` reads `X-CSRF-TOKEN`, and the checkout client sends it from
|
|
510
|
+
`<meta name="csrf-token" content="{{ csrf_token() }}">` automatically — keep
|
|
511
|
+
that tag in the layout that renders the checkout. The same group brings every
|
|
512
|
+
middleware it carries; a global `auth` redirect on the `web` group would send
|
|
513
|
+
the engine's JSON routes to a login page too, so keep such guards on your own
|
|
514
|
+
route groups, not on `web` itself.
|
|
515
|
+
|
|
516
|
+
The generated `Host.php` ships `use LoggingOnPaid;` — a placeholder that only
|
|
517
|
+
logs the settlement and fulfills nothing. Replace it with your real fulfillment
|
|
518
|
+
(as above); the engine warns every time your application boots while the
|
|
519
|
+
placeholder is still there, because orders would otherwise be recorded as
|
|
520
|
+
settled without ever being fulfilled. The same applies to `use
|
|
521
|
+
AllowAllAuthorize;`, the generated allow-all placeholder: it treats
|
|
522
|
+
possession of the reference as authorization, which is safe only while
|
|
523
|
+
references are unguessable, and the engine warns at boot until you replace it
|
|
524
|
+
with your own ownership check (as above). Replace both, not just `onPaid`.
|
|
525
|
+
|
|
526
|
+
The amount always comes from your own order record; payer-supplied amounts are
|
|
527
|
+
rejected. The engine's `PaymentRepository` interface remains the escape hatch
|
|
528
|
+
for custom-storage applications and is not part of the quickstart.
|
|
529
|
+
|
|
530
|
+
For public web shops, opt into the per-IP invoice cap with
|
|
531
|
+
`'rate_limiting' => true`; leave it off (the default) when many payers share
|
|
532
|
+
one IP. The client IP is `$request->ip()`, so Laravel's `TrustProxies` decides
|
|
533
|
+
who the payer is behind a proxy. → [Rate limiting](https://openreceive.org/guides/rate-limiting.md)
|
|
534
|
+
|
|
535
|
+
In production the engine builds the wallet client — and runs its receive-only
|
|
536
|
+
preflight — eagerly when your application boots, so a missing `NWC_URI`, a
|
|
537
|
+
dead relay, or a spend-capable wallet stops the deploy instead of surfacing as
|
|
538
|
+
customer-facing 500s on the first checkout. Outside production (tests,
|
|
539
|
+
consoles) the client is built lazily so no live wallet is needed.
|
|
540
|
+
|
|
541
|
+
PHP builds the whole engine again on every request, so that same preflight
|
|
542
|
+
would run per request under PHP-FPM or Apache; the package remembers the
|
|
543
|
+
wallet's info event in your default cache store for
|
|
544
|
+
`config('openreceive.wallet_info_cache_seconds')` (600) so a checkout request
|
|
545
|
+
costs one relay round trip, not two.
|
|
546
|
+
|
|
547
|
+
### Render the checkout
|
|
548
|
+
|
|
549
|
+
The engine serves JSON checkout routes only — rendering is your view. Any
|
|
550
|
+
OpenReceive frontend package works against the `/openreceive` mount; the
|
|
551
|
+
smallest is the custom element (its default `prefix` is already
|
|
552
|
+
`/openreceive`, and the package ships a self-contained `styles.css`; it is
|
|
553
|
+
scoped to what OpenReceive renders, so it sits safely next to any CSS
|
|
554
|
+
framework in any order). Laravel ships Vite, so the package installs like any
|
|
555
|
+
other frontend dependency:
|
|
556
|
+
|
|
557
|
+
```sh
|
|
558
|
+
npm install @openreceive/elements
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
```js
|
|
562
|
+
// resources/js/app.js — in the entry @vite() already loads
|
|
563
|
+
import { defineElements } from "@openreceive/elements";
|
|
564
|
+
import "@openreceive/elements/styles.css";
|
|
565
|
+
|
|
566
|
+
// Registers the <openreceive-checkout> tag with the browser. Without this,
|
|
567
|
+
// the tag in the Blade below is unknown markup and renders as nothing; with
|
|
568
|
+
// it, the element wakes up wherever the tag appears. Call once per page —
|
|
569
|
+
// order relative to the markup does not matter.
|
|
570
|
+
defineElements();
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
```blade
|
|
574
|
+
{{-- resources/views/orders/pay.blade.php --}}
|
|
575
|
+
<openreceive-checkout reference="{{ $order->id }}"></openreceive-checkout>
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
The element creates the checkout for `reference`, then renders and polls
|
|
579
|
+
itself. React/Vue/Svelte/Angular apps use the matching wrapper package instead
|
|
580
|
+
— same props and defaults ([Frontend checkout](https://openreceive.org/guides/frontend-checkout.md)). Build a
|
|
581
|
+
custom checkout only if this app cannot use a drop-in; then
|
|
582
|
+
`@openreceive/browser/headless` is the API
|
|
583
|
+
([Headless checkout](https://openreceive.org/guides/headless-checkout.md)).
|
|
584
|
+
|
|
585
|
+
Everything the checkout draws ships inside the JavaScript: the payment-method
|
|
586
|
+
icons, the wallet logos and the pay tutorials. There is no image file to copy
|
|
587
|
+
or serve and no asset option to set. Deploy your normal JavaScript and CSS
|
|
588
|
+
build output, including any generated JavaScript chunks. Bundlers with code
|
|
589
|
+
splitting can defer tutorial screenshots until first open; single-file builds
|
|
590
|
+
(including the standalone checkout) include them upfront. If your
|
|
591
|
+
Content-Security-Policy has a strict `img-src`, allow `data:`
|
|
592
|
+
([Provider registry](https://openreceive.org/guides/provider-registry.md#assets)).
|
|
593
|
+
|
|
594
|
+
Then open the checkout in a browser, confirm the payment-method icons and
|
|
595
|
+
wallet logos render, and open a wallet's pay tutorial to check its screenshots.
|
|
596
|
+
If an image is missing, inspect the console for CSP violations and the Network
|
|
597
|
+
panel for failed JavaScript chunks. Allow `data:` in `img-src` and deploy the
|
|
598
|
+
complete build output. Do not add image routes, copy package source images, or
|
|
599
|
+
use registry `icon_path` / tutorial `path` keys as browser URLs.
|
|
600
|
+
|
|
601
|
+
**No npm in this project?** Every GitHub release attaches
|
|
602
|
+
`standalone-checkout-<version>.tar.gz`: one self-registering ESM file, its
|
|
603
|
+
stylesheet and a `MANIFEST.json` of hashes. Unpack it into
|
|
604
|
+
`public/openreceive/` and the same element needs two tags:
|
|
605
|
+
|
|
606
|
+
```blade
|
|
607
|
+
<link rel="stylesheet" href="/openreceive/openreceive-checkout.css" />
|
|
608
|
+
<script type="module" src="/openreceive/openreceive-checkout.js"></script>
|
|
609
|
+
|
|
610
|
+
<openreceive-checkout reference="{{ $order->id }}"></openreceive-checkout>
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
`MANIFEST.json` carries the version and a SHA-256 per file; keep the tarball
|
|
614
|
+
version in step with the Composer package.
|
|
615
|
+
|
|
616
|
+
### Reconciliation
|
|
617
|
+
|
|
618
|
+
Settlement runs on the request path. You do not need a cron job. Disable or
|
|
619
|
+
tune it with `'opportunistic_reconcile'` (`false`, or
|
|
620
|
+
`['min_interval_seconds' => …]`).
|
|
621
|
+
|
|
622
|
+
Optionally, run one worker so settlement does not wait for the next page
|
|
623
|
+
load:
|
|
624
|
+
|
|
625
|
+
```sh
|
|
626
|
+
php artisan openreceive:notifications
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
It listens for the wallet's NWC-02 `payment_received` notifications and runs
|
|
630
|
+
a periodic reconcile pass in the same process — one per deployment, not per
|
|
631
|
+
web instance. `php artisan openreceive:reconcile` is the one-shot pass if you
|
|
632
|
+
want to drive one yourself, and `php artisan openreceive:doctor` reports every
|
|
633
|
+
credential as set/unset, the host and its placeholders, the route mount and
|
|
634
|
+
the wallet preflight.
|
|
635
|
+
|
|
636
|
+
### Swap secrets
|
|
637
|
+
|
|
638
|
+
The package recognizes `LSC_URI_PRIMARY` and `LSC_URI_BACKUP` using the
|
|
639
|
+
shared [Lightning Swap Connect](https://openreceive.org/guides/lightning-swap-connect.md) vectors: setting either one
|
|
640
|
+
auto-builds the matching provider, so an app that wants swaps only supplies the
|
|
641
|
+
connection strings ([Environment variables](https://openreceive.org/guides/environment-variables.md)). The
|
|
642
|
+
`openreceive.swap_providers` container binding is the override knob — bind a
|
|
643
|
+
list of your own adapters to replace the auto-built set, or `[]` to disable
|
|
644
|
+
swaps.
|
|
645
|
+
|
|
646
|
+
One `openreceive_payments` row holds at most one provider order in its
|
|
647
|
+
server-only `swap_data`. The engine never returns `swap_data` from its routes
|
|
648
|
+
or writes it to a log. Do not select it into your own API, serialize it, or
|
|
649
|
+
log it; it may contain a provider credential.
|
|
650
|
+
|
|
651
|
+
**Setting either connection string commits you to refunds.** A swap deposit can
|
|
652
|
+
arrive short or late, which leaves it `refund_required` at the provider with
|
|
653
|
+
only your UI able to claim it — and the payer claims it on a second visit,
|
|
654
|
+
after leaving your page for an address in another wallet. That needs a
|
|
655
|
+
per-order URL your app serves, a route that restores the order behind it, and
|
|
656
|
+
something that restores the ATTEMPT, since `/checkouts/prepare` returns none.
|
|
657
|
+
[Swap refunds](https://openreceive.org/guides/swap-refunds.md) is the whole of it; read it before you set
|
|
658
|
+
`LSC_URI_PRIMARY`.
|