@openfacilitator/sdk 0.5.0 → 0.6.1
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/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +49 -35
- package/dist/index.mjs +49 -35
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -527,12 +527,12 @@ declare function createPaymentContext(settleResponse: {
|
|
|
527
527
|
interface PaymentMiddlewareConfig {
|
|
528
528
|
/** Facilitator instance or URL */
|
|
529
529
|
facilitator: OpenFacilitator | string;
|
|
530
|
-
/** Function to get payment requirements for the request */
|
|
531
|
-
getRequirements: (req: unknown) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
530
|
+
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
531
|
+
getRequirements: (req: unknown) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
532
532
|
/** Optional: Refund protection config (enables auto failure reporting) */
|
|
533
533
|
refundProtection?: RefundProtectionConfig;
|
|
534
534
|
/** Optional: Custom 402 response handler */
|
|
535
|
-
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements) => void | Promise<void>;
|
|
535
|
+
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements[]) => void | Promise<void>;
|
|
536
536
|
}
|
|
537
537
|
/**
|
|
538
538
|
* Create x402 payment middleware that handles verification, settlement, and optional refund protection.
|
|
@@ -580,8 +580,8 @@ declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (req:
|
|
|
580
580
|
interface HonoPaymentConfig {
|
|
581
581
|
/** Facilitator instance or URL */
|
|
582
582
|
facilitator: OpenFacilitator | string;
|
|
583
|
-
/** Function to get payment requirements for the request */
|
|
584
|
-
getRequirements: (c: HonoContext) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
583
|
+
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
584
|
+
getRequirements: (c: HonoContext) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
585
585
|
/** Optional: Refund protection config */
|
|
586
586
|
refundProtection?: RefundProtectionConfig;
|
|
587
587
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -527,12 +527,12 @@ declare function createPaymentContext(settleResponse: {
|
|
|
527
527
|
interface PaymentMiddlewareConfig {
|
|
528
528
|
/** Facilitator instance or URL */
|
|
529
529
|
facilitator: OpenFacilitator | string;
|
|
530
|
-
/** Function to get payment requirements for the request */
|
|
531
|
-
getRequirements: (req: unknown) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
530
|
+
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
531
|
+
getRequirements: (req: unknown) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
532
532
|
/** Optional: Refund protection config (enables auto failure reporting) */
|
|
533
533
|
refundProtection?: RefundProtectionConfig;
|
|
534
534
|
/** Optional: Custom 402 response handler */
|
|
535
|
-
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements) => void | Promise<void>;
|
|
535
|
+
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements[]) => void | Promise<void>;
|
|
536
536
|
}
|
|
537
537
|
/**
|
|
538
538
|
* Create x402 payment middleware that handles verification, settlement, and optional refund protection.
|
|
@@ -580,8 +580,8 @@ declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (req:
|
|
|
580
580
|
interface HonoPaymentConfig {
|
|
581
581
|
/** Facilitator instance or URL */
|
|
582
582
|
facilitator: OpenFacilitator | string;
|
|
583
|
-
/** Function to get payment requirements for the request */
|
|
584
|
-
getRequirements: (c: HonoContext) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
583
|
+
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
584
|
+
getRequirements: (c: HonoContext) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
585
585
|
/** Optional: Refund protection config */
|
|
586
586
|
refundProtection?: RefundProtectionConfig;
|
|
587
587
|
}
|
package/dist/index.js
CHANGED
|
@@ -572,31 +572,36 @@ function createPaymentMiddleware(config) {
|
|
|
572
572
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
573
573
|
return async (req, res, next) => {
|
|
574
574
|
try {
|
|
575
|
-
const
|
|
575
|
+
const rawRequirements = await config.getRequirements(req);
|
|
576
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
576
577
|
const paymentHeader = req.headers["x-payment"];
|
|
577
578
|
const paymentString = Array.isArray(paymentHeader) ? paymentHeader[0] : paymentHeader;
|
|
578
579
|
if (!paymentString) {
|
|
579
580
|
if (config.on402) {
|
|
580
|
-
await config.on402(req, res,
|
|
581
|
+
await config.on402(req, res, requirementsArray);
|
|
581
582
|
} else {
|
|
582
|
-
const
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
583
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
584
|
+
const extra = {
|
|
585
|
+
...requirements2.extra
|
|
586
|
+
};
|
|
587
|
+
if (config.refundProtection) {
|
|
588
|
+
extra.supportsRefunds = true;
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
scheme: requirements2.scheme,
|
|
592
|
+
network: requirements2.network,
|
|
593
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
594
|
+
asset: requirements2.asset,
|
|
595
|
+
payTo: requirements2.payTo,
|
|
596
|
+
resource: requirements2.resource || req.url,
|
|
597
|
+
description: requirements2.description,
|
|
598
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
599
|
+
};
|
|
600
|
+
});
|
|
588
601
|
res.status(402).json({
|
|
602
|
+
x402Version: 2,
|
|
589
603
|
error: "Payment Required",
|
|
590
|
-
accepts
|
|
591
|
-
scheme: requirements.scheme,
|
|
592
|
-
network: requirements.network,
|
|
593
|
-
maxAmountRequired: requirements.maxAmountRequired,
|
|
594
|
-
asset: requirements.asset,
|
|
595
|
-
payTo: requirements.payTo,
|
|
596
|
-
resource: requirements.resource || req.url,
|
|
597
|
-
description: requirements.description,
|
|
598
|
-
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
599
|
-
}]
|
|
604
|
+
accepts
|
|
600
605
|
});
|
|
601
606
|
}
|
|
602
607
|
return;
|
|
@@ -611,6 +616,8 @@ function createPaymentMiddleware(config) {
|
|
|
611
616
|
res.status(400).json({ error: "Invalid X-PAYMENT header" });
|
|
612
617
|
return;
|
|
613
618
|
}
|
|
619
|
+
const paymentNetwork = paymentPayload.network;
|
|
620
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
614
621
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
615
622
|
if (!verifyResult.isValid) {
|
|
616
623
|
res.status(402).json({
|
|
@@ -675,27 +682,32 @@ function createPaymentMiddleware(config) {
|
|
|
675
682
|
function honoPaymentMiddleware(config) {
|
|
676
683
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
677
684
|
return async (c, next) => {
|
|
678
|
-
const
|
|
685
|
+
const rawRequirements = await config.getRequirements(c);
|
|
686
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
679
687
|
const paymentString = c.req.header("x-payment");
|
|
680
688
|
if (!paymentString) {
|
|
681
|
-
const
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
689
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
690
|
+
const extra = {
|
|
691
|
+
...requirements2.extra
|
|
692
|
+
};
|
|
693
|
+
if (config.refundProtection) {
|
|
694
|
+
extra.supportsRefunds = true;
|
|
695
|
+
}
|
|
696
|
+
return {
|
|
697
|
+
scheme: requirements2.scheme,
|
|
698
|
+
network: requirements2.network,
|
|
699
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
700
|
+
asset: requirements2.asset,
|
|
701
|
+
payTo: requirements2.payTo,
|
|
702
|
+
resource: requirements2.resource || c.req.url,
|
|
703
|
+
description: requirements2.description,
|
|
704
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
705
|
+
};
|
|
706
|
+
});
|
|
687
707
|
return c.json({
|
|
708
|
+
x402Version: 2,
|
|
688
709
|
error: "Payment Required",
|
|
689
|
-
accepts
|
|
690
|
-
scheme: requirements.scheme,
|
|
691
|
-
network: requirements.network,
|
|
692
|
-
maxAmountRequired: requirements.maxAmountRequired,
|
|
693
|
-
asset: requirements.asset,
|
|
694
|
-
payTo: requirements.payTo,
|
|
695
|
-
resource: requirements.resource || c.req.url,
|
|
696
|
-
description: requirements.description,
|
|
697
|
-
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
698
|
-
}]
|
|
710
|
+
accepts
|
|
699
711
|
}, 402);
|
|
700
712
|
}
|
|
701
713
|
let paymentPayload;
|
|
@@ -707,6 +719,8 @@ function honoPaymentMiddleware(config) {
|
|
|
707
719
|
} catch {
|
|
708
720
|
return c.json({ error: "Invalid X-PAYMENT header" }, 400);
|
|
709
721
|
}
|
|
722
|
+
const paymentNetwork = paymentPayload.network;
|
|
723
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
710
724
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
711
725
|
if (!verifyResult.isValid) {
|
|
712
726
|
return c.json({
|
package/dist/index.mjs
CHANGED
|
@@ -521,31 +521,36 @@ function createPaymentMiddleware(config) {
|
|
|
521
521
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
522
522
|
return async (req, res, next) => {
|
|
523
523
|
try {
|
|
524
|
-
const
|
|
524
|
+
const rawRequirements = await config.getRequirements(req);
|
|
525
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
525
526
|
const paymentHeader = req.headers["x-payment"];
|
|
526
527
|
const paymentString = Array.isArray(paymentHeader) ? paymentHeader[0] : paymentHeader;
|
|
527
528
|
if (!paymentString) {
|
|
528
529
|
if (config.on402) {
|
|
529
|
-
await config.on402(req, res,
|
|
530
|
+
await config.on402(req, res, requirementsArray);
|
|
530
531
|
} else {
|
|
531
|
-
const
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
532
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
533
|
+
const extra = {
|
|
534
|
+
...requirements2.extra
|
|
535
|
+
};
|
|
536
|
+
if (config.refundProtection) {
|
|
537
|
+
extra.supportsRefunds = true;
|
|
538
|
+
}
|
|
539
|
+
return {
|
|
540
|
+
scheme: requirements2.scheme,
|
|
541
|
+
network: requirements2.network,
|
|
542
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
543
|
+
asset: requirements2.asset,
|
|
544
|
+
payTo: requirements2.payTo,
|
|
545
|
+
resource: requirements2.resource || req.url,
|
|
546
|
+
description: requirements2.description,
|
|
547
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
548
|
+
};
|
|
549
|
+
});
|
|
537
550
|
res.status(402).json({
|
|
551
|
+
x402Version: 2,
|
|
538
552
|
error: "Payment Required",
|
|
539
|
-
accepts
|
|
540
|
-
scheme: requirements.scheme,
|
|
541
|
-
network: requirements.network,
|
|
542
|
-
maxAmountRequired: requirements.maxAmountRequired,
|
|
543
|
-
asset: requirements.asset,
|
|
544
|
-
payTo: requirements.payTo,
|
|
545
|
-
resource: requirements.resource || req.url,
|
|
546
|
-
description: requirements.description,
|
|
547
|
-
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
548
|
-
}]
|
|
553
|
+
accepts
|
|
549
554
|
});
|
|
550
555
|
}
|
|
551
556
|
return;
|
|
@@ -560,6 +565,8 @@ function createPaymentMiddleware(config) {
|
|
|
560
565
|
res.status(400).json({ error: "Invalid X-PAYMENT header" });
|
|
561
566
|
return;
|
|
562
567
|
}
|
|
568
|
+
const paymentNetwork = paymentPayload.network;
|
|
569
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
563
570
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
564
571
|
if (!verifyResult.isValid) {
|
|
565
572
|
res.status(402).json({
|
|
@@ -624,27 +631,32 @@ function createPaymentMiddleware(config) {
|
|
|
624
631
|
function honoPaymentMiddleware(config) {
|
|
625
632
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
626
633
|
return async (c, next) => {
|
|
627
|
-
const
|
|
634
|
+
const rawRequirements = await config.getRequirements(c);
|
|
635
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
628
636
|
const paymentString = c.req.header("x-payment");
|
|
629
637
|
if (!paymentString) {
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
638
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
639
|
+
const extra = {
|
|
640
|
+
...requirements2.extra
|
|
641
|
+
};
|
|
642
|
+
if (config.refundProtection) {
|
|
643
|
+
extra.supportsRefunds = true;
|
|
644
|
+
}
|
|
645
|
+
return {
|
|
646
|
+
scheme: requirements2.scheme,
|
|
647
|
+
network: requirements2.network,
|
|
648
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
649
|
+
asset: requirements2.asset,
|
|
650
|
+
payTo: requirements2.payTo,
|
|
651
|
+
resource: requirements2.resource || c.req.url,
|
|
652
|
+
description: requirements2.description,
|
|
653
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
654
|
+
};
|
|
655
|
+
});
|
|
636
656
|
return c.json({
|
|
657
|
+
x402Version: 2,
|
|
637
658
|
error: "Payment Required",
|
|
638
|
-
accepts
|
|
639
|
-
scheme: requirements.scheme,
|
|
640
|
-
network: requirements.network,
|
|
641
|
-
maxAmountRequired: requirements.maxAmountRequired,
|
|
642
|
-
asset: requirements.asset,
|
|
643
|
-
payTo: requirements.payTo,
|
|
644
|
-
resource: requirements.resource || c.req.url,
|
|
645
|
-
description: requirements.description,
|
|
646
|
-
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
647
|
-
}]
|
|
659
|
+
accepts
|
|
648
660
|
}, 402);
|
|
649
661
|
}
|
|
650
662
|
let paymentPayload;
|
|
@@ -656,6 +668,8 @@ function honoPaymentMiddleware(config) {
|
|
|
656
668
|
} catch {
|
|
657
669
|
return c.json({ error: "Invalid X-PAYMENT header" }, 400);
|
|
658
670
|
}
|
|
671
|
+
const paymentNetwork = paymentPayload.network;
|
|
672
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
659
673
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
660
674
|
if (!verifyResult.isValid) {
|
|
661
675
|
return c.json({
|