Skip to content

Commit 5ef4988

Browse files
chore: wip
1 parent ad45ab3 commit 5ef4988

File tree

10 files changed

+105
-13
lines changed

10 files changed

+105
-13
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { RequestInstance } from '@stacksjs/types'
2+
import { Action } from '@stacksjs/actions'
3+
import User from '../../../storage/framework/orm/src/models/User.ts'
4+
5+
export default new Action({
6+
name: 'StoreTransactionAction',
7+
description: 'Store transactions',
8+
method: 'POST',
9+
async handle(request: RequestInstance) {
10+
try {
11+
const userId = Number(request.getParam('id'))
12+
const productId = Number(request.get('productId'))
13+
14+
const user = await User.find(userId)
15+
16+
const transaction = await user?.storeTransaction(productId)
17+
18+
return transaction
19+
}
20+
catch (err) {
21+
throw err
22+
}
23+
},
24+
})

resources/functions/billing/payments.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ export function useBillable() {
6868
async function handlePayment(elements: any) {
6969
try {
7070
const data = await confirmPayment(elements)
71-
72-
console.log(data)
7371
}
7472
catch (err) {
7573
console.error('Error processing payment:', err)

resources/stores/payment.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ export const usePaymentStore = defineStore('payment', {
5858
return clientSecret
5959
},
6060

61+
async storeTransaction(id: number, productId: number): Promise<string> {
62+
const body = { productId }
63+
64+
const url = `http://localhost:3008/payments/store-transaction/${id}`
65+
66+
const response = await fetch(url, {
67+
method: 'POST',
68+
headers: {
69+
'Content-Type': 'application/json',
70+
'Accept': 'application/json',
71+
},
72+
body: JSON.stringify(body),
73+
})
74+
75+
const client: any = await response.json()
76+
const clientSecret = client.client_secret
77+
78+
return clientSecret
79+
},
80+
6181
async subscribeToPlan(body: { type: string, plan: string, description: string }): Promise<string> {
6282
const url = 'http://localhost:3008/payments/create-subscription'
6383

resources/views/dashboard/components/billing/payment-form.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ onMounted(async () => {
2222
})
2323
2424
async function pay() {
25+
await paymentStore.fetchPaymentIntent(1, props.productId)
26+
2527
await handlePayment(element.value)
2628
2729
emit('cancelPayment')

resources/views/dashboard/settings/billing.vue

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script setup lang="ts">
22
import { useBillable } from '../../../functions/billing/payments'
33
4-
import ActivePlan from '../components/billing/active-plan.vue'
4+
// import ActivePlan from '../components/billing/active-plan.vue'
55
6-
import PaymentMethod from '../components/billing/payment-method.vue'
7-
import Plans from '../components/billing/plans.vue'
6+
// import PaymentMethod from '../components/billing/payment-method.vue'
7+
// import Plans from '../components/billing/plans.vue'
88
9-
import LoadingDetails from '../components/skeleton/loading-card.vue'
10-
import TransactionHistory from '../components/transaction/index.vue'
9+
// import LoadingDetails from '../components/skeleton/loading-card.vue'
10+
// import TransactionHistory from '../components/transaction/index.vue'
11+
12+
import OneTimePayment from '../components/billing/one-time-payment.vue'
1113
1214
const { isEmpty, showCurrentPlan } = useBillable()
1315
@@ -26,7 +28,7 @@ onMounted(async () => {
2628

2729
<template>
2830
<div class="mx-auto px-4 py-8 container lg:px-8">
29-
<div id="subscribed">
31+
<!-- <div id="subscribed">
3032
<TransactionHistory />
3133
<div class="flex space-x-8">
3234
<div class="mt-16 w-2/3 bg-white px-8 py-6 shadow ring-1 ring-black ring-opacity-5 sm:rounded-lg">
@@ -38,6 +40,8 @@ onMounted(async () => {
3840
</div>
3941
<PaymentMethod />
4042
</div>
41-
</div>
43+
</div> -->
44+
45+
<OneTimePayment :product="1" />
4246
</div>
4347
</template>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ export async function generateModelString(
11891189
}
11901190

11911191
if (useBillable) {
1192-
paymentImports += `import { PaymentMethodModel } from './PaymentMethod'`
1192+
paymentImports += `import { PaymentMethodModel } from './PaymentMethod'\n`
1193+
paymentImports += `import { TransactionModel } from './Transaction'`
11931194

11941195
billableStatements += ` async createStripeUser(options: Stripe.CustomerCreateParams): Promise<Stripe.Response<Stripe.Customer>> {
11951196
const customer = await manageCustomer.createStripeCustomer(this, options)
@@ -1203,6 +1204,12 @@ export async function generateModelString(
12031204
return customer
12041205
}
12051206
1207+
async storeTransaction(productId: number): Promise<TransactionModel> {
1208+
const transaction = await manageTransaction.store(this, productId)
1209+
1210+
return transaction
1211+
}
1212+
12061213
async deleteStripeUser(): Promise<Stripe.Response<Stripe.DeletedCustomer>> {
12071214
const deletedCustomer = await manageCustomer.deleteStripeUser(this)
12081215
return deletedCustomer
@@ -1558,7 +1565,7 @@ export async function generateModelString(
15581565
const fillable = JSON.stringify(getFillableAttributes(model, otherModelRelations))
15591566

15601567
return `import type { Generated, Insertable, Selectable, Updateable } from 'kysely'
1561-
import { manageCharge, manageCheckout, manageCustomer, manageInvoice, managePaymentMethod, manageSubscription, managePrice, manageSetupIntent, type Stripe } from '@stacksjs/payments'
1568+
import { manageCharge, manageCheckout, manageCustomer, manageInvoice, managePaymentMethod, manageSubscription, manageTransaction, managePrice, manageSetupIntent, type Stripe } from '@stacksjs/payments'
15621569
import { db, sql } from '@stacksjs/database'
15631570
import type { CheckoutLineItem, CheckoutOptions, StripeCustomerOptions } from '@stacksjs/types'
15641571
import { HttpError } from '@stacksjs/error-handling'

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './payment-method'
77
export * from './price'
88
export * from './setup-products'
99
export * from './subscription'
10+
export * from './transaction'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { UserModel } from '../../../../orm/src/models/User'
2+
import Product from '../../../../orm/src/models/Product'
3+
import { Transaction, type TransactionModel } from '../../../../orm/src/models/Transaction'
4+
5+
export interface manageTransaction {
6+
store: (user: UserModel, productId: number) => Promise<TransactionModel>
7+
}
8+
9+
export const manageTransaction: manageTransaction = (() => {
10+
async function store(user: UserModel, productId: number): Promise<TransactionModel> {
11+
const product = await Product.find(productId)
12+
13+
const data = {
14+
name: product?.name,
15+
description: '',
16+
amount: product?.unit_price,
17+
brand: 'visa',
18+
type: 'one-time',
19+
provider_id: '312321312',
20+
user_id: user.id,
21+
}
22+
23+
const transaction = await Transaction.create(data)
24+
25+
return transaction
26+
}
27+
28+
return { store }
29+
})()

storage/framework/database/models/generated/Transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default {
5252
required: 'amount is required',
5353
},
5454
},
55-
factory: () => collect(['visa', 'mastercard', 'amex', 'jcb']).random().first(),
55+
factory: () => faker.number.int({ min: 1000, max: 10000 }),
5656
},
5757

5858
brand: {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { CheckoutLineItem, CheckoutOptions, StripeCustomerOptions } from '@stacksjs/types'
22
import type { Generated, Insertable, Selectable, Updateable } from 'kysely'
33
import type { PaymentMethodModel } from './PaymentMethod'
4+
import type { TransactionModel } from './Transaction'
45
import { randomUUIDv7 } from 'bun'
56
import { cache } from '@stacksjs/cache'
67
import { db, sql } from '@stacksjs/database'
78
import { HttpError } from '@stacksjs/error-handling'
89
import { dispatch } from '@stacksjs/events'
9-
import { manageCharge, manageCheckout, manageCustomer, manageInvoice, managePaymentMethod, manageSetupIntent, manageSubscription, type Stripe } from '@stacksjs/payments'
10+
import { manageCharge, manageCheckout, manageCustomer, manageInvoice, managePaymentMethod, manageSetupIntent, manageSubscription, manageTransaction, type Stripe } from '@stacksjs/payments'
1011
import Deployment from './Deployment'
1112

1213
import PaymentMethod from './PaymentMethod'
@@ -837,6 +838,12 @@ export class UserModel {
837838
return customer
838839
}
839840

841+
async storeTransaction(productId: number): Promise<TransactionModel> {
842+
const transaction = await manageTransaction.store(this, productId)
843+
844+
return transaction
845+
}
846+
840847
async deleteStripeUser(): Promise<Stripe.Response<Stripe.DeletedCustomer>> {
841848
const deletedCustomer = await manageCustomer.deleteStripeUser(this)
842849
return deletedCustomer

0 commit comments

Comments
 (0)