Skip to content

Commit

Permalink
[server] Use BillingService.GetStripeCustomer to fetch customer
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ authored and roboquat committed Oct 7, 2022
1 parent 390379b commit d31c1a8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions components/server/ee/src/user/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
observeStripeClientRequestsCompleted,
stripeClientRequestsCompletedDurationSeconds,
} from "../../../src/prometheus-metrics";
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";

const POLL_CREATED_CUSTOMER_INTERVAL_MS = 1000;
const POLL_CREATED_CUSTOMER_MAX_ATTEMPTS = 30;
Expand All @@ -23,6 +24,9 @@ export class StripeService {

protected _stripe: Stripe | undefined;

@inject(BillingServiceDefinition.name)
protected readonly billingService: BillingServiceClient;

protected getStripe(): Stripe {
if (!this._stripe) {
if (!this.config.stripeSecrets?.secretKey) {
Expand All @@ -40,6 +44,16 @@ export class StripeService {
}

async findCustomerByAttributionId(attributionId: string): Promise<string | undefined> {
try {
const resp = await this.billingService.getStripeCustomer({ attributionId: attributionId });
return resp.customer?.id;
} catch (e) {
log.warn("Failed to retrieve Stripe Customer ID from billing service, falling back to search.", {
attributionId,
});
}

// Fallback to searching against stripe. To be removed once we've got confidence in billingService.getStripeCustomer
const query = `metadata['attributionId']:'${attributionId}'`;

const result = await reportStripeOutcome("customers_search", () => {
Expand Down

0 comments on commit d31c1a8

Please sign in to comment.