|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { cookies } from "next/headers"; |
| 3 | +import { exact } from "x402/schemes"; |
| 4 | +import { PAYMENT_REQUIREMENTS, COOKIE_TIMEOUT_SECONDS } from "../../constants"; |
| 5 | + |
| 6 | +export async function POST(req: Request) { |
| 7 | + try { |
| 8 | + const { paymentHeader } = await req.json(); |
| 9 | + |
| 10 | + if (!paymentHeader) { |
| 11 | + return NextResponse.json( |
| 12 | + { success: false, error: "Payment header is required" }, |
| 13 | + { status: 400 }, |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + // Decode the payment header |
| 18 | + const decodedPayment = exact.evm.decodePayment(paymentHeader); |
| 19 | + |
| 20 | + const facilitatorUrl = |
| 21 | + process.env.NEXT_PUBLIC_FACILITATOR_URL || |
| 22 | + "https://www.x402.org/facilitator"; |
| 23 | + |
| 24 | + // Call facilitator to verify |
| 25 | + const verifyResponse = await fetch(`${facilitatorUrl}/verify`, { |
| 26 | + method: "POST", |
| 27 | + headers: { "Content-Type": "application/json" }, |
| 28 | + body: JSON.stringify({ |
| 29 | + paymentPayload: decodedPayment, |
| 30 | + paymentRequirements: PAYMENT_REQUIREMENTS, |
| 31 | + }), |
| 32 | + }); |
| 33 | + |
| 34 | + const verifyResult = await verifyResponse.json(); |
| 35 | + |
| 36 | + if (!verifyResult.isValid) { |
| 37 | + return NextResponse.json( |
| 38 | + { |
| 39 | + success: false, |
| 40 | + x402Version: 1, |
| 41 | + error: verifyResult.invalidReason || "Payment verification failed", |
| 42 | + accepts: [PAYMENT_REQUIREMENTS], |
| 43 | + payer: verifyResult.payer, |
| 44 | + }, |
| 45 | + { status: 402 }, // Payment Required! |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + // Call facilitator to settle the payment |
| 50 | + const settleResponse = await fetch(`${facilitatorUrl}/settle`, { |
| 51 | + method: "POST", |
| 52 | + headers: { "Content-Type": "application/json" }, |
| 53 | + body: JSON.stringify({ |
| 54 | + paymentPayload: decodedPayment, |
| 55 | + paymentRequirements: PAYMENT_REQUIREMENTS, |
| 56 | + }), |
| 57 | + }); |
| 58 | + |
| 59 | + const settleResult = await settleResponse.json(); |
| 60 | + |
| 61 | + if (!settleResult.success) { |
| 62 | + return NextResponse.json( |
| 63 | + { |
| 64 | + success: false, |
| 65 | + error: settleResult.errorReason || "Settle failed", |
| 66 | + }, |
| 67 | + { status: 500 }, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // Payment is valid and settled, set the cookie |
| 72 | + const cookieStore = await cookies(); |
| 73 | + // This should be a JWT signed by the server following best practices for a session token |
| 74 | + // See: https://nextjs.org/docs/app/guides/authentication#stateless-sessions |
| 75 | + cookieStore.set("payment-session", paymentHeader, { |
| 76 | + maxAge: COOKIE_TIMEOUT_SECONDS, // 5 minutes |
| 77 | + }); |
| 78 | + |
| 79 | + return NextResponse.json( |
| 80 | + { |
| 81 | + success: true, |
| 82 | + payer: verifyResult.payer, |
| 83 | + }, |
| 84 | + { status: 200 }, |
| 85 | + ); |
| 86 | + } catch (error) { |
| 87 | + console.error("Error verifying payment:", error); |
| 88 | + return NextResponse.json( |
| 89 | + { |
| 90 | + success: false, |
| 91 | + x402Version: 1, |
| 92 | + error: error instanceof Error ? error.message : "Internal server error", |
| 93 | + accepts: [PAYMENT_REQUIREMENTS], |
| 94 | + }, |
| 95 | + { status: 500 }, |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments