From 7fefc460c249a9aa6789c0f67a9411605bfdd699 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Fri, 13 Mar 2026 14:05:01 +0000 Subject: [PATCH] updaing whatsapp --- admin_integrations.php | 14 ++++----- includes/NotificationService.php | 54 +++++++++++++++++++------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/admin_integrations.php b/admin_integrations.php index c0d0445..79cac83 100644 --- a/admin_integrations.php +++ b/admin_integrations.php @@ -19,16 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($testPhone === '') { $testError = 'Phone number is required for testing.'; } else { - // Check if settings are saved first? - // We rely on get_setting() which reads from DB. - // If user just changed input but didn't save, it won't work. - // That's standard behavior. - try { // We use the new public method in NotificationService // This method handles the 968 prefix logic internally. - NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink'); - $testSuccess = 'Test message sent (check logs/phone).'; + $res = NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink'); + + if ($res['success']) { + $testSuccess = 'Message sent! ' . $res['message']; + } else { + $testError = 'Failed: ' . $res['message']; + } } catch (Throwable $e) { $testError = 'Error sending test: ' . $e->getMessage(); } diff --git a/includes/NotificationService.php b/includes/NotificationService.php index a62305c..7d66360 100644 --- a/includes/NotificationService.php +++ b/includes/NotificationService.php @@ -71,6 +71,7 @@ class NotificationService if (get_setting('whatsapp_enabled') === '1') { $phone = self::getPhoneNumber($user); if ($phone) { + // Return value ignored in async/background context, but logged inside self::sendWhatsApp($phone, $whatsapp); } else { error_log("WHATSAPP Notification skipped for {$user['email']}: Phone number not found."); @@ -107,8 +108,9 @@ class NotificationService /** * Send a WhatsApp message via Wablas API. + * @return array ['success' => bool, 'message' => string, 'response' => mixed] */ - public static function sendWhatsApp(string $phone, string $message) + public static function sendWhatsApp(string $phone, string $message): array { // 1. Format Phone Number // Remove non-numeric characters @@ -125,12 +127,13 @@ class NotificationService if (!$domain || !$token) { error_log("WHATSAPP Error: Wablas domain or token not configured."); - return; + return ['success' => false, 'message' => 'Domain or Token missing']; } // Ensure domain has no trailing slash and correct scheme $domain = rtrim($domain, '/'); - if (!preg_match('/^https?:\\/', $domain)) { + // If domain doesn't start with http/https, prepend https:// + if (!preg_match('/^https?:\/\//', $domain)) { $domain = 'https://' . $domain; } @@ -138,40 +141,49 @@ class NotificationService // Endpoint: /api/send-message (StandardWablas) $url = $domain . "/api/send-message"; - $data = [ + $payload = json_encode([ 'phone' => $phone, 'message' => $message, - ]; + ]); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: $token", - "Content-Type: application/json" // Wablas often accepts JSON or form-data. Trying JSON first, or form-data if that fails. - // Actually, many Wablas instances use form-data or x-www-form-urlencoded. - // Let's try standard POST fields which curl sends as application/x-www-form-urlencoded by default if passing array. - ]); - - // Resetting headers to just Authorization for form-urlencoded default - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - "Authorization: $token" + "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_TIMEOUT, 10); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For dev env + curl_setopt($ch, CURLOPT_TIMEOUT, 15); + // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $error = curl_error($ch); + $curlErr = curl_error($ch); curl_close($ch); - if ($error) { - error_log("WHATSAPP cURL Error: $error"); - return; + if ($curlErr) { + error_log("WHATSAPP cURL Error: $curlErr"); + return ['success' => false, 'message' => "Connection error: $curlErr"]; } + $decoded = json_decode($response, true); + $statusMsg = $decoded['message'] ?? $response; + // Log result error_log("WHATSAPP Sent to $phone. Status: $httpCode. Response: " . substr($response, 0, 100)); + + // Wablas usually returns {'status': true, ...} or {'status': 'pending'} + // We consider 200 OK and no explicit 'status': false as success for now + $isSuccess = ($httpCode >= 200 && $httpCode < 300); + if (isset($decoded['status']) && $decoded['status'] === false) { + $isSuccess = false; + } + + return [ + 'success' => $isSuccess, + 'message' => "API ($httpCode): " . (is_string($statusMsg) ? $statusMsg : json_encode($statusMsg)), + 'response' => $decoded + ]; } -} +} \ No newline at end of file