Skip to content

Commit bc2323d

Browse files
chore: wip
1 parent a9535f7 commit bc2323d

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

bun.lockb

2.88 KB
Binary file not shown.

storage/framework/core/orm/src/utils.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,36 @@ export async function generateModelString(
10571057
10581058
hasStripeId(): boolean {
10591059
return manageCustomer.hasStripeId(this)
1060-
}`
1060+
}
1061+
1062+
async createOrUpdateStripeUser(options: Stripe.CustomerCreateParams | Stripe.CustomerUpdateParams): Promise<Stripe.Response<Stripe.Customer>> {
1063+
const customer = await manageCustomer.createOrUpdateStripeUser(this, options)
1064+
return customer
1065+
}
1066+
1067+
async addPaymentMethod(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
1068+
const paymentMethod = await managePaymentMethod.addPaymentMethod(this, paymentMethodId)
1069+
1070+
return paymentMethod
1071+
}
1072+
1073+
async updatePaymentMethod(paymentMethodId: string, params: Stripe.PaymentMethodUpdateParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
1074+
const updatedPaymentMethod = await managePaymentMethod.updatePaymentMethod(this, paymentMethodId, params)
1075+
1076+
return updatedPaymentMethod
1077+
}
1078+
1079+
async deletePaymentMethod(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
1080+
const deletedPaymentMethod = await managePaymentMethod.deletePaymentMethod(this, paymentMethodId)
1081+
return deletedPaymentMethod
1082+
}
1083+
1084+
async retrievePaymentMethod(paymentMethod: string): Promise<Stripe.Response<Stripe.PaymentMethod> | null> {
1085+
const defaultPaymentMethod = await managePaymentMethod.retrievePaymentMethod(this, paymentMethod)
1086+
1087+
return defaultPaymentMethod
1088+
}
1089+
`
10611090

10621091
declareFields += `public stripe_id: string | undefined\n`
10631092

storage/framework/core/payments/src/billable/payment-method.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ import type { UserModel } from '../../../../orm/src/models/User'
33
import { stripe } from '..'
44

55

6-
export interface ManageCharge {
7-
addPaymentMethod: (user: UserModel, paymentMethod: string | Stripe.PaymentMethod) => Promise<Stripe.PaymentMethod>
6+
export interface ManagePaymentMethod {
7+
addPaymentMethod: (user: UserModel, paymentMethod: string | Stripe.PaymentMethod) => Promise<Stripe.Response<Stripe.PaymentMethod>>
8+
updatePaymentMethod: (user: UserModel, paymentMethodId: string, updateParams: Stripe.PaymentMethodUpdateParams) => Promise<Stripe.Response<Stripe.PaymentMethod>>
9+
deletePaymentMethod: (user: UserModel, paymentMethodId: string) => Promise<Stripe.Response<Stripe.PaymentMethod>>
10+
retrievePaymentMethod: (user: UserModel, paymentMethodId: string) => Promise<Stripe.Response<Stripe.PaymentMethod>>
811
}
912

10-
export const managePaymentMethod: ManageCharge = (() => {
11-
async function addPaymentMethod(user: UserModel, paymentMethod: string | Stripe.PaymentMethod): Promise<Stripe.PaymentMethod> {
13+
export const managePaymentMethod: ManagePaymentMethod = (() => {
14+
async function addPaymentMethod(user: UserModel, paymentMethod: string | Stripe.PaymentMethod): Promise<Stripe.Response<Stripe.PaymentMethod>> {
1215
if (!user.hasStripeId()) {
1316
throw new Error('Customer does not exist in Stripe')
1417
}
1518

16-
let stripePaymentMethod: Stripe.PaymentMethod
19+
let stripePaymentMethod
1720

1821
if (typeof paymentMethod === 'string') {
1922
stripePaymentMethod = await stripe.paymentMethod.retrieve(paymentMethod)
@@ -27,10 +30,10 @@ export const managePaymentMethod: ManageCharge = (() => {
2730
})
2831
}
2932

30-
return stripePaymentMethod
33+
return stripePaymentMethod as Stripe.Response<Stripe.PaymentMethod>
3134
}
3235

33-
async function deletePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.PaymentMethod> {
36+
async function deletePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>>{
3437
if (!user.hasStripeId()) {
3538
throw new Error('Customer does not exist in Stripe')
3639
}
@@ -44,7 +47,7 @@ export const managePaymentMethod: ManageCharge = (() => {
4447
return await stripe.paymentMethod.detach(paymentMethodId)
4548
}
4649

47-
async function updatePaymentMethod(user: UserModel, paymentMethodId: string, updateParams: Stripe.PaymentMethodUpdateParams): Promise<Stripe.PaymentMethod> {
50+
async function updatePaymentMethod(user: UserModel, paymentMethodId: string, updateParams: Stripe.PaymentMethodUpdateParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
4851
if (!user.hasStripeId()) {
4952
throw new Error('Customer does not exist in Stripe')
5053
}
@@ -58,7 +61,7 @@ export const managePaymentMethod: ManageCharge = (() => {
5861
return await stripe.paymentMethod.update(paymentMethodId, updateParams)
5962
}
6063

61-
async function retrievePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.PaymentMethod> {
64+
async function retrievePaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>>{
6265
if (!user.hasStripeId()) {
6366
throw new Error('Customer does not exist in Stripe')
6467
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
export * from './billable/charge'
12
export * from './billable/customer'
3+
export * from './billable/payment-method'
24
export * as stripe from './drivers/stripe'
3-
45
export * from 'stripe'

storage/framework/core/plugins/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@stacksjs/slug",
2+
"name": "@stacksjs/slugs",
33
"type": "module",
44
"version": "0.67.0",
55
"description": "The core of what powers Stacks Plugins.",

storage/framework/orm/src/models/User.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { cache } from '@stacksjs/cache'
33
import { db, sql } from '@stacksjs/database'
44
import { HttpError } from '@stacksjs/error-handling'
55
import { dispatch } from '@stacksjs/events'
6-
import { manageCustomer, type Stripe } from '@stacksjs/payments'
6+
import { manageCustomer, managePaymentMethod, type Stripe } from '@stacksjs/payments'
77

88
import Post from './Post'
99

@@ -682,6 +682,29 @@ export class UserModel {
682682
return customer
683683
}
684684

685+
async addPaymentMethod(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
686+
const paymentMethod = await managePaymentMethod.addPaymentMethod(this, paymentMethodId)
687+
688+
return paymentMethod
689+
}
690+
691+
async updatePaymentMethod(paymentMethodId: string, params: Stripe.PaymentMethodUpdateParams): Promise<Stripe.Response<Stripe.PaymentMethod>> {
692+
const updatedPaymentMethod = await managePaymentMethod.updatePaymentMethod(this, paymentMethodId, params)
693+
694+
return updatedPaymentMethod
695+
}
696+
697+
async deletePaymentMethod(paymentMethodId: string): Promise<Stripe.Response<Stripe.PaymentMethod>> {
698+
const deletedPaymentMethod = await managePaymentMethod.deletePaymentMethod(this, paymentMethodId)
699+
return deletedPaymentMethod
700+
}
701+
702+
async retrievePaymentMethod(paymentMethod: string): Promise<Stripe.Response<Stripe.PaymentMethod> | null> {
703+
const defaultPaymentMethod = await managePaymentMethod.retrievePaymentMethod(this, paymentMethod)
704+
705+
return defaultPaymentMethod
706+
}
707+
685708
stripeId(): string {
686709
return manageCustomer.stripeId(this)
687710
}

0 commit comments

Comments
 (0)