|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Advanced Raja-SMS API PHP Library |
|
4
|
|
|
* |
|
5
|
|
|
* MIT License |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2018 Steeve Andrian Salim |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Steeve Andrian Salim |
|
28
|
|
|
* @copyright Copyright (c) 2018, Steeve Andrian Salim |
|
29
|
|
|
* @filesource |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
// ------------------------------------------------------------------------ |
|
33
|
|
|
|
|
34
|
|
|
namespace Steevenz; |
|
35
|
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------ |
|
37
|
|
|
|
|
38
|
|
|
use O2System\Curl; |
|
39
|
|
|
use O2System\Kernel\Http\Message\Uri; |
|
40
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
|
41
|
|
|
use O2System\Spl\Iterators\ArrayIterator; |
|
42
|
|
|
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
|
43
|
|
|
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* RajaSMS |
|
47
|
|
|
* |
|
48
|
|
|
* @version 1.0.0 |
|
49
|
|
|
* @author Steeven Andrian Salim |
|
50
|
|
|
*/ |
|
51
|
|
|
class Rajasms |
|
52
|
|
|
{ |
|
53
|
|
|
use ConfigCollectorTrait; |
|
54
|
|
|
use ErrorCollectorTrait; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Rajasms::$deliveryStatuses |
|
58
|
|
|
* |
|
59
|
|
|
* List of RajaSMS delivery status by code numbers. |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
public $deliveryStatusCodes = [ |
|
64
|
|
|
1 => 'Schedule', |
|
65
|
|
|
2 => 'Sent', |
|
66
|
|
|
3 => 'Delivered Success', |
|
67
|
|
|
4 => 'Delivered Error', |
|
68
|
|
|
5 => 'System Failed', |
|
69
|
|
|
6 => 'Saldo Minus/Expired', |
|
70
|
|
|
7 => 'Reject', |
|
71
|
|
|
8 => 'System Error', |
|
72
|
|
|
9 => 'Duplicate Message', |
|
73
|
|
|
10 => 'Delivered Success Backup', |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
public $globalErrorMessagesCodes = [ |
|
77
|
|
|
10 => 'Success', |
|
78
|
|
|
20 => 'Json Post Error', |
|
79
|
|
|
30 => 'ApiKey not register', |
|
80
|
|
|
40 => 'Ip address not register', |
|
81
|
|
|
50 => 'Expired Balance', |
|
82
|
|
|
55 => 'Maximum Data', |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Rajasms::$response |
|
87
|
|
|
* |
|
88
|
|
|
* RajaSMS original response. |
|
89
|
|
|
* |
|
90
|
|
|
* @access protected |
|
91
|
|
|
* @type mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $response; |
|
94
|
|
|
|
|
95
|
|
|
// ------------------------------------------------------------------------ |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Rajasms::__construct |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $config |
|
101
|
|
|
* |
|
102
|
|
|
* @access public |
|
103
|
|
|
*/ |
|
104
|
|
|
public function __construct(array $config = []) |
|
105
|
|
|
{ |
|
106
|
|
|
$defaultConfig = [ |
|
107
|
|
|
'serverIp' => null, |
|
108
|
|
|
'apiKey' => null, |
|
109
|
|
|
'callbackUrl' => null, |
|
110
|
|
|
'sendingTime' => null, |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
$this->setConfig(array_merge($defaultConfig, $config)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// ------------------------------------------------------------------------ |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Rajasms::setServerIp |
|
120
|
|
|
* |
|
121
|
|
|
* Set RajaSMS API Server Ip Address. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $serverIp RajaSMS API Server Ip Address. |
|
124
|
|
|
* |
|
125
|
|
|
* @access public |
|
126
|
|
|
* @return static |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setServerIp($serverIp) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->setConfig('serverIp', $serverIp); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// ------------------------------------------------------------------------ |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Rajasms::setApiKey |
|
139
|
|
|
* |
|
140
|
|
|
* Set RajaSMS API Key. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $apiKey RajaSMS API Key |
|
143
|
|
|
* |
|
144
|
|
|
* @access public |
|
145
|
|
|
* @return static |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setApiKey($apiKey) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->setConfig('apiKey', $apiKey); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// ------------------------------------------------------------------------ |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Rajasms::setCallbackUrl |
|
158
|
|
|
* |
|
159
|
|
|
* Set RajaSMS API Callback Url. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $callbackUrl RajaSMS API Callback Url |
|
162
|
|
|
* |
|
163
|
|
|
* @access public |
|
164
|
|
|
* @return static |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setCallbackUrl($callbackUrl) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->setConfig('callbackUrl', $callbackUrl); |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// ------------------------------------------------------------------------ |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Rajasms::setSendingTime |
|
177
|
|
|
* |
|
178
|
|
|
* Set RajaSMS Sending Time. |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $sendingTime Format: yyyy-mm-dd hh-mm-ss or empty |
|
181
|
|
|
* |
|
182
|
|
|
* @access public |
|
183
|
|
|
* @return static |
|
184
|
|
|
*/ |
|
185
|
|
|
public function setSendingTime($sendingTime) |
|
186
|
|
|
{ |
|
187
|
|
|
if (is_string($sendingTime)) { |
|
|
|
|
|
|
188
|
|
|
$sendingTime = strtotime($sendingTime); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$this->setConfig('sendingTime', date('Y-m-d H:m:s', $sendingTime)); |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// ------------------------------------------------------------------------ |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Rajasms::request |
|
200
|
|
|
* |
|
201
|
|
|
* Call API request. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $path |
|
204
|
|
|
* @param array $params |
|
205
|
|
|
* @param string $type |
|
206
|
|
|
* |
|
207
|
|
|
* @access protected |
|
208
|
|
|
* @return mixed |
|
209
|
|
|
* @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function request($path, $params = [], $type = 'GET') |
|
212
|
|
|
{ |
|
213
|
|
|
$path = 'sms/' . $path; |
|
214
|
|
|
|
|
215
|
|
|
// default params |
|
216
|
|
|
if (empty($this->config[ 'serverIp' ])) { |
|
217
|
|
|
throw new \InvalidArgumentException('RajaSMS: Server Ip Address is not set!'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (empty($this->config[ 'apiKey' ])) { |
|
221
|
|
|
throw new \InvalidArgumentException('RajaSMS: API Key is not set'); |
|
222
|
|
|
} else { |
|
223
|
|
|
$params[ 'apikey' ] = $this->config[ 'apiKey' ]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ( ! empty($this->config[ 'callbackUrl' ]) and isset($params[ 'datapacket' ])) { |
|
227
|
|
|
$params[ 'callbackurl' ] = $this->config[ 'callbackUrl' ]; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$uri = (new Uri($this->config[ 'serverIp' ]))->withPath($path); |
|
231
|
|
|
|
|
232
|
|
|
$request = new Curl\Request(); |
|
233
|
|
|
|
|
234
|
|
|
$request->setConnectionTimeout(500); |
|
235
|
|
|
|
|
236
|
|
|
switch ($type) { |
|
237
|
|
|
default: |
|
238
|
|
|
case 'GET': |
|
239
|
|
|
$this->response = $request->setUri($uri)->get($params); |
|
240
|
|
|
break; |
|
241
|
|
|
|
|
242
|
|
|
case 'POST': |
|
243
|
|
|
$request->addHeader('content-type', 'application/json'); |
|
244
|
|
|
$this->response = $request->setUri($uri)->post($params, 'JSON'); |
|
|
|
|
|
|
245
|
|
|
break; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
if (false !== ($error = $this->response->getError())) { |
|
|
|
|
|
|
249
|
|
|
$this->addError($error->code, $error->message); |
|
250
|
|
|
} elseif ($body = $this->response->getBody()) { |
|
251
|
|
|
return $body; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return false; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
// ------------------------------------------------------------------------ |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Rajasms::send |
|
261
|
|
|
* |
|
262
|
|
|
* Send SMS |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $msisdn MSISDN Number |
|
265
|
|
|
* @param string $message SMS Text |
|
266
|
|
|
* @param bool $masking Use SMS Masking |
|
267
|
|
|
* |
|
268
|
|
|
* @access public |
|
269
|
|
|
* @return mixed |
|
270
|
|
|
* @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
|
271
|
|
|
*/ |
|
272
|
|
|
public function send($msisdn, $message, $masking = false) |
|
273
|
|
|
{ |
|
274
|
|
|
$params[ 'datapacket' ] = []; |
|
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
if (is_array($msisdn)) { |
|
|
|
|
|
|
277
|
|
|
$i = 1; |
|
278
|
|
|
foreach ($msisdn as $number) { |
|
279
|
|
|
if (false !== ($sendPackageData = $this->buildSendPackageData([ |
|
280
|
|
|
'msisdn' => $number, |
|
281
|
|
|
'message' => $message, |
|
282
|
|
|
'sendingTime' => isset($this->config[ 'sendingTime' ]) ? $this->config[ 'sendingTime' ] : null, |
|
283
|
|
|
]))) { |
|
284
|
|
|
array_push($params[ 'datapacket' ], $sendPackageData); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
if ($i === 1000) { |
|
288
|
|
|
break; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
$i++; |
|
292
|
|
|
} |
|
293
|
|
|
} elseif (false !== ($sendPackageData = $this->buildSendPackageData([ |
|
|
|
|
|
|
294
|
|
|
'msisdn' => $msisdn, |
|
295
|
|
|
'message' => $message, |
|
296
|
|
|
'sendingTime' => isset($this->config[ 'sendingTime' ]) ? $this->config[ 'sendingTime' ] : null, |
|
297
|
|
|
]))) { |
|
298
|
|
|
array_push($params[ 'datapacket' ], $sendPackageData); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$credit = $this->getCreditBalance($masking); |
|
302
|
|
|
|
|
303
|
|
|
if (($credit->balance > 500) and (strtotime($credit->expired) > time()) and count($params[ 'datapacket' ]) > 0) { |
|
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
if ($masking === true) { |
|
306
|
|
|
$result = $this->request('api_sms_masking_send_json.php', $params, 'POST'); |
|
307
|
|
|
} else { |
|
308
|
|
|
$result = $this->request('api_sms_reguler_send_json.php', $params, 'POST'); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
if (isset($result[ 'sending_respon' ])) { |
|
312
|
|
|
if (isset($result[ 'sending_respon' ][ 0 ])) { |
|
313
|
|
|
if ($result[ 'sending_respon' ][ 0 ][ 'globalstatus' ] > 10) { |
|
314
|
|
|
$this->addError($result[ 'sending_respon' ][ 0 ][ 'globalstatus' ], |
|
315
|
|
|
$result[ 'sending_respon' ][ 0 ][ 'globalstatustext' ]); |
|
316
|
|
|
|
|
317
|
|
|
return false; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
if (isset($result[ 'sending_respon' ][ 0 ][ 'datapacket' ])) { |
|
322
|
|
|
$reports = new ArrayIterator(); |
|
323
|
|
|
|
|
324
|
|
|
foreach ($result[ 'sending_respon' ][ 0 ][ 'datapacket' ] as $respon) { |
|
325
|
|
|
$reports = new SplArrayObject([ |
|
326
|
|
|
'sendingId' => $respon[ 'sendingid' ], |
|
327
|
|
|
'number' => $respon[ 'number' ], |
|
328
|
|
|
'sending' => new SplArrayObject([ |
|
329
|
|
|
'status' => $respon[ 'sendingstatus' ], |
|
330
|
|
|
'message' => $respon[ 'sendingstatustext' ], |
|
331
|
|
|
]), |
|
332
|
|
|
'price' => $respon[ 'price' ], |
|
333
|
|
|
]); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return $reports; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
return $result; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
// ------------------------------------------------------------------------ |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Rajasms::buildSendPackageData |
|
348
|
|
|
* |
|
349
|
|
|
* @param array $data |
|
350
|
|
|
* |
|
351
|
|
|
* @return array|bool |
|
352
|
|
|
*/ |
|
353
|
|
|
protected function buildSendPackageData(array $data) |
|
354
|
|
|
{ |
|
355
|
|
|
if (preg_match('/^(62[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $data[ 'msisdn' ]) == 1) { |
|
356
|
|
|
$data[ 'msisdn' ] = '0' . substr($data[ 'msisdn' ], 2); |
|
357
|
|
|
} elseif (preg_match('/^(\+62[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $data[ 'msisdn' ]) == 1) { |
|
358
|
|
|
$data[ 'msisdn' ] = '0' . substr($data[ 'msisdn' ], 3); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
if (preg_match('/^(0[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $data[ 'msisdn' ]) == 1) { |
|
362
|
|
|
return [ |
|
363
|
|
|
'number' => trim($data[ 'msisdn' ]), |
|
364
|
|
|
'message' => urlencode(stripslashes(utf8_encode($data[ 'message' ]))), |
|
365
|
|
|
'sendingdatetime' => isset($data[ 'sendingTime' ]) ? $data[ 'sendingTime' ] : null, |
|
366
|
|
|
]; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
return false; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
// ------------------------------------------------------------------------ |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Rajasms::getCreditBalance |
|
376
|
|
|
* |
|
377
|
|
|
* Get RajaSMS account credit balance. |
|
378
|
|
|
* |
|
379
|
|
|
* @access public |
|
380
|
|
|
* @return mixed |
|
381
|
|
|
* @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
|
382
|
|
|
*/ |
|
383
|
|
|
public function getCreditBalance($masking = false) |
|
384
|
|
|
{ |
|
385
|
|
|
if ($masking === true) { |
|
386
|
|
|
$result = $this->request('api_sms_masking_balance_json.php', [], 'POST'); |
|
387
|
|
|
} else { |
|
388
|
|
|
$result = $this->request('api_sms_reguler_balance_json.php', [], 'POST'); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
if (isset($result[ 'balance_respon' ])) { |
|
392
|
|
|
if (isset($result[ 'balance_respon' ][ 0 ][ 'globalstatus' ])) { |
|
393
|
|
|
if ($result[ 'balance_respon' ][ 0 ][ 'globalstatus' ] > 10) { |
|
394
|
|
|
$this->addError($result[ 'balance_respon' ][ 0 ][ 'globalstatus' ], |
|
395
|
|
|
$result[ 'balance_respon' ][ 0 ][ 'globalstatustext' ]); |
|
396
|
|
|
|
|
397
|
|
|
return false; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
$credit = new SplArrayObject([ |
|
401
|
|
|
'balance' => $result[ 'balance_respon' ][ 0 ][ 'Balance' ], |
|
402
|
|
|
'expired' => $result[ 'balance_respon' ][ 0 ][ 'Expired' ], |
|
403
|
|
|
]); |
|
404
|
|
|
|
|
405
|
|
|
$result = $credit; |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
return $result; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
// ------------------------------------------------------------------------ |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Rajasms::getReports |
|
416
|
|
|
* |
|
417
|
|
|
* Get sms report delivery status. |
|
418
|
|
|
* |
|
419
|
|
|
* @return mixed Returns FALSE if failed. |
|
420
|
|
|
*/ |
|
421
|
|
|
public function getReports() |
|
422
|
|
|
{ |
|
423
|
|
|
$response = json_decode(file_get_contents('php://input'), true); |
|
424
|
|
|
|
|
425
|
|
|
if ( ! empty($response)) { |
|
426
|
|
|
$reports = new ArrayIterator(); |
|
427
|
|
|
|
|
428
|
|
|
foreach ($response[ 'status_respon' ] as $respon) { |
|
429
|
|
|
$reports[] = new SplArrayObject([ |
|
430
|
|
|
'sendingId' => $respon[ 'sendingid' ], |
|
431
|
|
|
'number' => $respon[ 'number' ], |
|
432
|
|
|
'delivery' => new SplArrayObject([ |
|
433
|
|
|
'status' => $respon[ 'deliverystatus' ], |
|
434
|
|
|
'message' => $respon[ 'deliverystatustext' ], |
|
435
|
|
|
]), |
|
436
|
|
|
]); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
return $reports; |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
return false; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
// ------------------------------------------------------------------------ |
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Rajasms::getResponse |
|
449
|
|
|
* |
|
450
|
|
|
* Get original response object. |
|
451
|
|
|
* |
|
452
|
|
|
* @access public |
|
453
|
|
|
* @return \O2System\Curl\Response|bool Returns FALSE if failed. |
|
454
|
|
|
*/ |
|
455
|
|
|
public function getResponse() |
|
456
|
|
|
{ |
|
457
|
|
|
return $this->response; |
|
458
|
|
|
} |
|
459
|
|
|
} |