Repository navigation
Expand file tree
/
Copy pathTextSmsGateway.php
More file actions
187 lines (159 loc) · 6.31 KB
/
Copy pathTextSmsGateway.php
File metadata and controls
187 lines (159 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace App\Gateways\UserDefined;
class TextSmsGateway
{
/**
* Get information about the Text SMS gateway.
*
* @return array
*/
public static function getGatewayInfo()
{
return [
'name' => 'Text SMS Gateway',
'description' => 'A gateway for sending SMS via Text API.',
'author' => 'SimpleISP',
'website' => 'https://simplux.africa',
];
}
public static function getConfigParameters()
{
return [
'gateway' => [
'label' => 'Gateway',
'type' => 'hidden',
'name' => 'text_gateway',
'value' => setting("text_gateway"),
],
'sender_id' => [
'label' => 'Sender ID',
'type' => 'text',
'name' => 'text_shortcode',
'value' => setting('text_shortcode'),
],
'partner_id' => [
'label' => 'Partner ID',
'type' => 'text',
'name' => 'text_partner_id',
'value' => setting('text_partner_id'),
],
'api_key' => [
'label' => 'API Key',
'type' => 'text',
'name' => 'text_api_key',
'value' => setting('text_api_key'),
],
];
}
/**
* Send SMS using the text API.
*
* @return array An array containing the parameters to send sms.
*/
public function sendSms($phone, $message)
{
$apiKey = setting('text_api_key');
$partnerID = setting('text_partner_id');
$shortcode = setting('text_shortcode');
// Build the URL for the API endpoint
$url = 'https://sms.textsms.co.ke/api/services/sendsms/';
// Validate required parameters
if (empty($apiKey) || !is_numeric($partnerID) || empty($phone) || empty($message)) {
return ['status' => 'error', 'message' => 'Missing or invalid parameters'];
}
// Create the request body
$requestBody = [
'apikey' => $apiKey,
'partnerID' => $partnerID,
'message' => rawurlencode($message), // URL encode the message
'shortcode' => $shortcode, // Replace with the appropriate sender ID
'mobile' => $phone,
];
// Initialize cURL session
$curl = curl_init($url . '?' . http_build_query($requestBody)); // Use GET method
// Set cURL options for GET request
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL request
$response = curl_exec($curl);
// Close the cURL session
curl_close($curl);
echo $response;
// Check for errors
if ($response === false) {
return ['status' => 'error', 'message' => 'cURL error: ' . curl_error($curl)];
} else {
$responseArray = json_decode($response, true);
if ($responseArray === null) {
return ['status' => 'error', 'message' => 'Could not parse API response'];
} else {
$responseCode = $responseArray['responses'][0]['response-code'];
if ($responseCode === 200) {
// Message sent successfully
return [
'status' => 'success',
'message' => 'Message sent successfully!',
'messageId' => $responseArray['responses'][0]['messageid']
];
} else {
// Handle the API error using the provided response description
return [
'status' => 'error',
'message' => $responseArray['responses'][0]['response-description'] ?? 'Unknown error'
];
}
}
}
}
/**
* Fetches account balance using the text API.
*
* @return array An array containing the balance information.
*/
public function textSmsBalance()
{
$apikey = setting('text_api_key');
$partnerID = setting('text_partner_id');
// Initialize a new cURL session
$curl = curl_init();
// Prepare the data to be sent
$curl_post_data = [
"partnerID" => $partnerID,
"apikey" => $apikey,
];
// Set the cURL options
curl_setopt_array($curl, [
CURLOPT_URL => 'https://sms.textsms.co.ke/api/services/getbalance/', // API URL for getting balance
CURLOPT_RETURNTRANSFER => true, // Return the result on success, FALSE on failure
CURLOPT_ENCODING => '', // No encoding
CURLOPT_MAXREDIRS => 10, // Maximum amount of HTTP redirections to follow
CURLOPT_TIMEOUT => 0, // No timeout
CURLOPT_FOLLOWLOCATION => true, // Follow any Location: headers sent by the server
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // Force HTTP 1.1
CURLOPT_CUSTOMREQUEST => 'POST', // Custom request method to use
CURLOPT_POSTFIELDS => json_encode($curl_post_data), // Data to post in HTTP "POST" operation
CURLOPT_HTTPHEADER => [
'Content-Type: application/json', // Set the content type of the request to JSON
],
]);
// Execute the cURL request and get the response
$response = curl_exec($curl);
// Check for any cURL errors
if (curl_errno($curl)) {
echo 'Error: ' . curl_error($curl);
}
// Close the cURL session
curl_close($curl);
// Convert the JSON response to an associative array
$balance = json_decode($response, true);
// Check if the balance is present in the response
if (isset($balance['credit'])) {
$credit_balance = $balance['credit'];
// Extract the balance value from the credit balance string
$value = floatval($credit_balance);
$currency = "KES"; // Currency code
return ['value' => $value, 'units' => $currency];
} else {
return null;
}
}
}