18
18
</template >
19
19
20
20
<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"
22
26
/**
23
27
* DEFERRED PAYMENT
24
28
* This Payment Element example is based on
25
29
* https://docs.stripe.com/payments/accept-a-payment-deferred?type=payment
26
30
*/
27
- import { onBeforeMount , ref } from " vue"
31
+ import { onBeforeMount , ref , useTemplateRef } from " vue"
28
32
import StripeElement from " ../src/components/StripeElement.vue"
29
33
import StripeElements from " ../src/components/StripeElements.vue"
30
34
31
- const stripeKey = ref (
32
- " pk_test_51NH8GWJOvIxb7wA7NEsy1lurmkwPRypNibLx2Dhvq28h2VICCXViQ5UI85vrPDwRwitMCVQeQTEBQOFKcLhaAr7i00aEax8rAB" ,
33
- )
35
+ const stripeKey = ref (" pk_test_f3duw0VsAEM2TJFMtWQ90QAT" )
34
36
const instanceOptions = ref ({
35
37
// https://stripe.com/docs/js/initializing#init_stripe_js-options
36
38
})
@@ -39,21 +41,54 @@ const elementsOptions = ref<StripeElementsOptionsMode>({
39
41
mode: " payment" ,
40
42
amount: 1099 ,
41
43
currency: " eur" ,
44
+ appearance: {
45
+ theme: " flat" ,
46
+ },
42
47
})
43
- const paymentElementOptions = ref ({
48
+ const paymentElementOptions = ref < StripePaymentElementOptions > ({
44
49
// https://stripe.com/docs/stripe.js#element-options
45
50
})
46
51
const stripeLoaded = ref (false )
52
+ const clientSecret = ref (" " )
47
53
48
54
// 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" )
53
57
54
58
onBeforeMount (() => {
55
59
loadStripe (stripeKey .value ).then (() => {
56
60
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
57
66
})
58
67
})
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
+ }
59
94
</script >
0 commit comments