1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Jaby\Sms\Drivers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Jaby\Sms\SmsInterface; |
8
|
|
|
|
9
|
|
|
class smsir implements SmsInterface |
10
|
|
|
{ |
11
|
|
|
protected $drive = 'smsir'; |
12
|
|
|
|
13
|
|
|
protected $method; |
14
|
|
|
|
15
|
|
|
protected $username; |
16
|
|
|
|
17
|
|
|
protected $password; |
18
|
|
|
|
19
|
|
|
protected $from; |
20
|
|
|
|
21
|
|
|
protected $pattern_code; |
22
|
|
|
|
23
|
|
|
protected $to; |
24
|
|
|
|
25
|
|
|
protected $input_data; |
26
|
|
|
|
27
|
|
|
protected $url; |
28
|
|
|
|
29
|
|
|
protected $numbers; |
30
|
|
|
|
31
|
|
|
protected $data; |
32
|
|
|
|
33
|
|
|
protected $text; |
34
|
|
|
|
35
|
|
|
protected $templateId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* farazsms constructor. |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->username = config('sms.drivers.' . $this->drive . '.username'); |
43
|
|
|
$this->password = config('sms.drivers.' . $this->drive . '.password'); |
44
|
|
|
$this->from = config('sms.drivers.' . $this->drive . '.from'); |
45
|
|
|
$this->url = config('sms.drivers.' . $this->drive . '.urlPattern'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool|mixed|string |
50
|
|
|
*/ |
51
|
|
|
public function send() |
52
|
|
|
{ |
53
|
|
|
if ($this->method == 'pattern') |
54
|
|
|
$res = $this->sendPattern(); |
55
|
|
|
else |
56
|
|
|
$res = $this->message($this->text); |
57
|
|
|
return $res; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $text |
62
|
|
|
* @return $this|mixed |
63
|
|
|
*/ |
64
|
|
|
public function text($text) |
65
|
|
|
{ |
66
|
|
|
$this->text = $text; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param null $pattern_code |
|
|
|
|
73
|
|
|
* @return $this|mixed |
74
|
|
|
*/ |
75
|
|
|
public function pattern($pattern_code = null) |
76
|
|
|
{ |
77
|
|
|
$this->method = 'pattern'; |
78
|
|
|
if ($pattern_code) |
|
|
|
|
79
|
|
|
$this->pattern_code = $pattern_code; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $data |
85
|
|
|
* @return $this|mixed |
86
|
|
|
*/ |
87
|
|
|
public function data(array $data) |
88
|
|
|
{ |
89
|
|
|
$this->data = $data; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $from |
96
|
|
|
* @return $this|mixed |
97
|
|
|
*/ |
98
|
|
|
public function from($from) |
99
|
|
|
{ |
100
|
|
|
$this->from = $from; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $numbers |
107
|
|
|
* @return $this|mixed |
108
|
|
|
*/ |
109
|
|
|
public function to(array $numbers) |
110
|
|
|
{ |
111
|
|
|
$this->numbers = $numbers; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int|null $templateId |
118
|
|
|
* @return $this|mixed |
119
|
|
|
*/ |
120
|
|
|
public function templateId($templateId = null) |
121
|
|
|
{ |
122
|
|
|
$this->templateId = $templateId; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return bool|mixed|string |
129
|
|
|
*/ |
130
|
|
|
public function sendPattern() |
131
|
|
|
{ |
132
|
|
|
$inputs = $this->setPatternExceptions(); |
133
|
|
|
$this->setOTP($inputs); |
134
|
|
|
|
135
|
|
|
$url = $this->url; |
136
|
|
|
$body = [ |
137
|
|
|
"mobile" => $inputs['mobile'][0], |
138
|
|
|
"templateId" => $inputs['templateId'], |
139
|
|
|
"parameters" => $inputs['parameters'] |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
$handler = curl_init($url); |
143
|
|
|
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST"); |
144
|
|
|
curl_setopt($handler, CURLOPT_POSTFIELDS, $body); |
145
|
|
|
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); |
146
|
|
|
$response = curl_exec($handler); |
147
|
|
|
$response2 = json_decode($response); |
|
|
|
|
148
|
|
|
|
149
|
|
|
return $response; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
private function setPatternExceptions() |
157
|
|
|
{ |
158
|
|
|
$parameters = $this->data; |
159
|
|
|
if (is_null($parameters)) |
160
|
|
|
throw new \Exception('The data must be set'); |
161
|
|
|
elseif (count($parameters) > 1) |
162
|
|
|
throw new \Exception('The data must have just one OTP code'); |
163
|
|
|
|
164
|
|
|
$mobile = $this->numbers; |
|
|
|
|
165
|
|
|
if ($mobile = []) |
166
|
|
|
throw new \Exception('The mobile number must be set'); |
167
|
|
|
if (count($mobile) > 1) |
168
|
|
|
throw new \Exception('The OTP code must send to just one mobile number'); |
169
|
|
|
|
170
|
|
|
$templateId = $this->templateId; |
171
|
|
|
if (is_null($templateId)) |
172
|
|
|
throw new \Exception('The templateId must be set'); |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
if (gettype($parameters[0]) != 'array') |
176
|
|
|
$checkParameter = $parameters[0]; |
177
|
|
|
else |
178
|
|
|
$checkParameter = $parameters; |
179
|
|
|
|
180
|
|
|
if (!isset($checkParameter['name'])) |
181
|
|
|
throw new \Exception('The `name` parameter not defined in data'); |
182
|
|
|
if (!isset($checkParameter['value'])) |
183
|
|
|
throw new \Exception('The `value` parameter not defined in data'); |
184
|
|
|
|
185
|
|
|
return compact('parameters', 'mobile', 'templateId'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $inputs |
190
|
|
|
*/ |
191
|
|
|
private function setOTP(&$inputs) |
192
|
|
|
{ |
193
|
|
|
$pattern_code = $this->pattern_code; |
194
|
|
|
if (is_null($pattern_code)) { |
195
|
|
|
if (is_null($inputs['parameters'][0]['value'])) { |
196
|
|
|
$inputs['parameters'][0]['value'] = rand(100000, 999999); |
197
|
|
|
} |
198
|
|
|
} else |
199
|
|
|
$inputs['parameters'][0]['value'] = $pattern_code; |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $text |
205
|
|
|
* @return mixed |
206
|
|
|
*/ |
207
|
|
|
public function message($text) |
208
|
|
|
{ |
209
|
|
|
$inputs = $this->setMessageExceptions(); |
210
|
|
|
|
211
|
|
|
$param = array |
212
|
|
|
( |
213
|
|
|
'lineNumber' => $this->from, |
214
|
|
|
'MessageText' => $text, |
215
|
|
|
'Mobiles' => json_encode($inputs['numbers']), |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$handler = curl_init($this->url); |
219
|
|
|
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST"); |
220
|
|
|
curl_setopt($handler, CURLOPT_POSTFIELDS, $param); |
221
|
|
|
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); |
222
|
|
|
curl_setopt($handler, CURLOPT_HTTPHEADER, array( |
223
|
|
|
"X-API-KEY: {$inputs['apiKey']}", |
224
|
|
|
)); |
225
|
|
|
$response = curl_exec($handler); |
226
|
|
|
$response2 = json_decode($response); |
|
|
|
|
227
|
|
|
|
228
|
|
|
return $response2; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private function setMessageExceptions() |
232
|
|
|
{ |
233
|
|
|
$apiKey = config('sms.drivers.' . $this->drive . '.apiKey'); |
234
|
|
|
if ($apiKey == '') |
235
|
|
|
throw new \Exception('The apiKey of SMS.ir muse be set in config'); |
236
|
|
|
|
237
|
|
|
$this->url = config('sms.drivers.' . $this->drive . '.urlNormal'); |
238
|
|
|
if ($this->url == '') |
239
|
|
|
throw new \Exception('The url of SMS.ir muse be set in config'); |
240
|
|
|
|
241
|
|
|
$numbers = $this->numbers; |
242
|
|
|
if (count($numbers) < 1) |
243
|
|
|
throw new \Exception('The numbers of mobiles must be set'); |
244
|
|
|
|
245
|
|
|
if ($this->from == '') |
246
|
|
|
throw new \Exception('The lineNumber of SMS.ir muse be set in config (set it in `from` key)'); |
247
|
|
|
|
248
|
|
|
return compact('apiKey', 'numbers'); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|