-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCardElementLegacy.vue
95 lines (86 loc) · 2.36 KB
/
CardElementLegacy.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<template>
<StripeElements
v-if="stripeLoaded"
v-slot="{ elements }"
:stripe-key="stripeKey"
:instance-options="stripeOptions"
:elements-options="elementsOptions"
ref="elms"
class="py-3"
>
<StripeElement
:elements="elements"
:options="cardOptions"
ref="card"
/>
</StripeElements>
<button
type="button"
@click="pay"
class="w-full focus:outline-none text-white bg-indigo-700 hover:bg-indigo-800 focus:ring-4 focus:ring-indigo-300 font-medium rounded-lg text-sm px-5 py-2.5 mb-2 dark:bg-indigo-600 dark:hover:bg-indigo-700 dark:focus:ring-indigo-900"
>
Pay now
</button>
</template>
<script lang="ts">
import { loadStripe } from "@stripe/stripe-js"
import { defineComponent, onBeforeMount, ref } from "vue"
import StripeElement from "../src/components/StripeElement.vue"
import StripeElements from "../src/components/StripeElements.vue"
export default defineComponent({
name: "CardElement",
components: {
StripeElements,
StripeElement,
},
setup() {
const stripeKey = "pk_test_TYooMQauvdEDq54NiTphI7jx" // test key
const stripeOptions = ref({
// https://stripe.com/docs/js/initializing#init_stripe_js-options
})
const elementsOptions = ref({
// https://stripe.com/docs/js/elements_object/create#stripe_elements-options
})
const cardOptions = ref({
// https://docs.stripe.com/js/elements_object/create_element?type=card#elements_create-options
value: {
postalCode: "12345",
},
})
const stripeLoaded = ref(false)
const card = ref()
const elms = ref()
onBeforeMount(() => {
const stripePromise = loadStripe(stripeKey)
stripePromise.then(() => {
stripeLoaded.value = true
})
})
return {
stripeKey,
stripeLoaded,
stripeOptions,
elementsOptions,
cardOptions,
card,
elms,
}
},
methods: {
pay() {
/*
WARNING:
legacy way to implement card payments,
for modern card example, see CardElement.vue
*/
// Get stripe element
const cardElement = this.card.stripeElement
// "instance" means Stripe Instance or Stripe object
this.elms.instance.createToken(cardElement).then((result: object) => {
// Handle result.error or result.token
console.log(result)
})
},
},
})
</script>