47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\MarketingAddon\App\Services;
|
||
|
|
|
||
|
|
use Exception;
|
||
|
|
use Modules\MarketingAddon\App\Models\Smsgateway;
|
||
|
|
|
||
|
|
class SmsService
|
||
|
|
{
|
||
|
|
public function send(string $phone, $template, array $data)
|
||
|
|
{
|
||
|
|
$gateways = Smsgateway::where('status', 1)->where('channel', 'sms')->where('business_id', $template->business_id)->get();
|
||
|
|
|
||
|
|
if (empty($gateways)) {
|
||
|
|
throw new \Exception('No active SMS gateway found for this business.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$response = [
|
||
|
|
'status' => false,
|
||
|
|
'message' => 'Something went wrong, Please try again.'
|
||
|
|
];
|
||
|
|
|
||
|
|
$contents = $this->buildContent($template, $data);
|
||
|
|
|
||
|
|
foreach ($gateways as $gateway) {
|
||
|
|
$response = $gateway->namespace::send_message($gateway, $phone, $contents);
|
||
|
|
|
||
|
|
if (($response['status'] ?? false) === true) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Exception('All SMS gateways failed');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function buildContent($template, array $data)
|
||
|
|
{
|
||
|
|
$message = $template->message;
|
||
|
|
|
||
|
|
foreach ($data as $key => $value) {
|
||
|
|
$message = str_replace('[' . $key . ']', $value, $message);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $message;
|
||
|
|
}
|
||
|
|
}
|