58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import Stripe from "stripe"
|
|
|
|
export const STRIPE_API_VERSION = "2025-02-24.acacia" as const
|
|
|
|
/**
|
|
* Initialize Stripe client with secret key from environment
|
|
* This should only be used server-side
|
|
*/
|
|
export function getStripeClient(): Stripe {
|
|
const secretKey = process.env.STRIPE_SECRET_KEY
|
|
|
|
if (!secretKey) {
|
|
// In development, provide a helpful error message
|
|
if (process.env.NODE_ENV === "development") {
|
|
throw new Error(
|
|
"STRIPE_SECRET_KEY is not set in environment variables.\n" +
|
|
"Please create a .env.local file with your Stripe API keys:\n" +
|
|
"STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here\n" +
|
|
"STRIPE_SECRET_KEY=sk_test_your_secret_key_here"
|
|
)
|
|
}
|
|
throw new Error("STRIPE_SECRET_KEY is required for Stripe integration")
|
|
}
|
|
|
|
return new Stripe(secretKey, {
|
|
apiVersion: STRIPE_API_VERSION,
|
|
typescript: true,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Initialize Stripe client for browser use
|
|
* This should only be used client-side
|
|
*/
|
|
export function getStripeBrowserClient() {
|
|
if (typeof window === "undefined") {
|
|
throw new Error("getStripeBrowserClient should only be used client-side")
|
|
}
|
|
|
|
const publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
|
|
|
|
if (!publishableKey) {
|
|
if (process.env.NODE_ENV === "development") {
|
|
throw new Error(
|
|
"NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY is not set in environment variables.\n" +
|
|
"Please create a .env.local file with your Stripe API keys:\n" +
|
|
"NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here"
|
|
)
|
|
}
|
|
throw new Error(
|
|
"Stripe publishable key is required for client-side integration"
|
|
)
|
|
}
|
|
|
|
return import("@stripe/stripe-js").then((stripeModule) => {
|
|
return stripeModule.loadStripe(publishableKey)
|
|
})
|
|
}
|