capsul-flask/capsulflask/static/pay-with-stripe.js

39 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

window.addEventListener('DOMContentLoaded', function(event) {
var httpRequest = new XMLHttpRequest();
httpRequest.onloadend = () => {
if (httpRequest.status < 300) {
try {
responseObject = JSON.parse(httpRequest.responseText);
if(!responseObject.hasRedirectedAlready) {
Stripe(document.getElementById("stripe_public_key").value)
.redirectToCheckout({
sessionId: document.getElementById("stripe_checkout_session_id").value,
})
.then(function(result) {
if (result.error) {
alert("Stripe.redirectToCheckout() failed with: " + result.error.message)
}
});
} else {
location.href = '/payment/stripe';
}
} catch (err) {
alert("could not redirect to stripe because capsul did not return valid json");
}
} else {
alert("could not redirect to stripe because capsul returned HTTP" + httpRequest.status + ", expected HTTP 200");
}
};
httpRequest.ontimeout = () => {
alert("could not redirect to stripe because capsul timed out");
};
httpRequest.open("GET", "/payment/stripe/"+document.getElementById("stripe_checkout_session_id").value+"/json?q="+String(Math.random()).substring(2, 8));
httpRequest.timeout = 10000;
httpRequest.send();
});