@@ -36,18 +36,15 @@ export class StripeService {
36
36
}
37
37
38
38
async findCustomerByUserId ( userId : string ) : Promise < Stripe . Customer | undefined > {
39
- return this . findCustomerByQuery (
40
- `metadata['${ ATTRIBUTION_ID_METADATA_KEY } ']:'${ AttributionId . render ( { kind : "user" , userId } ) } '` ,
41
- ) ;
39
+ return this . findCustomerByAttributionId ( AttributionId . render ( { kind : "user" , userId } ) ) ;
42
40
}
43
41
44
42
async findCustomerByTeamId ( teamId : string ) : Promise < Stripe . Customer | undefined > {
45
- return this . findCustomerByQuery (
46
- `metadata['${ ATTRIBUTION_ID_METADATA_KEY } ']:'${ AttributionId . render ( { kind : "team" , teamId } ) } '` ,
47
- ) ;
43
+ return this . findCustomerByAttributionId ( AttributionId . render ( { kind : "team" , teamId } ) ) ;
48
44
}
49
45
50
- async findCustomerByQuery ( query : string ) : Promise < Stripe . Customer | undefined > {
46
+ async findCustomerByAttributionId ( attributionId : string ) : Promise < Stripe . Customer | undefined > {
47
+ const query = `metadata['${ ATTRIBUTION_ID_METADATA_KEY } ']:'${ attributionId } '` ;
51
48
const result = await this . getStripe ( ) . customers . search ( { query } ) ;
52
49
if ( result . data . length > 1 ) {
53
50
throw new Error ( `Found more than one Stripe customer for query '${ query } '` ) ;
@@ -56,20 +53,21 @@ export class StripeService {
56
53
}
57
54
58
55
async createCustomerForUser ( user : User ) : Promise < Stripe . Customer > {
59
- if ( await this . findCustomerByUserId ( user . id ) ) {
56
+ const attributionId = AttributionId . render ( { kind : "user" , userId : user . id } ) ;
57
+ if ( await this . findCustomerByAttributionId ( attributionId ) ) {
60
58
throw new Error ( `A Stripe customer already exists for user '${ user . id } '` ) ;
61
59
}
62
60
// Create the customer in Stripe
63
61
const customer = await this . getStripe ( ) . customers . create ( {
64
62
email : User . getPrimaryEmail ( user ) ,
65
63
name : User . getName ( user ) ,
66
64
metadata : {
67
- [ ATTRIBUTION_ID_METADATA_KEY ] : AttributionId . render ( { kind : "user" , userId : user . id } ) ,
65
+ [ ATTRIBUTION_ID_METADATA_KEY ] : attributionId ,
68
66
} ,
69
67
} ) ;
70
68
// Wait for the customer to show up in Stripe search results before proceeding
71
69
let attempts = 0 ;
72
- while ( ! ( await this . findCustomerByUserId ( user . id ) ) ) {
70
+ while ( ! ( await this . findCustomerByAttributionId ( attributionId ) ) ) {
73
71
await new Promise ( ( resolve ) => setTimeout ( resolve , POLL_CREATED_CUSTOMER_INTERVAL_MS ) ) ;
74
72
if ( ++ attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS ) {
75
73
throw new Error ( `Could not confirm Stripe customer creation for user '${ user . id } '` ) ;
@@ -79,7 +77,8 @@ export class StripeService {
79
77
}
80
78
81
79
async createCustomerForTeam ( user : User , team : Team ) : Promise < Stripe . Customer > {
82
- if ( await this . findCustomerByTeamId ( team . id ) ) {
80
+ const attributionId = AttributionId . render ( { kind : "team" , teamId : team . id } ) ;
81
+ if ( await this . findCustomerByAttributionId ( attributionId ) ) {
83
82
throw new Error ( `A Stripe customer already exists for team '${ team . id } '` ) ;
84
83
}
85
84
// Create the customer in Stripe
@@ -88,12 +87,12 @@ export class StripeService {
88
87
email : User . getPrimaryEmail ( user ) ,
89
88
name : userName ? `${ userName } (${ team . name } )` : team . name ,
90
89
metadata : {
91
- [ ATTRIBUTION_ID_METADATA_KEY ] : AttributionId . render ( { kind : "team" , teamId : team . id } ) ,
90
+ [ ATTRIBUTION_ID_METADATA_KEY ] : attributionId ,
92
91
} ,
93
92
} ) ;
94
93
// Wait for the customer to show up in Stripe search results before proceeding
95
94
let attempts = 0 ;
96
- while ( ! ( await this . findCustomerByTeamId ( team . id ) ) ) {
95
+ while ( ! ( await this . findCustomerByAttributionId ( attributionId ) ) ) {
97
96
await new Promise ( ( resolve ) => setTimeout ( resolve , POLL_CREATED_CUSTOMER_INTERVAL_MS ) ) ;
98
97
if ( ++ attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS ) {
99
98
throw new Error ( `Could not confirm Stripe customer creation for team '${ team . id } '` ) ;
0 commit comments