Skip to content

[UBP] Add server method to create a stripe customers for an individual user #12753

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 2 commits into from
Sep 8, 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
1 change: 1 addition & 0 deletions components/gitpod-protocol/src/gitpod-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
getStripeSetupIntentClientSecret(): Promise<string>;
findStripeSubscriptionIdForTeam(teamId: string): Promise<string | undefined>;
createOrUpdateStripeCustomerForTeam(teamId: string, currency: string): Promise<void>;
createOrUpdateStripeCustomerForUser(currency: string): Promise<void>;
subscribeTeamToStripe(teamId: string, setupIntentId: string): Promise<void>;
getStripePortalUrlForTeam(teamId: string): Promise<string>;
getUsageLimitForTeam(teamId: string): Promise<number | undefined>;
Expand Down
18 changes: 7 additions & 11 deletions components/server/ee/src/user/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ export class StripeService {
}

async findCustomerByUserId(userId: string): Promise<Stripe.Customer | undefined> {
const result = await this.getStripe().customers.search({
query: `metadata['userId']:'${userId}'`,
});
if (result.data.length > 1) {
throw new Error(`Found more than one Stripe customer for user '${userId}'`);
}
return result.data[0];
return this.findCustomerByQuery(`metadata['userId']:'${userId}'`);
}

async findCustomerByTeamId(teamId: string): Promise<Stripe.Customer | undefined> {
const result = await this.getStripe().customers.search({
query: `metadata['teamId']:'${teamId}'`,
});
return this.findCustomerByQuery(`metadata['teamId']:'${teamId}'`);
}

async findCustomerByQuery(query: string): Promise<Stripe.Customer | undefined> {
const result = await this.getStripe().customers.search({ query });
if (result.data.length > 1) {
throw new Error(`Found more than one Stripe customer for team '${teamId}'`);
throw new Error(`Found more than one Stripe customer for query '${query}'`);
}
return result.data[0];
}
Expand Down
18 changes: 18 additions & 0 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,24 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
}
}

async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☁️ Originally I thought about merging these two methods on attributionId. But I think it makes sense to have them separate as they have different semantics around access rights etc.

const user = this.checkAndBlockUser("createOrUpdateStripeCustomerForUser");
await this.ensureStripeApiIsAllowed({ user });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

try {
let customer = await this.stripeService.findCustomerByUserId(user.id);
if (!customer) {
customer = await this.stripeService.createCustomerForUser(user);
}
await this.stripeService.setPreferredCurrencyForCustomer(customer, currency);
} catch (error) {
log.error(`Failed to update Stripe customer profile for user '${user.id}'`, error);
throw new ResponseError(
ErrorCodes.INTERNAL_SERVER_ERROR,
`Failed to update Stripe customer profile for user '${user.id}'`,
);
}
}

protected defaultSpendingLimit = 100;
async subscribeTeamToStripe(ctx: TraceContext, teamId: string, setupIntentId: string): Promise<void> {
this.checkAndBlockUser("subscribeUserToStripe");
Expand Down
1 change: 1 addition & 0 deletions components/server/src/auth/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const defaultFunctions: FunctionsConfig = {
getStripeSetupIntentClientSecret: { group: "default", points: 1 },
findStripeSubscriptionIdForTeam: { group: "default", points: 1 },
createOrUpdateStripeCustomerForTeam: { group: "default", points: 1 },
createOrUpdateStripeCustomerForUser: { group: "default", points: 1 },
subscribeTeamToStripe: { group: "default", points: 1 },
getStripePortalUrlForTeam: { group: "default", points: 1 },
listUsage: { group: "default", points: 1 },
Expand Down
3 changes: 3 additions & 0 deletions components/server/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3221,6 +3221,9 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
async createOrUpdateStripeCustomerForTeam(ctx: TraceContext, teamId: string, currency: string): Promise<void> {
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
}
async createOrUpdateStripeCustomerForUser(ctx: TraceContext, currency: string): Promise<void> {
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
}
async subscribeTeamToStripe(ctx: TraceContext, teamId: string, setupIntentId: string): Promise<void> {
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
}
Expand Down