@@ -43,55 +43,32 @@ export class StripeService {
43
43
return result . data [ 0 ] ?. id ;
44
44
}
45
45
46
- async createCustomerForUser ( user : User ) : Promise < string > {
47
- const attributionId = AttributionId . render ( { kind : "user" , userId : user . id } ) ;
46
+ async createCustomerForAttributionId (
47
+ attributionId : string ,
48
+ preferredCurrency : string ,
49
+ billingEmail ?: string ,
50
+ billingName ?: string ,
51
+ ) : Promise < string > {
48
52
if ( await this . findCustomerByAttributionId ( attributionId ) ) {
49
- throw new Error ( `A Stripe customer already exists for user '${ user . id } '` ) ;
53
+ throw new Error ( `A Stripe customer already exists for '${ attributionId } '` ) ;
50
54
}
51
55
// Create the customer in Stripe
52
56
const customer = await this . getStripe ( ) . customers . create ( {
53
- email : User . getPrimaryEmail ( user ) ,
54
- name : User . getName ( user ) ,
55
- metadata : { attributionId } ,
57
+ email : billingEmail ,
58
+ name : billingName ,
59
+ metadata : { attributionId, preferredCurrency } ,
56
60
} ) ;
57
61
// Wait for the customer to show up in Stripe search results before proceeding
58
62
let attempts = 0 ;
59
63
while ( ! ( await this . findCustomerByAttributionId ( attributionId ) ) ) {
60
64
await new Promise ( ( resolve ) => setTimeout ( resolve , POLL_CREATED_CUSTOMER_INTERVAL_MS ) ) ;
61
65
if ( ++ attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS ) {
62
- throw new Error ( `Could not confirm Stripe customer creation for user '${ user . id } '` ) ;
66
+ throw new Error ( `Could not confirm Stripe customer creation for '${ attributionId } '` ) ;
63
67
}
64
68
}
65
69
return customer . id ;
66
70
}
67
71
68
- async createCustomerForTeam ( user : User , team : Team ) : Promise < string > {
69
- const attributionId = AttributionId . render ( { kind : "team" , teamId : team . id } ) ;
70
- if ( await this . findCustomerByAttributionId ( attributionId ) ) {
71
- throw new Error ( `A Stripe customer already exists for team '${ team . id } '` ) ;
72
- }
73
- // Create the customer in Stripe
74
- const userName = User . getName ( user ) ;
75
- const customer = await this . getStripe ( ) . customers . create ( {
76
- email : User . getPrimaryEmail ( user ) ,
77
- name : userName ? `${ userName } (${ team . name } )` : team . name ,
78
- metadata : { attributionId } ,
79
- } ) ;
80
- // Wait for the customer to show up in Stripe search results before proceeding
81
- let attempts = 0 ;
82
- while ( ! ( await this . findCustomerByAttributionId ( attributionId ) ) ) {
83
- await new Promise ( ( resolve ) => setTimeout ( resolve , POLL_CREATED_CUSTOMER_INTERVAL_MS ) ) ;
84
- if ( ++ attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS ) {
85
- throw new Error ( `Could not confirm Stripe customer creation for team '${ team . id } '` ) ;
86
- }
87
- }
88
- return customer . id ;
89
- }
90
-
91
- async setPreferredCurrencyForCustomer ( customerId : string , currency : string ) : Promise < void > {
92
- await this . getStripe ( ) . customers . update ( customerId , { metadata : { preferredCurrency : currency } } ) ;
93
- }
94
-
95
72
async setDefaultPaymentMethodForCustomer ( customerId : string , setupIntentId : string ) : Promise < void > {
96
73
const setupIntent = await this . getStripe ( ) . setupIntents . retrieve ( setupIntentId ) ;
97
74
if ( typeof setupIntent . payment_method !== "string" ) {
0 commit comments