-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_intents.ts
41 lines (39 loc) · 933 Bytes
/
payment_intents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type Stripe from 'stripe'
export interface Config {
pi_id: string
stripe: Stripe
}
/**
* Extracts the Stripe Customer from a Payment Intent Id.
*
* @public
*/
export const customerFromPaymentIntentId = async ({
stripe,
pi_id
}: Config) => {
const pi = await stripe.paymentIntents.retrieve(pi_id)
const customer = pi.customer
if (customer === null) {
return {
error: new Error(
`Stripe event ${pi.object}.${pi.status} ${pi.id} has no customer`
)
}
} else if (typeof customer === 'string') {
const cus = await stripe.customers.retrieve(customer)
if (cus.deleted) {
return { error: new Error(`Stripe customer ${cus.id} was deleted`) }
} else {
return {
value: cus
}
}
} else if (customer.deleted) {
return { error: new Error(`Stripe customer ${customer.id} was deleted`) }
} else {
return {
value: customer
}
}
}