51 lines
2.0 KiB
TypeScript
51 lines
2.0 KiB
TypeScript
|
|
import { PLAN_ORDER, type PlanKey } from "./plans";
|
||
|
|
|
||
|
|
export type BillingInterval = "month" | "year";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Maps plan keys ↔ provider price/plan IDs, read from env. This is the bridge
|
||
|
|
* between our provider-agnostic plan catalog and Stripe/PayPal. Set the matching
|
||
|
|
* env vars (STRIPE_PRICE_*, PAYPAL_PLAN_*) for the paid tiers.
|
||
|
|
*/
|
||
|
|
const STRIPE_PRICE_ENV: Record<Exclude<PlanKey, "free">, Record<BillingInterval, string | undefined>> = {
|
||
|
|
creator: { month: process.env.STRIPE_PRICE_CREATOR_MONTHLY, year: process.env.STRIPE_PRICE_CREATOR_YEARLY },
|
||
|
|
pro: { month: process.env.STRIPE_PRICE_PRO_MONTHLY, year: process.env.STRIPE_PRICE_PRO_YEARLY },
|
||
|
|
agency: { month: process.env.STRIPE_PRICE_AGENCY_MONTHLY, year: process.env.STRIPE_PRICE_AGENCY_YEARLY },
|
||
|
|
};
|
||
|
|
|
||
|
|
// PayPal plans are created per tier (one billing cycle each); we key by tier and
|
||
|
|
// optionally distinguish interval if you create yearly PayPal plans too.
|
||
|
|
const PAYPAL_PLAN_ENV: Record<Exclude<PlanKey, "free">, string | undefined> = {
|
||
|
|
creator: process.env.PAYPAL_PLAN_CREATOR,
|
||
|
|
pro: process.env.PAYPAL_PLAN_PRO,
|
||
|
|
agency: process.env.PAYPAL_PLAN_AGENCY,
|
||
|
|
};
|
||
|
|
|
||
|
|
export function stripePriceId(plan: PlanKey, interval: BillingInterval): string | undefined {
|
||
|
|
if (plan === "free") return undefined;
|
||
|
|
return STRIPE_PRICE_ENV[plan]?.[interval];
|
||
|
|
}
|
||
|
|
|
||
|
|
export function planFromStripePrice(priceId: string): { plan: PlanKey; interval: BillingInterval } | null {
|
||
|
|
for (const plan of PLAN_ORDER) {
|
||
|
|
if (plan === "free") continue;
|
||
|
|
const intervals = STRIPE_PRICE_ENV[plan];
|
||
|
|
if (intervals.month === priceId) return { plan, interval: "month" };
|
||
|
|
if (intervals.year === priceId) return { plan, interval: "year" };
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function paypalPlanId(plan: PlanKey): string | undefined {
|
||
|
|
if (plan === "free") return undefined;
|
||
|
|
return PAYPAL_PLAN_ENV[plan];
|
||
|
|
}
|
||
|
|
|
||
|
|
export function planFromPaypalPlan(paypalPlanId: string): PlanKey | null {
|
||
|
|
for (const plan of PLAN_ORDER) {
|
||
|
|
if (plan === "free") continue;
|
||
|
|
if (PAYPAL_PLAN_ENV[plan] === paypalPlanId) return plan;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|