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 if (!stripeSecretKey) { return NextResponse.json({ error: "Stripe not configured" }, { status: 500 }) } const stripe = new Stripe(stripeSecretKey, { apiVersion: "2024-12-18.acacia", }) const formData = await request.formData() const paymentMethod = formData.get("paymentMethod") as string const registrationDataStr = formData.get("registrationData") as string // Parse registration data const registrationData = registrationDataStr ? JSON.parse(registrationDataStr) : null // Configure payment method types based on selection let paymentMethodTypes: Stripe.Checkout.SessionCreateParams.PaymentMethodType[] = ["card"] if (paymentMethod === "sepa_debit") { paymentMethodTypes = ["sepa_debit"] } else if (paymentMethod === "crypto") { paymentMethodTypes = ["customer_balance"] } const session = await stripe.checkout.sessions.create({ payment_method_types: paymentMethodTypes, line_items: [ { price_data: { currency: "eur", product_data: { name: "Crypto Commons Gathering 2026", description: "Full event access including all sessions and infrastructure", images: ["https://ccg2025.vercel.app/og-image.png"], }, unit_amount: 20000, // €200 }, quantity: 1, adjustable_quantity: { enabled: true, minimum: 1, maximum: 5, }, }, ], mode: "payment", success_url: `${request.nextUrl.origin}/success?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${request.nextUrl.origin}/register`, metadata: registrationData ? { name: registrationData.name, contact: registrationData.contact, contributions: registrationData.contributions.substring(0, 500), // Stripe has length limits expectations: registrationData.expectations.substring(0, 500), howHeard: registrationData.howHeard || "", dietary: registrationData.dietary.join(", ") + (registrationData.dietaryOther ? `, ${registrationData.dietaryOther}` : ""), crewConsent: registrationData.crewConsent, } : {}, // For stablecoin payments ...(paymentMethod === "crypto" && { payment_method_options: { customer_balance: { funding_type: "crypto", bank_transfer: { type: "crypto", }, }, }, }), allow_promotion_codes: true, billing_address_collection: "required", phone_number_collection: { enabled: true, }, }) return NextResponse.redirect(session.url!) } catch (err) { console.error("[v0] Error creating checkout session:", err) return NextResponse.json({ error: "Error creating checkout session" }, { status: 500 }) } }