Meta Checkout

<!DOCTYPE html><html lang="es"><head>  <meta charset="utf-8">  <title>Meta Checkout Bridge</title>  <meta name="robots" content="noindex,nofollow">  <style>    body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Arial,sans-serif;padding:24px}  </style></head><body>  <h1>Preparando tu carrito…</h1>  <p>No cierres esta ventana.</p>
  <script>  (function(){    // Lee parámetros de la URL    const qs = new URLSearchParams(window.location.search);
    // products=12345:2,67890:1 (Meta codifica , como %2C y : como %3A; URLSearchParams ya lo decodifica)    const raw = (qs.get('products') || '').trim();
    // Opcional: cupón ?coupon=MI-CUPON    const coupon = (qs.get('coupon') || '').trim();
    // Admite separadores coma o punto y coma    const sep = raw.includes(';') && !raw.includes(',') ? ';' : ',';
    const items = raw.split(sep).map(s => s.trim()).filter(Boolean).map(pair => {      const parts = pair.split(':');      const id = (parts[0] || '').trim();      const qty = Math.max(parseInt((parts[1] || '1'), 10) || 1, 1);      return { id, qty };    }).filter(x => x.id);
    // Helper: POST add-to-cart en Jumpseller    // La mayoría de temas aceptan /cart/add?variant_id=XXX&quantity=YYY (POST)    function addItem(variantId, quantity){      const url = `/cart/add?variant_id=${encodeURIComponent(variantId)}&quantity=${encodeURIComponent(quantity)}`;      return fetch(url, {        method: 'POST',        credentials: 'include',        headers: { 'X-Requested-With': 'XMLHttpRequest' }      });    }
    // Opcional: aplicar cupón (si tu tema expone este endpoint)    function applyCoupon(code){      if(!code) return Promise.resolve();      // En muchos temas funciona /cart/coupons (POST) o /checkout/discount      // Si tu tema no tiene endpoint, puedes omitir esta parte sin problemas.      return fetch('/cart/coupons', {        method: 'POST',        credentials: 'include',        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },        body: 'coupon_code=' + encodeURIComponent(code)      }).catch(() => Promise.resolve()); // que no bloquee el flujo    }
    // Flujo    (async function run(){      try{        if(items.length === 0){          // Si Meta no envió productos, manda al checkout normal          window.location.href = '/checkout';          return;        }
        // Agrega cada ítem en serie (evita condiciones de carrera)        for(const it of items){          try { await addItem(it.id, it.qty); }          catch(e){ /* continúa con el siguiente */ }        }
        // Aplica cupón si viene        await applyCoupon(coupon);
        // Redirige al checkout        window.location.replace('/checkout');      }catch(e){        // Fallback: ve al carrito para que al menos se vea el resultado parcial        window.location.href = '/cart';      }    })();  })();  </script></body></html>