Skip to content

Commit a9535f7

Browse files
chore: wip
1 parent c1d2e78 commit a9535f7

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
})()

storage/framework/core/payments/src/drivers/stripe.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,44 @@ export const refund: Refund = (() => {
140140
return { create, retrieve, update, list }
141141
})()
142142

143+
export interface PaymentMethod {
144+
create: (params: Stripe.PaymentMethodCreateParams) => Promise<Stripe.Response<Stripe.PaymentMethod>>
145+
retrieve: (paymentMethodId: string) => Promise<Stripe.Response<Stripe.PaymentMethod>>
146+
update: (paymentMethodId: string, params: Stripe.PaymentMethodUpdateParams) => Promise<Stripe.Response<Stripe.PaymentMethod>>
147+
list: (params: Stripe.PaymentMethodListParams) => Promise<Stripe.Response<Stripe.ApiList<Stripe.PaymentMethod>>>
148+
attach: (paymentMethodId: string, params: Stripe.PaymentMethodAttachParams) => Promise<Stripe.Response<Stripe.PaymentMethod>>
149+
detach: (paymentMethodId: string) => Promise<Stripe.Response<Stripe.PaymentMethod>>
150+
}
151+
152+
export const paymentMethod: PaymentMethod = (() => {
153+
async function create(params: Stripe.PaymentMethodCreateParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
154+
return await client.paymentMethods.create(params)
155+
}
156+
157+
async function retrieve(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
158+
return await client.paymentMethods.retrieve(paymentMethodId)
159+
}
160+
161+
async function update(paymentMethodId: string, params: Stripe.PaymentMethodUpdateParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
162+
return await client.paymentMethods.update(paymentMethodId, params)
163+
}
164+
165+
async function list(params: Stripe.PaymentMethodListParams): Promise<Stripe.Response<Stripe.ApiList<Stripe.PaymentMethod>>> {
166+
return await client.paymentMethods.list(params)
167+
}
168+
169+
async function attach(paymentMethodId: string, params: Stripe.PaymentMethodAttachParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
170+
return await client.paymentMethods.attach(paymentMethodId, params)
171+
}
172+
173+
async function detach(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
174+
return await client.paymentMethods.detach(paymentMethodId)
175+
}
176+
177+
return { create, retrieve, update, list, attach, detach }
178+
})()
179+
180+
143181
export interface BalanceTransactions {
144182
retrieve: (stripeId: string) => Promise<Stripe.Response<Stripe.BalanceTransaction>>
145183
list: (limit: number) => Promise<Stripe.Response<Stripe.ApiList<Stripe.BalanceTransaction>>>

0 commit comments

Comments
 (0)