diff --git a/resources/views/pago_exitoso.html b/resources/views/pago_exitoso.html index 3494d48..c51e944 100644 --- a/resources/views/pago_exitoso.html +++ b/resources/views/pago_exitoso.html @@ -91,7 +91,7 @@ function pagoApp() { async verificar() { this.intentos++; try { - const r = await fetch(`/api/pago-estado?ref=${encodeURIComponent(this.ref)}`); + const r = await fetch(`/pago/estado?ref=${encodeURIComponent(this.ref)}`); const data = await r.json(); if (data.estado === 'confirmado' || data.confirmado) { diff --git a/rest/controllers/api/pago_controller.go b/rest/controllers/api/pago_controller.go index 85224a6..cee6180 100644 --- a/rest/controllers/api/pago_controller.go +++ b/rest/controllers/api/pago_controller.go @@ -14,11 +14,16 @@ import ( func PagoExitosoPage(c *fiber.Ctx) error { // Recolectar todos los parámetros posibles que Bold puede enviar params := map[string]string{} + // Parámetros clave que Bold envía en la callback_url for _, k := range []string{"bold-order-id", "order_id", "payment_link", "reference", "id", "ref"} { if v := c.Query(k, ""); v != "" { params[k] = v } } + // bold-tx-status: Bold envía el resultado directamente en la URL + if v := c.Query("bold-tx-status", ""); v != "" { + params["bold-tx-status"] = v + } c.Request().URI().QueryArgs().VisitAll(func(k, v []byte) { params[string(k)] = string(v) }) @@ -43,6 +48,19 @@ func PagoExitosoPage(c *fiber.Ctx) error { } paramsJSON += "}" + // Si Bold ya envía el resultado en la URL (bold-tx-status), usarlo directamente + // antes de hacer cualquier llamada a la API externa. + boldTxStatus := params["bold-tx-status"] + if boldTxStatus == "rejected" || boldTxStatus == "cancelled" || boldTxStatus == "failed" { + _ = models.SaveBoldCallbackLog(models.BoldCallbackLog{ + Referencia: ref, PaymentLink: paymentLink, Params: paramsJSON, + Estado: "fallido", IP: c.IP(), UserAgent: string(c.Request().Header.UserAgent()), + }) + return c.Render("pago_exitoso", fiber.Map{ + "Ref": ref, "Estado": "rechazado", "FechaPago": "", + }, "layouts/landing") + } + // Registrar el intento de pago en la tabla de callbacks if ref != "" || paymentLink != "" { entry := models.BoldCallbackLog{ diff --git a/rest/routes/publicas.go b/rest/routes/publicas.go index 8926515..fc60cc4 100755 --- a/rest/routes/publicas.go +++ b/rest/routes/publicas.go @@ -23,6 +23,7 @@ func RutasPublicas(web fiber.Router) { // ─── Página de confirmación de pago ─────────────────────────────────── web.Get("/pago-exitoso", apiControllers.PagoExitosoPage) - web.Get("/api/pago-estado", apiControllers.PagoEstadoAPI) + // Ruta fuera del prefijo /api para evitar que AuthApi() la intercepte + web.Get("/pago/estado", apiControllers.PagoEstadoAPI) }