feat: refactor WhatsApp autenticación y configuración de token en .env
This commit is contained in:
parent
aff2448356
commit
c1d6f58697
@ -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=
|
||||
|
||||
@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Servicio de autenticación para WhatsApp API
|
||||
*/
|
||||
class WhatsAppAuthService
|
||||
{
|
||||
protected string $loginUrl;
|
||||
protected string $email;
|
||||
protected string $password;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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'),
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user