Sindbad~EG File Manager
| Current Path : /home/zada/www/temp/pHF9g7NR7K/addons/ |
|
|
| Current File : /home/zada/www/temp/pHF9g7NR7K/addons/whatsapp_notifications\files\app\Utility\SmsUtility.php |
<?php
namespace App\Utility;
use App\Models\SmsTemplate;
use App\Utility\SendSMSUtility;
use App\Models\WhatsAppLog;
class SmsUtility
{
public static function broadcast($to, $text)
{
self::send($to, $text, 'custom_broadcast');
}
public static function order_placed($to, $order)
{
$text = "Your order {$order->code} has been placed";
self::send($to, $text, 'order_placement');
}
public static function delivery_status_change($to, $order)
{
$status = str_replace('_', ' ', $order->delivery_status);
$text = "Your order {$order->code} has been {$status}";
if (!empty($order->tracking_code)) {
$text .= " | Tracking: {$order->tracking_code}";
}
self::send($to, $text, 'delivery_status_change');
}
public static function payment_status_change($to, $order)
{
$status = str_replace('_', ' ', $order->payment_status);
$text = "Your order {$order->code} payment {$status}";
self::send($to, $text, 'payment_status_change');
}
public static function assign_delivery_boy($to, $order_code)
{
$text = "You are assigned to order {$order_code}";
self::send($to, $text, 'assign_delivery_boy');
}
public static function payment_reminder($to, $order)
{
$text = "Payment pending for order {$order->code}";
self::send($to, $text, 'complete_unpaid_order_payment');
}
protected static function send($to, $text, $identifier)
{
$template = SmsTemplate::where('identifier', $identifier)->first();
$template_id = $template->template_id ?? null;
$from = env('WHATSAPP_PHONE_ID') ?: env('APP_NAME');
if (env('WHATSAPP_CLOUD_ENABLED') == '1' && env('WHATSAPP_TOKEN') && env('WHATSAPP_PHONE_ID')) {
$response = self::sendWhatsAppCloud($to, $text);
$decoded = json_decode($response, true);
$status = is_array($decoded) && isset($decoded['messages'][0]['id']) ? 'sent' : 'failed';
$message_id = is_array($decoded) && isset($decoded['messages'][0]['id']) ? $decoded['messages'][0]['id'] : null;
WhatsAppLog::create([
'to' => self::normalizePhone($to),
'from' => env('WHATSAPP_PHONE_ID'),
'message' => $text,
'status' => $status,
'error_message' => is_array($decoded) && isset($decoded['error']) ? json_encode($decoded['error']) : null,
'message_id' => $message_id,
]);
return $status === 'sent';
}
$result = SendSMSUtility::sendSMS($to, $from, $text, $template_id);
WhatsAppLog::create([
'to' => self::normalizePhone($to),
'from' => $from,
'message' => $text,
'status' => $result ? 'sent' : 'failed',
'error_message' => $result ? null : 'sms_failed',
'message_id' => null,
]);
return (bool) $result;
}
protected static function sendWhatsAppCloud($to, $text)
{
$token = env('WHATSAPP_TOKEN');
$phone_id = env('WHATSAPP_PHONE_ID');
$url = "https://graph.facebook.com/v20.0/{$phone_id}/messages";
$payload = [
'messaging_product' => 'whatsapp',
'to' => self::normalizePhone($to),
'type' => 'text',
'text' => [
'preview_url' => false,
'body' => $text
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer {$token}",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
protected static function normalizePhone($to)
{
return preg_replace('/\D+/', '', $to);
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists