// This is your test publishable API key. //pk_live_yOmaGMgVLLY9G715qE3ZYMAg00OHUdlBVg //const stripe = Stripe("pk_test_51Ml6nsJJZrJpuNh5qBQEw8wRMhK8MaC8bfTDo0zIFJHo7IOJOIoUT98KGmQVVsTWpnqEGzqXNavPvL9sKUjMKEvk004TIYJ3qA"); const stripe = Stripe("pk_live_yOmaGMgVLLY9G715qE3ZYMAg00OHUdlBVg"); // var stripe = ''; // $.ajax({ // url: 'main.php', // type: "POST", // data : {Request: 'PublishKey'}, // success: function(data) { // console.log(data) // stripe = data; // console.log(stripe) // } // }); let elements; // initialize(); // checkStatus(); document .querySelector("#payment-form") .addEventListener("submit", handleSubmit); let emailAddress = ''; // function asyncAjax(url){ // return new Promise(function(resolve, reject) { // $.ajax({ // url: url, // type: "POST", // data : {'Request' : 'clientsecret'}, // success: function(data) { // resolve(data) // Resolve promise and when success // }, // error: function(err) { // reject(err) // Reject the promise and go to catch() // } // }); // }); // } // Fetches a payment intent and captures the client secret function initialize(clientSecret) { $("#PaymentModal").modal('show'); //const clientSecret = clientsecret; //JSON.parse(await asyncAjax('main.php')); //console.log({ clientSecret }) elements = stripe.elements({clientSecret}); // const linkAuthenticationElement = elements.create("linkAuthentication"); // linkAuthenticationElement.mount("#link-authentication-element"); // const appearance = { // theme: 'stripe', // // variables: { // // colorPrimary: '#0570de', // // colorBackground: '#ffffff', // // colorText: '#30313d', // // colorDanger: '#df1b41', // // fontFamily: 'Poppins, sans-serif' , // // spacingUnit: '2px', // // borderRadius: '0px', // // // See all possible variables below // // }, // rules :{ // 'Input--empty': { // background: 'white', // }, // } // }; const paymentElement = elements.create("payment"); paymentElement.mount("#payment-element"); } async function handleSubmit(e) { e.preventDefault(); setLoading(true); let returnURL = window.location.href + '/main.php/?msg=success' const { error } = await stripe.confirmPayment({ elements, confirmParams: { // Make sure to change this to your payment completion page return_url: returnURL, receipt_email: emailAddress, }, }); // 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."); } setLoading(false); } // Fetches the payment intent status after payment submission async function checkStatus(clientSecret) { // 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); } // Show a spinner on payment submission function setLoading(isLoading) { if (isLoading) { // Disable the button and show a spinner document.querySelector("#submit").disabled = true; document.querySelector("#spinner").classList.remove("hidden"); document.querySelector("#button-text").classList.add("hidden"); } else { document.querySelector("#submit").disabled = false; document.querySelector("#spinner").classList.add("hidden"); document.querySelector("#button-text").classList.remove("hidden"); } }