Skip to content
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

[stripe] Use AttributionID metadata when querying for customers #12762

Merged
merged 1 commit into from
Sep 8, 2022
Merged
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
18 changes: 10 additions & 8 deletions components/server/ee/src/user/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
const POLL_CREATED_CUSTOMER_INTERVAL_MS = 1000;
const POLL_CREATED_CUSTOMER_MAX_ATTEMPTS = 30;

const ATTRIBUTION_ID_METADATA_KEY = "attributionId";

@injectable()
export class StripeService {
@inject(Config) protected readonly config: Config;
Expand All @@ -34,11 +36,15 @@ export class StripeService {
}

async findCustomerByUserId(userId: string): Promise<Stripe.Customer | undefined> {
return this.findCustomerByQuery(`metadata['userId']:'${userId}'`);
return this.findCustomerByQuery(
`metadata['${ATTRIBUTION_ID_METADATA_KEY}']:'${AttributionId.render({ kind: "user", userId })}'`,
);
}

async findCustomerByTeamId(teamId: string): Promise<Stripe.Customer | undefined> {
return this.findCustomerByQuery(`metadata['teamId']:'${teamId}'`);
return this.findCustomerByQuery(
`metadata['${ATTRIBUTION_ID_METADATA_KEY}']:'${AttributionId.render({ kind: "team", teamId })}'`,
);
}

async findCustomerByQuery(query: string): Promise<Stripe.Customer | undefined> {
Expand All @@ -58,9 +64,7 @@ export class StripeService {
email: User.getPrimaryEmail(user),
name: User.getName(user),
metadata: {
// userId is deprecated, use attributionId where possible
userId: user.id,
attributionId: AttributionId.render({ kind: "user", userId: user.id }),
ATTRIBUTION_ID_METADATA_KEY: AttributionId.render({ kind: "user", userId: user.id }),
Copy link
Contributor

@jankeromnes jankeromnes Sep 8, 2022

Choose a reason for hiding this comment

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

I don't think this is going to work (you're creating an object key that's literally "ATTRIBUTION_ID_METADATA_KEY").

If you really want to make ATTRIBUTION_ID_METADATA_KEY configurable with a global const in this file, you'd want to first create a const customerSpec = { email, name, metadata: {} } and then set customerSpec.metadata[ATTRIBUTION_ID_METADATA_KEY] = ....

Copy link
Contributor

Choose a reason for hiding this comment

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

However, I don't think it's useful to factor out ATTRIBUTION_ID_METADATA_KEY. So, maybe it's simpler to just use attributionId everywhere in-line. (This is unlikely to change in the future, but still easy to change if needed.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Revert is here. Thanks for spotting this. I applied Go style thinking it would use it as a key, not as a literal..

},
});
// Wait for the customer to show up in Stripe search results before proceeding
Expand All @@ -84,9 +88,7 @@ export class StripeService {
email: User.getPrimaryEmail(user),
name: userName ? `${userName} (${team.name})` : team.name,
metadata: {
// teamId is deprecated, use attributionId where possible
teamId: team.id,
attributionId: AttributionId.render({ kind: "team", teamId: team.id }),
ATTRIBUTION_ID_METADATA_KEY: AttributionId.render({ kind: "team", teamId: team.id }),
},
});
// Wait for the customer to show up in Stripe search results before proceeding
Expand Down