71 lines
2.3 KiB
PHP
71 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Exception;
|
|
|
|
class SmsMachineService
|
|
{
|
|
protected string $gatewayIp;
|
|
protected string $apiKey;
|
|
protected int $timeout;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->gatewayIp = env('SMS_GATEWAY_IP', '192.168.1.100'); // IP di default del dispositivo Android locale
|
|
$this->apiKey = env('SMS_GATEWAY_API_KEY', 'default_local_key_netgescon');
|
|
$this->timeout = (int)env('SMS_GATEWAY_TIMEOUT', 5);
|
|
}
|
|
|
|
/**
|
|
* Invia un messaggio SMS con codice OTP al numero di telefono specificato
|
|
* connettendosi direttamente al gateway locale Android.
|
|
*/
|
|
public function sendOtp(string $phoneNumber, string $otpCode): bool
|
|
{
|
|
$message = "NetGescon - Codice di verifica OTP per aggiornamento anagrafica: {$otpCode}. Valido per 10 minuti.";
|
|
return $this->sendSms($phoneNumber, $message);
|
|
}
|
|
|
|
/**
|
|
* Invia un SMS generico tramite il gateway locale Android.
|
|
*/
|
|
public function sendSms(string $phoneNumber, string $message): bool
|
|
{
|
|
$url = "http://{$this->gatewayIp}/send";
|
|
|
|
$payload = [
|
|
'to' => $phoneNumber,
|
|
'message' => $message,
|
|
'api_key' => $this->apiKey,
|
|
];
|
|
|
|
try {
|
|
Log::info("Invio SMS local-gateway in corso a {$phoneNumber} tramite {$url}");
|
|
|
|
// Chiamata HTTP al dispositivo Android in LAN
|
|
$response = Http::timeout($this->timeout)
|
|
->asJson()
|
|
->post($url, $payload);
|
|
|
|
if ($response->successful()) {
|
|
Log::info("SMS inviato con successo a {$phoneNumber}. Risposta gateway: " . $response->body());
|
|
return true;
|
|
}
|
|
|
|
Log::error("Mancato invio SMS locale. Risposta gateway fallita: " . $response->status() . " - " . $response->body());
|
|
return false;
|
|
} catch (Exception $e) {
|
|
Log::error("Errore di connessione all'SMS Gateway Android locale ({$url}): " . $e->getMessage());
|
|
// Fallback locale simulato per lo sviluppo/testing offline
|
|
if (config('app.debug')) {
|
|
Log::warning("[DEBUG ON] SMS inviato in simulazione a {$phoneNumber}: {$message}");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|