1818</template >
1919
2020<script setup lang="ts">
21- import { type StripeElementsOptionsMode , loadStripe } from " @stripe/stripe-js"
21+ import { loadStripe } from " @stripe/stripe-js"
22+ import type {
23+ StripeElementsOptionsMode ,
24+ StripePaymentElementOptions ,
25+ } from " @stripe/stripe-js"
2226/**
2327 * DEFERRED PAYMENT
2428 * This Payment Element example is based on
2529 * https://docs.stripe.com/payments/accept-a-payment-deferred?type=payment
2630 */
27- import { onBeforeMount , ref } from " vue"
31+ import { onBeforeMount , ref , useTemplateRef } from " vue"
2832import StripeElement from " ../src/components/StripeElement.vue"
2933import StripeElements from " ../src/components/StripeElements.vue"
3034
31- const stripeKey = ref (
32- " pk_test_51NH8GWJOvIxb7wA7NEsy1lurmkwPRypNibLx2Dhvq28h2VICCXViQ5UI85vrPDwRwitMCVQeQTEBQOFKcLhaAr7i00aEax8rAB" ,
33- )
35+ const stripeKey = ref (" pk_test_f3duw0VsAEM2TJFMtWQ90QAT" )
3436const instanceOptions = ref ({
3537 // https://stripe.com/docs/js/initializing#init_stripe_js-options
3638})
@@ -39,21 +41,54 @@ const elementsOptions = ref<StripeElementsOptionsMode>({
3941 mode: " payment" ,
4042 amount: 1099 ,
4143 currency: " eur" ,
44+ appearance: {
45+ theme: " flat" ,
46+ },
4247})
43- const paymentElementOptions = ref ({
48+ const paymentElementOptions = ref < StripePaymentElementOptions > ({
4449 // https://stripe.com/docs/stripe.js#element-options
4550})
4651const stripeLoaded = ref (false )
52+ const clientSecret = ref (" " )
4753
4854// Define component refs
49- const elementsComponent = ref ()
50- const paymentComponent = ref ()
51-
52- function handleSubmit() {}
55+ const elementsComponent = useTemplateRef (" elementsComponent" )
56+ const paymentComponent = useTemplateRef (" paymentComponent" )
5357
5458onBeforeMount (() => {
5559 loadStripe (stripeKey .value ).then (() => {
5660 stripeLoaded .value = true
61+
62+ // Good place to call your backend to create PaymentIntent
63+ // Skipping to the point when you got client_secret
64+
65+ // clientSecret.value = client_secret
5766 })
5867})
68+
69+ async function handleSubmit() {
70+ // Confirm the PaymentIntent using the details collected by the Payment Element
71+ const stripeInstance = elementsComponent .value ?.instance
72+ const elements = elementsComponent .value ?.elements
73+
74+ if (stripeInstance ) {
75+ const { error } = await stripeInstance .confirmPayment ({
76+ elements ,
77+ clientSecret: clientSecret .value ,
78+ confirmParams: {
79+ return_url: " https://example.com/order/123/complete" ,
80+ },
81+ })
82+
83+ if (error ) {
84+ // This point is only reached if there's an immediate error when
85+ // confirming the payment. Show the error to your customer (for example, payment details incomplete)
86+ console .log (error )
87+ } else {
88+ // Your customer is redirected to your `return_url`. For some payment
89+ // methods like iDEAL, your customer is redirected to an intermediate
90+ // site first to authorize the payment, then redirected to the `return_url`.
91+ }
92+ }
93+ }
5994 </script >
0 commit comments