update marketing
All checks were successful
All checks were successful
This commit is contained in:
64
Modules/MarketingAddon/App/Services/NotifyService.php
Normal file
64
Modules/MarketingAddon/App/Services/NotifyService.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MarketingAddon\App\Services;
|
||||
|
||||
use Modules\MarketingAddon\App\Models\Template;
|
||||
use Modules\MarketingAddon\App\Models\MessageReport;
|
||||
use Modules\MarketingAddon\App\Jobs\SendNotificationJob;
|
||||
|
||||
class NotifyService
|
||||
{
|
||||
public function send(string $event, array $data, string $businessId)
|
||||
{
|
||||
$settings = get_option('notification_settings_' . $businessId);
|
||||
|
||||
if (!isset($settings[$event])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$array = [];
|
||||
|
||||
foreach (['sms', 'whatsapp', 'email'] as $channel) {
|
||||
|
||||
// Check enabled in settings
|
||||
if (empty($settings[$event][$channel]['enabled'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check required data per channel
|
||||
if (in_array($channel, ['sms', 'whatsapp']) && empty($data['phone'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($channel === 'email' && empty($data['email'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$array[] = $channel;
|
||||
$template = Template::where('business_id', $businessId)->where('title', $event)->where('type', $channel)->first();
|
||||
|
||||
if (empty($template)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$notification = MessageReport::create([
|
||||
'title' => $event,
|
||||
'channel' => $channel,
|
||||
'business_id' => $businessId,
|
||||
'template_id' => $template->id,
|
||||
'message' => $template->message,
|
||||
]);
|
||||
|
||||
// Dispatch job
|
||||
SendNotificationJob::dispatch(
|
||||
data: $data,
|
||||
channel: $channel,
|
||||
template: $template,
|
||||
businessId: $businessId,
|
||||
notificationId: $notification->id,
|
||||
)->onQueue('high');
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
33
Modules/MarketingAddon/App/Services/WhatsAppService.php
Normal file
33
Modules/MarketingAddon/App/Services/WhatsAppService.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MarketingAddon\App\Services;
|
||||
|
||||
use Modules\MarketingAddon\App\Models\Smsgateway;
|
||||
|
||||
class WhatsAppService
|
||||
{
|
||||
/**
|
||||
* Send WhatsApp campaign via AiSensy
|
||||
*/
|
||||
public function send(
|
||||
string $phone,
|
||||
object $template,
|
||||
int $businessId,
|
||||
string $name,
|
||||
array $data = [],
|
||||
) {
|
||||
|
||||
$gateways = Smsgateway::where('channel', 'whatsapp')
|
||||
->where('business_id', $businessId)
|
||||
->where('status', 1)
|
||||
->get();
|
||||
|
||||
if (empty($gateways)) {
|
||||
throw new \Exception('No active WhatsApp gateway found for this business.');
|
||||
}
|
||||
|
||||
foreach ($gateways as $gateway) {
|
||||
$gateway->namespace::send_message($gateway, $phone, $template, $name, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user