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
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Services\Sms;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class IranPayamakService
{
public function sendPattern(string $phone, string $patternKey, array $attributes): array
{
$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.{$patternKey}"),
'attributes' => $attributes,
'recipient' => $this->formatRecipient($phone),
'line_number' => config('services.iranpayamak.line_number'),
'number_format' => 'english',
]);
if (!$response->successful()) {
throw new RuntimeException('SMS sending failed: ' . $response->status() . ' ' . $response->body());
}
return $response->json() ?? [];
}
protected function formatRecipient(string $phone): string
{
if (str_starts_with($phone, '+98')) {
return '0' . substr($phone, 3);
}
return $phone;
}
}
@@ -0,0 +1,42 @@
<?php
namespace App\Services\Sms;
use App\Jobs\SendPatternSms;
use App\Models\Document;
use App\Models\Invoice;
use App\Models\User;
class SmsNotificationService
{
public function requestRegistered(Document $document): void
{
$this->dispatch($document->requester, 'request_registered', [
'code' => $document->code,
]);
}
public function paymentConfirmed(Invoice $invoice): void
{
$this->dispatch($invoice->user, 'payment_confirmed', [
'invoice' => $invoice->number,
'amount' => number_format($invoice->amount),
]);
}
public function reportReady(Document $document): void
{
$this->dispatch($document->requester, 'report_ready', [
'code' => $document->code,
]);
}
protected function dispatch(?User $user, string $patternKey, array $attributes): void
{
if (empty($user?->phone) || empty(config("services.iranpayamak.patterns.{$patternKey}"))) {
return;
}
SendPatternSms::dispatch($user->phone, $patternKey, $attributes);
}
}