Skip to content

Commit 037994d

Browse files
chore: wip
1 parent 983a35a commit 037994d

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

resources/functions/billing/payments.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export const publishableKey = import.meta.env.FRONTEND_STRIPE_PUBLIC_KEY
22
import { loadStripe } from '@stripe/stripe-js'
33

4+
const stripe = ref(null as any)
5+
const elements = ref(null as any)
6+
47
export function useBillable() {
58
async function fetchSetupIntent(): Promise<string> {
69
const url = 'http://localhost:3008/stripe/create-setup-intent'
@@ -19,20 +22,41 @@ export function useBillable() {
1922
return clientSecret
2023
}
2124

22-
async function loadStripeElement(clientSecret: string): Promise<any | undefined> {
23-
const stripe = await loadStripe(publishableKey)
25+
async function loadStripeElement(clientSecret: string): Promise<boolean> {
26+
stripe.value = await loadStripe(publishableKey)
2427

2528
if (stripe) {
26-
const elements = stripe.elements({ clientSecret })
27-
const paymentElement = elements.create('payment', {
29+
elements.value = stripe.value.elements({ clientSecret })
30+
31+
const paymentElement = elements.value.create('payment', {
2832
fields: { billingDetails: 'auto' },
2933
})
3034

31-
return paymentElement
35+
paymentElement.mount('#payment-element')
36+
37+
return true
3238
}
3339

34-
return undefined
40+
return false
41+
}
42+
43+
async function handleAddPaymentMethod() {
44+
if (!stripe.value || !elements.value) return
45+
46+
const { setupIntent, error } = await stripe.value.confirmSetup({
47+
elements: elements.value,
48+
confirmParams: {
49+
return_url: 'http://localhost:5173/settings/billing'
50+
},
51+
})
52+
53+
if (error) {
54+
console.error(error.message) // Display or handle error for the user
55+
} else {
56+
console.log('Setup Intent successful:', setupIntent)
57+
// You might save setupIntent.id to your database here
58+
}
3559
}
3660

37-
return { fetchSetupIntent, loadStripeElement }
61+
return { fetchSetupIntent, loadStripeElement, handleAddPaymentMethod }
3862
}

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ import { useBillable } from '../../../../functions/billing/payments'
44
const stripeLoading = ref(true)
55
const showStripe = ref(false)
66
7-
const { fetchSetupIntent, loadStripeElement } = useBillable()
7+
const { fetchSetupIntent, loadStripeElement, handleAddPaymentMethod } = useBillable()
88
9-
async function addPaymentMethod() {
9+
async function loadWebElement() {
1010
const clientSecret = await fetchSetupIntent()
1111
12-
const paymentElement = await loadStripeElement(clientSecret)
13-
14-
if (paymentElement) {
15-
paymentElement.mount('#payment-element')
16-
17-
showStripe.value = true
18-
}
12+
showStripe.value = await loadStripeElement(clientSecret)
1913
2014
stripeLoading.value = false
2115
}
@@ -55,7 +49,7 @@ function cancelPaymentForm() {
5549
<button
5650
type="button"
5751
class="rounded-md bg-blue-600 px-2.5 py-1.5 text-sm text-white font-semibold shadow-sm hover:bg-blue-gray-500 focus-visible:outline-2 focus-visible:outline-blue-600 focus-visible:outline-offset-2 focus-visible:outline"
58-
@click="cancelPaymentForm()"
52+
@click="handleAddPaymentMethod()"
5953
>
6054
Save Payment Method
6155
</button>
@@ -76,7 +70,7 @@ function cancelPaymentForm() {
7670
<button
7771
type="button"
7872
class="rounded-md bg-blue-600 px-2.5 py-1.5 text-sm text-white font-semibold shadow-sm hover:bg-blue-gray-500 focus-visible:outline-2 focus-visible:outline-blue-600 focus-visible:outline-offset-2 focus-visible:outline"
79-
@click="addPaymentMethod()"
73+
@click="loadWebElement()"
8074
>
8175
Add Payment Method
8276
</button>

0 commit comments

Comments
 (0)