@@ -4,14 +4,14 @@ import { stripe } from '..'
4
4
5
5
export const paymentIntent : any = ( ( ) => {
6
6
function stripeId ( user : UserModel ) : string {
7
- return user . stripe_id
7
+ return user . stripe_id || ''
8
8
}
9
9
10
10
function hasStripeId ( user : UserModel ) : boolean {
11
11
return user . stripe_id !== null || user . stripe_id !== undefined
12
12
}
13
13
14
- function createStripeCustomer ( user : UserModel , options : any = { } ) : Promise < Stripe . Response < Stripe . Customer > > {
14
+ async function createStripeCustomer ( user : UserModel , options : any = { } ) : Promise < Stripe . Response < Stripe . Customer > > {
15
15
if ( hasStripeId ( user ) ) {
16
16
throw new Error ( 'Customer already created' )
17
17
}
@@ -24,20 +24,59 @@ export const paymentIntent: any = (() => {
24
24
options . email = stripeEmail ( user )
25
25
}
26
26
27
- if ( ! options . preferred_locales && stripePreferredLocales ) {
28
- options . preferred_locales = stripePreferredLocales ( user )
27
+ const customer = await stripe . customer . create ( options )
28
+
29
+ user . update ( { stripe_id : customer . id } )
30
+
31
+ return customer
32
+ }
33
+
34
+ async function updateStripeCustomer ( user : UserModel , options : Stripe . CustomerUpdateParams ) : Promise < Stripe . Response < Stripe . Customer > > {
35
+ const customer = await stripe . customer . update ( user . stripe_id || '' , options )
36
+
37
+ return customer
38
+ }
39
+
40
+ async function createOrGetStripeUser ( user : UserModel , options : Stripe . CustomerCreateParams = { } ) : Promise < Stripe . Response < Stripe . Customer > > {
41
+ if ( ! hasStripeId ( user ) ) {
42
+ return await createStripeCustomer ( user , options )
29
43
}
30
44
31
- if ( ! options . metadata && stripeMetadata ( user ) ) {
32
- options . metadata = stripeMetadata ( user )
45
+ try {
46
+ const customer = await stripe . customer . retrieve ( user . stripe_id || '' )
47
+ if ( ( customer as Stripe . DeletedCustomer ) . deleted ) {
48
+ throw new Error ( 'Customer was deleted' )
49
+ }
50
+
51
+ return customer as Stripe . Response < Stripe . Customer >
52
+
53
+ } catch ( error ) {
54
+ if ( ( error as any ) . statusCode === 404 ) {
55
+ return await createStripeCustomer ( user , options )
56
+ }
57
+ throw error
33
58
}
59
+ }
34
60
35
- // Here we will create the customer instance on Stripe and store the ID of the
36
- // user from Stripe. This ID will correspond with the Stripe user instances
37
- // and allow us to retrieve users from Stripe later when we need to work.
38
- return stripe . customer . create ( options ) . then ( ( customer : any ) => {
39
- return user . update ( { stripe_id : customer . id } )
40
- } )
61
+ async function createOrUpdateStripeUser ( user : UserModel , options : Stripe . CustomerCreateParams | Stripe . CustomerCreateParams ) : Promise < Stripe . Response < Stripe . Customer > > {
62
+ if ( ! hasStripeId ( user ) ) {
63
+ return await createStripeCustomer ( user , options )
64
+ }
65
+
66
+ try {
67
+ const customer = await stripe . customer . retrieve ( user . stripe_id || '' )
68
+ if ( ( customer as Stripe . DeletedCustomer ) . deleted ) {
69
+ return await createStripeCustomer ( user , options )
70
+ }
71
+
72
+ return await updateStripeCustomer ( user , options )
73
+ } catch ( error ) {
74
+ if ( ( error as any ) . statusCode === 404 ) {
75
+ return await createStripeCustomer ( user , options )
76
+ }
77
+
78
+ throw error
79
+ }
41
80
}
42
81
43
82
function stripeName ( user : UserModel ) : string {
@@ -48,13 +87,5 @@ export const paymentIntent: any = (() => {
48
87
return user . email || ''
49
88
}
50
89
51
- function stripePreferredLocales ( user : any ) : string {
52
- return user . preferred_locales || [ ]
53
- }
54
-
55
- function stripeMetadata ( user : any ) : string {
56
- return user . metadata || { }
57
- }
58
-
59
- return { stripeId, hasStripeId, createStripeCustomer }
90
+ return { stripeId, hasStripeId, createStripeCustomer, updateStripeCustomer, createOrGetStripeUser, createOrUpdateStripeUser }
60
91
} ) ( )
0 commit comments