Skip to content

[server] Remove createStripeCustomer, handled by usage instead #14060

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 1 commit into from
Oct 25, 2022
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
31 changes: 0 additions & 31 deletions components/server/ee/src/user/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import {
} from "../../../src/prometheus-metrics";
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";

const POLL_CREATED_CUSTOMER_INTERVAL_MS = 1000;
const POLL_CREATED_CUSTOMER_MAX_ATTEMPTS = 30;

@injectable()
export class StripeService {
@inject(Config) protected readonly config: Config;
Expand Down Expand Up @@ -58,34 +55,6 @@ export class StripeService {
}
}

async createCustomerForAttributionId(
attributionId: string,
preferredCurrency: string,
billingEmail?: string,
billingName?: string,
): Promise<string> {
if (await this.findCustomerByAttributionId(attributionId)) {
throw new Error(`A Stripe customer already exists for '${attributionId}'`);
}
// Create the customer in Stripe
const customer = await reportStripeOutcome("customers_create", () => {
return this.getStripe().customers.create({
email: billingEmail,
name: billingName,
metadata: { attributionId, preferredCurrency },
});
});
// Wait for the customer to show up in Stripe search results before proceeding
let attempts = 0;
while (!(await this.findCustomerByAttributionId(attributionId))) {
await new Promise((resolve) => setTimeout(resolve, POLL_CREATED_CUSTOMER_INTERVAL_MS));
if (++attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS) {
throw new Error(`Could not confirm Stripe customer creation for '${attributionId}'`);
}
}
return customer.id;
}

async setDefaultPaymentMethodForCustomer(customerId: string, setupIntentId: string): Promise<void> {
const setupIntent = await reportStripeOutcome("intent_retrieve", () => {
return this.getStripe().setupIntents.retrieve(setupIntentId);
Expand Down
51 changes: 11 additions & 40 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ import { EntitlementService, MayStartWorkspaceResult } from "../../../src/billin
import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode";
import { BillingModes } from "../billing/billing-mode";
import { UsageServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/usage.pb";
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";

@injectable()
Expand Down Expand Up @@ -2112,48 +2111,20 @@ export class GitpodServerEEImpl extends GitpodServerImpl {

const billingEmail = User.getPrimaryEmail(user);
const billingName = attrId.kind === "team" ? team!.name : User.getName(user);

const isCreateStripeCustomerOnUsageEnabled = await getExperimentsClientForBackend().getValueAsync(
"createStripeCustomersOnUsage",
false,
{
user: user,
teamId: team ? team.id : undefined,
},
);
if (isCreateStripeCustomerOnUsageEnabled) {
try {
try {
try {
// customer already exists, we don't need to create a new one.
await this.billingService.getStripeCustomer({ attributionId });
return;
} catch (e) {}

await this.billingService.createStripeCustomer({
attributionId,
currency,
email: billingEmail,
name: billingName,
});
// customer already exists, we don't need to create a new one.
await this.billingService.getStripeCustomer({ attributionId });
return;
} catch (error) {
log.error(`Failed to create Stripe customer profile for '${attributionId}'`, error);
throw new ResponseError(
ErrorCodes.INTERNAL_SERVER_ERROR,
`Failed to create Stripe customer profile for '${attributionId}'`,
);
}
}
} catch (e) {}

try {
if (!(await this.stripeService.findCustomerByAttributionId(attributionId))) {
await this.stripeService.createCustomerForAttributionId(
attributionId,
currency,
billingEmail,
billingName,
);
}
await this.billingService.createStripeCustomer({
attributionId,
currency,
email: billingEmail,
name: billingName,
});
return;
} catch (error) {
log.error(`Failed to create Stripe customer profile for '${attributionId}'`, error);
throw new ResponseError(
Expand Down