Skip to content

[server] Only try to delete Stripe subscription if the user's BillingMode is "usage-based" #17318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion components/server/ee/src/user/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";
import { ResponseError } from "vscode-ws-jsonrpc";
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";

@injectable()
export class StripeService {
Expand Down Expand Up @@ -98,7 +99,22 @@ export class StripeService {
return priceInformation.humanReadableDescription;
}

async cancelSubscription(subscriptionId: string): Promise<void> {
async cancelSubscriptionForUser(userId: string) {
let subscriptionId;
try {
subscriptionId = await this.findUncancelledSubscriptionByAttributionId(
AttributionId.render({ kind: "user", userId }),
);
if (subscriptionId) {
await this.cancelSubscription(subscriptionId);
}
} catch (error) {
log.error("Error cancelling Stripe user subscription", error, { subscriptionId });
throw new Error(`Failed to cancel stripe subscription. ${error}`);
}
}

protected async cancelSubscription(subscriptionId: string): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is a remnant of our split brain dealing with Stripe.

On usage, there's CancelSubscription which would ideally handle every detail of this, such that it can be uniformly called.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, totally agree that we should fold this code into the usage side.
So far I've refrained because I'm waiting for authN. But we could also do it earlier, too! Maybe out of scope for this PR, though.

await reportStripeOutcome("subscriptions_cancel", () => {
return this.getStripe().subscriptions.del(subscriptionId, { invoice_now: true });
});
Expand Down
21 changes: 6 additions & 15 deletions components/server/src/user/user-deletion-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AuthProviderService } from "../auth/auth-provider-service";
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";
import { Config } from "../config";
import { StripeService } from "../../ee/src/user/stripe-service";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { BillingModes } from "../../ee/src/billing/billing-mode";

@injectable()
export class UserDeletionService {
Expand All @@ -33,6 +33,7 @@ export class UserDeletionService {
@inject(AuthProviderService) protected readonly authProviderService: AuthProviderService;
@inject(IAnalyticsWriter) protected readonly analytics: IAnalyticsWriter;
@inject(StripeService) protected readonly stripeService: StripeService;
@inject(BillingModes) protected readonly billingMode: BillingModes;

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

if (this.config.enablePayment) {
let subscriptionId;
try {
// Also cancel any usage-based (Stripe) subscription
subscriptionId = await this.stripeService.findUncancelledSubscriptionByAttributionId(
AttributionId.render({ kind: "user", userId: user.id }),
);
if (subscriptionId) {
await this.stripeService.cancelSubscription(subscriptionId);
}
} catch (error) {
log.error("Error cancelling Stripe user subscription", error, { subscriptionId });
throw new Error(`Failed to cancel stripe subscription. ${error}`);
}
const billingMode = await this.billingMode.getBillingModeForUser(user, new Date());
if (billingMode.mode === "usage-based") {
// Also cancel any usage-based (Stripe) subscription
await this.stripeService.cancelSubscriptionForUser(user.id);
}

// Stop all workspaces
Expand Down