-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTurbosms.php
More file actions
262 lines (228 loc) · 5.58 KB
/
Turbosms.php
File metadata and controls
262 lines (228 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace avator\turbosms;
use Yii;
use SoapClient;
use yii\base\InvalidConfigException;
use yii\base\Component;
use avator\turbosms\models\TurboSmsSent;
/**
*
* @author AVATOR (Oleksii Golub) <oleksii.v.golub@gmail.com>
* @since 1.0
*/
class Turbosms extends Component
{
/**
* Soap login
*
* @var string
*/
public $login;
/**
* Soap password
*
* @var string
*/
public $password;
/**
* @var string
*/
public $sender;
/**
* Debug mode
*
* @var bool
*/
public $debug = false;
/**
* @var SoapClient
*/
protected $client;
/**
* Wsdl url
*
* @var string
*/
protected $wsdl = 'http://turbosms.in.ua/api/wsdl.html';
/**
* Debug suffix message
*
* @var string
*/
public $debugSuffixMessage = ' (тестовый режим)';
/**
* Success message
*
* @var string
*/
public $successMessage = 'Сообщения успешно отправлено';
/**
* Error message
*
* @var string
*/
public $errorMessage = 'Сообщения не отправлено (ошибка: "%error%")';
/**
* Save to db log
*
* @var bool
*/
public $saveToDb = true;
/**
* @var int
*/
protected $sendStatus = 1;
/**
* @var string
*/
protected $lastSendMessageId = '';
/**
* @var array
*/
protected $lastSendMessagesIds = [];
/**
* Send sms and return array of message's ids in database
*
* @param string $text
* @param $phones
*
* @return array
*
* @throws InvalidConfigException
*/
public function send($text, $phones)
{
if (!is_array($phones)) {
$phones = [$phones];
}
foreach ($phones as $phone) {
if (!$phone) {
continue;
}
$message = $this->sendMessage($text, $phone);
$this->saveToDb($text, $phone, $message);
}
return $this->lastSendMessagesIds;
}
/**
* Connect to Turbosms by Soap
*
* @return SoapClient
* @throws InvalidConfigException
*/
protected function connect()
{
if ($this->client) {
return $this->client;
}
$client = new SoapClient($this->wsdl);
if (!$this->login || !$this->password) {
throw new InvalidConfigException('Enter login and password from Turbosms');
}
$result = $client->Auth([
'login' => $this->login,
'password' => $this->password,
]);
if ($result->AuthResult . '' != 'Вы успешно авторизировались') {
throw new InvalidConfigException($result->AuthResult);
}
$this->client = $client;
return $this->client;
}
/**
* Save sms to db
*
* @param string $text
* @param string $phone
* @param string $message
*
* @return bool
*/
public function saveToDb($text, $phone, $message)
{
if (!$this->saveToDb) {
return false;
}
$model = new TurboSmsSent();
$model->text = $text;
$model->phone = $phone;
$model->message = $message . ($this->debug ? $this->debugSuffixMessage : '');
if ($this->lastSendMessageId) {
$model->message_id = $this->lastSendMessageId;
}
$model->status = $this->sendStatus;
$model->save();
if ((int)$model->id) {
$this->lastSendMessagesIds[$model->id] = $this->lastSendMessageId;
}
return true;
}
/**
* Get balance
*
* @return int
*/
public function getBalance()
{
return $this->debug ? 0 : intval($this->getClient()->GetCreditBalance()->GetCreditBalanceResult);
}
/**
* Get message status
*
* @param $messageId
*
* @return string
*/
public function getMessageStatus($messageId)
{
if ($this->debug || !$messageId) {
return '';
}
$result = $this->getClient()->GetMessageStatus(['MessageId' => $messageId]);
return $result->GetMessageStatusResult;
}
/**
* Get Soap client
*
* @return SoapClient
* @throws InvalidConfigException
*/
protected function getClient()
{
if (!$this->client) {
return $this->connect();
}
return $this->client;
}
/**
* @param $text
* @param $phone
* @return array
*/
protected function sendMessage($text, $phone)
{
$message = $this->successMessage;
// set default status
$this->sendStatus = 1;
// clear variable
$this->lastSendMessageId = '';
if ($this->debug) {
return $message;
}
$result = $this->getClient()->SendSMS([
'sender' => $this->sender,
'destination' => $phone,
'text' => $text
]);
if (is_array($result->SendSMSResult->ResultArray) && !empty($result->SendSMSResult->ResultArray[1])) {
$this->lastSendMessageId = $result->SendSMSResult->ResultArray[1];
}
if (empty($result->SendSMSResult->ResultArray[0]) ||
$result->SendSMSResult->ResultArray[0] != 'Сообщения успешно отправлены'
) {
$this->sendStatus = 0;
$message = preg_replace('/%error%/i', $result->SendSMSResult->ResultArray[0], $this->errorMessage);
}
return $message;
}
}