|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import crypto from "node:crypto"; |
| 3 | + |
| 4 | +type MockInvoice = { |
| 5 | + invoice: string; |
| 6 | + amountSats: number; |
| 7 | + amountSatsReceived?: number; |
| 8 | + expiresAt: string; |
| 9 | + fiatAmount: number; |
| 10 | + btcPrice: number; |
| 11 | +}; |
| 12 | + |
| 13 | +type MockCheckout = { |
| 14 | + id: string; |
| 15 | + status: "PENDING_PAYMENT" | "PAYMENT_RECEIVED"; |
| 16 | + currency: "USD" | "SAT"; |
| 17 | + successUrl?: string; |
| 18 | + userMetadata?: Record<string, unknown>; |
| 19 | + invoiceAmountSats: number; |
| 20 | + invoice?: MockInvoice; |
| 21 | +}; |
| 22 | + |
| 23 | +type MockState = { |
| 24 | + checkout: MockCheckout | null; |
| 25 | + log: string[]; |
| 26 | +}; |
| 27 | + |
| 28 | +type GlobalForMock = { |
| 29 | + mockState?: MockState; |
| 30 | +} |
| 31 | + |
| 32 | +// Persist state across Next.js module reloads in development mode |
| 33 | +const globalForMock = globalThis as GlobalForMock; |
| 34 | + |
| 35 | +const mockState: MockState = globalForMock.mockState ?? { |
| 36 | + checkout: null, |
| 37 | + log: [], |
| 38 | +}; |
| 39 | + |
| 40 | +globalForMock.mockState = mockState; |
| 41 | + |
| 42 | +function nowPlusMinutes(minutes: number) { |
| 43 | + return new Date(Date.now() + minutes * 60 * 1000).toISOString(); |
| 44 | +} |
| 45 | + |
| 46 | +function buildMockInvoice(amountSats: number): MockInvoice { |
| 47 | + const fiatAmount = amountSats / 10; // Pretend 10 sats = $1 (only for display) |
| 48 | + return { |
| 49 | + invoice: `lnmock${crypto.randomBytes(16).toString("hex")}`, |
| 50 | + amountSats, |
| 51 | + amountSatsReceived: 0, |
| 52 | + expiresAt: nowPlusMinutes(10), |
| 53 | + fiatAmount, |
| 54 | + btcPrice: 68000, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function createMockCheckout(params: { |
| 59 | + amount: number; |
| 60 | + currency?: "USD" | "SAT"; |
| 61 | + metadata?: Record<string, unknown>; |
| 62 | + successUrl?: string; |
| 63 | +}): MockCheckout { |
| 64 | + const amountSats = params.currency === "SAT" ? params.amount : Math.max(50_000, params.amount * 4); |
| 65 | + return { |
| 66 | + id: `mock-${Date.now()}`, |
| 67 | + status: "PENDING_PAYMENT", |
| 68 | + currency: params.currency ?? "USD", |
| 69 | + successUrl: params.successUrl ?? "/checkout/success", |
| 70 | + invoiceAmountSats: amountSats, |
| 71 | + invoice: buildMockInvoice(amountSats), |
| 72 | + userMetadata: params.metadata, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +function setCheckout(checkout: MockCheckout) { |
| 77 | + mockState.checkout = checkout; |
| 78 | + mockState.log = ["client.create", "client.confirm", "client.registerInvoice"]; |
| 79 | +} |
| 80 | + |
| 81 | +function markPaid() { |
| 82 | + if (!mockState.checkout) return; |
| 83 | + mockState.checkout.status = "PAYMENT_RECEIVED"; |
| 84 | + if (mockState.checkout.invoice) { |
| 85 | + mockState.checkout.invoice.amountSatsReceived = mockState.checkout.invoice.amountSats; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function handlerFromBody(body: unknown): string | null { |
| 90 | + if (!body || typeof body !== "object") return null; |
| 91 | + const maybe = (body as any).handler ?? (body as any).route ?? (body as any).target; |
| 92 | + return typeof maybe === "string" ? maybe.toLowerCase() : null; |
| 93 | +} |
| 94 | + |
| 95 | +export async function POST(request: Request) { |
| 96 | + let body: any = null; |
| 97 | + try { |
| 98 | + body = await request.json(); |
| 99 | + } catch { |
| 100 | + // ignore, some callers (Cypress webhook) might send empty bodies |
| 101 | + } |
| 102 | + |
| 103 | + const handler = handlerFromBody(body); |
| 104 | + |
| 105 | + if (handler === "create_checkout") { |
| 106 | + const params = body?.params ?? {}; |
| 107 | + const checkout = createMockCheckout({ |
| 108 | + amount: Number(params.amount) || 2500, |
| 109 | + currency: params.currency === "SAT" ? "SAT" : "USD", |
| 110 | + metadata: params.metadata ?? params, |
| 111 | + successUrl: params.successUrl, |
| 112 | + }); |
| 113 | + setCheckout(checkout); |
| 114 | + return NextResponse.json({ data: checkout }); |
| 115 | + } |
| 116 | + |
| 117 | + if (handler === "get_checkout") { |
| 118 | + const checkoutId: string | undefined = body?.checkoutId; |
| 119 | + if (!mockState.checkout || !checkoutId || mockState.checkout.id !== checkoutId) { |
| 120 | + return NextResponse.json({ error: "Checkout not found" }, { status: 404 }); |
| 121 | + } |
| 122 | + mockState.log.push("client.get"); |
| 123 | + return NextResponse.json({ data: mockState.checkout }); |
| 124 | + } |
| 125 | + |
| 126 | + if (handler === "confirm_checkout") { |
| 127 | + if (!mockState.checkout) { |
| 128 | + return NextResponse.json({ error: "No checkout to confirm" }, { status: 404 }); |
| 129 | + } |
| 130 | + return NextResponse.json({ data: mockState.checkout }); |
| 131 | + } |
| 132 | + |
| 133 | + if (handler === "webhook" || handler === "webhooks") { |
| 134 | + markPaid(); |
| 135 | + mockState.log.push("webhook.incoming-payment"); |
| 136 | + return NextResponse.json({ ok: true }); |
| 137 | + } |
| 138 | + |
| 139 | + if (handler === "ping") { |
| 140 | + return NextResponse.json({ ok: true }); |
| 141 | + } |
| 142 | + |
| 143 | + return NextResponse.json({ error: "Unsupported handler" }, { status: 400 }); |
| 144 | +} |
| 145 | + |
| 146 | +export async function GET() { |
| 147 | + if (!mockState.checkout) { |
| 148 | + return NextResponse.json({ status: "NO_CHECKOUT", log: mockState.log }); |
| 149 | + } |
| 150 | + return NextResponse.json({ |
| 151 | + status: mockState.checkout.status, |
| 152 | + log: mockState.log, |
| 153 | + checkout: mockState.checkout, |
| 154 | + }); |
| 155 | +} |
0 commit comments