Skip to content

Commit cfa3bed

Browse files
committed
[server][dashboard] Refactor createOrUpdateStripeCustomerFor{User,Team} to createStripeCustomer
1 parent 7217ad3 commit cfa3bed

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

components/dashboard/src/components/UsageBasedBillingConfig.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,8 @@ function CreditCardInputForm(props: { attributionId: string }) {
295295
setBillingError(undefined);
296296
setIsLoading(true);
297297
try {
298-
if (attrId.kind === "team") {
299-
// Create Stripe customer for team & currency (or update currency)
300-
await getGitpodService().server.createOrUpdateStripeCustomerForTeam(attrId.teamId, currency);
301-
} else {
302-
// Create Stripe customer for user & currency (or update currency)
303-
await getGitpodService().server.createOrUpdateStripeCustomerForUser(currency);
304-
}
298+
// Create Stripe customer with currency
299+
await getGitpodService().server.createStripeCustomer(props.attributionId, currency);
305300
const result = await stripe.confirmSetup({
306301
elements,
307302
confirmParams: {

components/gitpod-protocol/src/gitpod-service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
287287
getStripePublishableKey(): Promise<string>;
288288
getStripeSetupIntentClientSecret(): Promise<string>;
289289
findStripeSubscriptionId(attributionId: string): Promise<string | undefined>;
290-
createOrUpdateStripeCustomerForTeam(teamId: string, currency: string): Promise<void>;
291-
createOrUpdateStripeCustomerForUser(currency: string): Promise<void>;
290+
createStripeCustomer(attributionId: string, currency: string): Promise<void>;
292291
subscribeToStripe(attributionId: string, setupIntentId: string): Promise<void>;
293292
getStripePortalUrl(attributionId: string): Promise<string>;
294293
getUsageLimit(attributionId: string): Promise<number | undefined>;

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,39 +2082,42 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20822082
}
20832083
}
20842084

2085-
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
2086-
const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForTeam");
2087-
const team = await this.guardTeamOperation(teamId, "update");
2088-
await this.ensureStripeApiIsAllowed({ team });
2089-
try {
2090-
let customerId = await this.stripeService.findCustomerByTeamId(team!.id);
2091-
if (!customerId) {
2092-
customerId = await this.stripeService.createCustomerForTeam(user, team!);
2085+
async createStripeCustomer(ctx: TraceContext, attributionId: string, currency: string): Promise<void> {
2086+
const user = this.checkAndBlockUser("createStripeCustomer");
2087+
const attrId = AttributionId.parse(attributionId);
2088+
if (!attrId) {
2089+
throw new ResponseError(ErrorCodes.BAD_REQUEST, `Invalid attributionId '${attributionId}'`);
2090+
}
2091+
let team: Team | undefined;
2092+
if (attrId.kind === "team") {
2093+
team = await this.guardTeamOperation(attrId.teamId, "update");
2094+
await this.ensureStripeApiIsAllowed({ team });
2095+
} else {
2096+
if (attrId.userId !== user.id) {
2097+
throw new ResponseError(
2098+
ErrorCodes.PERMISSION_DENIED,
2099+
"Cannot create Stripe customer profile for another user",
2100+
);
20932101
}
2094-
await this.stripeService.setPreferredCurrencyForCustomer(customerId, currency);
2095-
} catch (error) {
2096-
log.error(`Failed to update Stripe customer profile for team '${teamId}'`, error);
2097-
throw new ResponseError(
2098-
ErrorCodes.INTERNAL_SERVER_ERROR,
2099-
`Failed to update Stripe customer profile for team '${teamId}'`,
2100-
);
2102+
await this.ensureStripeApiIsAllowed({ user });
21012103
}
2102-
}
2103-
2104-
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
2105-
const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForUser");
2106-
await this.ensureStripeApiIsAllowed({ user });
21072104
try {
2108-
let customerId = await this.stripeService.findCustomerByUserId(user.id);
2109-
if (!customerId) {
2110-
customerId = await this.stripeService.createCustomerForUser(user);
2105+
if (await this.stripeService.findCustomerByAttributionId(attributionId)) {
2106+
throw new ResponseError(
2107+
ErrorCodes.BAD_REQUEST,
2108+
"A Stripe customer profile already exists for this attributionId",
2109+
);
21112110
}
2111+
const customerId =
2112+
attrId.kind === "team"
2113+
? await this.stripeService.createCustomerForTeam(user, team!)
2114+
: await this.stripeService.createCustomerForUser(user);
21122115
await this.stripeService.setPreferredCurrencyForCustomer(customerId, currency);
21132116
} catch (error) {
2114-
log.error(`Failed to update Stripe customer profile for user '${user.id}'`, error);
2117+
log.error(`Failed to create Stripe customer profile for '${attributionId}'`, error);
21152118
throw new ResponseError(
21162119
ErrorCodes.INTERNAL_SERVER_ERROR,
2117-
`Failed to update Stripe customer profile for user '${user.id}'`,
2120+
`Failed to create Stripe customer profile for '${attributionId}'`,
21182121
);
21192122
}
21202123
}
@@ -2144,7 +2147,7 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
21442147
await this.stripeService.setDefaultPaymentMethodForCustomer(customerId, setupIntentId);
21452148
await this.stripeService.createSubscriptionForCustomer(customerId);
21462149

2147-
// Creating a cost center for this team
2150+
// Creating a cost center for this customer
21482151
await this.usageService.setCostCenter({
21492152
costCenter: {
21502153
attributionId: attributionId,

components/server/src/auth/rate-limiter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ const defaultFunctions: FunctionsConfig = {
201201
getStripePublishableKey: { group: "default", points: 1 },
202202
getStripeSetupIntentClientSecret: { group: "default", points: 1 },
203203
findStripeSubscriptionId: { group: "default", points: 1 },
204-
createOrUpdateStripeCustomerForTeam: { group: "default", points: 1 },
205-
createOrUpdateStripeCustomerForUser: { group: "default", points: 1 },
204+
createStripeCustomer: { group: "default", points: 1 },
206205
subscribeToStripe: { group: "default", points: 1 },
207206
getStripePortalUrl: { group: "default", points: 1 },
208207
listUsage: { group: "default", points: 1 },

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,10 +3130,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
31303130
async findStripeSubscriptionId(ctx: TraceContext, attributionId: string): Promise<string | undefined> {
31313131
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31323132
}
3133-
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
3134-
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
3135-
}
3136-
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
3133+
async createStripeCustomer(ctx: TraceContext, attributionId: string, currency: string): Promise<void> {
31373134
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31383135
}
31393136
async subscribeToStripe(ctx: TraceContext, attributionId: string, setupIntentId: string): Promise<void> {

0 commit comments

Comments
 (0)