This commit is contained in:
Mekan1206
2026-05-19 21:22:36 +05:00
parent b1a6c12a00
commit e66ce8fdcd
21 changed files with 677 additions and 150 deletions

View File

@@ -1,6 +1,8 @@
<?php
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -8,27 +10,49 @@ if (! function_exists('sendSMS')) {
/**
* Send a sms
*/
function sendSMS(string|int $phone, string|int $message): mixed
function sendSMS(string|int $phone, string|int $message): bool
{
$response = Http::retry(
times: 3,
sleepMilliseconds: 50,
throw: false,
when: function (Exception $exception, PendingRequest $request) {
Log::error('Exception: ', [
'message' => $exception->getMessage(),
'line' => $exception->getLine(),
]);
try {
$response = Http::timeout(config('services.sms.timeout'))
->connectTimeout(config('services.sms.connect_timeout'))
->retry(
times: 3,
sleepMilliseconds: 50,
throw: false,
when: function (Throwable $exception, PendingRequest $request): bool {
if ($exception instanceof ConnectionException) {
return true;
}
return true;
if ($exception instanceof RequestException) {
return $exception->response->serverError();
}
return false;
}
)
->post(config('services.sms.url'), [
'phone' => '+993'.$phone,
'code' => $message,
]);
if (! $response->successful()) {
Log::error('SMS API request failed', [
'status' => $response->status(),
'body' => $response->body(),
]);
return false;
}
)
->post('http://216.250.14.144:3000/api/data', [
'phone' => '+993' . $phone,
'code' => $message,
return true;
} catch (Throwable $exception) {
Log::error('SMS API exception', [
'message' => $exception->getMessage(),
]);
return $response->body();
return false;
}
}
}
@@ -38,19 +62,36 @@ if (! function_exists('unmask_phone')) {
*/
function unmask_phone(string $phone): string
{
// Keep digits only
$digits = preg_replace('/\D+/', '', $phone);
// Remove Turkmenistan country code if present
if (str_starts_with($digits, '993')) {
$digits = substr($digits, 3);
}
// Return only valid 8-digit TM mobile number
if (preg_match('/^6\d{7}$/', $digits)) {
return $digits;
}
return '';
}
}
}
if (! function_exists('format_phone')) {
/**
* Format an 8-digit TM mobile number for display (+993 6X XX XX XX).
*/
function format_phone(string $phone): string
{
if (! preg_match('/^6\d{7}$/', $phone)) {
return $phone;
}
return sprintf(
'+993 %s %s %s %s',
substr($phone, 0, 2),
substr($phone, 2, 2),
substr($phone, 4, 2),
substr($phone, 6, 2),
);
}
}