pdo = $pdo; $this->loadConfig(); } private function loadConfig() { $stmt = $this->pdo->prepare("SELECT setting_key, setting_value FROM integration_settings WHERE provider = 'wablas'"); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $settings = []; foreach ($rows as $r) { $settings[$r['setting_key']] = $r['setting_value']; } $this->domain = $settings['domain'] ?? ''; $this->token = $settings['token'] ?? ''; $this->secret_key = $settings['secret_key'] ?? ''; } public function testConnection() { if (empty($this->domain) || empty($this->token)) { return ['success' => false, 'message' => 'Missing Domain or Token']; } // Common Wablas endpoint to check device status $url = rtrim($this->domain, '/') . '/api/device/info?token=' . $this->token; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { return ['success' => false, 'message' => 'Connection failed: ' . $curlError]; } $data = json_decode($response, true); if ($httpCode >= 200 && $httpCode < 300) { // Some APIs return 200 even on logical error, check for status field if exists if (isset($data['status']) && $data['status'] === false) { return ['success' => false, 'message' => 'API Error: ' . ($data['message'] ?? 'Unknown error')]; } // Check for explicit disconnected status if available in data if (isset($data['data']['status']) && $data['data']['status'] !== 'connected') { return ['success' => false, 'message' => 'Device Status: ' . $data['data']['status'] . ' (May need re-scan)']; } return ['success' => true, 'message' => 'Connection successful! Device info retrieved.']; } // Handle error responses with JSON message if ($data && isset($data['message'])) { return ['success' => false, 'message' => 'Provider Error (' . $httpCode . '): ' . $data['message']]; } return ['success' => false, 'message' => 'HTTP Error: ' . $httpCode]; } public function sendMessage($phone, $message) { if (empty($this->domain) || empty($this->token)) { return ['success' => false, 'message' => 'Wablas configuration missing']; } // Format phone number: ensure 968 country code if only 8 digits provided $cleanPhone = preg_replace('/[^0-9]/', '', $phone); if (strlen($cleanPhone) === 8) { $phone = '968' . $cleanPhone; } else { $phone = $cleanPhone; } $payload = [ 'phone' => $phone, 'message' => $message, 'secret' => $this->secret_key ]; // Assuming standard endpoint. If users use v2, it might be /api/v2/send-message $url = rtrim($this->domain, '/') . '/api/send-message'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: " . $this->token, "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); $curlError = curl_error($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($curlError) { return ['success' => false, 'message' => 'cURL Error: ' . $curlError]; } $data = json_decode($response, true); $msg = $data['message'] ?? $response; if ($httpCode >= 200 && $httpCode < 300) { if (isset($data['status']) && $data['status'] === false) { return ['success' => false, 'message' => 'Wablas Error: ' . ($data['message'] ?? 'Unknown error')]; } return ['success' => true, 'message' => 'Message sent successfully. Response: ' . (is_string($msg) ? substr($msg, 0, 100) : 'OK')]; } return ['success' => false, 'message' => 'HTTP Error ' . $httpCode . ': ' . (is_string($msg) ? $msg : 'Unknown')]; } }