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 ===== */
const GENERIC_CHECKOUT = 'https://buy.stripe.com/test_generic';
document.getElementById('checkoutBtn').addEventListener('click', async () => {
const entries = Object.entries(state.cart);
if (!entries.length) return alert('Your cart is empty.');
const API = "https://affiliated-lets-automatic-oak.trycloudflare.com";
const items = entries.map(([id, qty]) => {
const p = products.find(p => p.id === id);
return { id, name: p?.name || id, qty, price: p?.price || 0 };
async function checkout(cart) {
// 1) reserve
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
const reserve = await fetch(`${API}/api/reserve`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
credentials: 'include', body: JSON.stringify({ items })
// 2) create checkout
const r2 = await fetch(`${API}/api/create-checkout`, {
method: 'POST',
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 rem = typeof reserve?.remaining === 'number' ? reserve.remaining : remaining;
alert(`Sold out. Remaining: ${rem}. Adjust your cart.`);
await refreshRemaining(); // sync the UI
return;
});
const j2 = await r2.json();
if (!j2.url) { alert(j2.error || 'create-checkout failed'); return; }
location.href = j2.url; // redirect to Stripe
}
// 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);
});
}
// client.js (in your existing <script>)
// client.js (in your existing
<script>
)
let remaining = 12;
async function refreshRemaining() {
try {