Skip to content

Commit 2c42ac2

Browse files
[SDK] Rename verifyPayment() to processPayment() for x402 payments
1 parent a81899d commit 2c42ac2

File tree

8 files changed

+109
-124
lines changed

8 files changed

+109
-124
lines changed

.changeset/some-moons-burn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"thirdweb": minor
33
---
44

5-
Accept arbitrary chain ids for x402 payments with new verifyPayment() backend utility
5+
Accept arbitrary chain ids for x402 payments with new processPayment() backend utility

apps/playground-web/src/app/payments/x402/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function ServerCodeExample() {
5757
className="h-full rounded-none border-none"
5858
code={`// src/middleware.ts
5959
60-
import { facilitator, verifyPayment } from "thirdweb/x402";
60+
import { facilitator, processPayment } from "thirdweb/x402";
6161
import { createThirdwebClient } from "thirdweb";
6262
6363
const client = createThirdwebClient({ secretKey: "your-secret-key" });
@@ -71,7 +71,7 @@ export async function middleware(request: NextRequest) {
7171
const resourceUrl = request.nextUrl.toString();
7272
const paymentData = request.headers.get("X-PAYMENT");
7373
74-
const result = await verifyPayment({
74+
const result = await processPayment({
7575
resourceUrl,
7676
method,
7777
paymentData,

apps/playground-web/src/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type NextRequest, NextResponse } from "next/server";
22
import { createThirdwebClient } from "thirdweb";
33
import { arbitrumSepolia } from "thirdweb/chains";
4-
import { facilitator, verifyPayment } from "thirdweb/x402";
4+
import { facilitator, processPayment } from "thirdweb/x402";
55

66
const client = createThirdwebClient({
77
secretKey: process.env.THIRDWEB_SECRET_KEY as string,
@@ -26,7 +26,7 @@ export async function middleware(request: NextRequest) {
2626
const resourceUrl = `${request.nextUrl.protocol}//${request.nextUrl.host}${pathname}`;
2727
const paymentData = request.headers.get("X-PAYMENT");
2828

29-
const result = await verifyPayment({
29+
const result = await processPayment({
3030
resourceUrl,
3131
method,
3232
paymentData,

apps/portal/src/app/payments/x402/page.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ const response = await fetchWithPay('https://api.example.com/paid-endpoint');
4141

4242
## Server Side
4343

44-
To make your API calls payable, you can use the `verifyPayment` function in a simple middleware or in your endpoint directly.
44+
To make your API calls payable, you can use the `processPayment` function in a simple middleware or in your endpoint directly.
4545

46-
Use the `facilitator` configuration function settle transactions with your thirdweb server wallet gaslessly and pass it to the `verifyPayment` function.
46+
Use the `facilitator` configuration function settle transactions with your thirdweb server wallet gaslessly and pass it to the `processPayment` function.
4747

4848
Here's an example with a Next.js middleware:
4949

5050
```typescript
5151
import { createThirdwebClient } from "thirdweb";
52-
import { facilitator, verifyPayment } from "thirdweb/x402";
52+
import { facilitator, processPayment } from "thirdweb/x402";
5353

5454
const client = createThirdwebClient({ secretKey: "your-secret-key" });
5555
const thirdwebX402Facilitator = facilitator({
@@ -62,7 +62,7 @@ export async function middleware(request: NextRequest) {
6262
const resourceUrl = request.nextUrl.toString();
6363
const paymentData = request.headers.get("X-PAYMENT");
6464

65-
const result = await verifyPayment({
65+
const result = await processPayment({
6666
resourceUrl,
6767
method,
6868
paymentData,

packages/thirdweb/src/exports/x402.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ export {
44
type ThirdwebX402FacilitatorConfig,
55
} from "../x402/facilitator.js";
66
export { wrapFetchWithPayment } from "../x402/fetchWithPayment.js";
7-
export {
8-
type VerifyPaymentArgs,
9-
type VerifyPaymentResult,
10-
verifyPayment,
11-
} from "../x402/verify-payment.js";
7+
export { processPayment } from "../x402/process-payment.js";
8+
export type { PaymentArgs, ProcessPaymentResult } from "../x402/types.js";

packages/thirdweb/src/x402/facilitator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
1919

2020
/**
2121
* Creates a facilitator for the x402 payment protocol.
22-
* You can use this with `verifyPayment` or with any x402 middleware to enable settling transactions with your thirdweb server wallet.
22+
* You can use this with `processPayment` or with any x402 middleware to enable settling transactions with your thirdweb server wallet.
2323
*
2424
* @param config - The configuration for the facilitator
2525
* @returns a x402 compatible FacilitatorConfig

packages/thirdweb/src/x402/verify-payment.ts renamed to packages/thirdweb/src/x402/process-payment.ts

Lines changed: 23 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
109
import { stringify } from "../utils/json.js";
1110
import { decodePayment, safeBase64Encode } from "./encode.js";
1211
import type { facilitator as facilitatorType } from "./facilitator.js";
1312
import {
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(
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type {
2+
ERC20TokenAmount,
3+
Money,
4+
PaymentMiddlewareConfig,
5+
} from "x402/types";
6+
import type { Address } from "../utils/address.js";
7+
import type { Prettify } from "../utils/type-utils.js";
8+
import type { facilitator as facilitatorType } from "./facilitator.js";
9+
import type {
10+
FacilitatorNetwork,
11+
FacilitatorSettleResponse,
12+
RequestedPaymentRequirements,
13+
} from "./schemas.js";
14+
15+
export const x402Version = 1;
16+
17+
/**
18+
* Configuration object for verifying or processing X402 payments.
19+
*
20+
* @public
21+
*/
22+
export type PaymentArgs = {
23+
/** The URL of the resource being protected by the payment */
24+
resourceUrl: string;
25+
/** The HTTP method used to access the resource */
26+
method: "GET" | "POST" | ({} & string);
27+
/** The payment data/proof provided by the client, typically from the X-PAYMENT header */
28+
paymentData?: string | null;
29+
/** The wallet address that should receive the payment */
30+
payTo: Address;
31+
/** The blockchain network where the payment should be processed */
32+
network: FacilitatorNetwork;
33+
/** The price for accessing the resource - either a USD amount (e.g., "$0.10") or a specific token amount */
34+
price: Money | ERC20TokenAmount;
35+
/** The payment facilitator instance used to verify and settle payments */
36+
facilitator: ReturnType<typeof facilitatorType>;
37+
/** Optional configuration for the payment middleware route */
38+
routeConfig?: PaymentMiddlewareConfig;
39+
};
40+
41+
type PaymentRequiredResult = {
42+
/** HTTP 402 - Payment Required, verification or processing failed or payment missing */
43+
status: 402;
44+
/** The error response body containing payment requirements */
45+
responseBody: {
46+
/** The X402 protocol version */
47+
x402Version: number;
48+
/** Human-readable error message */
49+
error: string;
50+
/** Array of acceptable payment methods and requirements */
51+
accepts: RequestedPaymentRequirements[];
52+
/** Optional payer address if verification partially succeeded */
53+
payer?: string;
54+
};
55+
/** Response headers for the error response */
56+
responseHeaders: Record<string, string>;
57+
};
58+
59+
/**
60+
* The result of a payment verification or processing operation.
61+
*
62+
* @public
63+
*/
64+
export type ProcessPaymentResult = Prettify<
65+
| {
66+
/** HTTP 200 - Payment was successfully processed */
67+
status: 200;
68+
/** Response headers including payment receipt information */
69+
responseHeaders: Record<string, string>;
70+
/** The payment receipt from the payment facilitator */
71+
paymentReceipt: FacilitatorSettleResponse;
72+
}
73+
| PaymentRequiredResult
74+
>;

0 commit comments

Comments
 (0)