New annual prices + fix switch subscription

This commit is contained in:
Vins 2024-04-16 16:34:17 +02:00
parent 70815657e8
commit 58665e1356
2 changed files with 18 additions and 4 deletions

View File

@ -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"]!;

View File

@ -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<Subscription>(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<Subscription>(subscriptionUpdate);
await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false });
await this.subscriptionsService.update(existingSubscription[0].uid ,subscriptionEntityUpdate);