Skip to content

Commit 7df06a8

Browse files
Andrew Farriesroboquat
Andrew Farries
authored andcommitted
Add createOrUpdateStripeCustomerForUser method
Directly analogous to createOrUpdateStripeCustomerForTeam, the only difference is the metadata on the resulting Stripe customer.
1 parent 81e189b commit 7df06a8

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
292292
getStripeSetupIntentClientSecret(): Promise<string>;
293293
findStripeSubscriptionIdForTeam(teamId: string): Promise<string | undefined>;
294294
createOrUpdateStripeCustomerForTeam(teamId: string, currency: string): Promise<void>;
295+
createOrUpdateStripeCustomerForUser(currency: string): Promise<void>;
295296
subscribeTeamToStripe(teamId: string, setupIntentId: string): Promise<void>;
296297
getStripePortalUrlForTeam(teamId: string): Promise<string>;
297298
getUsageLimitForTeam(teamId: string): Promise<number | undefined>;

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

+18
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,24 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20862086
}
20872087
}
20882088

2089+
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
2090+
const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForUser");
2091+
await this.ensureStripeApiIsAllowed({ user });
2092+
try {
2093+
let customer = await this.stripeService.findCustomerByUserId(user.id);
2094+
if (!customer) {
2095+
customer = await this.stripeService.createCustomerForUser(user);
2096+
}
2097+
await this.stripeService.setPreferredCurrencyForCustomer(customer, currency);
2098+
} catch (error) {
2099+
log.error(`Failed to update Stripe customer profile for user '${user.id}'`, error);
2100+
throw new ResponseError(
2101+
ErrorCodes.INTERNAL_SERVER_ERROR,
2102+
`Failed to update Stripe customer profile for user '${user.id}'`,
2103+
);
2104+
}
2105+
}
2106+
20892107
protected defaultSpendingLimit = 100;
20902108
async subscribeTeamToStripe(ctx: TraceContext, teamId: string, setupIntentId: string): Promise<void> {
20912109
this.checkAndBlockUser("subscribeUserToStripe");

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

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const defaultFunctions: FunctionsConfig = {
207207
getStripeSetupIntentClientSecret: { group: "default", points: 1 },
208208
findStripeSubscriptionIdForTeam: { group: "default", points: 1 },
209209
createOrUpdateStripeCustomerForTeam: { group: "default", points: 1 },
210+
createOrUpdateStripeCustomerForUser: { group: "default", points: 1 },
210211
subscribeTeamToStripe: { group: "default", points: 1 },
211212
getStripePortalUrlForTeam: { group: "default", points: 1 },
212213
listUsage: { group: "default", points: 1 },

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

+3
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,9 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
32213221
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
32223222
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
32233223
}
3224+
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
3225+
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
3226+
}
32243227
async subscribeTeamToStripe(ctx: TraceContext, teamId: string, setupIntentId: string): Promise<void> {
32253228
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
32263229
}

0 commit comments

Comments
 (0)