import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; import { requireAuth } from "@/lib/auth"; const TOP_UP_AMOUNTS = { 500: { usd: 4.99 }, 1100: { usd: 9.99 }, 3000: { usd: 24.99 }, } as const; export async function POST(req: NextRequest) { const jwtPayload = await requireAuth(req); if (!jwtPayload) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } try { const body = await req.json(); const { amount } = body; if (!amount || !(amount in TOP_UP_AMOUNTS)) { return NextResponse.json( { error: "Invalid amount. Choose 500, 1100, or 3000 FLH." }, { status: 400 } ); } const pricing = TOP_UP_AMOUNTS[amount as keyof typeof TOP_UP_AMOUNTS]; // In production, this would create a Polar.sh checkout session. // For development, we mock the flow by directly adding FLH balance. // Replace with actual Polar API integration when ready. const useMock = process.env.MOCK_PAYMENTS !== "false"; if (useMock) { await prisma.user.update({ where: { id: jwtPayload.userId }, data: { flhBalance: { increment: amount } }, }); return NextResponse.json({ success: true, url: "/wallet?topup=success", mock: true, amount, amountUsd: pricing.usd, }); } // Production path — Polar checkout creation // const polarSession = await createPolarCheckout({ // amount, // customerId: user.stripeCustomerId, // metadata: { userId: jwtPayload.userId, type: "top-up", amount }, // }); return NextResponse.json({ success: true, url: "/wallet?topup=success", // would be polarSession.url amount, amountUsd: pricing.usd, }); } catch (error) { console.error("Top-up checkout error:", error); return NextResponse.json( { error: "Failed to create checkout" }, { status: 500 } ); } }