CI = &get_instance(); // Load Hubtel credentials from options $this->client_id = get_option('hubtel_sms_client_id'); $this->client_secret = get_option('hubtel_sms_client_secret'); } /** * Send SMS using Hubtel API * * @param string $to Recipient phone number (E164 format) * @param string $message Message content (max 160 characters) * @param string $from Sender ID (max 11 alpha-numeric characters) * @return array Response with success status and message */ public function send_sms($to, $message, $from) { // Validate credentials if (empty($this->client_id) || empty($this->client_secret)) { return [ 'success' => false, 'message' => 'API credentials not configured' ]; } // Validate sender ID (must be 11 alpha-numeric characters or less) if (strlen($from) > 11 || !preg_match('/^[a-zA-Z0-9]+$/', $from)) { return [ 'success' => false, 'message' => 'Invalid Sender ID. Must be 11 alpha-numeric characters or less.' ]; } // Check message length if (strlen($message) > 160) { return [ 'success' => false, 'message' => 'Message content exceeds 160 characters' ]; } // Prepare the request URL with parameters $params = [ 'clientid' => $this->client_id, 'clientsecret' => $this->client_secret, 'from' => $from, 'to' => $to, 'content' => $message ]; $url = $this->api_endpoint . '?' . http_build_query($params); // Set up cURL request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPGET, true); // Using GET method as per API spec // Execute the request $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_error = curl_error($ch); curl_close($ch); // Handle cURL errors if ($curl_error) { return [ 'success' => false, 'message' => 'cURL Error: ' . $curl_error ]; } // Parse response $result = json_decode($response, true); // Handle API response if ($http_code === 200) { return [ 'success' => true, 'message' => $result['message'] ?? 'Message sent', 'responseCode' => $result['responseCode'] ?? '', 'data' => [ 'rate' => $result['data']['rate'] ?? 0, 'messageId' => $result['data']['messageId'] ?? '', 'status' => $result['data']['status'] ?? 0, 'networkId' => $result['data']['networkId'] ?? '' ] ]; } return [ 'success' => false, 'message' => $result['message'] ?? 'Failed to send SMS', 'responseCode' => $result['responseCode'] ?? '', 'data' => $result['data'] ?? null ]; } /** * Get account balance * * @return array Response with account balance */ public function get_balance() { if (empty($this->client_id) || empty($this->client_secret)) { return [ 'success' => false, 'message' => 'API credentials not configured' ]; } $params = [ 'clientid' => $this->client_id, 'clientsecret' => $this->client_secret ]; $url = 'https://smsc.hubtel.com/v1/accounts/balance?' . http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPGET, true); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_error = curl_error($ch); curl_close($ch); if ($curl_error) { return [ 'success' => false, 'message' => 'cURL Error: ' . $curl_error ]; } $result = json_decode($response, true); if ($http_code === 200) { return [ 'success' => true, 'balance' => $result['data']['balance'] ?? 0, 'currency' => $result['data']['currency'] ?? 'GHS', 'responseCode' => $result['responseCode'] ?? '', 'response' => $result ]; } return [ 'success' => false, 'message' => $result['message'] ?? 'Failed to get balance', 'responseCode' => $result['responseCode'] ?? '', 'response' => $result ]; } }