@moneydevkit/nextjs 0.14.0 → 0.15.0
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 +41 -3
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -401,15 +401,53 @@ async function callPaidEndpoint(url: string, payFn: (invoice: string) => Promise
|
|
|
401
401
|
}
|
|
402
402
|
```
|
|
403
403
|
|
|
404
|
+
### Deferred settlement
|
|
405
|
+
|
|
406
|
+
By default, `withPayment` marks the credential as used immediately before your handler runs. If your handler fails after the credential is consumed, the payer can't retry.
|
|
407
|
+
|
|
408
|
+
Use `withDeferredSettlement` when the service delivery might fail and you want the payer to be able to retry. Your handler receives a `settle()` callback - call it only after you've successfully delivered the service:
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
// app/api/ai/route.ts
|
|
412
|
+
import { withDeferredSettlement } from '@moneydevkit/nextjs/server'
|
|
413
|
+
|
|
414
|
+
const handler = async (req: Request, settle: () => Promise<SettleResult>) => {
|
|
415
|
+
const { prompt } = await req.json()
|
|
416
|
+
|
|
417
|
+
// Do the expensive work first
|
|
418
|
+
const result = await runAiInference(prompt)
|
|
419
|
+
|
|
420
|
+
// Work succeeded - now mark the credential as used
|
|
421
|
+
const { settled } = await settle()
|
|
422
|
+
if (!settled) {
|
|
423
|
+
return Response.json({ error: 'settlement_failed' }, { status: 500 })
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return Response.json({ result })
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export const POST = withDeferredSettlement(
|
|
430
|
+
{ amount: 100, currency: 'SAT' },
|
|
431
|
+
handler,
|
|
432
|
+
)
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
If your handler returns without calling `settle()` (e.g. it throws or the service fails), the credential stays valid and the payer can retry with the same macaroon and preimage.
|
|
436
|
+
|
|
437
|
+
`settle()` is callable only once per request. A second call returns `{ settled: false, error: 'already_settled' }` without hitting the backend.
|
|
438
|
+
|
|
404
439
|
### Error codes
|
|
405
440
|
|
|
406
441
|
| Status | Code | Meaning |
|
|
407
442
|
|--------|------|---------|
|
|
408
|
-
| 402 | `payment_required` | No
|
|
409
|
-
| 401 | `invalid_credential` | Credential is malformed
|
|
443
|
+
| 402 | `payment_required` | No credentials provided - pay the returned invoice |
|
|
444
|
+
| 401 | `invalid_credential` | Credential is malformed, has a bad signature, or the L402 header is garbled |
|
|
410
445
|
| 401 | `invalid_payment_proof` | Preimage does not match the payment hash |
|
|
446
|
+
| 401 | `credential_consumed` | Credential has already been used |
|
|
411
447
|
| 403 | `resource_mismatch` | Credential was issued for a different endpoint |
|
|
412
448
|
| 403 | `amount_mismatch` | Credential was issued for a different price |
|
|
413
449
|
| 500 | `configuration_error` | `MDK_ACCESS_TOKEN` is not set |
|
|
414
450
|
| 500 | `pricing_error` | Dynamic pricing function threw an error |
|
|
415
|
-
| 502 | `checkout_creation_failed` | Failed to create the checkout or invoice |
|
|
451
|
+
| 502 | `checkout_creation_failed` | Failed to create the checkout or invoice |
|
|
452
|
+
|
|
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.
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createCheckoutUrl } from '@moneydevkit/core/route';
|
|
2
2
|
export type { CreateCheckoutUrlOptions } from '@moneydevkit/core/route';
|
|
3
|
-
export { withPayment } from '@moneydevkit/core/mdk402';
|
|
4
|
-
export type { PaymentConfig } from '@moneydevkit/core/mdk402';
|
|
3
|
+
export { withPayment, withDeferredSettlement } from '@moneydevkit/core/mdk402';
|
|
4
|
+
export type { PaymentConfig, SettleResult } from '@moneydevkit/core/mdk402';
|
package/dist/server/index.js
CHANGED
package/dist/server/index.js.map
CHANGED
|
@@ -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,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;AAG3D,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.
|
|
3
|
+
"version": "0.15.0",
|
|
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.
|
|
67
|
-
"@moneydevkit/core": "0.
|
|
66
|
+
"@moneydevkit/api-contract": "0.1.23",
|
|
67
|
+
"@moneydevkit/core": "0.15.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@types/node": "^20.10.5",
|