├── migrations ├── m160622_222849_create_message_id_field.php ├── m160117_203749_rename_and_create_field.php └── m141211_142603_create_table.php ├── composer.json ├── models └── TurboSmsSent.php ├── LICENSE ├── README.md └── Turbosms.php /migrations/m160622_222849_create_message_id_field.php: -------------------------------------------------------------------------------- 1 | addColumn('{{%turbo_sms_sent}}', 'message_id', Schema::TYPE_STRING); 11 | } 12 | 13 | public function safeDown() 14 | { 15 | $this->dropColumn('{{%turbo_sms_sent}}', 'message_id'); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "avator/yii2-turbosms", 3 | "description": "Yii2 turbosms", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","helpers","turbosms","sms"], 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Oleksii Golub", 10 | "email": "oleksii.v.golub@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "avator\\turbosms\\": "" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /migrations/m160117_203749_rename_and_create_field.php: -------------------------------------------------------------------------------- 1 | renameColumn('{{%turbo_sms_sent}}', 'status', 'message'); 11 | $this->addColumn('{{%turbo_sms_sent}}', 'status', Schema::TYPE_SMALLINT); 12 | 13 | } 14 | 15 | public function down() 16 | { 17 | $this->renameColumn('{{%turbo_sms_sent}}', 'message', 'status'); 18 | $this->dropColumn('{{%turbo_sms_sent}}', 'status'); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /migrations/m141211_142603_create_table.php: -------------------------------------------------------------------------------- 1 | db->driverName === 'mysql') { 12 | // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci 13 | $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; 14 | } 15 | 16 | $this->createTable('{{%turbo_sms_sent}}', [ 17 | 'id' => Schema::TYPE_PK, 18 | 'date_sent' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP', 19 | 'text' => Schema::TYPE_TEXT, 20 | 'phone' => Schema::TYPE_STRING, 21 | 'status' => Schema::TYPE_STRING, 22 | ], $tableOptions); 23 | } 24 | 25 | public function down() 26 | { 27 | $this->dropTable('{{%turbo_sms_sent}}'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /models/TurboSmsSent.php: -------------------------------------------------------------------------------- 1 | 255], 41 | ]; 42 | } 43 | 44 | /** 45 | * @inheritdoc 46 | */ 47 | public function attributeLabels() 48 | { 49 | return [ 50 | 'id' => Yii::t('app', 'ID'), 51 | 'date_sent' => Yii::t('app', 'Date Sent'), 52 | 'text' => Yii::t('app', 'Text'), 53 | 'phone' => Yii::t('app', 'Phone'), 54 | 'message' => Yii::t('app', 'Message'), 55 | 'status' => Yii::t('app', 'Status'), 56 | 'message_id' => Yii::t('app', 'Message ID'), 57 | ]; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Oleksii Golub 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of avator or yii2-turbosms nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 turbosms 2 | ============= 3 | Yii2 send sms from Turbosms.ua by SOAP 4 | 5 | Requirements 6 | ------------ 7 | * PHP >=5.4.0 8 | * Install and Enable `extension=php_soap.dll` in your `php.ini` file 9 | 10 | Installation 11 | ------------ 12 | 13 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 14 | 15 | Either run 16 | 17 | ``` 18 | php composer.phar require --prefer-dist avator/yii2-turbosms "*" 19 | ``` 20 | 21 | or add 22 | 23 | ``` 24 | "avator/yii2-turbosms": "*" 25 | ``` 26 | 27 | to the require section of your `composer.json` file. 28 | 29 | Subsequently, run 30 | 31 | ``` 32 | ./yii migrate/up --migrationPath=@vendor/avator/yii2-turbosms/migrations 33 | ``` 34 | 35 | ## Basic setup 36 | 37 | You should: 38 | * registered account at http://turbosms.ua/ 39 | * add sender in page https://turbosms.ua/sign/add.html 40 | * create login and password for soap api in page https://turbosms.ua/route.html 41 | 42 | ### Configuration 43 | 44 | Add the following in your config: 45 | 46 | ```php 47 | [ 50 | 'turbosms' => [ 51 | 'class' => 'avator\turbosms\Turbosms', 52 | 'sender' => 'your_sender', 53 | 'login' => 'your_login', 54 | 'password' => 'your_password', 55 | ], 56 | ... 57 | ], 58 | ... 59 | ``` 60 | If you want test sms in debug mode change config: 61 | ```php 62 | [ 65 | 'turbosms' => [ 66 | 'class' => 'avator\turbosms\Turbosms', 67 | 'sender' => 'your_sender', 68 | 'login' => 'your_login', 69 | 'password' => 'your_password', 70 | 'debug' => true, 71 | ], 72 | ... 73 | ], 74 | ... 75 | ``` 76 | in debug mode sms not send only add to db table. 77 | 78 | Usage 79 | ----- 80 | 81 | Once the extension is installed, simply use it in your code by : 82 | 83 | ```php 84 | turbosms->send('test', '+380XXXXXXXXX'); ?> 85 | ``` 86 | 87 | TODO 88 | ----- 89 | * Translate message 90 | * Save log to file 91 | 92 | ## License 93 | 94 | **yii2-turbosms** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details. 95 | -------------------------------------------------------------------------------- /Turbosms.php: -------------------------------------------------------------------------------- 1 | 13 | * @since 1.0 14 | */ 15 | class Turbosms extends Component 16 | { 17 | /** 18 | * Soap login 19 | * 20 | * @var string 21 | */ 22 | public $login; 23 | 24 | /** 25 | * Soap password 26 | * 27 | * @var string 28 | */ 29 | public $password; 30 | 31 | /** 32 | * @var string 33 | */ 34 | public $sender; 35 | 36 | /** 37 | * Debug mode 38 | * 39 | * @var bool 40 | */ 41 | public $debug = false; 42 | 43 | /** 44 | * @var SoapClient 45 | */ 46 | protected $client; 47 | 48 | /** 49 | * Wsdl url 50 | * 51 | * @var string 52 | */ 53 | protected $wsdl = 'http://turbosms.in.ua/api/wsdl.html'; 54 | 55 | /** 56 | * Debug suffix message 57 | * 58 | * @var string 59 | */ 60 | public $debugSuffixMessage = ' (тестовый режим)'; 61 | 62 | /** 63 | * Success message 64 | * 65 | * @var string 66 | */ 67 | public $successMessage = 'Сообщения успешно отправлено'; 68 | 69 | /** 70 | * Error message 71 | * 72 | * @var string 73 | */ 74 | public $errorMessage = 'Сообщения не отправлено (ошибка: "%error%")'; 75 | 76 | /** 77 | * Save to db log 78 | * 79 | * @var bool 80 | */ 81 | public $saveToDb = true; 82 | 83 | /** 84 | * @var int 85 | */ 86 | protected $sendStatus = 1; 87 | 88 | /** 89 | * @var string 90 | */ 91 | protected $lastSendMessageId = ''; 92 | 93 | /** 94 | * @var array 95 | */ 96 | protected $lastSendMessagesIds = []; 97 | 98 | /** 99 | * Send sms and return array of message's ids in database 100 | * 101 | * @param string $text 102 | * @param $phones 103 | * 104 | * @return array 105 | * 106 | * @throws InvalidConfigException 107 | */ 108 | public function send($text, $phones) 109 | { 110 | if (!is_array($phones)) { 111 | $phones = [$phones]; 112 | } 113 | 114 | foreach ($phones as $phone) { 115 | if (!$phone) { 116 | continue; 117 | } 118 | $message = $this->sendMessage($text, $phone); 119 | $this->saveToDb($text, $phone, $message); 120 | } 121 | 122 | return $this->lastSendMessagesIds; 123 | } 124 | 125 | /** 126 | * Connect to Turbosms by Soap 127 | * 128 | * @return SoapClient 129 | * @throws InvalidConfigException 130 | */ 131 | protected function connect() 132 | { 133 | if ($this->client) { 134 | return $this->client; 135 | } 136 | 137 | $client = new SoapClient($this->wsdl); 138 | if (!$this->login || !$this->password) { 139 | throw new InvalidConfigException('Enter login and password from Turbosms'); 140 | } 141 | 142 | $result = $client->Auth([ 143 | 'login' => $this->login, 144 | 'password' => $this->password, 145 | ]); 146 | if ($result->AuthResult . '' != 'Вы успешно авторизировались') { 147 | throw new InvalidConfigException($result->AuthResult); 148 | } 149 | $this->client = $client; 150 | 151 | return $this->client; 152 | } 153 | 154 | /** 155 | * Save sms to db 156 | * 157 | * @param string $text 158 | * @param string $phone 159 | * @param string $message 160 | * 161 | * @return bool 162 | */ 163 | public function saveToDb($text, $phone, $message) 164 | { 165 | if (!$this->saveToDb) { 166 | return false; 167 | } 168 | $model = new TurboSmsSent(); 169 | $model->text = $text; 170 | $model->phone = $phone; 171 | $model->message = $message . ($this->debug ? $this->debugSuffixMessage : ''); 172 | if ($this->lastSendMessageId) { 173 | $model->message_id = $this->lastSendMessageId; 174 | } 175 | $model->status = $this->sendStatus; 176 | $model->save(); 177 | 178 | if ((int)$model->id) { 179 | $this->lastSendMessagesIds[$model->id] = $this->lastSendMessageId; 180 | } 181 | 182 | return true; 183 | } 184 | 185 | /** 186 | * Get balance 187 | * 188 | * @return int 189 | */ 190 | public function getBalance() 191 | { 192 | return $this->debug ? 0 : intval($this->getClient()->GetCreditBalance()->GetCreditBalanceResult); 193 | } 194 | 195 | /** 196 | * Get message status 197 | * 198 | * @param $messageId 199 | * 200 | * @return string 201 | */ 202 | public function getMessageStatus($messageId) 203 | { 204 | if ($this->debug || !$messageId) { 205 | return ''; 206 | } 207 | $result = $this->getClient()->GetMessageStatus(['MessageId' => $messageId]); 208 | 209 | return $result->GetMessageStatusResult; 210 | } 211 | 212 | /** 213 | * Get Soap client 214 | * 215 | * @return SoapClient 216 | * @throws InvalidConfigException 217 | */ 218 | protected function getClient() 219 | { 220 | if (!$this->client) { 221 | return $this->connect(); 222 | } 223 | 224 | return $this->client; 225 | } 226 | 227 | /** 228 | * @param $text 229 | * @param $phone 230 | * @return array 231 | */ 232 | protected function sendMessage($text, $phone) 233 | { 234 | $message = $this->successMessage; 235 | // set default status 236 | $this->sendStatus = 1; 237 | // clear variable 238 | $this->lastSendMessageId = ''; 239 | if ($this->debug) { 240 | return $message; 241 | } 242 | 243 | $result = $this->getClient()->SendSMS([ 244 | 'sender' => $this->sender, 245 | 'destination' => $phone, 246 | 'text' => $text 247 | ]); 248 | 249 | if (is_array($result->SendSMSResult->ResultArray) && !empty($result->SendSMSResult->ResultArray[1])) { 250 | $this->lastSendMessageId = $result->SendSMSResult->ResultArray[1]; 251 | } 252 | 253 | if (empty($result->SendSMSResult->ResultArray[0]) || 254 | $result->SendSMSResult->ResultArray[0] != 'Сообщения успешно отправлены' 255 | ) { 256 | $this->sendStatus = 0; 257 | $message = preg_replace('/%error%/i', $result->SendSMSResult->ResultArray[0], $this->errorMessage); 258 | } 259 | 260 | return $message; 261 | } 262 | } 263 | --------------------------------------------------------------------------------