Skip to main content

max / makenotwork

1.3 KB · 41 lines History Blame Raw
1 (function() {
2 var cfg = document.getElementById('page-buy-cfg');
3 if (!cfg) return;
4 var hostUrl = cfg.dataset.hostUrl;
5 var itemId = cfg.dataset.itemId;
6 var pwywEnabled = cfg.dataset.pwywEnabled === 'true';
7
8 window.buy = function() {
9 const btn = document.getElementById('buy-btn');
10 btn.disabled = true;
11 btn.textContent = 'Redirecting...';
12
13 const body = {};
14 if (pwywEnabled) {
15 const amount = parseFloat(document.getElementById('pwyw_amount').value);
16 body.amount_cents = Math.round(amount * 100);
17 }
18
19 fetch(hostUrl + '/api/checkout/guest/' + itemId, {
20 method: 'POST',
21 headers: { 'Content-Type': 'application/json' },
22 body: JSON.stringify(body),
23 })
24 .then(r => r.json())
25 .then(data => {
26 if (data.checkout_url) {
27 window.location.href = data.checkout_url;
28 } else {
29 btn.disabled = false;
30 btn.textContent = 'Buy Now';
31 alert(data.error || 'Something went wrong');
32 }
33 })
34 .catch(() => {
35 btn.disabled = false;
36 btn.textContent = 'Buy Now';
37 alert('Something went wrong. Please try again.');
38 });
39 };
40 })();
41