Skip to content

Commit 244e43b

Browse files
committed
[server] Refactor StripeService.createCustomer to only use attributionIds
1 parent d3e5716 commit 244e43b

File tree

2 files changed

+15
-40
lines changed

2 files changed

+15
-40
lines changed

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

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,32 @@ export class StripeService {
4343
return result.data[0]?.id;
4444
}
4545

46-
async createCustomerForUser(user: User): Promise<string> {
47-
const attributionId = AttributionId.render({ kind: "user", userId: user.id });
46+
async createCustomerForAttributionId(
47+
attributionId: string,
48+
preferredCurrency: string,
49+
billingEmail?: string,
50+
billingName?: string,
51+
): Promise<string> {
4852
if (await this.findCustomerByAttributionId(attributionId)) {
49-
throw new Error(`A Stripe customer already exists for user '${user.id}'`);
53+
throw new Error(`A Stripe customer already exists for '${attributionId}'`);
5054
}
5155
// Create the customer in Stripe
5256
const customer = await this.getStripe().customers.create({
53-
email: User.getPrimaryEmail(user),
54-
name: User.getName(user),
55-
metadata: { attributionId },
57+
email: billingEmail,
58+
name: billingName,
59+
metadata: { attributionId, preferredCurrency },
5660
});
5761
// Wait for the customer to show up in Stripe search results before proceeding
5862
let attempts = 0;
5963
while (!(await this.findCustomerByAttributionId(attributionId))) {
6064
await new Promise((resolve) => setTimeout(resolve, POLL_CREATED_CUSTOMER_INTERVAL_MS));
6165
if (++attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS) {
62-
throw new Error(`Could not confirm Stripe customer creation for user '${user.id}'`);
66+
throw new Error(`Could not confirm Stripe customer creation for '${attributionId}'`);
6367
}
6468
}
6569
return customer.id;
6670
}
6771

68-
async createCustomerForTeam(user: User, team: Team): Promise<string> {
69-
const attributionId = AttributionId.render({ kind: "team", teamId: team.id });
70-
if (await this.findCustomerByAttributionId(attributionId)) {
71-
throw new Error(`A Stripe customer already exists for team '${team.id}'`);
72-
}
73-
// Create the customer in Stripe
74-
const userName = User.getName(user);
75-
const customer = await this.getStripe().customers.create({
76-
email: User.getPrimaryEmail(user),
77-
name: userName ? `${userName} (${team.name})` : team.name,
78-
metadata: { attributionId },
79-
});
80-
// Wait for the customer to show up in Stripe search results before proceeding
81-
let attempts = 0;
82-
while (!(await this.findCustomerByAttributionId(attributionId))) {
83-
await new Promise((resolve) => setTimeout(resolve, POLL_CREATED_CUSTOMER_INTERVAL_MS));
84-
if (++attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS) {
85-
throw new Error(`Could not confirm Stripe customer creation for team '${team.id}'`);
86-
}
87-
}
88-
return customer.id;
89-
}
90-
91-
async setPreferredCurrencyForCustomer(customerId: string, currency: string): Promise<void> {
92-
await this.getStripe().customers.update(customerId, { metadata: { preferredCurrency: currency } });
93-
}
94-
9572
async setDefaultPaymentMethodForCustomer(customerId: string, setupIntentId: string): Promise<void> {
9673
const setupIntent = await this.getStripe().setupIntents.retrieve(setupIntentId);
9774
if (typeof setupIntent.payment_method !== "string") {

components/server/ee/src/workspace/gitpod-server-impl.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,14 +2099,12 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20992099
if (await this.stripeService.findCustomerByAttributionId(attributionId)) {
21002100
throw new ResponseError(
21012101
ErrorCodes.BAD_REQUEST,
2102-
"A Stripe customer profile already exists for this attributionId",
2102+
`A Stripe customer profile already exists for '${attributionId}'`,
21032103
);
21042104
}
2105-
const customerId =
2106-
attrId.kind === "team"
2107-
? await this.stripeService.createCustomerForTeam(user, team!)
2108-
: await this.stripeService.createCustomerForUser(user);
2109-
await this.stripeService.setPreferredCurrencyForCustomer(customerId, currency);
2105+
const billingEmail = User.getPrimaryEmail(user);
2106+
const billingName = attrId.kind === "team" ? team!.name : User.getName(user);
2107+
await this.stripeService.createCustomerForAttributionId(attributionId, currency, billingEmail, billingName);
21102108
} catch (error) {
21112109
log.error(`Failed to create Stripe customer profile for '${attributionId}'`, error);
21122110
throw new ResponseError(

0 commit comments

Comments
 (0)