46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import type { SubscriptionTier } from '@/lib/entitlements';
|
|
|
|
export type BillingTier = 'PRO' | 'BUSINESS';
|
|
export type BillingInterval = 'month' | 'year';
|
|
|
|
export function resolvePriceId(tier: BillingTier, interval: BillingInterval): string {
|
|
const map: Record<BillingTier, Record<BillingInterval, string>> = {
|
|
PRO: {
|
|
month: process.env.STRIPE_PRICE_PRO_MONTHLY!,
|
|
year: process.env.STRIPE_PRICE_PRO_ANNUAL!,
|
|
},
|
|
BUSINESS: {
|
|
month: process.env.STRIPE_PRICE_BUSINESS_MONTHLY!,
|
|
year: process.env.STRIPE_PRICE_BUSINESS_ANNUAL!,
|
|
},
|
|
};
|
|
const priceId = map[tier][interval];
|
|
if (!priceId) {
|
|
const isMock = !process.env.STRIPE_SECRET_KEY || process.env.STRIPE_SECRET_KEY === 'sk_test_placeholder';
|
|
if (isMock && process.env.NODE_ENV !== 'test') {
|
|
return `price_mock_${tier.toLowerCase()}_${interval}`;
|
|
}
|
|
throw new Error(`No Stripe price ID configured for ${tier}/${interval}`);
|
|
}
|
|
return priceId;
|
|
}
|
|
|
|
export function priceIdToTier(priceId: string): SubscriptionTier | null {
|
|
if (priceId && priceId.startsWith('price_mock_')) {
|
|
if (priceId.includes('pro')) return 'PRO';
|
|
if (priceId.includes('business')) return 'BUSINESS';
|
|
return 'BASIC';
|
|
}
|
|
|
|
const entries: Array<[string | undefined, SubscriptionTier]> = [
|
|
[process.env.STRIPE_PRICE_PRO_MONTHLY, 'PRO'],
|
|
[process.env.STRIPE_PRICE_PRO_ANNUAL, 'PRO'],
|
|
[process.env.STRIPE_PRICE_BUSINESS_MONTHLY, 'BUSINESS'],
|
|
[process.env.STRIPE_PRICE_BUSINESS_ANNUAL, 'BUSINESS'],
|
|
];
|
|
for (const [envPriceId, tier] of entries) {
|
|
if (envPriceId && envPriceId === priceId) return tier;
|
|
}
|
|
return null;
|
|
}
|