- Implementa CRUD de unidades y soporte para decimales en inventario. - Integra servicios de WhatsApp para envío de documentos y auth. - Ajusta validación de series y permisos (RoleSeeder).
95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?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();
|
|
}
|
|
}
|