`;
button_submit.hidden = true;
return;
}
// Set loading spinner
product_selector_display.innerHTML = `
`;
////-- Initialise
//// Get price
async function get_price() {
try {
const ajax_getprice = await fetch('/post/ecommerce/generate_price', {
method: 'POST',
headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": "WsG9BqTYvlC9J6ZzW3C2ZRQtYPRSw6B1k1GX5XGI" },
body: JSON.stringify({
product: forminput_product.value,
multiplicity: multiplicity,
duration: duration
})
});
if(ajax_getprice.status !== 200)
return null
else
return ajax_getprice.json();
} catch (error) {
return null;
}
}
const priceget = await get_price();
if(priceget == null) {
product_selector_display.innerHTML = `
ERROR 42500004: Unable to fetch product configuration.
Please try again later.
`;
button_submit.hidden = true;
return;
}
// Determine how to display pricing statement
let pricing_statement = `
Price for this transaction: €${priceget.price}
`;
if(priceget.promotion_applied) {
pricing_statement = `
Price for this transaction:
€${priceget.price_undiscounted}
€${priceget.price}
${priceget.promotion_details.description_customer}
`;
}
//// Update product selector display box with new information
const duration_text = duration === '1year' ? '1 year' : duration === '2year' ? '2 years' : '3 years';
const multiplicity_text = multiplicity === 1 ? '1 user' : `${multiplicity} users`;
product_selector_display.innerHTML = `
${product.name}
${multiplicity_text}, ${duration_text}
${product.blurb}
More info
${pricing_statement}
`;
//// Update submit button text with price
button_submit.hidden = false;
button_submit_text.innerHTML = `Purchase for €${priceget.price}`;
forminput_price_user_displayed.value = priceget.price;
}
//// Initialise product selector display box
document.addEventListener('DOMContentLoaded', function() {
//// Populate form fields with existing or default values
// The default product selection
forminput_product.value = 'rpcESET-HSP';
forminput_multiplicity.value = 1;
forminput_duration.value = '1year';
//// Add event listeners to form fields
forminput_product.addEventListener('change', updateProductDisplay);
forminput_multiplicity.addEventListener('change', updateProductDisplay);
forminput_duration.addEventListener('change', updateProductDisplay);
// Run initial update
updateProductDisplay();
});
////// Custom on-submit JS
async function ecommerce_main_onsubmit() {
try {
const formData = new FormData(document.getElementById('form0'));
const formObject = Object.fromEntries(formData);
const ajax_purchase = await fetch('/post/ecommerce/purchase', {
method: 'POST',
headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": "WsG9BqTYvlC9J6ZzW3C2ZRQtYPRSw6B1k1GX5XGI" },
body: JSON.stringify(formObject)
});
ajax_purchase_response = await ajax_purchase.json();
if(!ajax_purchase_response.success) {
window.location.href = (`/srv/ecommerce/error?title=${encodeURIComponent("ERROR 42500011: Order submission failed")}&message=${encodeURIComponent("There was an error when submitting your order. Please try again later.")}`);
return { success: false };
}
return ajax_purchase_response;
} catch (e) {
window.location.href = (`/srv/ecommerce/error?title=${encodeURIComponent("ERROR 42500007: Order submission failed")}&message=${encodeURIComponent("There was an error when submitting your order. Please try again later.")}`);
}
}
////// Custom Stripe ongetpaymentintent JS
function ecommerce_main_ongetpaymentintent(response) {
document.getElementById('hidden_transaction_reference_number').value = response.transaction_reference_number;
return { success: true };
}