diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index a77bb0c..f06e787 100755 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -149,6 +149,7 @@ class TelegramBotService 'profile' => $this->showProfile($chatId), 'agent' => $this->showMainMenu($chatId), 'pay_apply' => $this->applyPayment($chatId, (int) ($parts[1] ?? 0), $parts[2] ?? null), + 'pay_retry' => $this->retryPayment($chatId), default => $this->showMainMenu($chatId), }; } @@ -230,18 +231,30 @@ class TelegramBotService [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], ]; } else { - $motivo = match ($resultado['motivo'] ?? '') { - 'correo_deshabilitado' => '📧 Verificación por correo no configurada', - 'error_imap' => '📧 Error al leer correos IMAP', - 'sin_correos' => '📧 Sin correos recientes en bandeja', - 'sin_coincidencia' => '📧 Ningún correo coincide con este monto/fecha', - 'error_sistema' => '⚙️ Error interno, intenta de nuevo en unos minutos', - default => '📧 No se pudo verificar automáticamente', - }; - $texto .= "\nEstado: *⚠️ No confirmado*\n_{$motivo}_"; - $buttons = [ - [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], - ]; + $motivo = $resultado['motivo'] ?? ''; + if ($motivo === 'sin_coincidencia' || $motivo === 'sin_correos') { + // Guardar datosPago en estado para poder reintentar sin reenviar foto + $state['data']['pago_pendiente'] = $datosPago; + $this->setState($chatId, $state); + + $texto .= "\nEstado: *⏳ Por ahora no encontramos tu pago de confirmación*\n" + . "_Puede demorar entre 1 y 2 minutos en llegar. Espera un momento y presiona Reintentar._"; + $buttons = [ + [['text' => '🔄 Reintentar', 'callback_data' => 'pay_retry']], + [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], + ]; + } else { + $motivoTexto = match ($motivo) { + 'correo_deshabilitado' => '📧 Verificación por correo no configurada', + 'error_imap' => '📧 Error al leer correos IMAP', + 'error_sistema' => '⚙️ Error interno, intenta de nuevo en unos minutos', + default => '📧 No se pudo verificar automáticamente', + }; + $texto .= "\nEstado: *⚠️ No confirmado*\n_{$motivoTexto}_"; + $buttons = [ + [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], + ]; + } } $this->sendWithKeyboard($chatId, $texto, $buttons); @@ -251,6 +264,56 @@ class TelegramBotService } } + private function retryPayment(string $chatId): void + { + $state = $this->getState($chatId); + $this->convId = $state['conv_id'] ?? null; + $datosPago = $state['data']['pago_pendiente'] ?? null; + + if (! $datosPago) { + $this->send($chatId, "No hay ningún comprobante pendiente. Envía la foto de tu comprobante."); + return; + } + + $this->send($chatId, "⏳ Verificando de nuevo..."); + + $usuarioId = $state['user_id'] ?? null; + $nombreUsuario = $usuarioId ? (User::find($usuarioId)?->name ?? '') : ''; + + $resultado = app(PagoValidadorService::class)->validar($datosPago, $usuarioId, $nombreUsuario, 'telegram'); + $monto = number_format($datosPago['valor'] ?? 0); + $ref = $datosPago['referencia'] ?? 'N/A'; + $valor = (int) ($datosPago['valor'] ?? 0); + + if ($resultado['estado'] === 'confirmado') { + $buttons = [ + [['text' => "✅ Aplicar recarga de \$$monto", 'callback_data' => 'pay_apply|' . $valor . '|' . $ref]], + [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], + ]; + // Limpiar pago pendiente del estado + unset($state['data']['pago_pendiente']); + $this->setState($chatId, $state); + $this->sendWithKeyboard($chatId, "✅ *¡Pago confirmado!* Tu recarga de \$$monto está lista.", $buttons); + + } elseif ($resultado['estado'] === 'ya_usado') { + unset($state['data']['pago_pendiente']); + $this->setState($chatId, $state); + $this->sendWithKeyboard($chatId, "⛔ *Comprobante ya utilizado.*\nEste pago ya fue registrado anteriormente.", [ + [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], + ]); + + } else { + // Sigue sin encontrarse — dejar pendiente para otro reintento + $this->sendWithKeyboard($chatId, + "⏳ *Aún no encontramos tu pago.*\nPuede demorar entre 1 y 2 minutos en llegar. Espera un momento y presiona Reintentar.", + [ + [['text' => '🔄 Reintentar', 'callback_data' => 'pay_retry']], + [['text' => '🔙 Menú principal', 'callback_data' => 'menu']], + ] + ); + } + } + public function handleVoice(string $chatId, string $fileId, string $nombre = '', int $duracionSegundos = 0): void { $state = $this->getState($chatId);