const API_BASE = "https://qp2fhnpbhq4a5uvtp7bpe6ocfa0xzdgv.lambda-url.eu-west-2.on.aws"; const SITE_URL = "https://d4l9v4vlr9mz6.cloudfront.net"; const landingStatus = document.querySelector("#checkout-status"); const successStatus = document.querySelector("#success-status"); const licenseCard = document.querySelector("#license-card"); const licenseOutput = document.querySelector("#license-output"); const copyLicenseButton = document.querySelector("#copy-license-button"); const postJson = async (path, body) => { const response = await fetch(`${API_BASE}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); if (!response.ok) { throw new Error((await response.text()) || "Request failed."); } return response.json(); }; const startCheckout = async () => { if (!API_BASE.startsWith("http")) { throw new Error("Checkout API is not configured yet."); } if (landingStatus) { landingStatus.textContent = "Creating Stripe checkout session..."; } const payload = await postJson("/checkout/session", { origin: window.location.origin || SITE_URL || "unknown" }); window.location.href = payload.checkoutUrl; }; const loadSuccess = async () => { const params = new URLSearchParams(window.location.search); const sessionId = params.get("session_id"); if (!sessionId) { if (successStatus) { successStatus.textContent = "Stripe session_id is missing from this page URL."; } return; } if (successStatus) { successStatus.textContent = "Retrieving license key..."; } try { const payload = await postJson("/checkout/session-license", { sessionId }); if (licenseOutput) { licenseOutput.value = payload.licenseKey; } if (licenseCard) { licenseCard.classList.remove("hidden"); } if (successStatus) { successStatus.textContent = `Premium ready for ${payload.customerEmail}`; } } catch (error) { if (successStatus) { successStatus.textContent = error instanceof Error ? error.message : String(error); } } }; document.querySelectorAll("#buy-now-button, #buy-now-pricing-button").forEach((button) => { button.addEventListener("click", async () => { try { await startCheckout(); } catch (error) { if (landingStatus) { landingStatus.textContent = error instanceof Error ? error.message : String(error); } } }); }); copyLicenseButton?.addEventListener("click", async () => { if (!licenseOutput) { return; } await navigator.clipboard.writeText(licenseOutput.value); copyLicenseButton.textContent = "Copied"; window.setTimeout(() => { copyLicenseButton.textContent = "Copy license key"; }, 1200); }); if (document.body.dataset.page === "success") { loadSuccess(); }