|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Muhideen Mujeeb Adeoye <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Mujhtech\SendChamp; |
|
10
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Client; |
|
12
|
|
|
use Illuminate\Support\Facades\Config; |
|
13
|
|
|
use Mujhtech\SendChamp\Exceptions\SendChampException; |
|
14
|
|
|
|
|
15
|
|
|
class SendChamp |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $useCase = ['Transactional', 'Marketing', 'Transactional & Marketing']; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $channel = ['voice', 'sms', 'whatsapp', 'email']; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $tokenType = ['numeric', 'alphanumeric']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $smsRoute = ['non_dnd', 'dnd', 'international', 'PREMIUM_NG']; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $publicKey; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $client; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Response from sendchamp api |
|
49
|
|
|
* |
|
50
|
|
|
* @var mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $response; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $baseUrl; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->getKey(); |
|
62
|
|
|
$this->getBaseUrl(); |
|
63
|
|
|
$this->setRequestOptions(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get base url from sendchamp config |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getBaseUrl() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->baseUrl = Config::get('sendchamp.mode') == 'live' ? Config::get('sendchamp.baseUrl') : Config::get('sendchamp.sandboxUrl'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get public key from sendchamp cofig |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getKey() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->publicKey = Config::get('sendchamp.publicKey'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set request options |
|
84
|
|
|
* |
|
85
|
|
|
* @return SendChamp |
|
86
|
|
|
*/ |
|
87
|
|
|
private function setRequestOptions() |
|
88
|
|
|
{ |
|
89
|
|
|
$authBearer = 'Bearer '.$this->publicKey; |
|
90
|
|
|
|
|
91
|
|
|
$this->client = new Client( |
|
|
|
|
|
|
92
|
|
|
[ |
|
93
|
|
|
'base_uri' => $this->baseUrl, |
|
94
|
|
|
'headers' => [ |
|
95
|
|
|
'Authorization' => $authBearer, |
|
96
|
|
|
'Content-Type' => 'application/json', |
|
97
|
|
|
'Accept' => 'application/json', |
|
98
|
|
|
], |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set http response |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $url |
|
109
|
|
|
* @param null $method |
|
|
|
|
|
|
110
|
|
|
* @param array $body |
|
111
|
|
|
* @return SendChamp |
|
112
|
|
|
* |
|
113
|
|
|
* @throws SendChampException |
|
114
|
|
|
*/ |
|
115
|
|
|
private function setHttpResponse(string $url, $method = null, array $body = []) |
|
116
|
|
|
{ |
|
117
|
|
|
if (is_null($method)) { |
|
|
|
|
|
|
118
|
|
|
throw new SendChampException('Empty method not allowed'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->response = $this->client->{strtolower($method)}( |
|
122
|
|
|
$this->baseUrl.$url, |
|
123
|
|
|
['body' => json_encode($body)] |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Decode json response into an array |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getResponse() |
|
135
|
|
|
{ |
|
136
|
|
|
return json_decode($this->response->getBody(), true); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Create sms sender |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $send_name |
|
143
|
|
|
* @param string $use_case |
|
144
|
|
|
* You should pass either of the following: Transactional, Marketing, or Transactional & Marketing |
|
145
|
|
|
* @param string $sample_message |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
public function createSmsSender(string $sender_name, string $use_case, |
|
149
|
|
|
string $sample_message) |
|
150
|
|
|
{ |
|
151
|
|
|
if (! in_array($use_case, $this->useCase)) { |
|
152
|
|
|
throw new SendChampException('Invalid Use case'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$data = [ |
|
156
|
|
|
'sample' => $sample_message, |
|
157
|
|
|
'use_case' => $use_case, |
|
158
|
|
|
'sender_name' => $sender_name, |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
return $this->setRequestOptions()->setHttpResponse('/sms/sender/create', 'POST', $data)->getResponse(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Send sms |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $message |
|
168
|
|
|
* @param string $sender_name |
|
169
|
|
|
* Represents the sender of the message. |
|
170
|
|
|
* This Sender ID must have been requested |
|
171
|
|
|
* via the dashboard or use "Sendchamp" as default |
|
172
|
|
|
* @param array $numbers |
|
173
|
|
|
* This represents the destination phone number. |
|
174
|
|
|
* The phone number(s) must be in the international format |
|
175
|
|
|
* (Example: 23490126727). You can also send to multiple numbers. |
|
176
|
|
|
* To do that put numbers in an array |
|
177
|
|
|
* (Example: [ '234somenumber', '234anothenumber' ]). |
|
178
|
|
|
* @param string $route e.g ['non_dnd', 'dnd', 'international'] |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
|
|
public function sendSms(string $message, string $sender_name, array $numbers, string $route = '') |
|
182
|
|
|
{ |
|
183
|
|
|
if (! empty($route) && ! in_array($route, $this->smsRoute)) { |
|
184
|
|
|
throw new SendChampException('Invalid sms route'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$data = [ |
|
188
|
|
|
'to' => $numbers, |
|
189
|
|
|
'message' => $message, |
|
190
|
|
|
'sender_name' => $sender_name, |
|
191
|
|
|
'route' => $route, |
|
192
|
|
|
]; |
|
193
|
|
|
|
|
194
|
|
|
return $this->setRequestOptions()->setHttpResponse('/sms/send', 'POST', $data)->getResponse(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get sms status |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $sms_id |
|
201
|
|
|
* ID of the SMS that was sent |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
|
|
public function fetchSmsStatus(string $sms_id) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->setRequestOptions()->setHttpResponse('/sms/'.$sms_id.'/report', 'GET', [])->getResponse(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Send voice |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $message |
|
213
|
|
|
* @param string $sender_name |
|
214
|
|
|
* Represents the sender of the message. |
|
215
|
|
|
* This Sender ID must have been requested via |
|
216
|
|
|
* the dashboard or use "Sendchamp" as default |
|
217
|
|
|
* @param string $number |
|
218
|
|
|
* The number represents the destination phone number. |
|
219
|
|
|
* The number must be in international format (E.g. 2348012345678) |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
public function sendVoice(string $message, string $sender_name, string $number) |
|
223
|
|
|
{ |
|
224
|
|
|
$data = [ |
|
225
|
|
|
'customer_mobile_number' => $number, |
|
226
|
|
|
'message' => $message, |
|
227
|
|
|
'sender_name' => $sender_name, |
|
228
|
|
|
]; |
|
229
|
|
|
|
|
230
|
|
|
return $this->setRequestOptions()->setHttpResponse('/voice/send', 'POST', $data)->getResponse(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Send whatsapp otp |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $template_code |
|
237
|
|
|
* You can find this on the template page under Whatsapp Channel of your Sendchamp dashboard |
|
238
|
|
|
* @param string $sender_number |
|
239
|
|
|
* Your approved Whatsapp number on Sendchamp. |
|
240
|
|
|
* You can use our phone number if you have not registered a number 2347067959173 |
|
241
|
|
|
* @param string $recipient |
|
242
|
|
|
* Whatsapp number of the customer you are sending the message to |
|
243
|
|
|
* @param string $message |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
|
|
public function sendWhatsappOtp(string $template_code, string $message, |
|
247
|
|
|
string $sender_number, string $recipient) |
|
248
|
|
|
{ |
|
249
|
|
|
$data = [ |
|
250
|
|
|
'recipient' => $recipient, |
|
251
|
|
|
'template_code' => $template_code, |
|
252
|
|
|
'message' => $message, |
|
253
|
|
|
'sender' => $sender_number, |
|
254
|
|
|
]; |
|
255
|
|
|
|
|
256
|
|
|
return $this->setRequestOptions()->setHttpResponse('/whatsapp/template/send', 'POST', $data)->getResponse(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Send otp message |
|
261
|
|
|
* |
|
262
|
|
|
* @param string $channel |
|
263
|
|
|
* @param string $token_type |
|
264
|
|
|
* @param int $token_length |
|
265
|
|
|
* The length of the token you want to send to your customer. Minimum is 4 |
|
266
|
|
|
* @param int $expiry_day |
|
267
|
|
|
* How long you want to the to be active for in minutes. (E.g 10 means 10 minutes ) |
|
268
|
|
|
* @param string $customer_email |
|
269
|
|
|
* @param string $customer_mobile_number |
|
270
|
|
|
* @param array $meta_data |
|
271
|
|
|
* @param string $sender |
|
272
|
|
|
* Specify the sender you want to use. This is important |
|
273
|
|
|
* when using SMS OR Whatsapp Channel or we will select a |
|
274
|
|
|
* default sender from your account. Eg: KUDA OR +234810000000 |
|
275
|
|
|
* @return array |
|
276
|
|
|
*/ |
|
277
|
|
|
public function sendOtp(string $channel, string $token_type, int $token_length, |
|
278
|
|
|
int $expiry_day, string $customer_email, string $customer_mobile_number, array $meta_data, string $sender) |
|
279
|
|
|
{ |
|
280
|
|
|
if (! in_array($token_type, $this->tokenType)) { |
|
281
|
|
|
throw new SendChampException('Invalid token type'); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
if (! in_array($channel, $this->channel)) { |
|
285
|
|
|
throw new SendChampException('Invalid channel'); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
$data = [ |
|
289
|
|
|
'channel' => $channel, |
|
290
|
|
|
'token_type' => $token_type, |
|
291
|
|
|
'token_length' => $token_length, |
|
292
|
|
|
'expiration_time' => $expiry_day, |
|
293
|
|
|
'customer_email' => $customer_email, |
|
294
|
|
|
'customer_mobile_number' => $customer_mobile_number, |
|
295
|
|
|
'meta_data' => $meta_data, |
|
296
|
|
|
'sender' => $sender, |
|
297
|
|
|
]; |
|
298
|
|
|
|
|
299
|
|
|
return $this->setRequestOptions()->setHttpResponse('/verification/create', 'POST', $data)->getResponse(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Confirm otp |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $reference |
|
306
|
|
|
* The unique reference that was returned as response when the OTP was created |
|
307
|
|
|
* @param string $otp |
|
308
|
|
|
* The OTP that was sent to the customer. |
|
309
|
|
|
* @return array |
|
310
|
|
|
*/ |
|
311
|
|
|
public function confirmOtp(string $reference, string $otp) |
|
312
|
|
|
{ |
|
313
|
|
|
$data = [ |
|
314
|
|
|
'verification_reference' => $reference, |
|
315
|
|
|
'verification_code' => $otp, |
|
316
|
|
|
]; |
|
317
|
|
|
|
|
318
|
|
|
return $this->setRequestOptions()->setHttpResponse('/verification/confirm', 'POST', $data)->getResponse(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Get contacts list |
|
323
|
|
|
* |
|
324
|
|
|
* @return array |
|
325
|
|
|
*/ |
|
326
|
|
|
public function getContactList() |
|
327
|
|
|
{ |
|
328
|
|
|
return $this->setRequestOptions()->setHttpResponse('/contacts', 'GET', [])->getResponse(); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Create or update contact |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $lastname |
|
335
|
|
|
* @param string $firstname |
|
336
|
|
|
* @param string $phone |
|
337
|
|
|
* @param string $email |
|
338
|
|
|
* @param string $reference |
|
339
|
|
|
* @return array |
|
340
|
|
|
*/ |
|
341
|
|
|
public function createOrUpdateContact(string $lastname, string $firstname, string $phone, string $email, string $reference) |
|
342
|
|
|
{ |
|
343
|
|
|
$data = [ |
|
344
|
|
|
|
|
345
|
|
|
'last_name' => $lastname, |
|
346
|
|
|
'first_name' => $firstname, |
|
347
|
|
|
'phone_number' => $phone, |
|
348
|
|
|
'email' => $email, |
|
349
|
|
|
'external_user_id' => $reference, |
|
350
|
|
|
|
|
351
|
|
|
]; |
|
352
|
|
|
|
|
353
|
|
|
return $this->setRequestOptions()->setHttpResponse('/contacts', 'POST', $data)->getResponse(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Delete contact |
|
358
|
|
|
* |
|
359
|
|
|
* @param string $id |
|
360
|
|
|
* @return array |
|
361
|
|
|
*/ |
|
362
|
|
|
public function deleteContact(string $id) |
|
363
|
|
|
{ |
|
364
|
|
|
return $this->setRequestOptions()->setHttpResponse('/contact/'.$id.'/delete', 'POST', [])->getResponse(); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Get wallet report |
|
369
|
|
|
* |
|
370
|
|
|
* @return array |
|
371
|
|
|
*/ |
|
372
|
|
|
public function getWalletReport() |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->setRequestOptions()->setHttpResponse('/report/wallet/list', 'GET', [])->getResponse(); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..