|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | + |
| 3 | +import { DONATION_CONTRACT_ACCOUNT_ID } from "@/common/_config"; |
| 4 | + |
| 5 | +export const dynamic = "force-dynamic"; |
| 6 | + |
| 7 | +const PINGPAY_API_BASE = process.env.PINGPAY_API_BASE ?? "https://pay.pingpay.io/api"; |
| 8 | + |
| 9 | +// TODO: move to env var and rotate before going public. |
| 10 | +const PINGPAY_API_KEY_FALLBACK = "VquZNJbyXyPLyduKgCQDSttpXvRITYqjSGnguJogjezGINxYhsjsBAoEFCMXOVEk"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Creates a PingPay Hosted Checkout session that settles to the POTLOCK |
| 14 | + * donation contract for a direct project donation. Routing to the recipient |
| 15 | + * account is done via `customRecipientMsg`, which the contract's |
| 16 | + * `ft_on_transfer` decodes. |
| 17 | + * |
| 18 | + * Setup requirement: in the PingPay dashboard, the NEAR recipient address |
| 19 | + * must be set to {@link DONATION_CONTRACT_ACCOUNT_ID}. PingPay's API ignores |
| 20 | + * any per-request recipient and always settles to the dashboard-configured one. |
| 21 | + */ |
| 22 | +export async function POST(req: Request) { |
| 23 | + try { |
| 24 | + const apiKey = process.env.PINGPAY_API_KEY ?? PINGPAY_API_KEY_FALLBACK; |
| 25 | + |
| 26 | + const body = await req.json(); |
| 27 | + |
| 28 | + const { |
| 29 | + amount, |
| 30 | + asset, |
| 31 | + recipientAccountId, |
| 32 | + referrerAccountId, |
| 33 | + donorAccountId, |
| 34 | + donorMessage, |
| 35 | + successUrl, |
| 36 | + cancelUrl, |
| 37 | + } = body ?? {}; |
| 38 | + |
| 39 | + if (!amount || !recipientAccountId) { |
| 40 | + return NextResponse.json( |
| 41 | + { error: "Missing required fields: amount, recipientAccountId" }, |
| 42 | + { status: 400 }, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + // Embed donor account in the on-chain message so the real donor is |
| 47 | + // recoverable — sender_id on the FT transfer is intent.near (PingPay's |
| 48 | + // settlement gateway), not the human donor. |
| 49 | + const donorTag = donorAccountId ? `[PingPay donor: ${donorAccountId}]` : "[via PingPay]"; |
| 50 | + const taggedMessage = donorMessage ? `${donorTag} ${donorMessage}` : donorTag; |
| 51 | + |
| 52 | + const customRecipientMsg = JSON.stringify({ |
| 53 | + recipient_id: recipientAccountId, |
| 54 | + referrer_id: referrerAccountId ?? null, |
| 55 | + bypass_protocol_fee: false, |
| 56 | + message: taggedMessage, |
| 57 | + }); |
| 58 | + |
| 59 | + const response = await fetch(`${PINGPAY_API_BASE}/checkout/sessions`, { |
| 60 | + method: "POST", |
| 61 | + headers: { |
| 62 | + "Content-Type": "application/json", |
| 63 | + "x-api-key": apiKey, |
| 64 | + }, |
| 65 | + body: JSON.stringify({ |
| 66 | + amount, |
| 67 | + asset, |
| 68 | + recipient: DONATION_CONTRACT_ACCOUNT_ID, |
| 69 | + customRecipientMsg, |
| 70 | + successUrl, |
| 71 | + cancelUrl, |
| 72 | + metadata: { |
| 73 | + source: "potlock", |
| 74 | + recipientAccountId, |
| 75 | + donorAccountId: donorAccountId ?? null, |
| 76 | + }, |
| 77 | + }), |
| 78 | + }); |
| 79 | + |
| 80 | + const text = await response.text(); |
| 81 | + let data: any; |
| 82 | + |
| 83 | + try { |
| 84 | + data = text ? JSON.parse(text) : {}; |
| 85 | + } catch { |
| 86 | + data = { error: text || "Non-JSON response from PingPay" }; |
| 87 | + } |
| 88 | + |
| 89 | + if (!response.ok) { |
| 90 | + return NextResponse.json( |
| 91 | + { error: data?.message ?? data?.error ?? "Failed to create PingPay checkout" }, |
| 92 | + { status: response.status }, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return NextResponse.json(data, { status: 200 }); |
| 97 | + } catch (error) { |
| 98 | + console.error("PingPay project checkout error:", error); |
| 99 | + return NextResponse.json({ error: "Failed to create checkout" }, { status: 500 }); |
| 100 | + } |
| 101 | +} |
0 commit comments