Files
simstudioai--sim/apps/sim/lib/billing/plans.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

144 lines
3.8 KiB
TypeScript

import type Stripe from 'stripe'
import { CREDIT_TIERS } from '@/lib/billing/constants'
import { CREDIT_MULTIPLIER } from '@/lib/billing/credits/conversion'
import { isTeam } from '@/lib/billing/plan-helpers'
import { getFreeTierLimit } from '@/lib/billing/subscriptions/utils'
import { env } from '@/lib/core/config/env'
export interface BillingPlan {
name: string
priceId: string
annualDiscountPriceId?: string
limits: {
cost: number
}
}
/**
* Build the billing plans for the Better Auth Stripe plugin.
*
* Plans:
* - free
* - pro_6000 (Pro, $25/mo) + team_6000
* - pro_25000 (Max, $100/mo) + team_25000
* - enterprise (dynamic pricing)
*
* Legacy subscriptions with plan='pro' or plan='team' are handled by
* plan-helpers.ts which maps them to their original dollar amounts.
*/
export function getPlans(): BillingPlan[] {
const plans: BillingPlan[] = [
{
name: 'free',
priceId: env.STRIPE_FREE_PRICE_ID || '',
limits: { cost: getFreeTierLimit() },
},
]
const proPriceMap: Record<number, { monthly: string; annual: string }> = {
25: {
monthly: env.STRIPE_PRICE_TIER_25_MO || '',
annual: env.STRIPE_PRICE_TIER_25_YR || '',
},
100: {
monthly: env.STRIPE_PRICE_TIER_100_MO || '',
annual: env.STRIPE_PRICE_TIER_100_YR || '',
},
}
const teamPriceMap: Record<number, { monthly: string; annual: string }> = {
25: {
monthly: env.STRIPE_PRICE_TEAM_25_MO || '',
annual: env.STRIPE_PRICE_TEAM_25_YR || '',
},
100: {
monthly: env.STRIPE_PRICE_TEAM_100_MO || '',
annual: env.STRIPE_PRICE_TEAM_100_YR || '',
},
}
for (const tier of CREDIT_TIERS) {
const proPrices = proPriceMap[tier.dollars]
const teamPrices = teamPriceMap[tier.dollars]
const creditValueDollars = tier.credits / CREDIT_MULTIPLIER
if (proPrices?.monthly) {
plans.push({
name: `pro_${tier.credits}`,
priceId: proPrices.monthly,
annualDiscountPriceId: proPrices.annual || undefined,
limits: { cost: creditValueDollars },
})
}
if (teamPrices?.monthly) {
plans.push({
name: `team_${tier.credits}`,
priceId: teamPrices.monthly,
annualDiscountPriceId: teamPrices.annual || undefined,
limits: { cost: creditValueDollars },
})
}
}
plans.push({
name: 'enterprise',
priceId: 'price_dynamic',
limits: { cost: 200 },
})
return plans
}
/**
* Get a specific plan by name
*/
export function getPlanByName(planName: string): BillingPlan | undefined {
return getPlans().find((plan) => plan.name === planName)
}
/**
* Get a specific plan by Stripe price ID.
* Matches against both monthly (`priceId`) and annual (`annualDiscountPriceId`) prices.
*/
export function getPlanByPriceId(priceId: string): BillingPlan | undefined {
if (!priceId) return undefined
return getPlans().find(
(plan) => plan.priceId === priceId || plan.annualDiscountPriceId === priceId
)
}
/**
* Get plan limits for a given plan name
*/
export function getPlanLimits(planName: string): number {
const plan = getPlanByName(planName)
return plan?.limits.cost ?? getFreeTierLimit()
}
export interface StripePlanResolution {
priceId: string | undefined
planFromStripe: string | null
isTeamPlan: boolean
isAnnual: boolean
}
/**
* Resolve plan information from a Stripe subscription object.
*/
export function resolvePlanFromStripeSubscription(
stripeSubscription: Stripe.Subscription
): StripePlanResolution {
const priceId = stripeSubscription?.items?.data?.[0]?.price?.id
const interval = stripeSubscription?.items?.data?.[0]?.price?.recurring?.interval
const plan = priceId ? getPlanByPriceId(priceId) : undefined
return {
priceId,
planFromStripe: plan?.name ?? null,
isTeamPlan: plan ? isTeam(plan.name) : false,
isAnnual: interval === 'year',
}
}