· 5 years ago · Nov 06, 2020, 04:42 AM
1// A reference to Stripe.js initialized with your real test publishable API key.
2var stripe = Stripe("pk_test_51Hiu3uIHjp99TTJkeXmBM1qfZnlEKEkKM4n0zw3n7EkyeS4Co84pUiJan4bj6KZFIta8i5kISZtUPT5XiUQMCPNv00Yrss8gif");
3
4// The items the customer wants to buy
5var purchase = {
6 items: [{ id: "code-audit" }]
7};
8
9// Disable the button until we have Stripe set up on the page
10document.querySelector("button").disabled = true;
11fetch("/pay", {
12 method: "POST",
13 headers: {
14 "Content-Type": "application/json"
15 },
16 body: JSON.stringify(purchase)
17})
18 .then(function(result) {
19 return result.json();
20 })
21 .then(function(data) {
22 var elements = stripe.elements();
23
24 var style = {
25 base: {
26 color: "#161616",
27 fontFamily: "Roboto, sans-serif",
28 fontSmoothing: "antialiased",
29 fontSize: "16px",
30 "::placeholder": {
31 color: "#161616"
32 }
33 },
34 invalid: {
35 fontFamily: "Roboto, sans-serif",
36 color: "#fa755a",
37 iconColor: "#fa755a"
38 }
39 };
40
41 var card = elements.create("card", { style: style, hidePostalCode: true });
42 // Stripe injects an iframe into the DOM
43 card.mount("#card-element");
44
45 card.on("change", function (event) {
46 // Disable the Pay button if there are no card details in the Element
47 document.querySelector("button").disabled = event.empty;
48 document.querySelector("#card-error").textContent = event.error ? event.error.message : "";
49 });
50
51 var form = document.getElementById("payment-form");
52 form.addEventListener("submit", function(event) {
53 event.preventDefault();
54 // Complete payment when the submit button is clicked
55 payWithCard(stripe, card, data.clientSecret);
56 });
57 });
58
59// Calls stripe.confirmCardPayment
60// If the card requires authentication Stripe shows a pop-up modal to
61// prompt the user to enter authentication details without leaving your page.
62var payWithCard = function(stripe, card, clientSecret) {
63 loading(true);
64 stripe
65 .confirmCardPayment(clientSecret, {
66 receipt_email: document.getElementById('email').value,
67 payment_method: {
68 card: card
69 }
70 })
71 .then(function(result) {
72 if (result.error) {
73 // Show error to your customer
74 showError(result.error.message);
75 } else {
76 // The payment succeeded!
77 orderComplete(result.paymentIntent.id);
78 }
79 });
80};
81
82/* ------- UI helpers ------- */
83
84// Shows a success message when the payment is complete
85var orderComplete = function(paymentIntentId) {
86 loading(false);
87 document.querySelector(".result-message").classList.remove("hidden");
88 document.querySelector("button").disabled = true;
89};
90
91// Show the customer the error from Stripe if their card fails to charge
92var showError = function(errorMsgText) {
93 loading(false);
94 var errorMsg = document.querySelector("#card-error");
95 errorMsg.textContent = errorMsgText;
96 setTimeout(function() {
97 errorMsg.textContent = "";
98 }, 4000);
99};
100
101// Show a spinner on payment submission
102var loading = function(isLoading) {
103 if (isLoading) {
104 // Disable the button and show a spinner
105 document.querySelector("button").disabled = true;
106 document.querySelector("#spinner").classList.remove("hidden");
107 document.querySelector("#button-text").classList.add("hidden");
108 } else {
109 document.querySelector("button").disabled = false;
110 document.querySelector("#spinner").classList.add("hidden");
111 document.querySelector("#button-text").classList.remove("hidden");
112 }
113};
114