Line data Source code
1 : /**
2 : * The self-serve tiers' list prices (USD per month, billed monthly or
3 : * yearly) and monthly credit grants. The cloud app's pricing table and both
4 : * checkout hosts read this one table, so a plan cannot price or grant
5 : * differently between the app and billing-web.
6 : */
7 2356 : export const TIER_CATALOG = {
8 : standard: { monthly: 20, yearly: 16, credits: 4200 },
9 : creator: { monthly: 35, yearly: 28, credits: 7400 },
10 : pro: { monthly: 100, yearly: 80, credits: 21100 }
11 : } as const satisfies Record<
12 : string,
13 : { monthly: number; yearly: number; credits: number }
14 : >
15 :
16 : export type CatalogTierKey = keyof typeof TIER_CATALOG
17 :
18 : /** The catalog key for a server tier (`'CREATOR'`), if it is a self-serve one. */
19 : export function toCatalogTierKey(tier: string): CatalogTierKey | undefined {
20 : const key = tier.toLowerCase()
21 : return Object.hasOwn(TIER_CATALOG, key) ? (key as CatalogTierKey) : undefined
22 : }
23 :
24 : /** A team credit stop's list price and grant, as the pricing table shows it. */
25 : export interface TeamCreditStopPrice {
26 : readonly id: string
27 : /** Monthly list price in USD, before the yearly-commitment discount. */
28 : readonly usd: number
29 : readonly credits: number
30 : /** The yearly discount as a whole percent; monthly billing halves it. */
31 : readonly discountPercentYearly: number
32 : /** The served discounted price per month of each cycle, in USD. */
33 : readonly priceUsd: {
34 : readonly monthly: number
35 : readonly yearly: number
36 : }
37 : /** What the server bills for one period of each cycle, in USD. */
38 : readonly periodChargeUsd: {
39 : readonly monthly: number
40 : readonly yearly: number
41 : }
42 : }
43 :
44 : /**
45 : * Reads the server's `team_credit_stops`: the yearly list price is the
46 : * monthly list price, and the discount is the struck vs charged yearly price.
47 : */
48 0 : export function mapApiTeamCreditStops(
49 : stops: readonly {
50 : id: string
51 : credits: number
52 : monthly: { price_cents: number; period_charge_cents: number }
53 : yearly: {
54 : list_price_cents: number
55 : price_cents: number
56 : period_charge_cents: number
57 : }
58 : }[]
59 : ): TeamCreditStopPrice[] {
60 0 : return stops.map((stop) => {
61 0 : const listCents = stop.yearly.list_price_cents
62 0 : const discountPercentYearly =
63 : listCents > 0
64 : ? Math.round(((listCents - stop.yearly.price_cents) / listCents) * 100)
65 : : 0
66 0 : return {
67 : id: stop.id,
68 : usd: Math.round(listCents / 100),
69 : credits: stop.credits,
70 : discountPercentYearly,
71 : priceUsd: {
72 : monthly: stop.monthly.price_cents / 100,
73 : yearly: stop.yearly.price_cents / 100
74 : },
75 : periodChargeUsd: {
76 : monthly: stop.monthly.period_charge_cents / 100,
77 : yearly: stop.yearly.period_charge_cents / 100
78 : }
79 : }
80 : })
81 : }
82 :
83 : /**
84 : * The discounted monthly price of a credit stop for a billing cycle: the
85 : * served price when the stop has one, else the list price less its discount.
86 : */
87 0 : export function getStopDiscountedMonthlyUsd(
88 : stop: Pick<TeamCreditStopPrice, 'usd' | 'discountPercentYearly'> &
89 : Partial<Pick<TeamCreditStopPrice, 'priceUsd'>>,
90 : cycle: 'monthly' | 'yearly'
91 : ): number {
92 0 : if (stop.priceUsd) return stop.priceUsd[cycle]
93 0 : const percent =
94 : cycle === 'monthly'
95 : ? stop.discountPercentYearly / 2
96 : : stop.discountPercentYearly
97 0 : return Math.round(stop.usd * (1 - percent / 100))
98 : }
|