diff --git a/.env.example b/.env.example index 9892b4f..d005917 100644 --- a/.env.example +++ b/.env.example @@ -89,8 +89,6 @@ VITE_REVERB_PORT="${REVERB_PORT}" VITE_REVERB_SCHEME="${REVERB_SCHEME}" # WhatsApp API Configuracion -WHATSAPP_LOGIN_URL=https://whatsapp.golsystems.mx/api/login WHATSAPP_API_URL=https://whatsapp.golsystems.mx/api/send-whatsapp WHATSAPP_ORG_ID=1 -WHATSAPP_AUTH_EMAIL= -WHATSAPP_AUTH_PASSWORD= +WHATSAPP_TOKEN= diff --git a/app/Services/WhatsAppAuthService.php b/app/Services/WhatsAppAuthService.php deleted file mode 100644 index 157eafa..0000000 --- a/app/Services/WhatsAppAuthService.php +++ /dev/null @@ -1,94 +0,0 @@ -loginUrl = config('services.whatsapp.login_url', 'https://whatsapp.golsystems.mx/api/login'); - $this->email = config('services.whatsapp.auth_email', 'juan.zapata@golsystems.com.mx'); - $this->password = config('services.whatsapp.auth_password'); - } - - /** - * Obtener token de acceso - */ - public function getAccessToken(): string - { - if (!$this->email || !$this->password) { - throw new \Exception('Las credenciales de WhatsApp no están configuradas en .env'); - } - - return Cache::remember('whatsapp_access_token', 3000, function () { - try { - $response = Http::timeout(30) - ->withHeaders([ - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ]) - ->post($this->loginUrl, [ - 'email' => $this->email, - 'password' => $this->password, - ]); - - if (!$response->successful()) { - Log::channel('daily')->error('WhatsApp Auth Error', [ - 'status' => $response->status(), - 'body' => $response->body(), - ]); - - throw new \Exception('Error al autenticar con WhatsApp API: ' . $response->body()); - } - - $data = $response->json(); - - // La API puede retornar el token en diferentes formatos - // Ajusta según la respuesta real de la API - $token = $data['token'] - ?? $data['access_token'] - ?? $data['data']['token'] - ?? null; - - if (!$token) { - Log::channel('daily')->error('WhatsApp Auth Error: No token in response', [ - 'response' => $data - ]); - - throw new \Exception('La API de WhatsApp no retornó un token válido'); - } - - Log::channel('daily')->info('WhatsApp token obtenido exitosamente'); - - return $token; - - } catch (\Exception $e) { - Log::channel('daily')->error('WhatsApp Auth Exception', [ - 'error' => $e->getMessage(), - ]); - - throw $e; - } - }); - } - - /** - * Forzar renovación del token - */ - public function refreshToken(): string - { - Cache::forget('whatsapp_access_token'); - return $this->getAccessToken(); - } -} diff --git a/app/Services/WhatsappService.php b/app/Services/WhatsappService.php index a836041..c700dd8 100644 --- a/app/Services/WhatsappService.php +++ b/app/Services/WhatsappService.php @@ -12,13 +12,17 @@ class WhatsAppService { protected string $apiUrl; protected int $orgId; - protected WhatsAppAuthService $authService; + protected string $token; - public function __construct(WhatsAppAuthService $authService) + public function __construct() { $this->apiUrl = config('services.whatsapp.api_url', 'https://whatsapp.golsystems.mx/api/send-whatsapp'); $this->orgId = config('services.whatsapp.org_id', 1); - $this->authService = $authService; + $this->token = config('services.whatsapp.token'); + + if (!$this->token) { + throw new \Exception('El token de WhatsApp no está configurado. Agrega WHATSAPP_TOKEN en tu archivo .env'); + } } /** @@ -34,9 +38,6 @@ public function sendDocument( string $customerName ): array { try { - // Obtener token de autenticación - $token = $this->authService->getAccessToken(); - // Construir el mensaje de WhatsApp $whatsappMessage = json_encode([ 'messaging_product' => 'whatsapp', @@ -64,25 +65,10 @@ public function sendDocument( ->withHeaders([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . $token, + 'Authorization' => 'Bearer ' . $this->token, ]) ->post($this->apiUrl, $payload); - // Si el token expiró (401), renovar e intentar de nuevo - if ($response->status() === 401) { - Log::channel('daily')->warning('WhatsApp token expired, refreshing...'); - - $token = $this->authService->refreshToken(); - - $response = Http::timeout(30) - ->withHeaders([ - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . $token, - ]) - ->post($this->apiUrl, $payload); - } - // Registrar en logs Log::channel('daily')->info('WhatsApp message sent', [ 'phone' => $phoneNumber, diff --git a/config/services.php b/config/services.php index f8161be..f633127 100644 --- a/config/services.php +++ b/config/services.php @@ -36,11 +36,9 @@ ], 'whatsapp' => [ - 'login_url' => env('WHATSAPP_LOGIN_URL', 'https://whatsapp.golsystems.mx/api/login'), 'api_url' => env('WHATSAPP_API_URL', 'https://whatsapp.golsystems.mx/api/send-whatsapp'), 'org_id' => env('WHATSAPP_ORG_ID', 1), - 'auth_email' => env('WHATSAPP_AUTH_EMAIL', 'juan.zapata@golsystems.com.mx'), - 'auth_password' => env('WHATSAPP_AUTH_PASSWORD'), + 'token' => env('WHATSAPP_TOKEN'), ], ];