up
This commit is contained in:
@@ -186,6 +186,16 @@ func UpdateBoldCallbackEstado(referencia, estado string) {
|
|||||||
Update("estado", estado)
|
Update("estado", estado)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBoldCallbackLogByID carga un intento de pago por su PK.
|
||||||
|
func GetBoldCallbackLogByID(id uint, out *BoldCallbackLog) error {
|
||||||
|
return app.Http.Database.DB.First(out, id).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateBoldCallbackEstadoByID actualiza el estado de un intento de pago específico por su PK.
|
||||||
|
func UpdateBoldCallbackEstadoByID(id uint, estado string) error {
|
||||||
|
return app.Http.Database.DB.Model(&BoldCallbackLog{}).Where("id = ?", id).Update("estado", estado).Error
|
||||||
|
}
|
||||||
|
|
||||||
// GetBoldCallbackLogsPaginated devuelve los intentos de pago con paginación y filtro.
|
// GetBoldCallbackLogsPaginated devuelve los intentos de pago con paginación y filtro.
|
||||||
func GetBoldCallbackLogsPaginated(page, limit int, estado string) ([]BoldCallbackLog, int64, error) {
|
func GetBoldCallbackLogsPaginated(page, limit int, estado string) ([]BoldCallbackLog, int64, error) {
|
||||||
var logs []BoldCallbackLog
|
var logs []BoldCallbackLog
|
||||||
|
|||||||
@@ -434,6 +434,7 @@
|
|||||||
<td class="py-3 px-4 font-mono text-gray-400" x-text="cb.ip || '—'"></td>
|
<td class="py-3 px-4 font-mono text-gray-400" x-text="cb.ip || '—'"></td>
|
||||||
<td class="py-3 px-4 text-gray-400" x-text="cb.CreatedAt ? new Date(cb.CreatedAt).toLocaleString('es-CO',{dateStyle:'short',timeStyle:'short'}) : '—'"></td>
|
<td class="py-3 px-4 text-gray-400" x-text="cb.CreatedAt ? new Date(cb.CreatedAt).toLocaleString('es-CO',{dateStyle:'short',timeStyle:'short'}) : '—'"></td>
|
||||||
<td class="py-3 px-4">
|
<td class="py-3 px-4">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
<button @click="selectedCallback = cb; showCallbackModal = true" title="Ver parámetros recibidos"
|
<button @click="selectedCallback = cb; showCallbackModal = true" title="Ver parámetros recibidos"
|
||||||
class="text-gray-400 hover:text-[#8eb02f] transition-colors">
|
class="text-gray-400 hover:text-[#8eb02f] transition-colors">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||||
@@ -441,6 +442,17 @@
|
|||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
|
<path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
|
<!-- Validar: solo en intentos pendientes -->
|
||||||
|
<button x-show="cb.estado === 'pendiente'"
|
||||||
|
@click="validarCallback(cb)"
|
||||||
|
:disabled="validandoCallbackID === cb.ID"
|
||||||
|
title="Verificar si el pago realmente se realizó"
|
||||||
|
class="flex items-center gap-1 text-[10px] px-2 py-0.5 rounded border border-indigo-300 text-indigo-600 hover:bg-indigo-50 disabled:opacity-40 transition-colors">
|
||||||
|
<svg x-show="validandoCallbackID !== cb.ID" class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99"/></svg>
|
||||||
|
<svg x-show="validandoCallbackID === cb.ID" class="w-3 h-3 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
|
||||||
|
Validar
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</template>
|
</template>
|
||||||
@@ -1030,6 +1042,7 @@ function pasarelasApp() {
|
|||||||
showCallbackModal: false,
|
showCallbackModal: false,
|
||||||
selectedCallback: null,
|
selectedCallback: null,
|
||||||
validandoLogID: 0,
|
validandoLogID: 0,
|
||||||
|
validandoCallbackID: 0,
|
||||||
|
|
||||||
// ─── dLocal ─────────────────────────────────────────────────────
|
// ─── dLocal ─────────────────────────────────────────────────────
|
||||||
dlocalModo: 'dev',
|
dlocalModo: 'dev',
|
||||||
@@ -1155,6 +1168,24 @@ function pasarelasApp() {
|
|||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async validarCallback(cb) {
|
||||||
|
this.validandoCallbackID = cb.ID;
|
||||||
|
try {
|
||||||
|
const { data } = await axios.post(`/app/pasarelas/bold/callbacks/${cb.ID}/validar`);
|
||||||
|
if (data.ok && data.data) {
|
||||||
|
const idx = this.boldCallbacks.findIndex(c => c.ID === cb.ID);
|
||||||
|
if (idx !== -1) this.boldCallbacks[idx] = data.data;
|
||||||
|
}
|
||||||
|
const msg = data.mensaje || (data.confirmado ? 'Pago confirmado ✓' : 'Pago aún no confirmado');
|
||||||
|
this.showToast(msg, data.confirmado ? 'success' : 'error');
|
||||||
|
if (data.confirmado) this.loadBoldCallbacks();
|
||||||
|
} catch (e) {
|
||||||
|
this.showToast(e.response?.data?.error || 'Error al validar', 'error');
|
||||||
|
} finally {
|
||||||
|
this.validandoCallbackID = 0;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
copyWebhook() {
|
copyWebhook() {
|
||||||
const base = window.location.origin;
|
const base = window.location.origin;
|
||||||
const url = base + '/webhooks/bold';
|
const url = base + '/webhooks/bold';
|
||||||
|
|||||||
@@ -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 ─────────────────────────────────────────────
|
// ─── Validación de log API_CHECK ─────────────────────────────────────────────
|
||||||
|
|
||||||
// ValidarBoldLog verifica el estado de pago real de un log API_CHECK consultando
|
// ValidarBoldLog verifica el estado de pago real de un log API_CHECK consultando
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ func UserRoutes(app fiber.Router) {
|
|||||||
protected.Get("/pasarelas/bold/logs", controllers.BoldWebhookLogs)
|
protected.Get("/pasarelas/bold/logs", controllers.BoldWebhookLogs)
|
||||||
protected.Post("/pasarelas/bold/logs/:id/validar", controllers.ValidarBoldLog)
|
protected.Post("/pasarelas/bold/logs/:id/validar", controllers.ValidarBoldLog)
|
||||||
protected.Get("/pasarelas/bold/callbacks", controllers.BoldCallbackLogs)
|
protected.Get("/pasarelas/bold/callbacks", controllers.BoldCallbackLogs)
|
||||||
|
protected.Post("/pasarelas/bold/callbacks/:id/validar", controllers.ValidarBoldCallback)
|
||||||
// dLocal
|
// dLocal
|
||||||
protected.Get("/pasarelas/dlocal/config", controllers.GetDlocalConfigAPI)
|
protected.Get("/pasarelas/dlocal/config", controllers.GetDlocalConfigAPI)
|
||||||
protected.Post("/pasarelas/dlocal/save", controllers.SaveDlocalConfigWeb)
|
protected.Post("/pasarelas/dlocal/save", controllers.SaveDlocalConfigWeb)
|
||||||
|
|||||||
Reference in New Issue
Block a user