@moneydevkit/nextjs 0.16.0-beta.1 → 0.16.0-beta.10

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.
package/README.md CHANGED
@@ -92,6 +92,195 @@ export default withMdkCheckout({})
92
92
 
93
93
  You now have a complete Lightning checkout loop: the button creates a session, the dynamic route renders it, and the webhook endpoint signals your Lightning node to claim paid invoices.
94
94
 
95
+ ## Server-side payouts
96
+
97
+ Programmatic payouts let your server send sats out to a Lightning destination (BOLT11 invoice, BOLT12 offer, or LNURL / Lightning address) without any user interaction. They must run from a server function (Server Action, route handler, cron, webhook), and the app must have programmatic payouts enabled in the moneydevkit dashboard.
98
+
99
+ > **Trust:** the destination is whatever your server passes in. There is no end-user confirmation. Always apply your own authorization and business rules first (who is allowed to trigger this, how much, where to).
100
+
101
+ ### Minimal example
102
+
103
+ ```ts
104
+ // app/actions.ts
105
+ 'use server'
106
+
107
+ import { programmaticPayout } from '@moneydevkit/nextjs/server'
108
+
109
+ export async function sendTip(orderId: string) {
110
+ const result = await programmaticPayout({
111
+ amountSats: 10_000,
112
+ destination: 'lnbc...', // or 'satoshi@example.com', or 'lno1...'
113
+ idempotencyKey: orderId, // pass the SAME value if you ever retry
114
+ })
115
+
116
+ if (result.error) {
117
+ // See the next section for how to handle this properly.
118
+ throw new Error(result.error.message)
119
+ }
120
+
121
+ return result.data // { accepted: true, paymentId, paymentHash }
122
+ }
123
+ ```
124
+
125
+ ### About `idempotencyKey`
126
+
127
+ The key is how moneydevkit dedupes retries. If your code (or a cron, or a Vercel retry) fires the same payout twice with the same key, the second call is a no-op instead of a double-pay.
128
+
129
+ - **Do** use a stable id from your own database: `orderId`, `withdrawalId`, `userId + payoutDate`, etc.
130
+ - **Don't** generate a fresh `crypto.randomUUID()` on every call. That defeats the whole point and you can double-pay.
131
+ - It's just a string, any length, your choice.
132
+
133
+ ### Full example with error handling
134
+
135
+ The `result.error` object tells you whether the failure is worth retrying:
136
+
137
+ - `result.error.retryable === true` - the failure was transient (limits, transient routing, fee issues). Retry the same call with the same `idempotencyKey`.
138
+ - `result.error.retryable === false` - retrying won't help. Fix the input or your config.
139
+ - `result.error.retryable === undefined` - the SDK couldn't classify the failure. Treat as not retryable, log and inspect `result.error`.
140
+
141
+ ```ts
142
+ // app/actions.ts
143
+ 'use server'
144
+
145
+ import { programmaticPayout } from '@moneydevkit/nextjs/server'
146
+
147
+ const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
148
+
149
+ export async function sendPayout(orderId: string, destination: string) {
150
+ let attempt = 0
151
+ while (attempt < 3) {
152
+ const result = await programmaticPayout({
153
+ amountSats: 10_000,
154
+ destination,
155
+ idempotencyKey: orderId,
156
+ })
157
+
158
+ if (!result.error) {
159
+ // Note: result.data only confirms the node accepted the payout.
160
+ // The actual Lightning settlement happens asynchronously - listen
161
+ // for paymentSent / paymentFailed webhooks to confirm final outcome.
162
+ return { ok: true as const, paymentId: result.data.paymentId }
163
+ }
164
+
165
+ switch (result.error.reason) {
166
+ case 'app_scoped_api_key_required':
167
+ // The API key in MDK_ACCESS_TOKEN is not attached to a specific app.
168
+ // Copy the API key from your app's page in the Apps dashboard.
169
+ return { ok: false as const, fatal: 'use_the_app_api_key_from_dashboard' }
170
+
171
+ case 'programmatic_payouts_disabled':
172
+ // Toggle is off in the dashboard for this app.
173
+ return { ok: false as const, fatal: 'enable_programmatic_payouts_in_dashboard' }
174
+
175
+ case 'amount_too_large':
176
+ // Per-request cap. Ask the user for a smaller amount or split.
177
+ return { ok: false as const, fatal: 'amount_too_large' }
178
+
179
+ case 'amount_invalid':
180
+ // Non-positive or non-integer sat amount.
181
+ return { ok: false as const, fatal: 'amount_invalid' }
182
+
183
+ case 'daily_limit_exceeded':
184
+ // 24h rolling cap. Surface to the user; don't retry now.
185
+ return { ok: false as const, fatal: 'come_back_tomorrow' }
186
+
187
+ case 'payout_dispatch_failed':
188
+ // Backend dispatch failed (node offline, transient routing, fees).
189
+ // The error message has the specific cause. Safe to retry with the
190
+ // same idempotencyKey.
191
+ await sleep(1_000 * 2 ** attempt)
192
+ attempt++
193
+ continue
194
+
195
+ default:
196
+ // Unknown / unclassified. Don't retry blindly.
197
+ return {
198
+ ok: false as const,
199
+ fatal: 'unknown_error',
200
+ message: result.error.message,
201
+ code: result.error.code,
202
+ }
203
+ }
204
+ }
205
+
206
+ return { ok: false as const, fatal: 'retries_exhausted' }
207
+ }
208
+ ```
209
+
210
+ ### Common gotchas
211
+
212
+ - **Don't call from client code.** `programmaticPayout` checks for `window` and refuses to run in a browser. It only works in Server Actions, route handlers (`app/api/...`), cron jobs, or webhook receivers.
213
+ - **Set `MDK_ACCESS_TOKEN`.** Same env var as the rest of the SDK. If missing, you get `missing_access_token` (not retryable).
214
+ - **Always pass the same `idempotencyKey` on retry.** If you change it, moneydevkit treats it as a new payout and may charge you twice.
215
+
216
+ ### Error reference
217
+
218
+ `result.error.reason` is a short machine-readable string. Use it for branching; use `result.error.message` for logs.
219
+
220
+ | `reason` | `retryable` | What it means |
221
+ |-----------------------------------|-------------|----------------------------------------------------------------------------|
222
+ | `app_scoped_api_key_required` | false | The API key in `MDK_ACCESS_TOKEN` isn't tied to a specific app. Copy the key from the app's page in the Apps dashboard |
223
+ | `programmatic_payouts_disabled` | false | Toggle is off in dashboard for this app |
224
+ | `amount_too_large` | false | Above per-request cap |
225
+ | `amount_invalid` | false | Backend rejected the amount (non-positive or non-integer sats) |
226
+ | `daily_limit_exceeded` | true | 24h rolling cap hit; retry tomorrow |
227
+ | `payout_dispatch_failed` | true | Backend dispatch failed (node offline, transient routing, fee issues). Inspect `error.message` for the specific cause; safe to retry with the same `idempotencyKey` |
228
+ | _(undefined)_ | _(undefined)_ | New / unknown backend code. Log `error.code` and don't retry blindly |
229
+
230
+ Also returned for client-side validation issues (always `retryable: false`):
231
+
232
+ | `code` | When |
233
+ |-----------------------------|------------------------------------------------------------|
234
+ | `server_only` | Called from a browser runtime |
235
+ | `invalid_amount` | `amountSats` is not a positive integer |
236
+ | `invalid_destination` | Empty, too long, or contains control characters |
237
+ | `invalid_idempotency_key` | Empty / missing |
238
+ | `missing_access_token` | `MDK_ACCESS_TOKEN` not set |
239
+
240
+ ## Reading the merchant balance from a Server Action
241
+
242
+ `getBalance()` reads the spendable (outbound) balance of the Lightning node tied to your `MDK_ACCESS_TOKEN`. Same server-only constraints as `programmaticPayout`: the helper refuses to run in a browser and routes through mdk.com over HTTPS, which in turn dials the merchant node over the WS control plane.
243
+
244
+ ```ts
245
+ // app/actions.ts
246
+ 'use server'
247
+
248
+ import { getBalance } from '@moneydevkit/nextjs/server'
249
+
250
+ export async function fetchBalance() {
251
+ const result = await getBalance()
252
+
253
+ if (result.error) {
254
+ // retryable === true: transient (merchant function spinning up).
255
+ // retryable === false: terminal (invalid key, legacy org-level key, banned, or
256
+ // procedure-not-found from a pre-0.1.30 merchant / older mdk.com).
257
+ throw new Error(result.error.message)
258
+ }
259
+
260
+ return result.data.balanceSats // number, in sats
261
+ }
262
+ ```
263
+
264
+ ### Notes
265
+
266
+ - **App-scoped API key required.** Balance is meaningful per-app, not per-org. Legacy org-level keys return `GET_BALANCE_APP_KEY_REQUIRED` (not retryable). Use the API key from the App page in the dashboard.
267
+ - **First call may take a few seconds.** If the merchant function is cold, mdk.com fires a spin-up webhook and waits for the WS to register. Subsequent calls within the function's lifetime are fast.
268
+ - **Server-only.** Same `typeof window` check as `programmaticPayout`. Don't call from client components.
269
+ - **Idempotent.** Safe to retry. Transient errors are flagged `retryable: true`; auth, app-scope, and procedure-not-found errors are `retryable: false`.
270
+
271
+ ### Error reference
272
+
273
+ | `code` | `retryable` | What it means |
274
+ |-----------------------------------|-------------|----------------------------------------------------------------|
275
+ | `server_only` | false | Called from a browser runtime |
276
+ | `missing_access_token` | false | `MDK_ACCESS_TOKEN` not set |
277
+ | `GET_BALANCE_APP_KEY_REQUIRED` | false | Using a legacy org-level key. Copy the key from the App page |
278
+ | `UNAUTHORIZED` / `FORBIDDEN` | false | Invalid API key or banned user |
279
+ | `NOT_FOUND` | false | Procedure missing - pre-0.1.30 merchant SDK or older mdk.com |
280
+ | `BAD_REQUEST` | false | Server rejected the request as malformed |
281
+ | `GET_BALANCE_SPIN_UP_TIMEOUT` | true | Merchant function did not register WS in time. Safe to retry |
282
+ | `get_balance_failed` | true | Network / unclassified error |
283
+
95
284
  ## Customer Data
96
285
  Collect and store customer information with each checkout. Pass `customer` to pre-fill data and `requireCustomerData` to prompt the user for specific fields:
97
286
 
@@ -450,4 +639,4 @@ If your handler returns without calling `settle()` (e.g. it throws or the servic
450
639
  | 500 | `pricing_error` | Dynamic pricing function threw an error |
451
640
  | 502 | `checkout_creation_failed` | Failed to create the checkout or invoice |
452
641
 
453
- > **Note:** A 402 is only returned when no L402/LSAT authorization header is present. If the header is present but malformed or invalid, you get a 401 - not a new invoice.
642
+ > **Note:** A 402 is only returned when no L402/LSAT authorization header is present. If the header is present but malformed or invalid, you get a 401 - not a new invoice.
@@ -1,4 +1,7 @@
1
1
  export { createCheckoutUrl } from '@moneydevkit/core/route';
2
2
  export type { CreateCheckoutUrlOptions } from '@moneydevkit/core/route';
3
+ export { programmaticPayout } from '@moneydevkit/core/server';
4
+ export type { ProgrammaticPayoutOptions } from '@moneydevkit/core/server';
5
+ export { getBalance } from '@moneydevkit/core/server';
3
6
  export { withPayment, withDeferredSettlement } from '@moneydevkit/core/mdk402';
4
7
  export type { PaymentConfig, SettleResult } from '@moneydevkit/core/mdk402';
@@ -1,3 +1,5 @@
1
1
  export { createCheckoutUrl } from '@moneydevkit/core/route';
2
+ export { programmaticPayout } from '@moneydevkit/core/server';
3
+ export { getBalance } from '@moneydevkit/core/server';
2
4
  export { withPayment, withDeferredSettlement } from '@moneydevkit/core/mdk402';
3
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAG3D,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAE3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAErD,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneydevkit/nextjs",
3
- "version": "0.16.0-beta.1",
3
+ "version": "0.16.0-beta.10",
4
4
  "title": "@moneydevkit/nextjs",
5
5
  "description": "moneydevkit checkout components for Next.js.",
6
6
  "repository": {
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@hookform/resolvers": "^5.0.1",
49
- "@moneydevkit/lightning-js": "0.1.81",
49
+ "@moneydevkit/lightning-js": "0.1.82",
50
50
  "@orpc/client": "1.13.6",
51
51
  "@orpc/contract": "1.3.0",
52
52
  "@radix-ui/react-collapsible": "^1.1.11",
@@ -63,8 +63,8 @@
63
63
  "tailwind-merge": "^3.3.0",
64
64
  "vaul": "^1.1.2",
65
65
  "zod": "^3.25.42",
66
- "@moneydevkit/api-contract": "0.1.24",
67
- "@moneydevkit/core": "0.16.0-beta.1"
66
+ "@moneydevkit/api-contract": "0.1.31",
67
+ "@moneydevkit/core": "0.16.0-beta.10"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@types/node": "^20.10.5",