Files
archived-linsa/packages/web/src/lib/stripe.ts
Nikita 103a4ba19c feat: Add Cloudflare StreamPlayer component and update schema with billing and access control
- Introduced `CloudflareStreamPlayer` React component for embedding streams
- Updated `package.json` with new dependencies: `@cloudflare/stream-react` and `stripe`
- Extended database schema with user tiers, Stripe billing, storage, and archive management
- Added access control logic in `access.ts` for user tiers and feature permissions
- Enhanced billing logic with archive storage limits and subscription checks
2025-12-21 14:56:30 -08:00

52 lines
1.5 KiB
TypeScript

import Stripe from "stripe"
type StripeEnv = {
STRIPE_SECRET_KEY?: string
STRIPE_WEBHOOK_SECRET?: string
STRIPE_ARCHIVE_PRICE_ID?: string // Archive subscription price
}
const getEnv = (): StripeEnv => {
let STRIPE_SECRET_KEY: string | undefined
let STRIPE_WEBHOOK_SECRET: string | undefined
let STRIPE_ARCHIVE_PRICE_ID: string | undefined
try {
const { getServerContext } = require("@tanstack/react-start/server") as {
getServerContext: () => { cloudflare?: { env?: StripeEnv } } | null
}
const ctx = getServerContext()
STRIPE_SECRET_KEY = ctx?.cloudflare?.env?.STRIPE_SECRET_KEY
STRIPE_WEBHOOK_SECRET = ctx?.cloudflare?.env?.STRIPE_WEBHOOK_SECRET
STRIPE_ARCHIVE_PRICE_ID = ctx?.cloudflare?.env?.STRIPE_ARCHIVE_PRICE_ID
} catch {
// Not in server context
}
STRIPE_SECRET_KEY = STRIPE_SECRET_KEY ?? process.env.STRIPE_SECRET_KEY
STRIPE_WEBHOOK_SECRET =
STRIPE_WEBHOOK_SECRET ?? process.env.STRIPE_WEBHOOK_SECRET
STRIPE_ARCHIVE_PRICE_ID =
STRIPE_ARCHIVE_PRICE_ID ?? process.env.STRIPE_ARCHIVE_PRICE_ID
return { STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_ARCHIVE_PRICE_ID }
}
export const getStripe = (): Stripe | null => {
const env = getEnv()
if (!env.STRIPE_SECRET_KEY) {
return null
}
return new Stripe(env.STRIPE_SECRET_KEY)
}
export const getStripeConfig = () => {
const env = getEnv()
return {
webhookSecret: env.STRIPE_WEBHOOK_SECRET,
archivePriceId: env.STRIPE_ARCHIVE_PRICE_ID,
}
}