import { NextRequest } from "next/server"; import type Stripe from "stripe"; import { stripe } from "@/lib/billing/stripe"; import { handleStripeEvent } from "@/lib/billing/webhooks/stripe"; export const dynamic = "force-dynamic"; export async function POST(req: NextRequest) { const secret = process.env.STRIPE_WEBHOOK_SECRET; const signature = req.headers.get("stripe-signature"); if (!secret || !signature) return new Response("Webhook not configured", { status: 400 }); const body = await req.text(); let event: Stripe.Event; try { event = stripe().webhooks.constructEvent(body, signature, secret); } catch (err) { console.error("[stripe webhook] signature verification failed", err); return new Response("Invalid signature", { status: 400 }); } try { await handleStripeEvent(event); } catch (err) { console.error("[stripe webhook] handler error", err); return new Response("Handler error", { status: 500 }); } return new Response("ok"); }