import { type NextRequest, NextResponse } from "next/server" import Stripe from "stripe" export async function POST(request: NextRequest) { try { const stripeSecretKey = process.env.STRIPE_SECRET_KEY const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET if (!stripeSecretKey || !webhookSecret) { return NextResponse.json({ error: "Stripe not configured" }, { status: 500 }) } const stripe = new Stripe(stripeSecretKey, { apiVersion: "2024-12-18.acacia", }) const body = await request.text() const signature = request.headers.get("stripe-signature")! let event: Stripe.Event try { event = stripe.webhooks.constructEvent(body, signature, webhookSecret) } catch (err) { console.error("[v0] Webhook signature verification failed:", err) return NextResponse.json({ error: "Invalid signature" }, { status: 400 }) } // Handle the event switch (event.type) { case "checkout.session.completed": { const session = event.data.object as Stripe.Checkout.Session console.log("[v0] Payment successful:", session.id) // Here you would: // 1. Store the registration in your database // 2. Send confirmation email // 3. Add to attendee list break } case "payment_intent.succeeded": { const paymentIntent = event.data.object as Stripe.PaymentIntent console.log("[v0] PaymentIntent successful:", paymentIntent.id) break } case "payment_intent.payment_failed": { const paymentIntent = event.data.object as Stripe.PaymentIntent console.error("[v0] Payment failed:", paymentIntent.id) break } default: console.log("[v0] Unhandled event type:", event.type) } return NextResponse.json({ received: true }) } catch (err) { console.error("[v0] Webhook error:", err) return NextResponse.json({ error: "Webhook error" }, { status: 500 }) } }