diff --git a/components/server/ee/src/billing/billing-mode.spec.db.ts b/components/server/ee/src/billing/billing-mode.spec.db.ts index 8bcf05283a1c7f..bf1537c6da008d 100644 --- a/components/server/ee/src/billing/billing-mode.spec.db.ts +++ b/components/server/ee/src/billing/billing-mode.spec.db.ts @@ -487,6 +487,32 @@ class BillingModeSpec { paid: true, }, }, + // rollback: make sure users/teams with Stripe subscription stay UBP + { + name: "team: stripe paid, w/o UBP", + subject: team(), + config: { + enablePayment: true, + usageBasedPricingEnabled: false, + stripeSubscription: stripeSubscription(), + }, + expectation: { + mode: "usage-based", + paid: true, + }, + }, + { + name: "user: stripe paid, w/o UBP", + subject: user(), + config: { + enablePayment: true, + usageBasedPricingEnabled: false, + stripeSubscription: stripeSubscription(), + }, + expectation: { + mode: "usage-based", + }, + }, ]; const onlyTest = tests.find((t) => t.only); diff --git a/components/server/ee/src/billing/billing-mode.ts b/components/server/ee/src/billing/billing-mode.ts index 2aadd4fb382211..a59dbd40560bde 100644 --- a/components/server/ee/src/billing/billing-mode.ts +++ b/components/server/ee/src/billing/billing-mode.ts @@ -102,9 +102,17 @@ export class BillingModesImpl implements BillingModes { ); } + // Stripe: Active personal subsciption? + let hasUbbPersonal = false; + const billingStrategy = await this.usageService.getCurrentBillingStategy({ kind: "user", userId: user.id }); + if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) { + hasUbbPersonal = true; + } + // 1. UBB enabled? - if (!isUsageBasedBillingEnabled) { + if (!isUsageBasedBillingEnabled && !hasUbbPersonal) { // UBB is not enabled: definitely chargebee + // EXCEPT we're doing a rollback return { mode: "chargebee" }; } @@ -138,13 +146,6 @@ export class BillingModesImpl implements BillingModes { } } - // Stripe: Active personal subsciption? - let hasUbbPersonal = false; - const billingStrategy = await this.usageService.getCurrentBillingStategy({ kind: "user", userId: user.id }); - if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) { - hasUbbPersonal = true; - } - // 3. Check team memberships/plans // UBB overrides wins if there is _any_. But if there is none, use the existing Chargebee subscription. const teamsModes = await Promise.all(teams.map((t) => this.getBillingModeForTeam(t, now))); @@ -241,17 +242,18 @@ export class BillingModesImpl implements BillingModes { return { mode: "chargebee", teamNames: [team.name], paid: true }; } - // 2. UBB enabled at all? - if (!isUsageBasedBillingEnabled) { - return { mode: "chargebee" }; - } - - // 3. Now we're usage-based. We only have to figure out whether we have a plan yet or not. - const result: BillingMode = { mode: "usage-based" }; + // 2. UBP enabled OR respective BillingStrategy? const billingStrategy = await this.usageService.getCurrentBillingStategy(AttributionId.create(team)); - if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) { - result.paid = true; + if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE || isUsageBasedBillingEnabled) { + // Now we're usage-based. We only have to figure out whether we have a paid plan yet or not. + const result: BillingMode = { mode: "usage-based" }; + if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) { + result.paid = true; + } + return result; } - return result; + + // 3. Default case if none is enabled: fall back to Chargebee + return { mode: "chargebee" }; } }