|
| 1 | +import { NextResponse, NextRequest } from "next/server"; |
| 2 | +import { Engine } from "@thirdweb-dev/engine"; |
| 3 | +import crypto from "crypto"; |
| 4 | + |
| 5 | +const generateSignature = ( |
| 6 | + body: string, |
| 7 | + timestamp: string, |
| 8 | + secret: string |
| 9 | +): string => { |
| 10 | + const payload = `${timestamp}.${body}`; |
| 11 | + return crypto.createHmac("sha256", secret).update(payload).digest("hex"); |
| 12 | +}; |
| 13 | + |
| 14 | +const isValidSignature = ( |
| 15 | + body: string, |
| 16 | + timestamp: string, |
| 17 | + signature: string, |
| 18 | + secret: string |
| 19 | +): boolean => { |
| 20 | + const expectedSignature = generateSignature(body, timestamp, secret); |
| 21 | + return crypto.timingSafeEqual( |
| 22 | + Buffer.from(expectedSignature), |
| 23 | + Buffer.from(signature) |
| 24 | + ); |
| 25 | +}; |
| 26 | + |
| 27 | +const isExpired = (timestamp: string, expirationInSeconds: number): boolean => { |
| 28 | + const currentTime = Math.floor(Date.now() / 1000); |
| 29 | + return currentTime - parseInt(timestamp) > expirationInSeconds; |
| 30 | +}; |
| 31 | + |
| 32 | +export async function POST(req: NextRequest) { |
| 33 | + try { |
| 34 | + const signatureFromHeader = req.headers.get("X-Pay-Signature"); |
| 35 | + const timestampFromHeader = req.headers.get("X-Pay-Timestamp"); |
| 36 | + const body = await req.json(); |
| 37 | + |
| 38 | + if (!signatureFromHeader || !timestampFromHeader) { |
| 39 | + console.error("Missing signature or timestamp header"); |
| 40 | + return NextResponse.json( |
| 41 | + { error: "Missing signature or timestamp header" }, |
| 42 | + { status: 401 } |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + if ( |
| 47 | + !isValidSignature( |
| 48 | + JSON.stringify(body), |
| 49 | + timestampFromHeader, |
| 50 | + signatureFromHeader, |
| 51 | + process.env.PAY_WEBHOOK_SECRET as string |
| 52 | + ) |
| 53 | + ) { |
| 54 | + console.error("Invalid Signature"); |
| 55 | + return NextResponse.json({ error: "Invalid Signature" }, { status: 401 }); |
| 56 | + } |
| 57 | + |
| 58 | + if (isExpired(timestampFromHeader, 300)) { |
| 59 | + // Assuming expiration time is 5 minutes (300 seconds) |
| 60 | + console.error("Request has expired"); |
| 61 | + return NextResponse.json( |
| 62 | + { error: "Request has expired" }, |
| 63 | + { status: 401 } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const { data } = body; |
| 68 | + |
| 69 | + if ( |
| 70 | + data.buyWithFiatStatus && |
| 71 | + data.buyWithFiatStatus.status === "ON_RAMP_TRANSFER_COMPLETED" |
| 72 | + ) { |
| 73 | + const { |
| 74 | + fromAddress, |
| 75 | + purchaseData: { nftContractAddress, chainId, amount }, |
| 76 | + } = body.data.buyWithFiatStatus; |
| 77 | + |
| 78 | + // Validate the purchase and call the engine |
| 79 | + const result = await sendContractCallToEngine( |
| 80 | + fromAddress, |
| 81 | + nftContractAddress, |
| 82 | + chainId, |
| 83 | + amount |
| 84 | + ); |
| 85 | + |
| 86 | + return NextResponse.json({ |
| 87 | + message: "Purchase processed successfully", |
| 88 | + result, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + if ( |
| 93 | + data.buyWithCryptoStatus && |
| 94 | + data.buyWithCryptoStatus.status === "CRYPTO_SWAP_COMPLETED" |
| 95 | + ) { |
| 96 | + const { |
| 97 | + fromAddress, |
| 98 | + purchaseData: { nftContractAddress, chainId, metadata }, |
| 99 | + } = body.data.buyWithCryptoStatus; |
| 100 | + |
| 101 | + // Validate the purchase and call the engine |
| 102 | + const result = await sendContractCallToEngine( |
| 103 | + fromAddress, |
| 104 | + nftContractAddress, |
| 105 | + chainId, |
| 106 | + metadata |
| 107 | + ); |
| 108 | + |
| 109 | + return NextResponse.json({ |
| 110 | + message: "Purchase processed successfully", |
| 111 | + result, |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return NextResponse.json({ message: "Status received" }); |
| 116 | + } catch (error) { |
| 117 | + console.error("Error handling webhook:", error); |
| 118 | + return NextResponse.json( |
| 119 | + { error: "Error processing webhook" }, |
| 120 | + { status: 500 } |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +async function sendContractCallToEngine( |
| 126 | + fromAddress: string, |
| 127 | + nftContractAddress: string, |
| 128 | + chainId: string, |
| 129 | + metadata: Record<string, string> = {} |
| 130 | +) { |
| 131 | + try { |
| 132 | + const engine = new Engine({ |
| 133 | + url: process.env.ENGINE_API_URL as string, |
| 134 | + accessToken: process.env.ENGINE_ACCESS_TOKEN as string, |
| 135 | + }); |
| 136 | + |
| 137 | + const response = await engine.erc721.mintTo( |
| 138 | + chainId, |
| 139 | + nftContractAddress, |
| 140 | + process.env.BACKEND_WALLET_ADDRESS as string, |
| 141 | + { |
| 142 | + receiver: fromAddress, |
| 143 | + metadata: metadata, |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + console.log("Response", response.result); |
| 148 | + |
| 149 | + if (!response) { |
| 150 | + throw new Error("Error in Engine contract call"); |
| 151 | + } |
| 152 | + |
| 153 | + return response; |
| 154 | + } catch (error) { |
| 155 | + console.error("Error processing Engine contract call", error); |
| 156 | + return NextResponse.json( |
| 157 | + { error: "Error processing Engine contract call" }, |
| 158 | + { status: 500 } |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments