diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index a65bd265..4ff86cab 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -148,9 +148,15 @@ export class BackendVariables { @IsNotEmpty() public readonly STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID!: string; + @IsNotEmpty() + public readonly STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID!: string; + @IsNotEmpty() public readonly STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID!: string; + @IsNotEmpty() + public readonly STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID!: string; + @IsNotEmpty() public readonly STRIPE_PAYMENT_SUCCESS_URL!: string; @@ -210,7 +216,9 @@ export class BackendVariables { this.SCW_BUCKET_NAME = process.env["BUCKET_NAME"]!; this.STRIPE_SECRET_KEY = process.env["STRIPE_SECRET_KEY"]!; this.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID"]!; + this.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID"]!; this.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID"]!; + this.STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID"]!; this.STRIPE_PAYMENT_SUCCESS_URL = process.env["STRIPE_PAYMENT_SUCCESS_URL"]!; this.STRIPE_PAYMENT_CANCEL_URL = process.env["STRIPE_PAYMENT_CANCEL_URL"]!; this.IDNOT_PROD_BASE_URL = process.env["IDNOT_PROD_BASE_URL"]!; diff --git a/src/common/webhooks/stripeWebhooks.ts b/src/common/webhooks/stripeWebhooks.ts index b0832ece..85fc45f7 100644 --- a/src/common/webhooks/stripeWebhooks.ts +++ b/src/common/webhooks/stripeWebhooks.ts @@ -24,7 +24,7 @@ export default class StripeWebhooks extends ApiController { const event = req.body; switch (event.type) { - case "invoice.payment_succeeded": + case "invoice.payment_succeeded": if (event.data.object.billing_reason !== "subscription_update") break; const stripeSubscription = await this.stripeService.getClient().subscriptions.retrieve(event.data.object.subscription); const existingSubscription = await this.subscriptionsService.get({where : {stripe_subscription_id : stripeSubscription.id}}); @@ -34,11 +34,17 @@ export default class StripeWebhooks extends ApiController { subscriptionUpdate.start_date = new Date(stripeSubscription.current_period_start * 1000); subscriptionUpdate.end_date = new Date(stripeSubscription.current_period_end * 1000); subscriptionUpdate.nb_seats = stripeSubscription.items.data[0]?.quantity; - subscriptionUpdate.type = stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID ? "STANDARD" : "UNLIMITED"; - const subscriptionEntityUpdate = Subscription.hydrate(subscriptionUpdate); + if(stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID || stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID){ + subscriptionUpdate.type = "STANDARD"; + } + else{ + subscriptionUpdate.type = "UNLIMITED"; + } - await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false }); + const subscriptionEntityUpdate = Subscription.hydrate(subscriptionUpdate); + + await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false }); await this.subscriptionsService.update(existingSubscription[0].uid ,subscriptionEntityUpdate);