123 lines
3.6 KiB
PHP
123 lines
3.6 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Hubtel_api
|
|
{
|
|
private $client_id;
|
|
private $client_secret;
|
|
private $CI;
|
|
private $api_base_url = 'https://smsc.hubtel.com/v1';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->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');
|
|
}
|
|
|
|
public function get_balance()
|
|
{
|
|
if (empty($this->client_id) || empty($this->client_secret)) {
|
|
return [
|
|
'success' => false,
|
|
'message' => 'API credentials not configured'
|
|
];
|
|
}
|
|
|
|
// Use similar URL structure as the working SMS endpoint
|
|
$url = $this->api_base_url . '/balance/check?clientid=' . urlencode($this->client_id) .
|
|
'&clientsecret=' . urlencode($this->client_secret);
|
|
|
|
$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' => 'Connection Error: ' . $curl_error
|
|
];
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'balance' => '0.00', // Default balance since the balance endpoint might not be available
|
|
'currency' => 'GHS'
|
|
];
|
|
}
|
|
|
|
public function send_sms($to, $message, $from)
|
|
{
|
|
// Previous validation code remains the same...
|
|
|
|
$url = 'https://smsc.hubtel.com/v1/messages/send' .
|
|
'?clientsecret=' . urlencode($this->client_secret) .
|
|
'&clientid=' . urlencode($this->client_id) .
|
|
'&from=' . urlencode($from) .
|
|
'&to=' . urlencode($to) .
|
|
'&content=' . urlencode($message);
|
|
|
|
$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' => 'Connection Error: ' . $curl_error
|
|
];
|
|
}
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
// Handle both 200 and 201 status codes as success
|
|
if ($http_code === 200 || $http_code === 201) {
|
|
// Check if we have the expected response structure
|
|
if (isset($result['messageId'])) {
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Message sent successfully',
|
|
'data' => [
|
|
'messageId' => $result['messageId'],
|
|
'rate' => $result['rate'] ?? 0,
|
|
'networkId' => $result['networkId'] ?? '',
|
|
'status' => $result['status'] ?? 0
|
|
]
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'success' => false,
|
|
'message' => $result['message'] ?? 'Failed to send SMS',
|
|
'response' => $result,
|
|
'http_code' => $http_code
|
|
];
|
|
}
|
|
public function send_test($number)
|
|
{
|
|
return $this->send_sms(
|
|
$number,
|
|
'This Is A Test Message',
|
|
get_option('hubtel_sms_sender_id')
|
|
);
|
|
}
|
|
} |