main
parent
bf778b805d
commit
46c5b97c36
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,80 +1,74 @@
|
|||||||
document.addEventListener("DOMContentLoaded", function () {
|
const displayPaymentModalButtons = document.querySelectorAll('.displayPaymentModal');
|
||||||
|
const closeButton = document.querySelector('#closePaymentModalButton');
|
||||||
|
const paymentModal = document.getElementById('paymentModal');
|
||||||
|
|
||||||
Checkout.configure({
|
displayPaymentModalButtons.forEach(function (button) {
|
||||||
session: {
|
button.addEventListener('click', function () {
|
||||||
id: 1, // Default session ID, will be replaced with actual session ID after initiation
|
const item_id = button.getAttribute('item-id');
|
||||||
}
|
paymentModal.classList.remove('hidden');
|
||||||
|
document.body.style.overflow = 'hidden';
|
||||||
|
|
||||||
|
initiateCheckout(item_id);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function initiateCheckout(item_id) {
|
||||||
function initiateCheckout() {
|
const csrftoken = getCookie('csrftoken');
|
||||||
const csrftoken = getCookie('csrftoken');
|
console.log(item_id);
|
||||||
|
|
||||||
console.log('CSRF Token:', csrftoken);
|
console.log('CSRF Token:', csrftoken);
|
||||||
|
|
||||||
fetch('/initiate_checkout/', {
|
fetch('/initiate_checkout/', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-CSRFToken': csrftoken
|
'X-CSRFToken': csrftoken,
|
||||||
},
|
'X-Item-ID': item_id
|
||||||
body: JSON.stringify({})
|
},
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Network response was not ok');
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
})
|
|
||||||
.then(data => {
|
|
||||||
// Handle success response
|
|
||||||
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', () => { $('#paymentModal').modal(); });
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error('There was a problem with the fetch operation:', error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get CSRF token from cookie
|
body: JSON.stringify({ item_id: item_id }),
|
||||||
function getCookie(name) {
|
})
|
||||||
let cookieValue = null;
|
.then(response => {
|
||||||
if (document.cookie && document.cookie !== '') {
|
if (!response.ok) {
|
||||||
const cookies = document.cookie.split(';');
|
throw new Error('Network response was not ok');
|
||||||
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;
|
return response.json();
|
||||||
}
|
})
|
||||||
|
.then(data => {
|
||||||
const displayPaymentModalButton = document.querySelectorAll('.displayPaymentModal');
|
// Handle success response
|
||||||
const closeButton = document.querySelector('#closePaymentModalButton');
|
console.log("Session ID: " + data.session_id);
|
||||||
const paymentModal = document.getElementById('paymentModal');
|
// Update Checkout session with the fetched session ID and show the embedded page
|
||||||
|
Checkout.configure({
|
||||||
displayPaymentModalButton.forEach(function (button) {
|
session: {
|
||||||
button.addEventListener('click', function () {
|
id: data.session_id,
|
||||||
paymentModal.classList.remove('hidden');
|
}
|
||||||
document.body.style.overflow = 'hidden';
|
|
||||||
|
|
||||||
initiateCheckout();
|
|
||||||
});
|
});
|
||||||
|
$('#paymentContent').empty();
|
||||||
|
Checkout.showEmbeddedPage('#paymentContent', () => { $('#paymentModal').modal(); });
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('There was a problem with the fetch operation:', error);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
closeButton.addEventListener('click', function () {
|
closeButton.addEventListener('click', function () {
|
||||||
paymentModal.classList.add('hidden');
|
paymentModal.classList.add('hidden');
|
||||||
sessionStorage.clear();
|
sessionStorage.clear();
|
||||||
document.body.style.overflow = '';
|
document.body.style.overflow = '';
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue