Skip to content

Commit 1aa33d7

Browse files
chore: wip
1 parent 7c9e43e commit 1aa33d7

File tree

1 file changed

+52
-21
lines changed
  • storage/framework/core/payments/src/billable

1 file changed

+52
-21
lines changed

storage/framework/core/payments/src/billable/Customer.ts

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { stripe } from '..'
44

55
export const paymentIntent: any = (() => {
66
function stripeId(user: UserModel): string {
7-
return user.stripe_id
7+
return user.stripe_id || ''
88
}
99

1010
function hasStripeId(user: UserModel): boolean {
1111
return user.stripe_id !== null || user.stripe_id !== undefined
1212
}
1313

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>> {
1515
if (hasStripeId(user)) {
1616
throw new Error('Customer already created')
1717
}
@@ -24,20 +24,59 @@ export const paymentIntent: any = (() => {
2424
options.email = stripeEmail(user)
2525
}
2626

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)
2943
}
3044

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
3358
}
59+
}
3460

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+
}
4180
}
4281

4382
function stripeName(user: UserModel): string {
@@ -48,13 +87,5 @@ export const paymentIntent: any = (() => {
4887
return user.email || ''
4988
}
5089

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 }
6091
})()

0 commit comments

Comments
 (0)