Files
boursecu_server/app/Services/SmsBilling/Providers/CentralSmsApiProvider.php
T
2026-07-04 11:55:10 +03:30

70 lines
2.0 KiB
PHP

<?php
namespace App\Services\SmsBilling\Providers;
use App\Http\Controllers\Controller;
use App\Services\SmsBilling\Contracts\SmsProviderInterface;
use Illuminate\Support\Facades\Http;
class CentralSmsApiProvider implements SmsProviderInterface
{
public function name(): string
{
return 'rayegansms';
}
public function supports(array $config): bool
{
$meliUser = trim((string) ($config['melipayamak_user'] ?? ''));
return strlen($meliUser) <= 1;
}
public function send(string $to, string $message, array $config): array
{
$http = Http::timeout(15)
->withHeaders([
'Content-Type' => 'application/json',
'secret_key' => 'f21debfb-83ac-4624-a2f4-f08375043181',
]);
$curlOptions = Controller::httpClientOptions();
if ($curlOptions !== []) {
$http = $http->withOptions($curlOptions);
}
$host = trim((string) ($config['name_fa'] ?? ''));
if ($host === '') {
$host = trim((string) ($config['name_en'] ?? ''));
}
if ($host === '') {
$host = gethostname();
}
$response = $http->post('http://185.231.112.101/api/createsms', [
'phone' => $to,
'host' => $host,
'text' => trim($message),
'platform' => 'rayegansms',
]);
$body = $response->json() ?? $response->body();
$success = $response->successful();
if (is_array($body) && $success) {
if (array_key_exists('success', $body) && $body['success'] === false) {
$success = false;
} elseif (array_key_exists('ok', $body) && $body['ok'] === false) {
$success = false;
} elseif (isset($body['status']) && in_array(strtolower((string) $body['status']), ['error', 'failed', 'fail'], true)) {
$success = false;
}
}
return [
'success' => $success,
'raw' => $body,
];
}
}