diff --git a/app/api/create-checkout-session/route.ts b/app/api/create-checkout-session/route.ts index faca94a..2b68b08 100644 --- a/app/api/create-checkout-session/route.ts +++ b/app/api/create-checkout-session/route.ts @@ -1,9 +1,19 @@ import { type NextRequest, NextResponse } from "next/server" import Stripe from "stripe" -const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { - apiVersion: "2024-12-18.acacia", -}) +// Lazy initialization to avoid build-time errors when env vars aren't set +let stripeInstance: Stripe | null = null +function getStripe(): Stripe { + if (!stripeInstance) { + if (!process.env.STRIPE_SECRET_KEY) { + throw new Error("STRIPE_SECRET_KEY is not configured") + } + stripeInstance = new Stripe(process.env.STRIPE_SECRET_KEY, { + apiVersion: "2024-12-18.acacia", + }) + } + return stripeInstance +} const CCG_TICKET_PRICE_ID = "price_1SbokZ8IwXvKSVJpRvkTqePT" const CCG_ACCOMMODATION_PRICE_ID = "price_1Sboq08IwXvKSVJpf8RRSoCy" @@ -50,7 +60,7 @@ export async function POST(request: NextRequest) { paymentMethodTypes = ["customer_balance"] } - const session = await stripe.checkout.sessions.create({ + const session = await getStripe().checkout.sessions.create({ payment_method_types: paymentMethodTypes, line_items: lineItems, mode: "payment", diff --git a/app/api/webhook/route.ts b/app/api/webhook/route.ts index b5b050f..1b00084 100644 --- a/app/api/webhook/route.ts +++ b/app/api/webhook/route.ts @@ -1,11 +1,26 @@ import { type NextRequest, NextResponse } from "next/server" import Stripe from "stripe" -const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { - apiVersion: "2024-12-18.acacia", -}) +// Lazy initialization to avoid build-time errors when env vars aren't set +let stripeInstance: Stripe | null = null +function getStripe(): Stripe { + if (!stripeInstance) { + if (!process.env.STRIPE_SECRET_KEY) { + throw new Error("STRIPE_SECRET_KEY is not configured") + } + stripeInstance = new Stripe(process.env.STRIPE_SECRET_KEY, { + apiVersion: "2024-12-18.acacia", + }) + } + return stripeInstance +} -const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET! +function getWebhookSecret(): string { + if (!process.env.STRIPE_WEBHOOK_SECRET) { + throw new Error("STRIPE_WEBHOOK_SECRET is not configured") + } + return process.env.STRIPE_WEBHOOK_SECRET +} export async function POST(request: NextRequest) { try { @@ -15,7 +30,7 @@ export async function POST(request: NextRequest) { let event: Stripe.Event try { - event = stripe.webhooks.constructEvent(body, signature, webhookSecret) + event = getStripe().webhooks.constructEvent(body, signature, getWebhookSecret()) } catch (err) { console.error("[v0] Webhook signature verification failed:", err) return NextResponse.json({ error: "Invalid signature" }, { status: 400 })