|
1 | | -import { NextResponse } from "next/server"; |
| 1 | +import { type NextRequest, NextResponse } from "next/server"; |
| 2 | +import { createThirdwebClient, defineChain } from "thirdweb"; |
| 3 | +import { toUnits } from "thirdweb/utils"; |
| 4 | +import { facilitator, settlePayment } from "thirdweb/x402"; |
| 5 | +import { token } from "../../payments/x402/components/constants"; |
| 6 | + |
| 7 | +const client = createThirdwebClient({ |
| 8 | + secretKey: process.env.THIRDWEB_SECRET_KEY as string, |
| 9 | +}); |
| 10 | + |
| 11 | +const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string; |
| 12 | +// const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_SMART_WALLET as string; |
| 13 | +const ENGINE_VAULT_ACCESS_TOKEN = process.env |
| 14 | + .ENGINE_VAULT_ACCESS_TOKEN as string; |
| 15 | +const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`; |
| 16 | + |
| 17 | +const twFacilitator = facilitator({ |
| 18 | + baseUrl: `${API_URL}/v1/payments/x402`, |
| 19 | + client, |
| 20 | + serverWalletAddress: BACKEND_WALLET_ADDRESS, |
| 21 | + vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN, |
| 22 | +}); |
2 | 23 | // Allow streaming responses up to 5 minutes |
3 | 24 | export const maxDuration = 300; |
4 | 25 |
|
5 | | -export async function GET(_req: Request) { |
6 | | - return NextResponse.json({ |
7 | | - success: true, |
8 | | - message: "Payment successful. You have accessed the protected route.", |
| 26 | +export async function GET(request: NextRequest) { |
| 27 | + const paymentData = request.headers.get("X-PAYMENT"); |
| 28 | + const queryParams = request.nextUrl.searchParams; |
| 29 | + |
| 30 | + const chainId = queryParams.get("chainId"); |
| 31 | + |
| 32 | + if (!chainId) { |
| 33 | + return NextResponse.json( |
| 34 | + { error: "Missing required parameters" }, |
| 35 | + { status: 400 }, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const amount = queryParams.get("amount") || "0.01"; |
| 40 | + const tokenAddress = queryParams.get("tokenAddress") || token.address; |
| 41 | + const decimals = queryParams.get("decimals") || token.decimals.toString(); |
| 42 | + const waitUntil = |
| 43 | + (queryParams.get("waitUntil") as "simulated" | "submitted" | "confirmed") || |
| 44 | + "simulated"; |
| 45 | + |
| 46 | + const result = await settlePayment({ |
| 47 | + resourceUrl: "https://playground-web.thirdweb.com/api/paywall", |
| 48 | + method: "GET", |
| 49 | + paymentData, |
| 50 | + network: defineChain(Number(chainId)), |
| 51 | + price: { |
| 52 | + amount: toUnits(amount, parseInt(decimals)).toString(), |
| 53 | + asset: { |
| 54 | + address: tokenAddress as `0x${string}`, |
| 55 | + decimals: decimals ? parseInt(decimals) : token.decimals, |
| 56 | + }, |
| 57 | + }, |
| 58 | + routeConfig: { |
| 59 | + description: "Access to paid content", |
| 60 | + }, |
| 61 | + waitUntil, |
| 62 | + facilitator: twFacilitator, |
| 63 | + }); |
| 64 | + |
| 65 | + if (result.status === 200) { |
| 66 | + // payment successful, execute the request |
| 67 | + return NextResponse.json({ |
| 68 | + success: true, |
| 69 | + message: "Payment successful. You have accessed the protected route.", |
| 70 | + payment: { |
| 71 | + amount, |
| 72 | + tokenAddress, |
| 73 | + }, |
| 74 | + receipt: result.paymentReceipt, |
| 75 | + }, { |
| 76 | + status: 200, |
| 77 | + headers: result.responseHeaders, |
| 78 | + });; |
| 79 | + } |
| 80 | + |
| 81 | + // otherwise, request payment |
| 82 | + return NextResponse.json(result.responseBody, { |
| 83 | + status: result.status, |
| 84 | + headers: result.responseHeaders, |
9 | 85 | }); |
10 | 86 | } |
0 commit comments