@moneydevkit/nextjs 0.16.0-beta.2 → 0.16.0-beta.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.
package/README.md CHANGED
@@ -92,6 +92,151 @@ 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
+
95
240
  ## Customer Data
96
241
  Collect and store customer information with each checkout. Pass `customer` to pre-fill data and `requireCustomerData` to prompt the user for specific fields:
97
242
 
@@ -450,4 +595,4 @@ If your handler returns without calling `settle()` (e.g. it throws or the servic
450
595
  | 500 | `pricing_error` | Dynamic pricing function threw an error |
451
596
  | 502 | `checkout_creation_failed` | Failed to create the checkout or invoice |
452
597
 
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.
598
+ > **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,6 @@
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';
3
5
  export { withPayment, withDeferredSettlement } from '@moneydevkit/core/mdk402';
4
6
  export type { PaymentConfig, SettleResult } from '@moneydevkit/core/mdk402';
@@ -1,3 +1,4 @@
1
1
  export { createCheckoutUrl } from '@moneydevkit/core/route';
2
+ export { programmaticPayout } from '@moneydevkit/core/server';
2
3
  export { withPayment, withDeferredSettlement } from '@moneydevkit/core/mdk402';
3
4
  //# 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;AAG7D,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.2",
3
+ "version": "0.16.0-beta.4",
4
4
  "title": "@moneydevkit/nextjs",
5
5
  "description": "moneydevkit checkout components for Next.js.",
6
6
  "repository": {
@@ -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.2"
66
+ "@moneydevkit/api-contract": "0.1.26",
67
+ "@moneydevkit/core": "0.16.0-beta.4"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@types/node": "^20.10.5",