Skip to content

Commit 9086cdb

Browse files
[Playground] Add payTo parameter to X402 payment API (#8231)
1 parent 0082e99 commit 9086cdb

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

apps/playground-web/src/app/api/paywall/route.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ import { token } from "../../payments/x402/components/constants";
77
// Allow streaming responses up to 5 minutes
88
export const maxDuration = 300;
99

10-
export async function GET(request: NextRequest) {
11-
const client = createThirdwebClient({
12-
secretKey: process.env.THIRDWEB_SECRET_KEY as string,
13-
});
10+
const client = createThirdwebClient({
11+
secretKey: process.env.THIRDWEB_SECRET_KEY as string,
12+
});
1413

15-
const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string;
16-
// const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_SMART_WALLET as string;
17-
const ENGINE_VAULT_ACCESS_TOKEN = process.env
18-
.ENGINE_VAULT_ACCESS_TOKEN as string;
19-
const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
14+
const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string;
15+
// const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_SMART_WALLET as string;
16+
const ENGINE_VAULT_ACCESS_TOKEN = process.env
17+
.ENGINE_VAULT_ACCESS_TOKEN as string;
18+
// const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
19+
const API_URL = "http://localhost:3030";
2020

21-
const twFacilitator = facilitator({
22-
baseUrl: `${API_URL}/v1/payments/x402`,
23-
client,
24-
serverWalletAddress: BACKEND_WALLET_ADDRESS,
25-
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
26-
});
21+
const twFacilitator = facilitator({
22+
baseUrl: `${API_URL}/v1/payments/x402`,
23+
client,
24+
serverWalletAddress: BACKEND_WALLET_ADDRESS,
25+
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
26+
});
2727

28+
export async function GET(request: NextRequest) {
2829
const paymentData = request.headers.get("X-PAYMENT");
2930
const queryParams = request.nextUrl.searchParams;
3031

@@ -38,6 +39,7 @@ export async function GET(request: NextRequest) {
3839
}
3940

4041
const amount = queryParams.get("amount") || "0.01";
42+
const payTo = queryParams.get("payTo") ?? undefined;
4143
const tokenAddress = queryParams.get("tokenAddress") || token.address;
4244
const decimals = queryParams.get("decimals") || token.decimals.toString();
4345
const waitUntil =
@@ -49,6 +51,7 @@ export async function GET(request: NextRequest) {
4951
method: "GET",
5052
paymentData,
5153
network: defineChain(Number(chainId)),
54+
payTo,
5255
price: {
5356
amount: toUnits(amount, parseInt(decimals)).toString(),
5457
asset: {

apps/playground-web/src/app/payments/x402/components/X402LeftSection.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function X402LeftSection(props: {
4545
const tokenId = useId();
4646
const amountId = useId();
4747
const waitUntilId = useId();
48+
const payToId = useId();
4849

4950
const handleChainChange = (chainId: number) => {
5051
setSelectedChain(chainId);
@@ -81,6 +82,13 @@ export function X402LeftSection(props: {
8182
}));
8283
};
8384

85+
const handlePayToChange = (e: React.ChangeEvent<HTMLInputElement>) => {
86+
setOptions((v) => ({
87+
...v,
88+
payTo: e.target.value as `0x${string}`,
89+
}));
90+
};
91+
8492
const handleWaitUntilChange = (
8593
value: "simulated" | "submitted" | "confirmed",
8694
) => {
@@ -140,6 +148,22 @@ export function X402LeftSection(props: {
140148
)}
141149
</div>
142150

151+
{/* Pay To input */}
152+
<div className="flex flex-col gap-2">
153+
<Label htmlFor={payToId}>Pay To Address</Label>
154+
<Input
155+
id={payToId}
156+
type="text"
157+
placeholder="0x..."
158+
value={options.payTo}
159+
onChange={handlePayToChange}
160+
className="bg-card"
161+
/>
162+
<p className="text-sm text-muted-foreground">
163+
The wallet address that will receive the payment
164+
</p>
165+
</div>
166+
143167
{/* Wait Until selection */}
144168
<div className="flex flex-col gap-2">
145169
<Label htmlFor={waitUntilId}>Wait Until</Label>

0 commit comments

Comments
 (0)