var apiKey = "hmngy-60lXd3pZRePVdSpItDhnC3408GvLDHU4"; function stripeTokenHandler(token) { let form = document.getElementById("payment-form"); let hiddenInput = document.createElement("input"); hiddenInput.setAttribute("type", "hidden"); hiddenInput.setAttribute("name", "stripeToken"); hiddenInput.setAttribute("value", token.id); form.appendChild(hiddenInput); } document.addEventListener("DOMContentLoaded", () => { fetch("http://127.0.0.1:8000/api/obtLavePublicaStripe", { method: "POST", headers: { "Content-Type": "application/json", "Accept": "application/json", }, body: JSON.stringify({ apiKey: apiKey }), }) .then((response) => response.json()) .then((data) => { stripeLlave = data[0].clave_publica; if (data[0].clave_publica == undefined || data[0].clave_publica == []) { showToast("ApiKey no encontrado", "error"); } const stripe = Stripe(stripeLlave); let elements = stripe.elements(); let style = { base: { color: "#32325d", lineHeight: "24px", fontFamily: '"Helvetica Neue", Helvetica, sans-serif', fontSmoothing: "antialiased", fontSize: "16px", "::placeholder": { color: "#aab7c4", }, }, invalid: { color: "#fa755a", iconColor: "#fa755a", }, }; let card = elements.create("card", { style: style }); card.mount("#card-element"); card.addEventListener("change", function (event) { let displayError = document.getElementById("card-errors"); displayError.textContent = event.error ? event.error.message : ""; }); let form = document.getElementById("payment-form"); form.addEventListener("submit", function (event) { event.preventDefault(); let cantidad = document.getElementById("amountUser").value; let currency = document.getElementById("currency").value; let emailUser = document.getElementById("emailUser").value; let descripcion = document.getElementById("description").value; let nameUsuario = document.getElementById("nameUser").value; console.log("PASO 1"); stripe.createToken(card).then(function (result) { if (result.error) { document.getElementById("card-errors").textContent = result.error.message; document.getElementById("btnstripe").removeAttribute("disabled"); } else { stripeTokenHandler(result.token); let datos = []; const formElements = form.querySelectorAll("input, select, textarea"); formElements.forEach((el) => { if (el.name && !el.disabled) { datos.push({ name: el.name, value: el.value }); } }); datos.push({ name: "amount", value: cantidad }); datos.push({ name: "name", value: nameUsuario }); datos.push({ name: "currency", value: currency }); datos.push({ name: "email", value: emailUser }); datos.push({ name: "descripcion", value: descripcion }); datos.push({ name: "urlbase", value: window.location.origin }); datos.push({ name: "apiKey", value: apiKey }); console.log("DATOS ENVIADOS:", datos); const formBody = datos .map((pair) => encodeURIComponent(pair.name) + "=" + encodeURIComponent(pair.value) ) .join("&"); document.getElementById("btnstripe").style.display = "none"; document.getElementById("mensajePago").innerHTML = ` Espere un momento. Estamos procesando su pago. `; fetch("http://127.0.0.1:8000/api/createPayment", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: formBody, }) .then((response) => response.json()) .then((response) => { console.log(response.success); if (response.success) { document.getElementById("mensajePago").innerHTML = ` ✅ Pago realizado con éxito. ID Transacción: ${response.idTransaction} `; } else { document.getElementById("mensajePago").innerHTML = ` ❌ Hubo un error: ${response.error} `; document.getElementById("btnstripe").style.display = "inline-block"; } }) .catch((error) => { console.log(error); document.getElementById("mensajePago").innerHTML = ` ❌ Error de conexión o del servidor. Intente nuevamente. `; document.getElementById("btnstripe").style.display = "inline-block"; }); } }); }); }) .catch((error) => { showToast("Error: ApiKey no encontrado", "error"); }); }); function abrirModal() { const modal = document.getElementById("miModal"); const contenido = document.getElementById("modalContenido"); modal.style.display = "flex"; contenido.classList.remove("animar-salida"); setTimeout(() => contenido.classList.add("animar-entrada"), 10); } function cerrarModal() { const modal = document.getElementById("miModal"); const contenido = document.getElementById("modalContenido"); contenido.classList.remove("animar-entrada"); contenido.classList.add("animar-salida"); setTimeout(() => (modal.style.display = "none"), 300); } document.addEventListener("DOMContentLoaded", function () { const modalHTML = ` `; document.body.insertAdjacentHTML("beforeend", modalHTML); });