Skip to content

Commit 0a1ea38

Browse files
authored
[server] Only try to delete Stripe subscription if the user's BillingMode is "usage-based" (#17318)
* [server] Only try to delete Stripe subscription if the user's BillingMode is "usage-based" * [server] Push subscription cancellation into StripeService
1 parent b57424a commit 0a1ea38

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

components/server/ee/src/user/stripe-service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";
1717
import { ResponseError } from "vscode-ws-jsonrpc";
1818
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
19+
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
1920

2021
@injectable()
2122
export class StripeService {
@@ -98,7 +99,22 @@ export class StripeService {
9899
return priceInformation.humanReadableDescription;
99100
}
100101

101-
async cancelSubscription(subscriptionId: string): Promise<void> {
102+
async cancelSubscriptionForUser(userId: string) {
103+
let subscriptionId;
104+
try {
105+
subscriptionId = await this.findUncancelledSubscriptionByAttributionId(
106+
AttributionId.render({ kind: "user", userId }),
107+
);
108+
if (subscriptionId) {
109+
await this.cancelSubscription(subscriptionId);
110+
}
111+
} catch (error) {
112+
log.error("Error cancelling Stripe user subscription", error, { subscriptionId });
113+
throw new Error(`Failed to cancel stripe subscription. ${error}`);
114+
}
115+
}
116+
117+
protected async cancelSubscription(subscriptionId: string): Promise<void> {
102118
await reportStripeOutcome("subscriptions_cancel", () => {
103119
return this.getStripe().subscriptions.del(subscriptionId, { invoice_now: true });
104120
});

components/server/src/user/user-deletion-service.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { AuthProviderService } from "../auth/auth-provider-service";
1616
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";
1717
import { Config } from "../config";
1818
import { StripeService } from "../../ee/src/user/stripe-service";
19-
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
19+
import { BillingModes } from "../../ee/src/billing/billing-mode";
2020

2121
@injectable()
2222
export class UserDeletionService {
@@ -33,6 +33,7 @@ export class UserDeletionService {
3333
@inject(AuthProviderService) protected readonly authProviderService: AuthProviderService;
3434
@inject(IAnalyticsWriter) protected readonly analytics: IAnalyticsWriter;
3535
@inject(StripeService) protected readonly stripeService: StripeService;
36+
@inject(BillingModes) protected readonly billingMode: BillingModes;
3637

3738
/**
3839
* This method deletes a User logically. The contract here is that after running this method without receiving an
@@ -50,20 +51,10 @@ export class UserDeletionService {
5051
log.debug({ userId: id }, "Is deleted but markDeleted already set. Continuing.");
5152
}
5253

53-
if (this.config.enablePayment) {
54-
let subscriptionId;
55-
try {
56-
// Also cancel any usage-based (Stripe) subscription
57-
subscriptionId = await this.stripeService.findUncancelledSubscriptionByAttributionId(
58-
AttributionId.render({ kind: "user", userId: user.id }),
59-
);
60-
if (subscriptionId) {
61-
await this.stripeService.cancelSubscription(subscriptionId);
62-
}
63-
} catch (error) {
64-
log.error("Error cancelling Stripe user subscription", error, { subscriptionId });
65-
throw new Error(`Failed to cancel stripe subscription. ${error}`);
66-
}
54+
const billingMode = await this.billingMode.getBillingModeForUser(user, new Date());
55+
if (billingMode.mode === "usage-based") {
56+
// Also cancel any usage-based (Stripe) subscription
57+
await this.stripeService.cancelSubscriptionForUser(user.id);
6758
}
6859

6960
// Stop all workspaces

0 commit comments

Comments
 (0)