Skip to content

Commit 5621654

Browse files
chore: wip
1 parent fe50d79 commit 5621654

File tree

7 files changed

+46
-354
lines changed

7 files changed

+46
-354
lines changed

resources/functions/billing/payments.ts

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { loadStripe } from '@stripe/stripe-js'
1+
import { cardElement } from '@stacksjs/browser'
22

3-
export const publishableKey = import.meta.env.FRONTEND_STRIPE_PUBLIC_KEY
43
const paymentStore = usePaymentStore()
54

6-
const stripe = ref(null as any)
7-
85
export function useBillable() {
96
function convertUnixTimestampToDate(timestamp: number): string {
107
// Create a Date object from the Unix timestamp
@@ -35,15 +32,10 @@ export function useBillable() {
3532
return `${month} ${day}, ${year}`
3633
}
3734

38-
async function loadStripeElement(clientSecret: string): Promise<any> {
39-
stripe.value = await loadStripe(publishableKey)
40-
41-
const elements = stripe.value.elements()
42-
const cardElement = elements.create('card')
43-
44-
cardElement.mount('#payment-element')
35+
async function loadPaymentElement(clientSecret: string): Promise<any> {
36+
const element = cardElement()
4537

46-
return cardElement
38+
return element
4739

4840
// if (stripe) {
4941
// elements.value = stripe.value.elements({ clientSecret })
@@ -60,25 +52,6 @@ export function useBillable() {
6052
// return false
6153
}
6254

63-
async function setDefaultPaymentMethod(setupIntent: string): Promise<string> {
64-
const url = 'http://localhost:3008/stripe/set-default-payment-method'
65-
66-
const body = { setupIntent }
67-
68-
const response = await fetch(url, {
69-
method: 'POST',
70-
headers: {
71-
'Content-Type': 'application/json',
72-
'Accept': 'application/json',
73-
},
74-
body: JSON.stringify(body),
75-
})
76-
77-
const res: any = await response.json()
78-
79-
return res
80-
}
81-
8255
async function handleAddPaymentMethod(clientSecret: string, elements: any) {
8356
if (!stripe.value || !elements)
8457
return
@@ -98,7 +71,7 @@ export function useBillable() {
9871
} // Display or handle error for the user
9972
else {
10073
if (!paymentStore.hasPaymentMethods)
101-
await setDefaultPaymentMethod(setupIntent.payment_method)
74+
await paymentStore.setDefaultPaymentMethod(setupIntent.payment_method)
10275
}
10376
}
10477

@@ -128,5 +101,5 @@ export function useBillable() {
128101
paymentStore.closePlans()
129102
}
130103

131-
return { loadStripeElement, handleAddPaymentMethod, isEmpty, convertUnixTimestampToDate, editPlan, updatingPlanState, showCurrentPlan, cancelEditPlan, showPlans }
104+
return { loadPaymentElement, handleAddPaymentMethod, isEmpty, convertUnixTimestampToDate, editPlan, updatingPlanState, showCurrentPlan, cancelEditPlan, showPlans }
132105
}

resources/stores/payment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ export const usePaymentStore = defineStore('payment', {
7575
return client
7676
},
7777

78+
async setDefaultPaymentMethod(setupIntent: string): Promise<string> {
79+
const url = 'http://localhost:3008/stripe/set-default-payment-method'
80+
81+
const body = { setupIntent }
82+
83+
const response = await fetch(url, {
84+
method: 'POST',
85+
headers: {
86+
'Content-Type': 'application/json',
87+
'Accept': 'application/json',
88+
},
89+
body: JSON.stringify(body),
90+
})
91+
92+
const res: any = await response.json()
93+
94+
return res
95+
},
96+
7897
openPlans() {
7998
this.planState = true
8099
},

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ const clientSecret = ref('')
1212
1313
const isLoadingWebElement = ref(false)
1414
const showStripe = ref(false)
15-
const elements = ref('')
1615
17-
const { loadStripeElement, handleAddPaymentMethod } = useBillable()
16+
const { handleAddPaymentMethod } = useBillable()
1817
1918
async function loadWebElement() {
2019
isLoadingWebElement.value = true
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { loadStripe } from '@stripe/stripe-js'
2+
import { ref } from 'vue'
3+
4+
export const publishableKey: string = import.meta.env.FRONTEND_STRIPE_PUBLIC_KEY || ''
5+
6+
const client = ref(null as any)
7+
8+
9+
export async function cardElement(): Promise<any> {
10+
client.value = await loadStripe(publishableKey)
11+
12+
const elements = client.value.elements()
13+
const cardElement = elements.create('card')
14+
15+
cardElement.mount('#payment-element')
16+
17+
return cardElement
18+
}

storage/framework/core/browser/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './base'
2+
export * from './billlable'
23
export * from './debounce'
34
export * from './function'
45
export * from './guards'

0 commit comments

Comments
 (0)