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

View File

@ -45,7 +45,8 @@
"amount": "80.28",
"currency": "EUR"
}
]
],
"archivedPrices": []
},
"prod": {
"publishableKey": "pk_live_51IvkOPLx4fybOTqJow8RFsWs0eDznPeBlXMw6s8SIDQeCM8bAFNYlBdDsyonAwRcJgBCoSlvFzAbhJgLFxzzTu4r0006aw846C",
@ -93,6 +94,22 @@
"amount": "80.28",
"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"
}
]
}
}