
/* Custom CSS theme */
<script src="https://js.stripe.com/v3/"></script>
<script>
(function waitForStripeButton() {
  const btn = document.getElementById("stripe-checkout-btn");

  if (!btn || !window.Ecwid || !Ecwid.getOrder) {
    setTimeout(waitForStripeButton, 500);
    return;
  }

  const stripe = Stripe("pk_live_51ReypOGBUtISZFN562Vy1xro5sU3SGKfbMssk1KuU3Jds0a25F5QzTggv9FeVxVLXPxUEpBqVcPfSBEH2hWnjTo4005rOt8YIn"); // your PUBLIC key

  btn.addEventListener("click", function () {
    btn.disabled = true;
    const originalText = btn.innerText;
    btn.innerText = "Redirecting to secure checkout…";

    Ecwid.getOrder(function (order) {
      if (!order || order.total <= 0) {
        alert("Your cart is empty.");
        btn.disabled = false;
        btn.innerText = originalText;
        return;
      }

      fetch("https://guyshe.com/stripe/create-checkout-session.php", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          amount: Math.round(order.total * 100),
          currency: order.currency,
          orderId: order.orderNumber
        })
      })
      .then(res => res.json())
      .then(data => {
        if (!data.id) throw new Error("Invalid Stripe session");
        return stripe.redirectToCheckout({ sessionId: data.id });
      })
      .catch(err => {
        console.error(err);
        alert("Payment failed. Please try again.");
        btn.disabled = false;
        btn.innerText = originalText;
      });
    });
  });

} )();
</script>

 