update marketing
All checks were successful
All checks were successful
This commit is contained in:
46
Modules/MarketingAddon/App/Services/SmsService.php
Normal file
46
Modules/MarketingAddon/App/Services/SmsService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user