129 lines
5.4 KiB
PHP
129 lines
5.4 KiB
PHP
<?php
|
|
|
|
// app/Helpers/NotificationHelper.php
|
|
|
|
namespace App\Helpers;
|
|
|
|
class NotificationHelper
|
|
{
|
|
protected $email;
|
|
protected $logger;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->email = service('email');
|
|
$this->logger = service('logger');
|
|
}
|
|
|
|
public function sendEmail($records)
|
|
{
|
|
$this->logger->info('Helper : Email Request Data type = '.gettype($records));
|
|
|
|
$cc = isset($records['carboncopy']) ? $records['carboncopy'] : '';
|
|
$bcc = isset($records['blindcarboncopy']) ? $records['blindcarboncopy'] : '';
|
|
|
|
try {
|
|
if($records['recipient_email'] == "" && $records['subject'] == ""){
|
|
throw new \Exception("Email and Subject are Must Please Try again..");
|
|
}
|
|
$this->email->setTo($records['recipient_email']);
|
|
$this->logger->info('Helper : Email recipient = '.$records['recipient_email']);
|
|
$this->email->setFrom('no-reply@tripapprovaltool.com', isset($records['company'])?$records['company']:"VBP");
|
|
$this->email->setSubject($records['subject']);
|
|
$this->logger->info('Helper : Email subject = '.$records['subject']);
|
|
if($records['template_name'] != ""){
|
|
$this->logger->info('Helper : Email Template = '.$records['template_name']);
|
|
$this->logger->info("Helper : Email Request data = ".json_encode($records));
|
|
$emailContent = view($records['template_name'],$records);
|
|
$this->email->setMessage($emailContent);
|
|
}else{
|
|
$this->logger->info('Helper : Email Dynamic Template');
|
|
$this->email->setMessage($records['description']);
|
|
}
|
|
|
|
|
|
if (!empty($cc)) {
|
|
$ccRecipients = explode(',', $cc);
|
|
$this->logger->info('Helper : Email carboncopy CC recipient = '.$ccRecipients);
|
|
foreach ($ccRecipients as $ccRecipient) {
|
|
$this->email->setCC(trim($ccRecipient));
|
|
}
|
|
}
|
|
|
|
if (!empty($bcc)) {
|
|
$bccRecipients = explode(',', $bcc);
|
|
$this->logger->info('Helper : Email blindcarboncopy BCC recipient = '.$bccRecipients);
|
|
foreach ($bccRecipients as $bccRecipient) {
|
|
$this->email->setBCC(trim($bccRecipient));
|
|
}
|
|
}
|
|
|
|
if ($this->email->send()) {
|
|
$message['success'] = 'Email sent successfully';
|
|
$this->logger->info('Helper : Email = sent successfully');
|
|
} else {
|
|
throw new \Exception('Email sending failed.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Helper : Email Error: ' . $e->getMessage());
|
|
$message['error'] = 'Email Exception Occur : ' . $e->getMessage();
|
|
}
|
|
return $message;
|
|
}
|
|
|
|
public function sendWhatsAppMessage($url,$method,$data)
|
|
{
|
|
try{
|
|
$this->logger->info('Helper : Whatsapp');
|
|
$curl = curl_init();
|
|
$headers = array('Content-Type:application/json');
|
|
$jsonencoded = json_encode($data);
|
|
curl_setopt( $curl,CURLOPT_URL, $url);
|
|
$this->logger->info('Helper : Whatsapp url = '.$url);
|
|
switch ($method) {
|
|
case "POST":
|
|
curl_setopt( $curl,CURLOPT_POST, true );
|
|
if ($data) {
|
|
$this->logger->info('Helper : Whatsapp jsonencoded = '.$jsonencoded);
|
|
curl_setopt( $curl,CURLOPT_POSTFIELDS, $jsonencoded);
|
|
}
|
|
break;
|
|
case "PUT":
|
|
curl_setopt($curl, CURLOPT_PUT, 1);
|
|
break;
|
|
default:
|
|
if ($data) {
|
|
// print_r($data);die();
|
|
$url = sprintf("%s?%s", $url, http_build_query((array)$data));
|
|
}
|
|
}
|
|
curl_setopt( $curl,CURLOPT_HTTPHEADER, $headers );
|
|
curl_setopt( $curl,CURLOPT_RETURNTRANSFER, true );
|
|
curl_setopt( $curl,CURLOPT_SSL_VERIFYPEER, true );
|
|
$curl_exec = curl_exec($curl);
|
|
$this->logger->info('Helper : Whatsapp Reponse = '.$curl_exec);
|
|
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
if ($http_status === 200) {
|
|
$result = json_decode($curl_exec);
|
|
if ($result->status === "error") {
|
|
$this->logger->error('Helper : Whatsapp Err occur = '.$result->message);
|
|
throw new \Exception($result->message);
|
|
}else{
|
|
$final_result = " Sended Success ";
|
|
}
|
|
}else{
|
|
$curl_errno= curl_errno($curl);
|
|
$this->logger->error('Helper : Whatsapp curl error occur = '.$curl_errno);
|
|
throw new \Exception(" Errno returned ".$curl_errno);
|
|
}
|
|
curl_close($curl);
|
|
}catch (\Exception $e) {
|
|
$this->logger->error('Helper : Whatsapp Exception Occur = ' . $e->getMessage());
|
|
$final_result = "Exception Errno returned ".$e->getMessage()." <br/>";
|
|
}
|
|
return $final_result;
|
|
}
|
|
}
|
|
|
|
?>
|