fix(server): should auto apply ea price for users (#9082)

This commit is contained in:
forehalo
2024-12-10 05:31:19 +00:00
parent 36a95463b4
commit 564faa439a
6 changed files with 78 additions and 55 deletions
@@ -42,11 +42,9 @@ import { ScheduleManager } from './schedule';
import {
decodeLookupKey,
DEFAULT_PRICES,
encodeLookupKey,
KnownStripeInvoice,
KnownStripePrice,
KnownStripeSubscription,
LookupKey,
retriveLookupKeyFromStripePrice,
retriveLookupKeyFromStripeSubscription,
SubscriptionPlan,
@@ -129,19 +127,6 @@ export class SubscriptionService implements OnApplicationBootstrap {
throw new ActionForbidden();
}
const price = await this.getPrice({
plan,
recurring,
variant: variant ?? null,
});
if (!price) {
throw new SubscriptionPlanNotFound({
plan,
recurring,
});
}
const manager = this.select(plan);
const result = CheckoutExtraArgs.safeParse(args);
@@ -149,7 +134,15 @@ export class SubscriptionService implements OnApplicationBootstrap {
throw new InvalidCheckoutParameters();
}
return manager.checkout(price, params, args);
return manager.checkout(
{
plan,
recurring,
variant: variant ?? null,
},
params,
args
);
}
async cancelSubscription(
@@ -270,7 +263,7 @@ export class SubscriptionService implements OnApplicationBootstrap {
throw new SameSubscriptionRecurring({ recurring });
}
const price = await this.getPrice({
const price = await manager.getPrice({
plan: identity.plan,
recurring,
variant: null,
@@ -469,24 +462,6 @@ export class SubscriptionService implements OnApplicationBootstrap {
.filter(Boolean) as KnownStripePrice[];
}
private async getPrice(
lookupKey: LookupKey
): Promise<KnownStripePrice | null> {
const prices = await this.stripe.prices.list({
lookup_keys: [encodeLookupKey(lookupKey)],
limit: 1,
});
const price = prices.data[0];
return price
? {
lookupKey,
price,
}
: null;
}
private async parseStripeInvoice(
invoice: Stripe.Invoice
): Promise<KnownStripeInvoice | null> {