1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbuSalam; |
4
|
|
|
|
5
|
|
|
use AbuSalam\Contracts\SmsGatewayContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SMS Gateway Implementation |
9
|
|
|
*/ |
10
|
|
|
class SmsGateway implements SmsGatewayContract |
11
|
|
|
{ |
12
|
|
|
protected $recipients; |
13
|
|
|
|
14
|
|
|
protected $contents; |
15
|
|
|
|
16
|
|
|
protected $gateway; |
17
|
|
|
|
18
|
|
|
protected $apiEndpoint; |
19
|
|
|
|
20
|
|
|
protected $username; |
21
|
|
|
|
22
|
|
|
protected $senderid; |
23
|
|
|
|
24
|
|
|
protected $securekey; |
25
|
|
|
|
26
|
|
|
protected $tag; |
27
|
|
|
|
28
|
|
|
protected $payload; |
29
|
|
|
|
30
|
|
|
protected $response; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
return $this->setGateway() |
35
|
|
|
->setApiEndpoint() |
36
|
|
|
->setUsername() |
37
|
|
|
->setSenderid() |
38
|
|
|
->setSecurekey(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function sendSms() |
42
|
|
|
{ |
43
|
|
|
$post = curl_init(); |
44
|
|
|
//curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only |
45
|
|
|
//curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line |
46
|
|
|
curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0); |
47
|
|
|
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false); |
48
|
|
|
curl_setopt($post, CURLOPT_URL, $this->getApiEndpoint()); |
49
|
|
|
curl_setopt($post, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getSecurekey())); |
50
|
|
|
curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($this->getPayload())); |
51
|
|
|
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); |
52
|
|
|
$resp['data'] = curl_exec($post); |
|
|
|
|
53
|
|
|
if($resp['data'] === false) { |
54
|
|
|
$resp['error'] = curl_error($post); |
55
|
|
|
} |
56
|
|
|
curl_close($post); |
57
|
|
|
$this->setResponse($resp); |
58
|
|
|
return $this->getResponse(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function hashPayload() |
62
|
|
|
{ |
63
|
|
|
$payload = $this->buildConfigParams(); |
64
|
|
|
if (array_key_exists('key', $payload)) { |
65
|
|
|
$payload['key'] = hash('sha512', $this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
66
|
|
|
} |
67
|
|
|
//dump("data to hash => ".$this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
68
|
|
|
$this->setPayload($payload); |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
protected function buildConfigParams() |
74
|
|
|
{ |
75
|
|
|
$configParams = array_combine( |
76
|
|
|
config('smsgateway.' . config('smsgateway.default') . '.apiParams'), |
|
|
|
|
77
|
|
|
config('smsgateway.' . config('smsgateway.default') . '.apiValues') |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$form_params = [ |
81
|
|
|
config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(), |
82
|
|
|
config('smsgateway.' . config('smsgateway.default') . '.apiSmsParam') => $this->getContents(), |
83
|
|
|
config('smsgateway.' . config('smsgateway.default') . '.apiTagParam') => $this->getTag(), |
84
|
|
|
"smsservicetype" =>"singlemsg", |
85
|
|
|
]; |
86
|
|
|
$data = array_merge($form_params, $configParams); |
87
|
|
|
|
88
|
|
|
//dump($data); |
89
|
|
|
return $data; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the Message for the SMS |
94
|
|
|
* @param string |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function withSms($contents = '') |
98
|
|
|
{ |
99
|
|
|
$this->setContents($contents); |
100
|
|
|
$this->hashPayload(); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $templateId |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function withTemplateId($templateId = '') |
110
|
|
|
{ |
111
|
|
|
$this->senderid = $templateId == '' |
112
|
|
|
? config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId') |
|
|
|
|
113
|
|
|
: trim($templateId); |
114
|
|
|
//dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId')); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets the Tag for the SMS |
120
|
|
|
* @param mixed |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function withTag($tag) |
124
|
|
|
{ |
125
|
|
|
$this->setTag($tag); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function toRecipient($mobile = '') |
130
|
|
|
{ |
131
|
|
|
return $this->setRecipients($mobile); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function fromGateway($gateway = '') |
135
|
|
|
{ |
136
|
|
|
return $this->setGateway($gateway); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
protected function getRecipients() |
143
|
|
|
{ |
144
|
|
|
return $this->recipients; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $recipients |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
protected function setRecipients($recipients) |
153
|
|
|
{ |
154
|
|
|
$this->recipients = $recipients; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
protected function getContents() |
163
|
|
|
{ |
164
|
|
|
return $this->contents; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param mixed $contents |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
protected function setContents($contents) |
173
|
|
|
{ |
174
|
|
|
$this->contents = trim($contents); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
protected function getGateway() |
183
|
|
|
{ |
184
|
|
|
return $this->gateway; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param mixed $gateway |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
|
|
protected function setGateway($gateway = '') |
193
|
|
|
{ |
194
|
|
|
$this->gateway = $gateway == '' |
195
|
|
|
? config('smsgateway.default') |
|
|
|
|
196
|
|
|
: trim($gateway); |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
protected function getUsername() |
204
|
|
|
{ |
205
|
|
|
return $this->username; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param mixed $username |
210
|
|
|
* |
211
|
|
|
* @return self |
212
|
|
|
*/ |
213
|
|
|
protected function setUsername($username = '') |
214
|
|
|
{ |
215
|
|
|
$this->username = $username == '' |
216
|
|
|
? config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser') |
|
|
|
|
217
|
|
|
: trim($username); |
218
|
|
|
//dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser')); |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return mixed |
224
|
|
|
*/ |
225
|
|
|
protected function getSenderid() |
226
|
|
|
{ |
227
|
|
|
return $this->senderid; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param mixed $senderid |
232
|
|
|
* |
233
|
|
|
* @return self |
234
|
|
|
*/ |
235
|
|
|
protected function setSenderid($senderid = '') |
236
|
|
|
{ |
237
|
|
|
$this->senderid = $senderid == '' |
238
|
|
|
? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId') |
|
|
|
|
239
|
|
|
: trim($senderid); |
240
|
|
|
//dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')); |
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
protected function getSecurekey() |
248
|
|
|
{ |
249
|
|
|
return $this->securekey; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param mixed $securekey |
254
|
|
|
* |
255
|
|
|
* @return self |
256
|
|
|
*/ |
257
|
|
|
protected function setSecurekey($securekey = '') |
258
|
|
|
{ |
259
|
|
|
$this->securekey = $securekey == '' |
260
|
|
|
? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey') |
|
|
|
|
261
|
|
|
: trim($securekey); |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return mixed |
268
|
|
|
*/ |
269
|
|
|
public function getTag() |
270
|
|
|
{ |
271
|
|
|
return $this->tag; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param mixed $tag |
276
|
|
|
* |
277
|
|
|
* @return self |
278
|
|
|
*/ |
279
|
|
|
protected function setTag($tag) |
280
|
|
|
{ |
281
|
|
|
$this->tag = $tag; |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
|
|
public function getResponse() |
290
|
|
|
{ |
291
|
|
|
return json_encode($this->response, JSON_PRETTY_PRINT); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param mixed $response |
296
|
|
|
* |
297
|
|
|
* @return self |
298
|
|
|
*/ |
299
|
|
|
protected function setResponse($response) |
300
|
|
|
{ |
301
|
|
|
$this->response = $response; |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return mixed |
308
|
|
|
*/ |
309
|
|
|
protected function getPayload() |
310
|
|
|
{ |
311
|
|
|
return $this->payload; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param mixed $payload |
316
|
|
|
* |
317
|
|
|
* @return self |
318
|
|
|
*/ |
319
|
|
|
protected function setPayload($payload) |
320
|
|
|
{ |
321
|
|
|
$this->payload = $payload; |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return mixed |
328
|
|
|
*/ |
329
|
|
|
protected function getApiEndpoint() |
330
|
|
|
{ |
331
|
|
|
return $this->apiEndpoint; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param mixed $apiEndpoint |
336
|
|
|
* |
337
|
|
|
* @return self |
338
|
|
|
*/ |
339
|
|
|
protected function setApiEndpoint() |
340
|
|
|
{ |
341
|
|
|
$this->apiEndpoint = config('smsgateway.' . $this->getGateway() . '.apiEndpoint'); |
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function asOtpSms() |
346
|
|
|
{ |
347
|
|
|
$payload = $this->getPayload(); |
348
|
|
|
if (array_key_exists('smsservicetype', $payload)) { |
349
|
|
|
$payload['smsservicetype'] = 'otpmsg'; |
350
|
|
|
$this->setPayload($payload); |
351
|
|
|
} |
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function asUnicodeOtpSms() |
356
|
|
|
{ |
357
|
|
|
$payload = $this->getPayload(); |
358
|
|
|
if (array_key_exists('smsservicetype', $payload)) { |
359
|
|
|
$payload['smsservicetype'] = 'unicodeotpmsg'; |
360
|
|
|
$this->setPayload($payload); |
361
|
|
|
} |
362
|
|
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function asUnicodeSms() |
366
|
|
|
{ |
367
|
|
|
$payload = $this->getPayload(); |
368
|
|
|
if (array_key_exists('smsservicetype', $payload)) { |
369
|
|
|
$payload['smsservicetype'] = 'unicodemsg'; |
370
|
|
|
$this->setPayload($payload); |
371
|
|
|
} |
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
public function asBulkUnicodeSms() |
376
|
|
|
{ |
377
|
|
|
return $this->asUnicodeSms(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function asBulkSms() |
381
|
|
|
{ |
382
|
|
|
$payload = $this->getPayload(); |
383
|
|
|
if (array_key_exists('smsservicetype', $payload)) { |
384
|
|
|
$payload['smsservicetype'] = 'bulkmsg'; |
385
|
|
|
$this->setPayload($payload); |
386
|
|
|
} |
387
|
|
|
return $this; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|