125 lines
3.9 KiB
PHP
125 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
/**
|
|
* SendSMSHelper
|
|
*
|
|
* This class is responsible for sending SMS messages using a third-party SMS service.
|
|
* It constructs the URL for the API request, sends the SMS, and logs the response and record the status in db.
|
|
*
|
|
* @package App\Helpers
|
|
*/
|
|
|
|
use App\Models\NotificationModel;
|
|
|
|
|
|
class SendSMSHelper
|
|
{
|
|
protected $apiKey;
|
|
protected $logger;
|
|
protected $senderId;
|
|
protected $smsType;
|
|
protected $templateType;
|
|
protected $url;
|
|
protected $renewalTemplateID;
|
|
|
|
protected $notificationModel;
|
|
|
|
public function __construct(){
|
|
|
|
$this->apiKey = trim(getenv('SMS_API_KEY'));
|
|
$this->senderId = getenv('SMS_SENDER_ID');
|
|
$this->templateType = getenv('SMS_TEMPLATE_TYPE');
|
|
$this->logger = service('logger');
|
|
$this->url = trim(getenv('SMS_BASE_URL'));
|
|
$this->renewalTemplateID = getenv('SMS_RENEWAL_TEMPLATE_ID');
|
|
|
|
$this->notificationModel = new NotificationModel();
|
|
}
|
|
|
|
public function sendSMS($message,$mobile_number,$campaignID = null,$membershipID = null){
|
|
|
|
$this->logger->error('SMS Helper : SMS Request Data type = '.($campaignID));
|
|
$this->logger->error('SMS Helper : SMS Request Data = '.json_encode($message));
|
|
$this->logger->error('SMS Helper : SMS Request Mobile Number = '.json_encode($mobile_number));
|
|
|
|
if (is_array($mobile_number)){
|
|
|
|
$mobile_number = implode(",",$mobile_number);
|
|
}
|
|
|
|
if (empty($mobile_number) || $mobile_number == null || $mobile_number == ""){
|
|
$this->logger->error('SMS Helper : SMS Request Mobile Number is Empty');
|
|
return "Mobile Number is Empty";
|
|
}
|
|
|
|
$message_encode = ($message);
|
|
$this->logger->error('SMS Helper : SMS URLENCODED MESSAGE = '.$message_encode);
|
|
|
|
$url = $this->constructSMSURL($message_encode,$mobile_number);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// Set a timeout of 10 seconds
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
|
$output = curl_exec($ch);
|
|
if ($output === false) {
|
|
$this->logger->error('SMS Helper : SMS CURL Error = '.curl_error($ch));
|
|
} else {
|
|
$this->logger->error('SMS Helper : SMS CURL Response = '.$output);
|
|
// return $output;
|
|
}
|
|
|
|
|
|
curl_close($ch);
|
|
$mobile_number = str_replace("91","",$mobile_number);
|
|
|
|
if (!empty($campaignID)){
|
|
$this->saveSMSHistory($output,$mobile_number,$message,$campaignID,$membershipID);
|
|
}
|
|
|
|
$output_to_display = str_contains($output, 'success')? "SMS Sent Successfully" : "SMS Sending Failed";
|
|
|
|
return $output_to_display;
|
|
|
|
}
|
|
public function constructSMSURL($message, $mobile_number){
|
|
$this->logger->error('SMS Helper : SMS URL Construction');
|
|
|
|
$params = [
|
|
'APIKEY' => $this->apiKey,
|
|
'MobileNo' => $mobile_number,
|
|
'SenderID' => $this->senderId,
|
|
'Message' => $message,
|
|
'ServiceName' => $this->templateType,
|
|
'DLTTemplateID' => $this->renewalTemplateID,
|
|
];
|
|
|
|
$URL = $this->url.http_build_query($params);
|
|
$this->logger->error('SMS Helper : SMS URL = '.$URL);
|
|
return $URL;
|
|
|
|
}
|
|
|
|
public function saveSMSHistory($output,$mobile_number,$message,$campaignID = null,$membershipID = null){
|
|
|
|
$where = [
|
|
'mobile_no' => $mobile_number,
|
|
'isactive' => 1
|
|
];
|
|
|
|
$insertStatus = $this->notificationModel->saveSmSHistory($where,$campaignID,$message, $output,$membershipID);
|
|
if ($insertStatus){
|
|
$this->logger->error('SMS Helper : SMS History Inserted Successfully');
|
|
}else{
|
|
$this->logger->error('SMS Helper : SMS History Insertion Failed');
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |