28 lines
883 B
TypeScript
28 lines
883 B
TypeScript
import Stripe from "stripe"
|
|||
|
|
|
||
|
|
// Lazily construct the Stripe client so `next build` (which evaluates route
|
||
|
|
// modules to collect page data) does NOT require STRIPE_SECRET_KEY. The key is
|
||
|
|
// only needed at runtime. Call sites keep using `stripe.xxx` unchanged — the
|
||
|
|
// Proxy builds the real client on first property access.
|
||
|
|
let _stripe: Stripe | null = null
|
||
|
|
|
||
|
|
function getStripe(): Stripe {
|
||
|
|
if (!_stripe) {
|
||
|
|
const key = process.env.STRIPE_SECRET_KEY
|
||
|
|
if (!key) throw new Error("STRIPE_SECRET_KEY is not set")
|
||
|
|
_stripe = new Stripe(key, {
|
||
|
|
apiVersion: "2025-03-31.basil",
|
||
|
|
typescript: true,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
return _stripe
|
||
|
|
}
|
||
|
|
|
||
|
|
export const stripe = new Proxy({} as Stripe, {
|
||
|
|
get(_target, prop, receiver) {
|
||
|
|
const client = getStripe()
|
||
|
|
const value = Reflect.get(client, prop, receiver)
|
||
|
|
return typeof value === "function" ? value.bind(client) : value
|
||
|
|
},
|
||
|
|
})
|