import { NextRequest, NextResponse } from "next/server"; import { getStripe } from "@/lib/stripe"; import { setStatus } from "@/lib/orders-store"; // POST /api/webhooks/stripe (docs/04) — verify signature, advance order status. export async function POST(req: NextRequest) { const stripe = getStripe(); const whSecret = process.env.STRIPE_WEBHOOK_SECRET; const sig = req.headers.get("stripe-signature"); if (!stripe || !whSecret || !sig) { return NextResponse.json({ received: false, reason: "not_configured" }, { status: 200 }); } const body = await req.text(); let event; try { event = stripe.webhooks.constructEvent(body, sig, whSecret); } catch { return NextResponse.json({ error: "bad_signature" }, { status: 400 }); } if (event.type === "checkout.session.completed") { const s = event.data.object as { metadata?: { order_id?: string; kind?: string } }; const oid = s.metadata?.order_id; const kind = s.metadata?.kind; if (oid) await setStatus(oid, kind === "balance" ? "delivered" : "in_production"); } return NextResponse.json({ received: true }); }