-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_stripe.antlers.html
98 lines (80 loc) · 2.88 KB
/
_stripe.antlers.html
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
96
97
98
<div>
<div id="payment-element">
<!--Stripe.js injects the Payment Element-->
</div>
<div id="payment-message" class="hidden"></div>
</div>
<input id="stripePaymentMethod" type="hidden" name="payment_method">
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('{{ stripe:config:key }}');
let elements;
checkStatus();
elements = stripe.elements({
clientSecret: '{{ stripe:client_secret }}'
});
const paymentElementOptions = {
layout: "tabs",
};
const paymentElement = elements.create("payment", paymentElementOptions);
paymentElement.mount("#payment-element");
async function confirmPayment() {
const {
error
} = await stripe.confirmPayment({
elements,
confirmParams: {
return_url: "{{ stripe:callback_url }}",
},
});
// This point will only be reached if there is an immediate error when
// confirming the payment. Otherwise, your customer will be redirected to
// your `return_url`. For some payment methods like iDEAL, your customer will
// be redirected to an intermediate site first to authorize the payment, then
// redirected to the `return_url`.
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occurred.");
}
}
// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!clientSecret) {
return;
}
const {
paymentIntent
} = await stripe.retrievePaymentIntent(clientSecret);
switch (paymentIntent.status) {
case "succeeded":
showMessage("Payment succeeded!");
break;
case "processing":
showMessage("Your payment is processing.");
break;
case "requires_payment_method":
showMessage("Your payment was not successful, please try again.");
break;
default:
showMessage("Something went wrong.");
break;
}
}
// ------- UI helpers -------
function showMessage(messageText) {
const messageContainer = document.querySelector("#payment-message");
messageContainer.classList.remove("hidden");
messageContainer.textContent = messageText;
setTimeout(function() {
messageContainer.classList.add("hidden");
messageText.textContent = "";
}, 4000);
}
document.addEventListener('StripeConfirmPayment', function() {
confirmPayment();
});
</script>