You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB
JavaScript

const paymentContent = document.getElementById('paymentContent');
const paymentLoader = document.getElementById('paymentLoader');
const payment_id = document.getElementById('paymentId').textContent;
function initiatePaymentCheckout(payment_id) {
const csrftoken = getCookie('csrftoken');
console.log(payment_id);
console.log('CSRF Token:', csrftoken);
paymentLoader.classList.remove('hidden');
fetch('/payment/initiate-payment-checkout/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken,
},
body: JSON.stringify({ payment_id: payment_id }),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log("Session ID: " + data.session_id);
// Update Checkout session with the fetched session ID and show the embedded page
Checkout.configure({
session: {
id: data.session_id,
}
});
$('#paymentContent').empty();
Checkout.showEmbeddedPage('#paymentContent');
sessionStorage.clear();
paymentLoader.classList.add('hidden');
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
paymentLoader.classList.add('hidden');
});
}
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
initiatePaymentCheckout(payment_id);