|
| 1 | +import type Stripe from 'stripe' |
| 2 | +import type { UserModel } from '../../../../orm/src/models/User' |
| 3 | +import { stripe } from '..' |
| 4 | + |
| 5 | + |
| 6 | +export interface ManageCharge { |
| 7 | + addPaymentMethod: (user: UserModel, paymentMethod: string | Stripe.PaymentMethod) => Promise<Stripe.PaymentMethod> |
| 8 | +} |
| 9 | + |
| 10 | +export const managePaymentMethod: ManageCharge = (() => { |
| 11 | + async function addPaymentMethod(user: UserModel, paymentMethod: string | Stripe.PaymentMethod): Promise<Stripe.PaymentMethod> { |
| 12 | + if (!user.hasStripeId()) { |
| 13 | + throw new Error('Customer does not exist in Stripe') |
| 14 | + } |
| 15 | + |
| 16 | + let stripePaymentMethod: Stripe.PaymentMethod |
| 17 | + |
| 18 | + if (typeof paymentMethod === 'string') { |
| 19 | + stripePaymentMethod = await stripe.paymentMethod.retrieve(paymentMethod) |
| 20 | + } else { |
| 21 | + stripePaymentMethod = paymentMethod |
| 22 | + } |
| 23 | + |
| 24 | + if (stripePaymentMethod.customer !== user.stripe_id) { |
| 25 | + stripePaymentMethod = await stripe.paymentMethod.attach(stripePaymentMethod.id, { |
| 26 | + customer: user.stripe_id || '', |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + return stripePaymentMethod |
| 31 | + } |
| 32 | + |
| 33 | + async function deletePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.PaymentMethod> { |
| 34 | + if (!user.hasStripeId()) { |
| 35 | + throw new Error('Customer does not exist in Stripe') |
| 36 | + } |
| 37 | + |
| 38 | + const paymentMethod = await stripe.paymentMethod.retrieve(paymentMethodId) |
| 39 | + |
| 40 | + if (paymentMethod.customer !== user.stripe_id) { |
| 41 | + throw new Error('Payment method does not belong to this customer') |
| 42 | + } |
| 43 | + |
| 44 | + return await stripe.paymentMethod.detach(paymentMethodId) |
| 45 | + } |
| 46 | + |
| 47 | + async function updatePaymentMethod(user: UserModel, paymentMethodId: string, updateParams: Stripe.PaymentMethodUpdateParams): Promise<Stripe.PaymentMethod> { |
| 48 | + if (!user.hasStripeId()) { |
| 49 | + throw new Error('Customer does not exist in Stripe') |
| 50 | + } |
| 51 | + |
| 52 | + const paymentMethod = await stripe.paymentMethod.retrieve(paymentMethodId) |
| 53 | + |
| 54 | + if (paymentMethod.customer !== user.stripe_id) { |
| 55 | + throw new Error('Payment method does not belong to this customer') |
| 56 | + } |
| 57 | + |
| 58 | + return await stripe.paymentMethod.update(paymentMethodId, updateParams) |
| 59 | + } |
| 60 | + |
| 61 | + async function retrievePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.PaymentMethod> { |
| 62 | + if (!user.hasStripeId()) { |
| 63 | + throw new Error('Customer does not exist in Stripe') |
| 64 | + } |
| 65 | + |
| 66 | + const paymentMethod = await stripe.paymentMethod.retrieve(paymentMethodId) |
| 67 | + |
| 68 | + if (paymentMethod.customer !== user.stripe_id) { |
| 69 | + throw new Error('Payment method does not belong to this customer') |
| 70 | + } |
| 71 | + |
| 72 | + return paymentMethod |
| 73 | + } |
| 74 | + |
| 75 | + return { addPaymentMethod, deletePaymentMethod, retrievePaymentMethod, updatePaymentMethod } |
| 76 | +})() |
0 commit comments