Skip to content

Commit

Permalink
[server] BillingMode: Make sure that users&teams with a Stripe subscr…
Browse files Browse the repository at this point in the history
…iption always stay "usage-based", no matter the Feature-Flag status
  • Loading branch information
geropl authored and roboquat committed Nov 29, 2022
1 parent 0d08d0b commit abee341
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
26 changes: 26 additions & 0 deletions components/server/ee/src/billing/billing-mode.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 20 additions & 18 deletions components/server/ee/src/billing/billing-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
}

Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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" };
}
}

0 comments on commit abee341

Please sign in to comment.