From e0a4009cf64d6c78089ecb23bec88057f69dbd86 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 4 Aug 2023 08:43:15 +0100 Subject: [PATCH] fix metered plan --- Common/Types/Billing/MeteredPlan.ts | 6 ------ CommonServer/Services/BillingService.ts | 17 ++++++++--------- Model/Models/Project.ts | 22 ++++++++++++++++++++++ config.example.env | 2 +- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Common/Types/Billing/MeteredPlan.ts b/Common/Types/Billing/MeteredPlan.ts index 53693f63e8..93f885cb0d 100644 --- a/Common/Types/Billing/MeteredPlan.ts +++ b/Common/Types/Billing/MeteredPlan.ts @@ -1,17 +1,14 @@ export default class MeteredPlan { private monthlyPriceId: string; - private yearlyPriceId: string; private unitName: string; private pricePerUnit: number; public constructor( monthlyPriceId: string, - yearlyPriceId: string, pricePerUnit: number, unitName: string ) { this.monthlyPriceId = monthlyPriceId; - this.yearlyPriceId = yearlyPriceId; this.pricePerUnit = pricePerUnit; this.unitName = unitName; @@ -21,9 +18,6 @@ export default class MeteredPlan { return this.monthlyPriceId; } - public getYearlyPriceId(): string { - return this.yearlyPriceId; - } public getPricePerUnit(): number { return this.pricePerUnit; diff --git a/CommonServer/Services/BillingService.ts b/CommonServer/Services/BillingService.ts index 6972ec362d..7fb3230c81 100644 --- a/CommonServer/Services/BillingService.ts +++ b/CommonServer/Services/BillingService.ts @@ -96,6 +96,7 @@ export class BillingService extends BaseService { promoCode?: string | undefined; }): Promise<{ id: string; + meteredSubscriptionId: string; trialEndsAt: Date | null; }> { if (!this.isBillingEnabled()) { @@ -146,6 +147,10 @@ export class BillingService extends BaseService { const subscription: Stripe.Response = await this.stripe.subscriptions.create(subscriptionParams); + // Create metered subscriptions + const meteredSubscription: Stripe.Response = + await this.stripe.subscriptions.create(subscriptionParams); + for (const serverMeteredPlan of data.serverMeteredPlans) { await serverMeteredPlan.updateCurrentQuantity(data.projectId, { subscriptionId: subscription.id, @@ -221,9 +226,7 @@ export class BillingService extends BaseService { (item: Stripe.SubscriptionItem) => { return ( item.price?.id === - (isYearly - ? meteredPlan.getYearlyPriceId() - : meteredPlan.getMonthlyPriceId()) + meteredPlan.getMonthlyPriceId() ); } ); @@ -235,9 +238,7 @@ export class BillingService extends BaseService { (item: Stripe.SubscriptionItem) => { return ( item.price?.id === - (isYearly - ? meteredPlan.getYearlyPriceId() - : meteredPlan.getMonthlyPriceId()) + meteredPlan.getMonthlyPriceId() ); } )?.id; @@ -258,9 +259,7 @@ export class BillingService extends BaseService { const subscriptionItem: Stripe.SubscriptionItem = await this.stripe.subscriptionItems.create({ subscription: subscriptionId, - price: isYearly - ? meteredPlan.getYearlyPriceId() - : meteredPlan.getMonthlyPriceId(), + price: meteredPlan.getMonthlyPriceId() }); // use stripe usage based api to update the quantity. diff --git a/Model/Models/Project.ts b/Model/Models/Project.ts index e8a1195cd5..675ddc9507 100644 --- a/Model/Models/Project.ts +++ b/Model/Models/Project.ts @@ -158,6 +158,28 @@ export default class Model extends TenantModel { }) public paymentProviderSubscriptionId?: string = undefined; + + @ColumnAccessControl({ + create: [], + read: [ + Permission.ProjectOwner, + Permission.ProjectAdmin, + Permission.ProjectMember, + Permission.CanReadProject, + Permission.UnAuthorizedSsoUser, + Permission.ProjectUser, + ], + update: [], + }) + @TableColumn({ type: TableColumnType.ShortText }) + @Column({ + type: ColumnType.ShortText, + length: ColumnLength.ShortText, + nullable: true, + unique: false, + }) + public paymentProviderMeteredSubscriptionId?: string = undefined; + @ColumnAccessControl({ create: [], read: [ diff --git a/config.example.env b/config.example.env index fc0a379d49..5c729a48f9 100644 --- a/config.example.env +++ b/config.example.env @@ -189,7 +189,7 @@ GLOBAL_PROBE_2_DESCRIPTION="Global probe to monitor oneuptime resources" SENDGRID_API_KEY= # METERED PLANS: This is in the format of MONTHLY_PRICE_ID,YEARLY_PRICE_ID,value per unit,unitName -METERED_PLAN_ACTIVE_MONITORING=priceMonthlyId,priceYearlyId,1,active-monitor,month +METERED_PLAN_ACTIVE_MONITORING=priceMonthlyId,1,active-monitor,month # Twilio Settings