mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-07 19:22:14 +09:00
refactor(api): replace stripe mock currency ternary chains (#2688)
This commit is contained in:
@@ -353,6 +353,16 @@ interface MockStripeSubscriptionSchedule {
|
||||
}>;
|
||||
}
|
||||
|
||||
const PRICE_ID_CURRENCY_MARKERS = ['eur', 'brl', 'dkk', 'inr', 'nok', 'pln', 'sek', 'try'] as const;
|
||||
|
||||
function inferPriceIdCurrency(normalizedPriceId: string): string {
|
||||
return PRICE_ID_CURRENCY_MARKERS.find((marker) => normalizedPriceId.includes(marker)) ?? 'usd';
|
||||
}
|
||||
|
||||
function inferPriceIdInterval(normalizedPriceId: string): 'month' | 'year' {
|
||||
return normalizedPriceId.includes('year') ? 'year' : 'month';
|
||||
}
|
||||
|
||||
function parseFormDataToObject<T extends object = Record<string, unknown>>(formData: FormData): T {
|
||||
const result: Record<string, unknown> = {};
|
||||
const isArrayIndex = (segment: string): boolean => /^\d+$/.test(segment);
|
||||
@@ -797,24 +807,8 @@ export function createStripeApiHandlers(config: StripeApiMockConfig = {}): Strip
|
||||
function inferSubscriptionPriceState(priceId: string): Pick<MockStripeSubscriptionState, 'currency' | 'interval'> {
|
||||
const normalizedPriceId = priceId.toLowerCase();
|
||||
return {
|
||||
currency: normalizedPriceId.includes('eur')
|
||||
? 'eur'
|
||||
: normalizedPriceId.includes('brl')
|
||||
? 'brl'
|
||||
: normalizedPriceId.includes('dkk')
|
||||
? 'dkk'
|
||||
: normalizedPriceId.includes('inr')
|
||||
? 'inr'
|
||||
: normalizedPriceId.includes('nok')
|
||||
? 'nok'
|
||||
: normalizedPriceId.includes('pln')
|
||||
? 'pln'
|
||||
: normalizedPriceId.includes('sek')
|
||||
? 'sek'
|
||||
: normalizedPriceId.includes('try')
|
||||
? 'try'
|
||||
: 'usd',
|
||||
interval: normalizedPriceId.includes('year') ? 'year' : 'month',
|
||||
currency: inferPriceIdCurrency(normalizedPriceId),
|
||||
interval: inferPriceIdInterval(normalizedPriceId),
|
||||
};
|
||||
}
|
||||
function createDefaultSubscriptionState(): MockStripeSubscriptionState {
|
||||
@@ -1724,29 +1718,11 @@ export function createStripeApiHandlers(config: StripeApiMockConfig = {}): Strip
|
||||
id,
|
||||
object: 'price',
|
||||
active: true,
|
||||
currency:
|
||||
overrides?.currency ??
|
||||
(normalizedPriceId.includes('eur')
|
||||
? 'eur'
|
||||
: normalizedPriceId.includes('brl')
|
||||
? 'brl'
|
||||
: normalizedPriceId.includes('dkk')
|
||||
? 'dkk'
|
||||
: normalizedPriceId.includes('inr')
|
||||
? 'inr'
|
||||
: normalizedPriceId.includes('nok')
|
||||
? 'nok'
|
||||
: normalizedPriceId.includes('pln')
|
||||
? 'pln'
|
||||
: normalizedPriceId.includes('sek')
|
||||
? 'sek'
|
||||
: normalizedPriceId.includes('try')
|
||||
? 'try'
|
||||
: 'usd'),
|
||||
unit_amount: overrides?.unit_amount ?? (normalizedPriceId.includes('year') ? 4999 : 499),
|
||||
currency: overrides?.currency ?? inferPriceIdCurrency(normalizedPriceId),
|
||||
unit_amount: overrides?.unit_amount ?? (inferPriceIdInterval(normalizedPriceId) === 'year' ? 4999 : 499),
|
||||
type: 'recurring',
|
||||
recurring: {
|
||||
interval: overrides?.interval ?? (normalizedPriceId.includes('year') ? 'year' : 'month'),
|
||||
interval: overrides?.interval ?? inferPriceIdInterval(normalizedPriceId),
|
||||
interval_count: 1,
|
||||
},
|
||||
product: overrides?.product ?? 'prod_test_1',
|
||||
|
||||
@@ -918,7 +918,7 @@ Default empty. The webhook signing secret. Not forwarded by the shipped Compose
|
||||
|
||||
Default `{}`. Every price ID at once. JSON object. The individual price variables are declared after it and win.
|
||||
|
||||
Thirty-four individual price variables also exist, one per product and currency: `FLUXER_STRIPE_PRICE_MONTHLY_` and `FLUXER_STRIPE_PRICE_YEARLY_` in USD, EUR, BRL, DKK, INR, NOK, PLN, SEK, and TRY, `FLUXER_STRIPE_PRICE_VISIONARY_` and `FLUXER_STRIPE_PRICE_GIFT_VISIONARY_` in USD and EUR, and `FLUXER_STRIPE_PRICE_GIFT_1_MONTH_` and `FLUXER_STRIPE_PRICE_GIFT_1_YEAR_` in USD, EUR, BRL, INR, PLN, and TRY.
|
||||
Individual price variables also exist, one per product and currency: `FLUXER_STRIPE_PRICE_MONTHLY_`, `FLUXER_STRIPE_PRICE_YEARLY_`, `FLUXER_STRIPE_PRICE_GIFT_1_MONTH_` and `FLUXER_STRIPE_PRICE_GIFT_1_YEAR_` in USD, EUR, BRL, DKK, INR, NOK, PLN, SEK, and TRY, plus `FLUXER_STRIPE_PRICE_VISIONARY_` and `FLUXER_STRIPE_PRICE_GIFT_VISIONARY_` in USD and EUR.
|
||||
|
||||
#### `FLUXER_STRIPE_LEGACY_PRICES`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user