Skip to content

Commit 834e3ea

Browse files
committed
refactor: use await in Card example
1 parent 00d9e9a commit 834e3ea

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

examples/CardElement.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<script setup lang="ts">
2626
import { loadStripe } from "@stripe/stripe-js"
2727
import type {
28+
Stripe,
2829
StripeCardElement,
2930
StripeCardElementOptions,
3031
StripeConstructorOptions,
@@ -52,23 +53,24 @@ const stripeLoaded = ref(false)
5253
5354
const elementsComponent = useTemplateRef("elementsComponent")
5455
const cardComponent = useTemplateRef("cardComponent")
56+
const stripe = ref<Stripe | null>(null)
5557
56-
onBeforeMount(() => {
57-
loadStripe(publishableKey.value).then(() => {
58-
stripeLoaded.value = true
59-
})
58+
onBeforeMount(async () => {
59+
stripe.value = await loadStripe(publishableKey.value)
60+
stripeLoaded.value = true
6061
})
6162
6263
function handlePay() {
6364
// You need to implement backend for creating PaymentIntent
6465
// Learn more by reading https://docs.stripe.com/payments/card-element?lang=node
65-
const stripeInstance = elementsComponent.value?.instance
6666
const card = cardComponent.value?.stripeElement as StripeCardElement
67+
// You can also access stripe instance from the component
68+
const stripeInstance = elementsComponent.value?.instance // same as stripe.value
6769
6870
// Let's skip to the point you got clientSecret
6971
const clientSecret = "i_was_created_on_server"
7072
71-
stripeInstance
73+
stripe.value
7274
?.confirmCardPayment(clientSecret, {
7375
payment_method: {
7476
card,

0 commit comments

Comments
 (0)