fix: use lazy initialization for Stripe client
Prevents build-time errors when STRIPE_SECRET_KEY isn't available. The client is now initialized on first use instead of module load. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
6a9a6659bf
commit
60921db465
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
|
|
|
|||
Loading…
Reference in New Issue