Skip to content

Commit 64df336

Browse files
committed
[server][dashboard] Refactor createOrUpdateStripeCustomerFor{User,Team} to createStripeCustomer
1 parent fdb0663 commit 64df336

File tree

5 files changed

+33
-42
lines changed

5 files changed

+33
-42
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
@@ -288,8 +288,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
288288
getStripeSetupIntentClientSecret(): Promise<string>;
289289
findStripeSubscriptionId(attributionId: string): Promise<string | undefined>;
290290
findStripeSubscriptionIdForTeam(teamId: string): Promise<string | undefined>;
291-
createOrUpdateStripeCustomerForTeam(teamId: string, currency: string): Promise<void>;
292-
createOrUpdateStripeCustomerForUser(currency: string): Promise<void>;
291+
createStripeCustomer(attributionId: string, currency: string): Promise<void>;
293292
subscribeToStripe(attributionId: string, setupIntentId: string): Promise<void>;
294293
subscribeTeamToStripe(teamId: string, setupIntentId: string): Promise<void>;
295294
getStripePortalUrl(attributionId: string): Promise<string>;

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,41 +2099,42 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20992099
return this.findStripeSubscriptionId(ctx, AttributionId.render(attributionId));
21002100
}
21012101

2102-
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
2103-
const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForTeam");
2104-
const team = await this.guardTeamOperation(teamId, "update");
2105-
await this.ensureStripeApiIsAllowed({ team });
2106-
try {
2107-
let customerId = await this.stripeService.findCustomerByTeamId(team!.id);
2108-
if (!customerId) {
2109-
log.info("Creating customer for team", { teamId });
2110-
customerId = await this.stripeService.createCustomerForTeam(user, team!);
2102+
async createStripeCustomer(ctx: TraceContext, attributionId: string, currency: string): Promise<void> {
2103+
const user = this.checkAndBlockUser("createStripeCustomer");
2104+
const attrId = AttributionId.parse(attributionId);
2105+
if (!attrId) {
2106+
throw new ResponseError(ErrorCodes.BAD_REQUEST, `Invalid attributionId '${attributionId}'`);
2107+
}
2108+
let team: Team | undefined;
2109+
if (attrId.kind === "team") {
2110+
team = await this.guardTeamOperation(attrId.teamId, "update");
2111+
await this.ensureStripeApiIsAllowed({ team });
2112+
} else {
2113+
if (attrId.userId !== user.id) {
2114+
throw new ResponseError(
2115+
ErrorCodes.PERMISSION_DENIED,
2116+
"Cannot create Stripe customer profile for another user",
2117+
);
21112118
}
2112-
log.info("Setting preferred currency for team", { teamId, currency });
2113-
await this.stripeService.setPreferredCurrencyForCustomer(customerId, currency);
2114-
} catch (error) {
2115-
log.error(`Failed to update Stripe customer profile for team '${teamId}'`, error);
2116-
throw new ResponseError(
2117-
ErrorCodes.INTERNAL_SERVER_ERROR,
2118-
`Failed to update Stripe customer profile for team '${teamId}'`,
2119-
);
2119+
await this.ensureStripeApiIsAllowed({ user });
21202120
}
2121-
}
2122-
2123-
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
2124-
const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForUser");
2125-
await this.ensureStripeApiIsAllowed({ user });
21262121
try {
2127-
let customerId = await this.stripeService.findCustomerByUserId(user.id);
2128-
if (!customerId) {
2129-
customerId = await this.stripeService.createCustomerForUser(user);
2122+
if (await this.stripeService.findCustomerByAttributionId(attributionId)) {
2123+
throw new ResponseError(
2124+
ErrorCodes.BAD_REQUEST,
2125+
"A Stripe customer profile already exists for this attributionId",
2126+
);
21302127
}
2128+
const customerId =
2129+
attrId.kind === "team"
2130+
? await this.stripeService.createCustomerForTeam(user, team!)
2131+
: await this.stripeService.createCustomerForUser(user);
21312132
await this.stripeService.setPreferredCurrencyForCustomer(customerId, currency);
21322133
} catch (error) {
2133-
log.error(`Failed to update Stripe customer profile for user '${user.id}'`, error);
2134+
log.error(`Failed to create Stripe customer profile for '${attributionId}'`, error);
21342135
throw new ResponseError(
21352136
ErrorCodes.INTERNAL_SERVER_ERROR,
2136-
`Failed to update Stripe customer profile for user '${user.id}'`,
2137+
`Failed to create Stripe customer profile for '${attributionId}'`,
21372138
);
21382139
}
21392140
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ const defaultFunctions: FunctionsConfig = {
202202
getStripeSetupIntentClientSecret: { group: "default", points: 1 },
203203
findStripeSubscriptionId: { group: "default", points: 1 },
204204
findStripeSubscriptionIdForTeam: { group: "default", points: 1 },
205-
createOrUpdateStripeCustomerForTeam: { group: "default", points: 1 },
206-
createOrUpdateStripeCustomerForUser: { group: "default", points: 1 },
205+
createStripeCustomer: { group: "default", points: 1 },
207206
subscribeToStripe: { group: "default", points: 1 },
208207
subscribeTeamToStripe: { group: "default", points: 1 },
209208
getStripePortalUrl: { 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
@@ -3133,10 +3133,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
31333133
async findStripeSubscriptionIdForTeam(ctx: TraceContext, teamId: string): Promise<string | undefined> {
31343134
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31353135
}
3136-
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
3137-
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
3138-
}
3139-
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
3136+
async createStripeCustomer(ctx: TraceContext, attributionId: string, currency: string): Promise<void> {
31403137
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31413138
}
31423139
async subscribeTeamToStripe(ctx: TraceContext, teamId: string, setupIntentId: string): Promise<void> {

0 commit comments

Comments
 (0)