up
This commit is contained in:
@@ -315,6 +315,104 @@ func ValidarDlocalLog(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Validación de intento de pago (callback) ───────────────────────────────
|
||||
|
||||
// ValidarBoldCallback verifica si el pago de un intento pendiente realmente se realizó.
|
||||
// Recorre las mismas fuentes que verificarPago() y, si confirma, marca el contrato + actualiza el callback.
|
||||
// POST /app/pasarelas/bold/callbacks/:id/validar
|
||||
func ValidarBoldCallback(c *fiber.Ctx) error {
|
||||
cbID, err := c.ParamsInt("id")
|
||||
if err != nil || cbID <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "ID inválido"})
|
||||
}
|
||||
|
||||
var cb models.BoldCallbackLog
|
||||
if err := models.GetBoldCallbackLogByID(uint(cbID), &cb); err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Intento no encontrado"})
|
||||
}
|
||||
|
||||
var contratoID uint
|
||||
if _, err := fmt.Sscanf(cb.Referencia, "contrato-%d", &contratoID); err != nil || contratoID == 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Referencia no tiene formato contrato-{id}"})
|
||||
}
|
||||
|
||||
contrato, err := models.GetContratoParaVerificacion(contratoID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Contrato no encontrado"})
|
||||
}
|
||||
|
||||
pagoConfirmado := contrato.PagoConfirmado
|
||||
fuente := ""
|
||||
|
||||
// ─── 1. Ya confirmado en DB ───────────────────────────────────────────────
|
||||
if pagoConfirmado {
|
||||
fuente = "db"
|
||||
}
|
||||
|
||||
// ─── 2. dlocal_payment_log ───────────────────────────────────────────────
|
||||
if !pagoConfirmado {
|
||||
if dlLogs, dlErr := models.GetDlocalPaymentLogsByRef(cb.Referencia); dlErr == nil {
|
||||
for _, l := range dlLogs {
|
||||
if l.Estado == "PAID" || l.Estado == "AUTHORIZED" {
|
||||
pagoConfirmado = true
|
||||
fuente = "dlocal_log"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 3. Bold API ─────────────────────────────────────────────────────────
|
||||
if !pagoConfirmado && contrato.EnlacePagoLinkID != "" {
|
||||
if boldCfg, boldErr := models.GetBoldConfig(); boldErr == nil {
|
||||
if paid, _, _, apiErr := services.CheckBoldLinkPaid(boldCfg, contrato.EnlacePagoLinkID); apiErr == nil && paid {
|
||||
pagoConfirmado = true
|
||||
fuente = "bold_api"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 4. dLocal API ───────────────────────────────────────────────────────
|
||||
if !pagoConfirmado {
|
||||
if dlocalCfg, dlErr := models.GetLastActiveDlocalApi(); dlErr == nil {
|
||||
if paid, _, _, _, apiErr := services.CheckDlocalPaymentByOrderID(*dlocalCfg, cb.Referencia); apiErr == nil && paid {
|
||||
pagoConfirmado = true
|
||||
fuente = "dlocal_api"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualizar contrato si se confirmó ahora
|
||||
if pagoConfirmado && !contrato.PagoConfirmado {
|
||||
_ = models.MarcarContratoPagado(contratoID)
|
||||
go services.EnviarCorreoConfirmacionPago(contratoID)
|
||||
}
|
||||
|
||||
// Actualizar estado del callback
|
||||
nuevoEstado := cb.Estado
|
||||
if pagoConfirmado && cb.Estado == "pendiente" {
|
||||
nuevoEstado = "pagado"
|
||||
_ = models.UpdateBoldCallbackEstadoByID(uint(cbID), "pagado")
|
||||
cb.Estado = "pagado"
|
||||
}
|
||||
|
||||
msg := "Pago aún no confirmado"
|
||||
if pagoConfirmado && !contrato.PagoConfirmado {
|
||||
msg = fmt.Sprintf("Pago confirmado via %s — contrato marcado como pagado", fuente)
|
||||
} else if pagoConfirmado {
|
||||
msg = fmt.Sprintf("Pago ya confirmado (%s)", fuente)
|
||||
}
|
||||
_ = nuevoEstado
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"ok": true,
|
||||
"confirmado": pagoConfirmado,
|
||||
"fuente": fuente,
|
||||
"mensaje": msg,
|
||||
"data": cb,
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Validación de log API_CHECK ─────────────────────────────────────────────
|
||||
|
||||
// ValidarBoldLog verifica el estado de pago real de un log API_CHECK consultando
|
||||
|
||||
Reference in New Issue
Block a user