Skip to content

Commit fb24ef9

Browse files
chore: wip
1 parent 5621654 commit fb24ef9

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

resources/functions/billing/payments.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cardElement } from '@stacksjs/browser'
1+
import { loadCardElement, confirmCardSetup } from '@stacksjs/browser'
22

33
const paymentStore = usePaymentStore()
44

@@ -32,39 +32,22 @@ export function useBillable() {
3232
return `${month} ${day}, ${year}`
3333
}
3434

35-
async function loadPaymentElement(clientSecret: string): Promise<any> {
36-
const element = cardElement()
35+
async function loadPaymentElement(clientSecret: string): Promise<boolean> {
36+
const isCreated = await loadCardElement(clientSecret)
3737

38-
return element
39-
40-
// if (stripe) {
41-
// elements.value = stripe.value.elements({ clientSecret })
42-
43-
// const paymentElement = elements.value.create('payment', {
44-
// fields: { billingDetails: 'auto' },
45-
// })
46-
47-
// paymentElement.mount('#payment-element')
48-
49-
// return true
50-
// }
51-
52-
// return false
38+
return isCreated
5339
}
5440

5541
async function handleAddPaymentMethod(clientSecret: string, elements: any) {
56-
if (!stripe.value || !elements)
57-
return
58-
59-
const { setupIntent, error } = await stripe.value.confirmCardSetup(
42+
const param = {
6043
clientSecret,
61-
{
62-
payment_method: {
63-
card: elements,
64-
billing_details: { name: 'Chris Breuer' },
65-
},
44+
payment_method: {
45+
card: elements,
46+
billing_details: { name: 'Chris Breuer' },
6647
},
67-
)
48+
};
49+
50+
const { setupIntent, error } = await confirmCardSetup(param)
6851

6952
if (error) {
7053
console.error(error.message)
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
import { loadStripe } from '@stripe/stripe-js'
22
import { ref } from 'vue'
33

4+
interface PaymentMethod {
5+
card: any; // Replace `any` with the appropriate type for `elements`
6+
billing_details: {
7+
name: string;
8+
};
9+
}
10+
11+
interface PaymentParam {
12+
clientSecret: string;
13+
payment_method: PaymentMethod;
14+
}
15+
416
export const publishableKey: string = import.meta.env.FRONTEND_STRIPE_PUBLIC_KEY || ''
517

618
const client = ref(null as any)
719

8-
9-
export async function cardElement(): Promise<any> {
20+
export async function loadCardElement(clientSecret: string): Promise<boolean> {
1021
client.value = await loadStripe(publishableKey)
1122

1223
const elements = client.value.elements()
1324
const cardElement = elements.create('card')
1425

1526
cardElement.mount('#payment-element')
1627

17-
return cardElement
28+
if (client) {
29+
elements.value = client.value.elements({ clientSecret })
30+
31+
const paymentElement = elements.value.create('payment', {
32+
fields: { billingDetails: 'auto' },
33+
})
34+
35+
paymentElement.mount('#payment-element')
36+
37+
return true
38+
}
39+
40+
return false
41+
}
42+
43+
export async function confirmCardSetup(card: PaymentParam): Promise<{ setupIntent: any, error: any }> {
44+
const clientSecret = card.clientSecret
45+
46+
const { setupIntent, error } = await client.value.confirmCardSetup(card)
47+
48+
return { setupIntent, error }
1849
}

0 commit comments

Comments
 (0)