|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaby\Sms\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Jaby\Sms\SmsInterface; |
|
6
|
|
|
|
|
7
|
|
|
class melipayamak implements SmsInterface |
|
8
|
|
|
{ |
|
9
|
|
|
protected $drive = 'melipayamak'; |
|
10
|
|
|
|
|
11
|
|
|
protected $method; |
|
12
|
|
|
|
|
13
|
|
|
protected $username; |
|
14
|
|
|
|
|
15
|
|
|
protected $password; |
|
16
|
|
|
|
|
17
|
|
|
protected $from; |
|
18
|
|
|
|
|
19
|
|
|
protected $pattern_code; |
|
20
|
|
|
|
|
21
|
|
|
protected $to; |
|
22
|
|
|
|
|
23
|
|
|
protected $numbers; |
|
24
|
|
|
|
|
25
|
|
|
protected $data; |
|
26
|
|
|
|
|
27
|
|
|
protected $text; |
|
28
|
|
|
|
|
29
|
|
|
protected $templateId; |
|
30
|
|
|
protected $urlٔNormal; |
|
31
|
|
|
protected $urlPattern; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->username = config('sms.drivers.' . $this->drive . '.username'); |
|
36
|
|
|
$this->password = config('sms.drivers.' . $this->drive . '.password'); |
|
37
|
|
|
$this->from = config('sms.drivers.' . $this->drive . '.from'); |
|
38
|
|
|
$this->urlٔNormal = config('sms.drivers.'.$this->drive.'.urlNormal'); |
|
39
|
|
|
$this->urlPattern = config('sms.drivers.'.$this->drive.'.urlPattern'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function send() |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->method === 'pattern') { |
|
45
|
|
|
return $this->sendPattern(); |
|
46
|
|
|
} |
|
47
|
|
|
return $this->message($this->text); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function text($text) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->text = $text; |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function pattern($pattern_code = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->method = 'pattern'; |
|
59
|
|
|
if ($pattern_code) |
|
60
|
|
|
$this->pattern_code = $pattern_code; |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function data(array $data) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->data = $data; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function from($from) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->from = $from; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function to(array $numbers) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->numbers = $numbers; |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function templateId($templateId = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->templateId = $templateId; |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function sendPattern() |
|
89
|
|
|
{ |
|
90
|
|
|
$inputs = $this->setPatternExceptions(); |
|
91
|
|
|
$this->setOTP($inputs); |
|
92
|
|
|
|
|
93
|
|
|
$mobile = $inputs['mobile'][0]; |
|
94
|
|
|
$bodyId = $inputs['templateId']; |
|
95
|
|
|
$parameters = $inputs['parameters']; |
|
96
|
|
|
|
|
97
|
|
|
$values = []; |
|
98
|
|
|
foreach ($parameters as $param) { |
|
99
|
|
|
$values[] = $param['value']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$text = implode(';', $values); |
|
103
|
|
|
|
|
104
|
|
|
$data = [ |
|
105
|
|
|
'username' => $this->username, |
|
106
|
|
|
'password' => $this->password, |
|
107
|
|
|
'to' => $mobile, |
|
108
|
|
|
'bodyId' => $bodyId, |
|
109
|
|
|
'text' => $text |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
|
|
return $this->post($this->urlPattern, $data); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function message($text) |
|
116
|
|
|
{ |
|
117
|
|
|
$inputs = $this->setMessageExceptions(); |
|
118
|
|
|
|
|
119
|
|
|
$data = [ |
|
120
|
|
|
'username' => $this->username, |
|
121
|
|
|
'password' => $this->password, |
|
122
|
|
|
'from' => $this->from, |
|
123
|
|
|
'to' => implode(',', $inputs['numbers']), |
|
124
|
|
|
'text' => $text |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
return $this->post($this->urlٔNormal, $data); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function setPatternExceptions() |
|
131
|
|
|
{ |
|
132
|
|
|
$parameters = $this->data; |
|
133
|
|
|
$this->validateData($parameters); |
|
134
|
|
|
|
|
135
|
|
|
$mobile = $this->numbers; |
|
136
|
|
|
$this->validateMobile($mobile); |
|
137
|
|
|
|
|
138
|
|
|
$templateId = $this->templateId; |
|
139
|
|
|
$this->validateTemplateId($templateId); |
|
140
|
|
|
|
|
141
|
|
|
$checkParameter = (gettype($parameters[0]) != 'array') ? $parameters[0] : $parameters; |
|
142
|
|
|
$this->validateParameterStructure($checkParameter); |
|
143
|
|
|
|
|
144
|
|
|
return compact('parameters', 'mobile', 'templateId'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function validateData($parameters) |
|
148
|
|
|
{ |
|
149
|
|
|
if (is_null($parameters)) |
|
150
|
|
|
throw new \Exception('The data must be set'); |
|
151
|
|
|
if (count($parameters) < 1) |
|
152
|
|
|
throw new \Exception('The data must have at least one item'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function validateMobile($mobile) |
|
156
|
|
|
{ |
|
157
|
|
|
if (empty($mobile)) |
|
158
|
|
|
throw new \Exception('The mobile number must be set'); |
|
159
|
|
|
if (count($mobile) > 1) |
|
160
|
|
|
throw new \Exception('Only one number is allowed for OTP'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function validateTemplateId($templateId) |
|
164
|
|
|
{ |
|
165
|
|
|
if (is_null($templateId)) |
|
166
|
|
|
throw new \Exception('The templateId must be set'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
private function validateParameterStructure($param) |
|
170
|
|
|
{ |
|
171
|
|
|
if (!isset($param['name'])) |
|
172
|
|
|
throw new \Exception('The `name` parameter not defined in data'); |
|
173
|
|
|
if (!isset($param['value'])) |
|
174
|
|
|
throw new \Exception('The `value` parameter not defined in data'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
private function setOTP(&$inputs) |
|
178
|
|
|
{ |
|
179
|
|
|
if (is_null($this->pattern_code)) { |
|
180
|
|
|
if (is_null($inputs['parameters'][0]['value'])) { |
|
181
|
|
|
$inputs['parameters'][0]['value'] = rand(100000, 999999); |
|
182
|
|
|
} |
|
183
|
|
|
} else { |
|
184
|
|
|
$inputs['parameters'][0]['value'] = $this->pattern_code; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
private function setMessageExceptions() |
|
189
|
|
|
{ |
|
190
|
|
|
$numbers = $this->numbers; |
|
191
|
|
|
if (empty($numbers)) |
|
192
|
|
|
throw new \Exception('The numbers of mobiles must be set'); |
|
193
|
|
|
|
|
194
|
|
|
if (empty($this->from)) |
|
195
|
|
|
throw new \Exception('The lineNumber must be set in config (set it in `from` key)'); |
|
196
|
|
|
|
|
197
|
|
|
return compact('numbers'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
private function post($url, $data) |
|
201
|
|
|
{ |
|
202
|
|
|
$post_data = http_build_query($data); |
|
203
|
|
|
$handle = curl_init($url); |
|
204
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, ['content-type: application/x-www-form-urlencoded']); |
|
205
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
206
|
|
|
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); |
|
207
|
|
|
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); |
|
208
|
|
|
curl_setopt($handle, CURLOPT_POST, true); |
|
209
|
|
|
curl_setopt($handle, CURLOPT_POSTFIELDS, $post_data); |
|
210
|
|
|
return curl_exec($handle); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|