mirror of
https://github.com/YunoHost/pepettes.git
synced 2024-09-03 20:06:20 +02:00
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
|
// Fetch your Stripe publishable key to initialize Stripe.js
|
||
|
// In practice, you might just hard code the publishable API
|
||
|
// key here.
|
||
|
fetch('/config')
|
||
|
.then(function (result) {
|
||
|
return result.json();
|
||
|
})
|
||
|
.then(function (json) {
|
||
|
window.config = json;
|
||
|
window.stripe = Stripe(config.publicKey);
|
||
|
});
|
||
|
|
||
|
// When the form is submitted...
|
||
|
var submitBtn = document.querySelector('#submit');
|
||
|
submitBtn.addEventListener('click', function (evt) {
|
||
|
var inputEl = document.getElementById('quantity');
|
||
|
var quantity = parseInt(inputEl.value);
|
||
|
inputEl = document.getElementById('currency');
|
||
|
var currency = inputEl.value;
|
||
|
inputEl = document.getElementById('frequency');
|
||
|
var frequency = inputEl.value;
|
||
|
|
||
|
// Create the checkout session.
|
||
|
fetch('/create-checkout-session', {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json',
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
quantity: quantity,
|
||
|
currency: currency,
|
||
|
frequency: frequency
|
||
|
}),
|
||
|
}).then(function (result) {
|
||
|
return result.json();
|
||
|
}).then(function (data) {
|
||
|
// Redirect to Checkout. with the ID of the
|
||
|
// CheckoutSession created on the server.
|
||
|
stripe.redirectToCheckout({
|
||
|
sessionId: data.sessionId,
|
||
|
})
|
||
|
.then(function(result) {
|
||
|
// If redirection fails, display an error to the customer.
|
||
|
if (result.error) {
|
||
|
var displayError = document.getElementById('error-message');
|
||
|
displayError.textContent = result.error.message;
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|