feat: implement SMS notification service and integrate with existing services
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user