@@ -3,78 +3,23 @@ import {
33 type Money ,
44 moneySchema ,
55 type Network ,
6- type PaymentMiddlewareConfig ,
76 SupportedEVMNetworks ,
87} from "x402/types" ;
9- import { type Address , getAddress } from "../utils/address.js" ;
8+ import { getAddress } from "../utils/address.js" ;
109import { stringify } from "../utils/json.js" ;
1110import { decodePayment , safeBase64Encode } from "./encode.js" ;
1211import type { facilitator as facilitatorType } from "./facilitator.js" ;
1312import {
1413 type FacilitatorNetwork ,
15- type FacilitatorSettleResponse ,
1614 networkToChainId ,
1715 type RequestedPaymentPayload ,
1816 type RequestedPaymentRequirements ,
1917} from "./schemas.js" ;
20-
21- const x402Version = 1 ;
22-
23- /**
24- * Configuration object for verifying X402 payments.
25- *
26- * @public
27- */
28- export type VerifyPaymentArgs = {
29- /** The URL of the resource being protected by the payment */
30- resourceUrl : string ;
31- /** The HTTP method used to access the resource */
32- method : "GET" | "POST" | ( { } & string ) ;
33- /** The payment data/proof provided by the client, typically from the X-PAYMENT header */
34- paymentData ?: string | null ;
35- /** The wallet address that should receive the payment */
36- payTo : Address ;
37- /** The blockchain network where the payment should be processed */
38- network : FacilitatorNetwork ;
39- /** The price for accessing the resource - either a USD amount (e.g., "$0.10") or a specific token amount */
40- price : Money | ERC20TokenAmount ;
41- /** The payment facilitator instance used to verify and settle payments */
42- facilitator : ReturnType < typeof facilitatorType > ;
43- /** Optional configuration for the payment middleware route */
44- routeConfig ?: PaymentMiddlewareConfig ;
45- } ;
46-
47- /**
48- * The result of a payment verification operation.
49- *
50- * @public
51- */
52- export type VerifyPaymentResult =
53- | {
54- /** HTTP 200 - Payment was successfully verified and settled */
55- status : 200 ;
56- /** Response headers including payment receipt information */
57- responseHeaders : Record < string , string > ;
58- /** The settlement receipt from the payment facilitator */
59- paymentReceipt : FacilitatorSettleResponse ;
60- }
61- | {
62- /** HTTP 402 - Payment Required, verification failed or payment missing */
63- status : 402 ;
64- /** The error response body containing payment requirements */
65- responseBody : {
66- /** The X402 protocol version */
67- x402Version : number ;
68- /** Human-readable error message */
69- error : string ;
70- /** Array of acceptable payment methods and requirements */
71- accepts : RequestedPaymentRequirements [ ] ;
72- /** Optional payer address if verification partially succeeded */
73- payer ?: string ;
74- } ;
75- /** Response headers for the error response */
76- responseHeaders : Record < string , string > ;
77- } ;
18+ import {
19+ type PaymentArgs ,
20+ type ProcessPaymentResult ,
21+ x402Version ,
22+ } from "./types.js" ;
7823
7924/**
8025 * Verifies and processes X402 payments for protected resources.
@@ -89,7 +34,7 @@ export type VerifyPaymentResult =
8934 * @example
9035 * ```ts
9136 * // Usage in a Next.js API route
92- * import { verifyPayment , facilitator } from "thirdweb/x402";
37+ * import { processPayment , facilitator } from "thirdweb/x402";
9338 * import { createThirdwebClient } from "thirdweb";
9439 *
9540 * const client = createThirdwebClient({
@@ -104,7 +49,7 @@ export type VerifyPaymentResult =
10449 * export async function GET(request: Request) {
10550 * const paymentData = request.headers.get("x-payment");
10651 *
107- * const result = await verifyPayment ({
52+ * const result = await processPayment ({
10853 * resourceUrl: "https://api.example.com/premium-content",
10954 * method: "GET",
11055 * paymentData,
@@ -138,12 +83,22 @@ export type VerifyPaymentResult =
13883 * ```ts
13984 * // Usage in Express middleware
14085 * import express from "express";
141- * import { verifyPayment, facilitator } from "thirdweb/x402";
86+ * import { processPayment, facilitator } from "thirdweb/x402";
87+ * import { createThirdwebClient } from "thirdweb";
88+ *
89+ * const client = createThirdwebClient({
90+ * secretKey: process.env.THIRDWEB_SECRET_KEY,
91+ * });
92+ *
93+ * const thirdwebFacilitator = facilitator({
94+ * client,
95+ * serverWalletAddress: "0x1234567890123456789012345678901234567890",
96+ * });
14297 *
14398 * const app = express();
14499 *
145100 * async function paymentMiddleware(req, res, next) {
146- * const result = await verifyPayment ({
101+ * const result = await processPayment ({
147102 * resourceUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
148103 * method: req.method,
149104 * paymentData: req.headers["x-payment"],
@@ -176,9 +131,9 @@ export type VerifyPaymentResult =
176131 * @beta
177132 * @bridge x402
178133 */
179- export async function verifyPayment (
180- args : VerifyPaymentArgs ,
181- ) : Promise < VerifyPaymentResult > {
134+ export async function processPayment (
135+ args : PaymentArgs ,
136+ ) : Promise < ProcessPaymentResult > {
182137 const {
183138 price,
184139 network,
@@ -316,47 +271,6 @@ export async function verifyPayment(
316271 } ;
317272 }
318273
319- try {
320- const verification = await facilitator . verify (
321- decodedPayment ,
322- selectedPaymentRequirements ,
323- ) ;
324-
325- if ( ! verification . isValid ) {
326- return {
327- status : 402 ,
328- responseHeaders : {
329- "Content-Type" : "application/json" ,
330- } ,
331- responseBody : {
332- x402Version,
333- error :
334- errorMessages ?. verificationFailed ||
335- verification . invalidReason ||
336- "Payment verification failed" ,
337- accepts : paymentRequirements ,
338- payer : verification . payer ,
339- } ,
340- } ;
341- }
342- } catch ( error ) {
343- return {
344- status : 402 ,
345- responseHeaders : {
346- "Content-Type" : "application/json" ,
347- } ,
348- responseBody : {
349- x402Version,
350- error :
351- errorMessages ?. verificationFailed ||
352- ( error instanceof Error
353- ? error . message
354- : "Payment Verification error" ) ,
355- accepts : paymentRequirements ,
356- } ,
357- } ;
358- }
359-
360274 // Settle payment
361275 try {
362276 const settlement = await facilitator . settle (
0 commit comments