feat: implement SMS notification service and integrate with existing services

This commit is contained in:
2026-09-21 02:47:12 +03:30
parent 51b13631a3
commit 1e599b917b
9 changed files with 153 additions and 48 deletions
+12 -42
View File
@@ -3,10 +3,17 @@
namespace App\Services\Auth;
use App\Models\OtpCode;
use Illuminate\Support\Facades\Http;
use App\Services\Sms\IranPayamakService;
class OtpService
{
protected IranPayamakService $iranPayamakService;
public function __construct(IranPayamakService $iranPayamakService)
{
$this->iranPayamakService = $iranPayamakService;
}
public function generateCode(): string
{
return (string) random_int(1000, 9999);
@@ -45,51 +52,14 @@ class OtpService
->exists();
}
private function sendSms(string $phone, string $code): array
private function sendSms(string $phone, string $code): void
{
try {
$formattedRecipient = $this->formatPhoneForSms($phone);
$response = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Api-Key' => config('services.iranpayamak.api_key'),
])
->post(config('services.iranpayamak.api_url'), [
'code' => config('services.iranpayamak.patterns.verify_code'),
'attributes' => [
'otp' => $code,
'status' => 'بازنشانی رمز عبور'
],
'recipient' => $formattedRecipient,
'line_number' => config('services.iranpayamak.line_number'),
'number_format' => 'english',
$this->iranPayamakService->sendPattern($phone, 'verify_code', [
'otp' => $code,
'status' => 'بازنشانی رمز عبور',
]);
if ($response->successful()) {
return $response->json();
} else {
return [
'status' => 'error',
'message' => 'SMS sending failed: ' . $response->status(),
'data' => $response->body()
];
}
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => 'SMS sending exception: ' . $e->getMessage(),
'data' => null
];
}
}
private function formatPhoneForSms(string $phone): string
{
if (str_starts_with($phone, '+98')) {
return '0' . substr($phone, 3);
}
return $phone;
}
}