File tree Expand file tree Collapse file tree 2 files changed +45
-31
lines changed
resources/functions/billing
storage/framework/core/browser/src/utils Expand file tree Collapse file tree 2 files changed +45
-31
lines changed Original file line number Diff line number Diff line change 1
- import { cardElement } from '@stacksjs/browser'
1
+ import { loadCardElement , confirmCardSetup } from '@stacksjs/browser'
2
2
3
3
const paymentStore = usePaymentStore ( )
4
4
@@ -32,39 +32,22 @@ export function useBillable() {
32
32
return `${ month } ${ day } , ${ year } `
33
33
}
34
34
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 )
37
37
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
53
39
}
54
40
55
41
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 = {
60
43
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' } ,
66
47
} ,
67
- )
48
+ } ;
49
+
50
+ const { setupIntent, error } = await confirmCardSetup ( param )
68
51
69
52
if ( error ) {
70
53
console . error ( error . message )
Original file line number Diff line number Diff line change 1
1
import { loadStripe } from '@stripe/stripe-js'
2
2
import { ref } from 'vue'
3
3
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
+
4
16
export const publishableKey : string = import . meta. env . FRONTEND_STRIPE_PUBLIC_KEY || ''
5
17
6
18
const client = ref ( null as any )
7
19
8
-
9
- export async function cardElement ( ) : Promise < any > {
20
+ export async function loadCardElement ( clientSecret : string ) : Promise < boolean > {
10
21
client . value = await loadStripe ( publishableKey )
11
22
12
23
const elements = client . value . elements ( )
13
24
const cardElement = elements . create ( 'card' )
14
25
15
26
cardElement . mount ( '#payment-element' )
16
27
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 }
18
49
}
You can’t perform that action at this time.
0 commit comments