Add files via upload

This commit is contained in:
James 2025-10-04 18:18:34 -07:00 committed by GitHub
parent f634293837
commit eb4e4b47f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 266 additions and 271 deletions

View File

@ -665,41 +665,34 @@
}); });
/* ===== Checkout ===== */ /* ===== Checkout ===== */
const GENERIC_CHECKOUT = 'https://buy.stripe.com/test_generic'; const API = "https://affiliated-lets-automatic-oak.trycloudflare.com";
document.getElementById('checkoutBtn').addEventListener('click', async () => {
const entries = Object.entries(state.cart);
if (!entries.length) return alert('Your cart is empty.');
const items = entries.map(([id, qty]) => { async function checkout(cart) {
const p = products.find(p => p.id === id); // 1) reserve
return { id, name: p?.name || id, qty, price: p?.price || 0 }; const r1 = await fetch(`${API}/api/reserve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: cart })
}); });
const j1 = await r1.json();
if (!j1.ok) { alert(j1.error || 'reserve failed'); return; }
// 1) Try to reserve on the server // 2) create checkout
const reserve = await fetch(`${API}/api/reserve`, { const r2 = await fetch(`${API}/api/create-checkout`, {
method: 'POST', headers: { 'Content-Type': 'application/json' }, method: 'POST',
credentials: 'include', body: JSON.stringify({ items }) headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
resKey: j1.resKey,
items: [],
success_url: location.origin + location.pathname + '?ok=1',
cancel_url: location.origin + location.pathname + '?cancel=1'
}) })
});
if (!reserve?.ok) { const j2 = await r2.json();
const rem = typeof reserve?.remaining === 'number' ? reserve.remaining : remaining; if (!j2.url) { alert(j2.error || 'create-checkout failed'); return; }
alert(`Sold out. Remaining: ${rem}. Adjust your cart.`); location.href = j2.url; // redirect to Stripe
await refreshRemaining(); // sync the UI
return;
} }
// 2) Create a Stripe Checkout session
const session = await fetch(`${API}/api/create-checkout`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
credentials: 'include', body: JSON.stringify({ items })
})
if (!session?.url) {
alert('Could not start checkout. Try again.'); return;
}
window.location.href = session.url;
});
@ -723,7 +716,9 @@
ctx.fillText(products.find(p => p.id === id)?.name || id, 12, cv.height - 12); ctx.fillText(products.find(p => p.id === id)?.name || id, 12, cv.height - 12);
}); });
} }
// client.js (in your existing <script>) // client.js (in your existing
<script>
)
let remaining = 12; let remaining = 12;
async function refreshRemaining() { async function refreshRemaining() {
try { try {