(function() { var cfg = document.getElementById('page-purchase-cfg'); if (!cfg) return; var itemId = cfg.dataset.itemId; var pwywEnabled = cfg.dataset.pwywEnabled === 'true'; window.guestCheckout = function() { const btn = document.getElementById('guest-checkout-btn'); btn.disabled = true; btn.textContent = 'Redirecting...'; const body = {}; if (pwywEnabled) { const amount = parseFloat(document.getElementById('guest_pwyw_amount').value); body.amount_cents = Math.round(amount * 100); } fetch('/api/checkout/guest/' + itemId, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) .then(r => r.json()) .then(data => { if (data.checkout_url) { window.location.href = data.checkout_url; } else { btn.disabled = false; btn.textContent = 'Buy Now'; alert(data.error || 'Something went wrong'); } }) .catch(() => { btn.disabled = false; btn.textContent = 'Buy Now'; alert('Something went wrong. Please try again.'); }); }; })();