|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sarahman\SmsService; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use Illuminate\Support\Facades\Validator; |
|
10
|
|
|
use Sarahman\HttpRequestApiLog\Traits\WritesHttpLogs; |
|
11
|
|
|
use Sarahman\SmsService\Interfaces\NeedsAuthenticationInterface; |
|
12
|
|
|
use Sarahman\SmsService\Interfaces\ProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Client |
|
15
|
|
|
{ |
|
16
|
|
|
use WritesHttpLogs; |
|
17
|
|
|
|
|
18
|
|
|
const PROVIDER_BANGLALINK = Providers\Banglalink::class; |
|
19
|
|
|
const PROVIDER_BD_WEB_HOST_24 = Providers\BdWebHost24::class; |
|
20
|
|
|
const PROVIDER_BOOM_CAST = Providers\BoomCast::class; |
|
21
|
|
|
const PROVIDER_ELITBUZZ = Providers\Elitbuzz::class; |
|
22
|
|
|
const PROVIDER_GRAMEENPHONE = Providers\Grameenphone::class; |
|
23
|
|
|
const PROVIDER_NOVOCOM = Providers\Novocom::class; |
|
24
|
|
|
const PROVIDER_PAYSTATION = Providers\Paystation::class; |
|
25
|
|
|
const PROVIDER_ROBI = Providers\Robi::class; |
|
26
|
|
|
const PROVIDER_SSL = Providers\Ssl::class; |
|
27
|
|
|
const PROVIDER_SSL_PLUS = Providers\SslPlus::class; |
|
28
|
|
|
const PROVIDER_VALUE_FIRST = Providers\ValueFirst::class; |
|
29
|
|
|
|
|
30
|
|
|
private $provider; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(ProviderInterface $provider) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->provider = $provider; |
|
35
|
|
|
$this->enableLogging = Config::get('sms-service-with-bd-providers::config.enable_api_call_logging', false); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Return a SMS provider according to the given provider name. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $providerName |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
* @param string $url |
|
44
|
|
|
* |
|
45
|
|
|
* @return ProviderInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getProvider($providerName = self::PROVIDER_SSL, array $config = [], $url = null) |
|
48
|
|
|
{ |
|
49
|
|
|
switch ($providerName) { |
|
50
|
|
|
case self::PROVIDER_BANGLALINK: |
|
51
|
|
|
case self::PROVIDER_BD_WEB_HOST_24: |
|
52
|
|
|
case self::PROVIDER_BOOM_CAST: |
|
53
|
|
|
case self::PROVIDER_ELITBUZZ: |
|
54
|
|
|
case self::PROVIDER_GRAMEENPHONE: |
|
55
|
|
|
case self::PROVIDER_NOVOCOM: |
|
56
|
|
|
case self::PROVIDER_PAYSTATION: |
|
57
|
|
|
case self::PROVIDER_ROBI: |
|
58
|
|
|
case self::PROVIDER_SSL: |
|
59
|
|
|
case self::PROVIDER_SSL_PLUS: |
|
60
|
|
|
case self::PROVIDER_VALUE_FIRST: |
|
61
|
|
|
return new $providerName($config, $url); |
|
62
|
|
|
|
|
63
|
|
|
default: |
|
64
|
|
|
throw new Exception('Invalid SMS provider name is given.'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function send($recipients, $message, array $params = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$log = ['sent' => [], 'failed' => []]; |
|
71
|
|
|
is_array($recipients) || $recipients = [$recipients]; |
|
72
|
|
|
|
|
73
|
|
|
foreach ($recipients as $recipient) { |
|
74
|
|
|
$options = ['url' => $this->provider->getUrl()]; |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
if (!$data = $this->provider->mapParams($recipient, $message, $params)) { |
|
78
|
|
|
throw new Exception(json_encode('Failed to map the params.'), 422); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$data = array_merge($this->provider->getConfig(), $data); |
|
82
|
|
|
$validator = Validator::make($data, $this->provider->getValidationRules()); |
|
83
|
|
|
|
|
84
|
|
|
if ($validator->fails()) { |
|
85
|
|
|
throw new Exception(json_encode($validator->messages()->all()), 422); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$options += $this->prepareCurlOptions($data); |
|
89
|
|
|
$response = $this->provider->parseResponse($this->executeWithCurl($options)); |
|
90
|
|
|
|
|
91
|
|
|
if (!$response->getStatus()) { |
|
92
|
|
|
throw new Exception($response->getResponseString(), 500); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$log['sent'][$recipient] = $response->toArray(); |
|
96
|
|
|
|
|
97
|
|
|
$this->log('POST', $options['url'], $options, new GuzzleResponse(200, [], $response->getResponseString())); |
|
98
|
|
|
} catch (Exception $e) { |
|
99
|
|
|
$errorCode = $e->getCode() >= 100 ? $e->getCode() : 500; |
|
100
|
|
|
$errorMessage = 422 != $errorCode ? $e->getMessage() : json_decode($e->getMessage(), true); |
|
101
|
|
|
$log['failed'][$recipient] = (new Response(false, $errorMessage))->toArray(); |
|
102
|
|
|
|
|
103
|
|
|
$this->log('POST', $options['url'], $options, new GuzzleResponse($errorCode, [], $errorMessage)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->getSummaryWithLogs($log); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function sendWithFallback($recipients, $message, array $params = []) |
|
111
|
|
|
{ |
|
112
|
|
|
$log = ['sent' => [], 'failed' => []]; |
|
113
|
|
|
is_array($recipients) || $recipients = [$recipients]; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($recipients as $recipient) { |
|
116
|
|
|
$options = ['url' => $this->provider->getUrl()]; |
|
117
|
|
|
|
|
118
|
|
|
try { |
|
119
|
|
|
if (!$data = $this->provider->mapParams($recipient, $message, $params)) { |
|
120
|
|
|
throw new Exception(json_encode('Failed to map the params.'), 422); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$data = array_merge($this->provider->getConfig(), $data); |
|
124
|
|
|
$validator = Validator::make($data, $this->provider->getValidationRules()); |
|
125
|
|
|
|
|
126
|
|
|
if ($validator->fails()) { |
|
127
|
|
|
throw new Exception(json_encode($validator->messages()->all()), 422); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$options += $this->prepareCurlOptions($data); |
|
131
|
|
|
|
|
132
|
|
|
try { |
|
133
|
|
|
$response = $this->executeWithCurl($options); |
|
134
|
|
|
} catch (Exception $e) { |
|
135
|
|
|
$log['failed'][$recipient] = (new Response(false, $e->getMessage()))->toArray(); |
|
136
|
|
|
$response = ''; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$response = $this->provider->parseResponse($response); |
|
140
|
|
|
|
|
141
|
|
|
if (!$response->getStatus()) { |
|
142
|
|
|
$this->log('POST', $options['url'], $options, new GuzzleResponse(500, [], $response->getResponseString())); |
|
143
|
|
|
|
|
144
|
|
|
//Resend sms |
|
145
|
|
|
Log::info('SMS sending failed response!'); |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$response = $this->provider->parseResponse($this->executeWithCurl($options)); |
|
149
|
|
|
Log::info('Second try of sending SMS', $response); |
|
150
|
|
|
|
|
151
|
|
|
if (!$response->getStatus()) { |
|
152
|
|
|
throw new Exception($response->getResponseString(), 500); |
|
153
|
|
|
} |
|
154
|
|
|
} catch (Exception $e) { |
|
155
|
|
|
Log::error('Curl error response: '.$e->getMessage()); |
|
156
|
|
|
|
|
157
|
|
|
throw $e; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$log['sent'][$recipient] = $response->toArray(); |
|
162
|
|
|
|
|
163
|
|
|
$this->log('POST', $options['url'], $options, new GuzzleResponse(200, [], $response->getResponseString())); |
|
164
|
|
|
} catch (Exception $e) { |
|
165
|
|
|
$errorCode = $e->getCode() >= 100 ? $e->getCode() : 500; |
|
166
|
|
|
$errorMessage = 422 != $errorCode ? $e->getMessage() : json_decode($e->getMessage(), true); |
|
167
|
|
|
$log['failed'][$recipient] = (new Response(false, $errorMessage))->toArray(); |
|
168
|
|
|
|
|
169
|
|
|
$this->log('POST', $options['url'], $options, new GuzzleResponse($errorCode, [], $errorMessage)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this->getSummaryWithLogs($log); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Prepare the curl options array according to the given data. |
|
178
|
|
|
* |
|
179
|
|
|
* @param array $data |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
private function prepareCurlOptions(array $data) |
|
184
|
|
|
{ |
|
185
|
|
|
$options = [ |
|
186
|
|
|
'timeout' => 30, |
|
187
|
|
|
]; |
|
188
|
|
|
|
|
189
|
|
|
switch(get_class($this->provider)) { |
|
190
|
|
|
case self::PROVIDER_GRAMEENPHONE: |
|
191
|
|
|
case self::PROVIDER_NOVOCOM: |
|
192
|
|
|
$options += [ |
|
193
|
|
|
'httpheader' => ['Content-Type: application/json'], |
|
194
|
|
|
'post' => 1, |
|
195
|
|
|
'postfields' => json_encode($data), |
|
196
|
|
|
]; |
|
197
|
|
|
break; |
|
198
|
|
|
|
|
199
|
|
|
case self::PROVIDER_PAYSTATION: |
|
200
|
|
|
$options += [ |
|
201
|
|
|
'httpheader' => [ |
|
202
|
|
|
'Content-Type: application/json', |
|
203
|
|
|
'Accept: application/json', |
|
204
|
|
|
'user_id:'.$data['user_id'], |
|
205
|
|
|
'password:'.$data['password'], |
|
206
|
|
|
], |
|
207
|
|
|
'post' => 1, |
|
208
|
|
|
'postfields' => json_encode($data), |
|
209
|
|
|
]; |
|
210
|
|
|
break; |
|
211
|
|
|
|
|
212
|
|
|
case self::PROVIDER_SSL_PLUS: |
|
213
|
|
|
$encodedData = json_encode($data); |
|
214
|
|
|
$options += [ |
|
215
|
|
|
'httpheader' => [ |
|
216
|
|
|
'Content-Type: application/json', |
|
217
|
|
|
'Content-Length: ' . strlen($encodedData), |
|
218
|
|
|
'Accept: application/json', |
|
219
|
|
|
], |
|
220
|
|
|
'customrequest' => 'POST', |
|
221
|
|
|
'post' => 1, |
|
222
|
|
|
'postfields' => $encodedData, |
|
223
|
|
|
]; |
|
224
|
|
|
break; |
|
225
|
|
|
|
|
226
|
|
|
default: |
|
227
|
|
|
$options += [ |
|
228
|
|
|
'post' => count($data), |
|
229
|
|
|
'postfields' => http_build_query($data), |
|
230
|
|
|
]; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($this->provider instanceof NeedsAuthenticationInterface) { |
|
234
|
|
|
$options['httpheader'][] = 'Authorization: Bearer '.$this->provider->getAccessToken(); |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $options; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Execute the Curl request according to the given curl options. |
|
242
|
|
|
* |
|
243
|
|
|
* @param array $options |
|
244
|
|
|
* |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
private function executeWithCurl(array $options) |
|
248
|
|
|
{ |
|
249
|
|
|
$curlOptions = []; |
|
250
|
|
|
isset($options['returntransfer']) || $options['returntransfer'] = true; |
|
251
|
|
|
|
|
252
|
|
|
foreach ($options as $key => $value) { |
|
253
|
|
|
$option = 'CURLOPT_'.strtoupper($key); |
|
254
|
|
|
$curlOptions[constant($option)] = $value; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$ch = curl_init(); |
|
258
|
|
|
|
|
259
|
|
|
curl_setopt_array($ch, $curlOptions); |
|
260
|
|
|
|
|
261
|
|
|
$response = curl_exec($ch); |
|
262
|
|
|
|
|
263
|
|
|
if ($response != true) { |
|
264
|
|
|
$eMsg = 'cURL Error # '.curl_errno($ch).' | cURL Error Message: '.curl_error($ch); |
|
265
|
|
|
|
|
266
|
|
|
curl_close($ch); |
|
267
|
|
|
|
|
268
|
|
|
throw new Exception($eMsg); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
curl_close($ch); |
|
272
|
|
|
|
|
273
|
|
|
return (string) $response; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
private function getSummaryWithLogs(array $log) |
|
277
|
|
|
{ |
|
278
|
|
|
$sent = count($log['sent']); |
|
279
|
|
|
$failed = count($log['failed']); |
|
280
|
|
|
|
|
281
|
|
|
return [ |
|
282
|
|
|
'summary' => [ |
|
283
|
|
|
'sent' => $sent, |
|
284
|
|
|
'failed' => $failed, |
|
285
|
|
|
'total' => $sent + $failed, |
|
286
|
|
|
], |
|
287
|
|
|
'log' => $log, |
|
288
|
|
|
]; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|