222 lines
8.7 KiB
PHP
Executable File
222 lines
8.7 KiB
PHP
Executable File
<?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->error('Helper : Email Request Data type = '.gettype($records));
|
|
|
|
$cc = isset($records['carboncopy']) ? $records['carboncopy'] : '';
|
|
$bcc = isset($records['blindcarboncopy']) ? $records['blindcarboncopy'] : '';
|
|
|
|
try {
|
|
$this->email->clear();
|
|
if($records['recipient_email'] == "" && $records['subject'] == ""){
|
|
throw new \Exception("Email and Subject are Must Please Try again..");
|
|
}
|
|
$this->email->setTo($records['recipient_email']);
|
|
if (!empty($records['cc'])) {
|
|
if (is_array($records['cc'])) {
|
|
$this->email->setCC($records['cc']); // Directly use array
|
|
} else {
|
|
$this->email->setCC(explode(',', $records['cc'])); // Convert string to array
|
|
}
|
|
}
|
|
|
|
$this->logger->error('Helper : Email recipient = '.$records['recipient_email']);
|
|
$this->email->setFrom('web@vijayabharathambooks.com', isset($records['company'])?$records['company']:"VB");
|
|
$this->email->setSubject($records['subject']);
|
|
$this->logger->error('Helper : Email subject = '.$records['subject']);
|
|
$this->email->setMessage($records['description']);
|
|
|
|
// if($records['template_name'] != ""){
|
|
// $this->logger->error('Helper : Email Template = '.$records['template_name']);
|
|
// $this->logger->error("Helper : Email Request data = ".json_encode($records));
|
|
// $emailContent = view($records['template_name'],$records);
|
|
// $this->email->setMessage($emailContent);
|
|
// }else{
|
|
// $this->logger->error('Helper : Email Dynamic Template');
|
|
// $this->email->setMessage($records['description']);
|
|
// }
|
|
|
|
|
|
if ($cc !== '') {
|
|
$ccRecipients = explode(',', trim($cc));
|
|
$this->logger->error('Helper : Email carbon copy CC recipient = ' . implode(',', $ccRecipients));
|
|
if (!empty($ccRecipients)) {
|
|
if (getenv('CI_ENVIRONMENT') === "production") {
|
|
$this->email->setCC('contact@vijayabharathambooks.com');
|
|
}
|
|
$this->email->setCC($ccRecipients);
|
|
}
|
|
} else {
|
|
if (getenv('CI_ENVIRONMENT') === "production") {
|
|
$this->email->setCC('contact@vijayabharathambooks.com');
|
|
}
|
|
}
|
|
|
|
if ($bcc !== '') {
|
|
$bccRecipients = explode(',', trim($bcc));
|
|
$this->logger->error('Helper : Email blindcarboncopy BCC recipient = ' . implode(',', $bccRecipients));
|
|
if(!empty($bccRecipients)) {
|
|
$this->email->setBCC($bccRecipients);
|
|
}
|
|
}
|
|
|
|
if ($this->email->send()) {
|
|
$message['success'] = 'Email sent successfully';
|
|
$this->logger->error('Helper : Email Status = 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)
|
|
{
|
|
$final_result = []; // Initialize outside try-catch block
|
|
|
|
try {
|
|
$this->logger->error('Helper : Whatsapp called');
|
|
$curl = curl_init();
|
|
$headers = array('Content-Type:application/json');
|
|
$jsonencoded = json_encode($data);
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
$this->logger->error('Helper : Whatsapp url = ' . $url);
|
|
|
|
switch ($method) {
|
|
case "POST":
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
if ($data) {
|
|
$this->logger->error('Helper : Whatsapp Request in jsonencoded Type = ' . $jsonencoded);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonencoded);
|
|
}
|
|
break;
|
|
case "PUT":
|
|
curl_setopt($curl, CURLOPT_PUT, 1);
|
|
break;
|
|
default:
|
|
if ($data) {
|
|
$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->error('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") {
|
|
$final_result['message'] = $result->message;
|
|
$final_result['receiving_status'] = 0 ;
|
|
$this->logger->error('Helper : Whatsapp Err occur = ' . $result->message);
|
|
throw new \Exception($result->message);
|
|
}
|
|
|
|
if ($result->status === "success") {
|
|
$final_result['message'] = isset($result->message) ? (
|
|
(strtolower(gettype($result->message)) === 'string') ? $result->message : "Sent Success") : " Not Sent.";
|
|
$final_result['receiving_status'] = isset($result->message) ? ((strtolower(gettype($result->message)) === 'string') ? 0 : 1 ) : 0 ;
|
|
}
|
|
} 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['message'] = $e->getMessage();
|
|
$final_result['receiving_status'] = 0 ;
|
|
}
|
|
$final_result['reponse'] = json_decode($curl_exec);
|
|
return $final_result;
|
|
}
|
|
|
|
// public function send_whatsapp_message($to , $doc_url)
|
|
// {
|
|
// $url = 'https://graph.facebook.com/v20.0/355752820961010/messages';
|
|
// $headers = [
|
|
// 'Authorization: Bearer '.$_ENV['whatsAppToken'],
|
|
// 'Content-Type: application/json'
|
|
// ];
|
|
|
|
// $templateName = 'bbsigndoc';
|
|
// $languageCode = 'en';
|
|
// $parsedUrl = parse_url($doc_url, PHP_URL_QUERY);
|
|
|
|
// $data = [
|
|
// 'messaging_product' => 'whatsapp',
|
|
// 'to' => $to,
|
|
// 'type' => 'template',
|
|
// 'template' => [
|
|
// 'name' => $templateName,
|
|
// 'language' => [
|
|
// 'code' => $languageCode
|
|
// ],
|
|
// 'components' => [
|
|
// [
|
|
// 'type' => 'body',
|
|
// ],
|
|
// [
|
|
// 'type' => 'button',
|
|
// 'sub_type' => 'url',
|
|
// 'index' => 0,
|
|
// 'parameters' => [
|
|
|
|
// [
|
|
// 'type' => 'text',
|
|
// 'text' => $parsedUrl
|
|
// ]
|
|
// ]
|
|
// ]
|
|
// ]
|
|
// ]
|
|
// ];
|
|
|
|
|
|
// $ch = curl_init();
|
|
// curl_setopt($ch, CURLOPT_URL, $url);
|
|
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
// curl_setopt($ch, CURLOPT_POST, true);
|
|
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
|
|
// $response = curl_exec($ch);
|
|
// $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
// curl_close($ch);
|
|
|
|
// return [
|
|
// 'status_code' => $statusCode,
|
|
// 'response' => json_decode($response, true)
|
|
// ];
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
?>
|