Skip to content

Commit abee341

Browse files
geroplroboquat
authored andcommitted
[server] BillingMode: Make sure that users&teams with a Stripe subscription always stay "usage-based", no matter the Feature-Flag status
1 parent 0d08d0b commit abee341

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

components/server/ee/src/billing/billing-mode.spec.db.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,32 @@ class BillingModeSpec {
487487
paid: true,
488488
},
489489
},
490+
// rollback: make sure users/teams with Stripe subscription stay UBP
491+
{
492+
name: "team: stripe paid, w/o UBP",
493+
subject: team(),
494+
config: {
495+
enablePayment: true,
496+
usageBasedPricingEnabled: false,
497+
stripeSubscription: stripeSubscription(),
498+
},
499+
expectation: {
500+
mode: "usage-based",
501+
paid: true,
502+
},
503+
},
504+
{
505+
name: "user: stripe paid, w/o UBP",
506+
subject: user(),
507+
config: {
508+
enablePayment: true,
509+
usageBasedPricingEnabled: false,
510+
stripeSubscription: stripeSubscription(),
511+
},
512+
expectation: {
513+
mode: "usage-based",
514+
},
515+
},
490516
];
491517

492518
const onlyTest = tests.find((t) => t.only);

components/server/ee/src/billing/billing-mode.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,17 @@ export class BillingModesImpl implements BillingModes {
102102
);
103103
}
104104

105+
// Stripe: Active personal subsciption?
106+
let hasUbbPersonal = false;
107+
const billingStrategy = await this.usageService.getCurrentBillingStategy({ kind: "user", userId: user.id });
108+
if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) {
109+
hasUbbPersonal = true;
110+
}
111+
105112
// 1. UBB enabled?
106-
if (!isUsageBasedBillingEnabled) {
113+
if (!isUsageBasedBillingEnabled && !hasUbbPersonal) {
107114
// UBB is not enabled: definitely chargebee
115+
// EXCEPT we're doing a rollback
108116
return { mode: "chargebee" };
109117
}
110118

@@ -138,13 +146,6 @@ export class BillingModesImpl implements BillingModes {
138146
}
139147
}
140148

141-
// Stripe: Active personal subsciption?
142-
let hasUbbPersonal = false;
143-
const billingStrategy = await this.usageService.getCurrentBillingStategy({ kind: "user", userId: user.id });
144-
if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) {
145-
hasUbbPersonal = true;
146-
}
147-
148149
// 3. Check team memberships/plans
149150
// UBB overrides wins if there is _any_. But if there is none, use the existing Chargebee subscription.
150151
const teamsModes = await Promise.all(teams.map((t) => this.getBillingModeForTeam(t, now)));
@@ -241,17 +242,18 @@ export class BillingModesImpl implements BillingModes {
241242
return { mode: "chargebee", teamNames: [team.name], paid: true };
242243
}
243244

244-
// 2. UBB enabled at all?
245-
if (!isUsageBasedBillingEnabled) {
246-
return { mode: "chargebee" };
247-
}
248-
249-
// 3. Now we're usage-based. We only have to figure out whether we have a plan yet or not.
250-
const result: BillingMode = { mode: "usage-based" };
245+
// 2. UBP enabled OR respective BillingStrategy?
251246
const billingStrategy = await this.usageService.getCurrentBillingStategy(AttributionId.create(team));
252-
if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) {
253-
result.paid = true;
247+
if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE || isUsageBasedBillingEnabled) {
248+
// Now we're usage-based. We only have to figure out whether we have a paid plan yet or not.
249+
const result: BillingMode = { mode: "usage-based" };
250+
if (billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE) {
251+
result.paid = true;
252+
}
253+
return result;
254254
}
255-
return result;
255+
256+
// 3. Default case if none is enabled: fall back to Chargebee
257+
return { mode: "chargebee" };
256258
}
257259
}

0 commit comments

Comments
 (0)