up
This commit is contained in:
@@ -373,3 +373,57 @@ func calcularFechaVencimiento(desde time.Time, periodicidad string) time.Time {
|
||||
return desde.AddDate(1, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// VerificarPagoBold consulta la API de Bold para un contrato específico y confirma el pago si está pagado.
|
||||
// POST /api/contratos/:id/verificar-pago
|
||||
func VerificarPagoBold(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
||||
}
|
||||
|
||||
contrato, err := models.GetContratoParaVerificacion(uint(id))
|
||||
if err != nil {
|
||||
return c.Status(404).JSON(fiber.Map{"error": "Contrato no encontrado"})
|
||||
}
|
||||
if contrato.PagoConfirmado {
|
||||
return c.JSON(fiber.Map{"ok": true, "confirmado": true, "mensaje": "El pago ya estaba confirmado"})
|
||||
}
|
||||
if contrato.EnlacePagoLinkID == "" {
|
||||
return c.JSON(fiber.Map{"ok": false, "confirmado": false, "mensaje": "Este contrato no tiene enlace de pago generado"})
|
||||
}
|
||||
|
||||
boldCfg, err := models.GetBoldConfig()
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Sin configuración Bold activa"})
|
||||
}
|
||||
|
||||
paid, paymentID, monto, err := services.CheckBoldLinkPaid(boldCfg, contrato.EnlacePagoLinkID)
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Error consultando Bold: " + err.Error()})
|
||||
}
|
||||
if !paid {
|
||||
return c.JSON(fiber.Map{"ok": true, "confirmado": false, "mensaje": "El enlace aún no ha sido pagado"})
|
||||
}
|
||||
|
||||
ref := "contrato-" + strconv.FormatUint(id, 10)
|
||||
if ok, _ := models.MarcarContratoPagado(uint(id)); ok {
|
||||
go services.EnviarCorreoConfirmacionPago(uint(id), "bold")
|
||||
}
|
||||
go models.EnrichBoldCallbackLog(ref, contrato.Cliente.Email, monto)
|
||||
|
||||
notifID := "api-check-" + paymentID
|
||||
if paymentID != "" && !models.IsBoldNotificationDuplicate(notifID) {
|
||||
_ = models.SaveBoldWebhookLog(models.BoldWebhookLog{
|
||||
NotificationID: notifID,
|
||||
Tipo: "API_CHECK",
|
||||
PaymentID: paymentID,
|
||||
Referencia: ref,
|
||||
PayerEmail: contrato.Cliente.Email,
|
||||
Monto: monto,
|
||||
Procesado: true,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{"ok": true, "confirmado": true, "monto": monto, "mensaje": "¡Pago confirmado!"})
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ func RenovacionesRoutes(protected fiber.Router) {
|
||||
protected.Post("/api/contratos/:id/renovar", controllers.RenovarContrato)
|
||||
protected.Post("/api/contratos/:id/enviar-correo", controllers.EnviarCorreoContrato)
|
||||
protected.Get("/api/contratos/:id/historial", controllers.GetHistorialContrato)
|
||||
protected.Post("/api/contratos/:id/verificar-pago", controllers.VerificarPagoBold)
|
||||
|
||||
// ─── Plantillas de correo ─────────────────────────────────────────
|
||||
protected.Get("/plantillas-correo", middlewares.MenuMiddleware, controllers.PlantillasView)
|
||||
|
||||
Reference in New Issue
Block a user