1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Doc: Handle archived prices

This commit is contained in:
Laurent Cozic 2023-10-06 20:53:23 +01:00
parent 33c64f98ef
commit b1461d699f
2 changed files with 41 additions and 16 deletions

View File

@ -65,6 +65,7 @@ export interface StripePublicConfigPrice {
export interface StripePublicConfig { export interface StripePublicConfig {
publishableKey: string; publishableKey: string;
prices: StripePublicConfigPrice[]; prices: StripePublicConfigPrice[];
archivedPrices: StripePublicConfigPrice[];
webhookBaseUrl: string; webhookBaseUrl: string;
} }
@ -86,26 +87,33 @@ export function loadStripeConfig(env: string, filePath: string): StripePublicCon
const config: StripePublicConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))[env]; const config: StripePublicConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))[env];
if (!config) throw new Error(`Invalid env: ${env}`); if (!config) throw new Error(`Invalid env: ${env}`);
config.prices = config.prices.map(p => { const decoratePrices = (p: StripePublicConfigPrice) => {
return { return {
...p, ...p,
formattedAmount: formatPrice(p.amount, p.currency), formattedAmount: formatPrice(p.amount, p.currency),
formattedMonthlyAmount: p.period === PricePeriod.Monthly ? formatPrice(p.amount, p.currency) : formatPrice(Number(p.amount) / 12, p.currency), formattedMonthlyAmount: p.period === PricePeriod.Monthly ? formatPrice(p.amount, p.currency) : formatPrice(Number(p.amount) / 12, p.currency),
}; };
}); };
config.prices = config.prices.map(decoratePrices);
config.archivedPrices = config.archivedPrices.map(decoratePrices);
return config; return config;
} }
export function findPrice(prices: StripePublicConfigPrice[], query: FindPriceQuery): StripePublicConfigPrice { export function findPrice(config: StripePublicConfig, query: FindPriceQuery): StripePublicConfigPrice {
let output: StripePublicConfigPrice = null; let output: StripePublicConfigPrice = null;
if (query.accountType && query.period) { for (const prices of [config.prices, config.archivedPrices]) {
output = prices.filter(p => p.accountType === query.accountType).find(p => p.period === query.period); if (query.accountType && query.period) {
} else if (query.priceId) { output = prices.filter(p => p.accountType === query.accountType).find(p => p.period === query.period);
output = prices.find(p => p.id === query.priceId); } else if (query.priceId) {
} else { output = prices.find(p => p.id === query.priceId);
throw new Error(`Invalid query: ${JSON.stringify(query)}`); } else {
throw new Error(`Invalid query: ${JSON.stringify(query)}`);
}
if (output) break;
} }
if (!output) throw new Error(`Not found: ${JSON.stringify(query)}`); if (!output) throw new Error(`Not found: ${JSON.stringify(query)}`);
@ -332,11 +340,11 @@ export function getPlans(stripeConfig: StripePublicConfig): Record<PlanName, Pla
basic: { basic: {
name: 'basic', name: 'basic',
title: _('Basic'), title: _('Basic'),
priceMonthly: findPrice(stripeConfig.prices, { priceMonthly: findPrice(stripeConfig, {
accountType: 1, accountType: 1,
period: PricePeriod.Monthly, period: PricePeriod.Monthly,
}), }),
priceYearly: findPrice(stripeConfig.prices, { priceYearly: findPrice(stripeConfig, {
accountType: 1, accountType: 1,
period: PricePeriod.Yearly, period: PricePeriod.Yearly,
}), }),
@ -354,11 +362,11 @@ export function getPlans(stripeConfig: StripePublicConfig): Record<PlanName, Pla
pro: { pro: {
name: 'pro', name: 'pro',
title: _('Pro'), title: _('Pro'),
priceMonthly: findPrice(stripeConfig.prices, { priceMonthly: findPrice(stripeConfig, {
accountType: 2, accountType: 2,
period: PricePeriod.Monthly, period: PricePeriod.Monthly,
}), }),
priceYearly: findPrice(stripeConfig.prices, { priceYearly: findPrice(stripeConfig, {
accountType: 2, accountType: 2,
period: PricePeriod.Yearly, period: PricePeriod.Yearly,
}), }),
@ -376,11 +384,11 @@ export function getPlans(stripeConfig: StripePublicConfig): Record<PlanName, Pla
teams: { teams: {
name: 'teams', name: 'teams',
title: _('Teams'), title: _('Teams'),
priceMonthly: findPrice(stripeConfig.prices, { priceMonthly: findPrice(stripeConfig, {
accountType: 3, accountType: 3,
period: PricePeriod.Monthly, period: PricePeriod.Monthly,
}), }),
priceYearly: findPrice(stripeConfig.prices, { priceYearly: findPrice(stripeConfig, {
accountType: 3, accountType: 3,
period: PricePeriod.Yearly, period: PricePeriod.Yearly,
}), }),

View File

@ -45,7 +45,8 @@
"amount": "80.28", "amount": "80.28",
"currency": "EUR" "currency": "EUR"
} }
] ],
"archivedPrices": []
}, },
"prod": { "prod": {
"publishableKey": "pk_live_51IvkOPLx4fybOTqJow8RFsWs0eDznPeBlXMw6s8SIDQeCM8bAFNYlBdDsyonAwRcJgBCoSlvFzAbhJgLFxzzTu4r0006aw846C", "publishableKey": "pk_live_51IvkOPLx4fybOTqJow8RFsWs0eDznPeBlXMw6s8SIDQeCM8bAFNYlBdDsyonAwRcJgBCoSlvFzAbhJgLFxzzTu4r0006aw846C",
@ -93,6 +94,22 @@
"amount": "80.28", "amount": "80.28",
"currency": "EUR" "currency": "EUR"
} }
],
"archivedPrices": [
{
"accountType": 1,
"id": "price_1JAzWBLx4fybOTqJw64zxJRJ",
"period": "monthly",
"amount": "1.99",
"currency": "EUR"
},
{
"accountType": 1,
"id": "price_1JJIPZLx4fybOTqJHvxiQ7bV",
"period": "yearly",
"amount": "17.88",
"currency": "EUR"
}
] ]
} }
} }