@openfacilitator/sdk 0.5.0 → 0.6.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/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +47 -35
- package/dist/index.mjs +47 -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,35 @@ 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({
|
|
589
602
|
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
|
-
}]
|
|
603
|
+
accepts
|
|
600
604
|
});
|
|
601
605
|
}
|
|
602
606
|
return;
|
|
@@ -611,6 +615,8 @@ function createPaymentMiddleware(config) {
|
|
|
611
615
|
res.status(400).json({ error: "Invalid X-PAYMENT header" });
|
|
612
616
|
return;
|
|
613
617
|
}
|
|
618
|
+
const paymentNetwork = paymentPayload.network;
|
|
619
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
614
620
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
615
621
|
if (!verifyResult.isValid) {
|
|
616
622
|
res.status(402).json({
|
|
@@ -675,27 +681,31 @@ function createPaymentMiddleware(config) {
|
|
|
675
681
|
function honoPaymentMiddleware(config) {
|
|
676
682
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
677
683
|
return async (c, next) => {
|
|
678
|
-
const
|
|
684
|
+
const rawRequirements = await config.getRequirements(c);
|
|
685
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
679
686
|
const paymentString = c.req.header("x-payment");
|
|
680
687
|
if (!paymentString) {
|
|
681
|
-
const
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
688
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
689
|
+
const extra = {
|
|
690
|
+
...requirements2.extra
|
|
691
|
+
};
|
|
692
|
+
if (config.refundProtection) {
|
|
693
|
+
extra.supportsRefunds = true;
|
|
694
|
+
}
|
|
695
|
+
return {
|
|
696
|
+
scheme: requirements2.scheme,
|
|
697
|
+
network: requirements2.network,
|
|
698
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
699
|
+
asset: requirements2.asset,
|
|
700
|
+
payTo: requirements2.payTo,
|
|
701
|
+
resource: requirements2.resource || c.req.url,
|
|
702
|
+
description: requirements2.description,
|
|
703
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
704
|
+
};
|
|
705
|
+
});
|
|
687
706
|
return c.json({
|
|
688
707
|
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
|
-
}]
|
|
708
|
+
accepts
|
|
699
709
|
}, 402);
|
|
700
710
|
}
|
|
701
711
|
let paymentPayload;
|
|
@@ -707,6 +717,8 @@ function honoPaymentMiddleware(config) {
|
|
|
707
717
|
} catch {
|
|
708
718
|
return c.json({ error: "Invalid X-PAYMENT header" }, 400);
|
|
709
719
|
}
|
|
720
|
+
const paymentNetwork = paymentPayload.network;
|
|
721
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
710
722
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
711
723
|
if (!verifyResult.isValid) {
|
|
712
724
|
return c.json({
|
package/dist/index.mjs
CHANGED
|
@@ -521,31 +521,35 @@ 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({
|
|
538
551
|
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
|
-
}]
|
|
552
|
+
accepts
|
|
549
553
|
});
|
|
550
554
|
}
|
|
551
555
|
return;
|
|
@@ -560,6 +564,8 @@ function createPaymentMiddleware(config) {
|
|
|
560
564
|
res.status(400).json({ error: "Invalid X-PAYMENT header" });
|
|
561
565
|
return;
|
|
562
566
|
}
|
|
567
|
+
const paymentNetwork = paymentPayload.network;
|
|
568
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
563
569
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
564
570
|
if (!verifyResult.isValid) {
|
|
565
571
|
res.status(402).json({
|
|
@@ -624,27 +630,31 @@ function createPaymentMiddleware(config) {
|
|
|
624
630
|
function honoPaymentMiddleware(config) {
|
|
625
631
|
const facilitator = typeof config.facilitator === "string" ? new OpenFacilitator({ url: config.facilitator }) : config.facilitator;
|
|
626
632
|
return async (c, next) => {
|
|
627
|
-
const
|
|
633
|
+
const rawRequirements = await config.getRequirements(c);
|
|
634
|
+
const requirementsArray = Array.isArray(rawRequirements) ? rawRequirements : [rawRequirements];
|
|
628
635
|
const paymentString = c.req.header("x-payment");
|
|
629
636
|
if (!paymentString) {
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
637
|
+
const accepts = requirementsArray.map((requirements2) => {
|
|
638
|
+
const extra = {
|
|
639
|
+
...requirements2.extra
|
|
640
|
+
};
|
|
641
|
+
if (config.refundProtection) {
|
|
642
|
+
extra.supportsRefunds = true;
|
|
643
|
+
}
|
|
644
|
+
return {
|
|
645
|
+
scheme: requirements2.scheme,
|
|
646
|
+
network: requirements2.network,
|
|
647
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
648
|
+
asset: requirements2.asset,
|
|
649
|
+
payTo: requirements2.payTo,
|
|
650
|
+
resource: requirements2.resource || c.req.url,
|
|
651
|
+
description: requirements2.description,
|
|
652
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
653
|
+
};
|
|
654
|
+
});
|
|
636
655
|
return c.json({
|
|
637
656
|
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
|
-
}]
|
|
657
|
+
accepts
|
|
648
658
|
}, 402);
|
|
649
659
|
}
|
|
650
660
|
let paymentPayload;
|
|
@@ -656,6 +666,8 @@ function honoPaymentMiddleware(config) {
|
|
|
656
666
|
} catch {
|
|
657
667
|
return c.json({ error: "Invalid X-PAYMENT header" }, 400);
|
|
658
668
|
}
|
|
669
|
+
const paymentNetwork = paymentPayload.network;
|
|
670
|
+
const requirements = requirementsArray.find((r) => r.network === paymentNetwork) || requirementsArray[0];
|
|
659
671
|
const verifyResult = await facilitator.verify(paymentPayload, requirements);
|
|
660
672
|
if (!verifyResult.isValid) {
|
|
661
673
|
return c.json({
|