1 <?php
2 3 4 5 6 7
8 class SMSCOrderNotification extends OrderHandlersDecorator {
9
10
11 protected static $smscSendURL = 'https://smsc.ru/sys/send.php';
12
13
14
15 protected static $memberPhoneField = 'Phone';
16
17 static $smscErrors = array(
18 '1' => 'Ошибка в параметрах.',
19 '2' => 'Неверный логин или пароль.',
20 '3' => 'Недостаточно средств на счете Клиента.',
21 '4' => 'IP-адрес временно заблокирован из-за частых ошибок в запросах. Подробнее',
22 '5' => 'Неверный формат даты.',
23 '6' => 'Сообщение запрещено (по тексту или по имени отправителя).',
24 '7' => 'Неверный формат номера телефона.',
25 '8' => 'Сообщение на указанный номер не может быть доставлено.',
26 '9' => 'Отправка более одного одинакового запроса на передачу SMS-сообщения либо более пяти одинаковых запросов на получение стоимости сообщения в течение минуты.',
27 );
28
29 30 31 32 33
34 static function setMemberPhoneField($field) {
35 self::$memberPhoneField = $field;
36 }
37
38 39 40 41 42
43 static function getSMSCURL() {
44 $conf = SiteConfig::current_site_config();
45 if ($conf->SMSCLogin && $conf->SMSCPassword) {
46 $url = self::$smscSendURL . '?login='.$conf->SMSCLogin.'&psw='.$conf->SMSCPassword.'&charset=utf-8';
47 return $url;
48 }
49 }
50
51 52 53 54 55 56
57 function OnAfterCreateOrder(& $member) {
58 $conf = SiteConfig::current_site_config();
59
60 $phone = $this->getClientPhone();
61 if ($conf->ClientSMSCOrderNotificationActive && $phone) {
62 $this->send(
63 $phone,
64 $this->owner->customise(array(
65 'Member' => $this->owner->Member(),
66 'Order' => $this->owner,
67 'SiteTitle' => $conf->Title,
68 'CatalogCurrency' => $conf->CatalogCurrency,
69 ))->owner->renderWith('SMSC_ClientCreateOrderMessage'),
70 $conf->ClientNotificationType
71 );
72 }
73
74 if ($conf->ManagerSMSCOrderNotificationActive && $conf->ManagerPhones) {
75 $this->send(
76 $conf->ManagerPhones,
77 $this->owner->customise(array(
78 'Member' => $this->owner->Member(),
79 'Order' => $this->owner,
80 'SiteTitle' => $conf->Title,
81 'CatalogCurrency' => $conf->CatalogCurrency,
82 ))->owner->renderWith('SMSC_ManagerCreateOrderMessage'),
83 $conf->ManagerNotificationType
84 );
85 }
86 }
87
88 89 90 91 92 93 94
95 function OnAfterChangeStatus($member, $changedStatus, $note) {
96 $conf = SiteConfig::current_site_config();
97
98 $phone = $this->getClientPhone();
99 if ($conf->ClientSMSCOrderNotificationActive && $phone) {
100 $this->send(
101 $phone,
102 $this->owner->customise(array(
103 'Member' => $this->owner->Member(),
104 'Note' => $note,
105 'Order' => $this->owner,
106 ))->owner->renderWith('SMSC_ClientChangeOrderStatusMessage'),
107 $conf->ClientNotificationType
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 private function getClientPhone() {
133 $phone = $this->owner->Phone;
134 if (!$phone) {
135 if ($member = $this->owner->Member()) {
136 $phoneField = self::$memberPhoneField;
137 $phone = $member->{$phoneField};
138 }
139 }
140 return $phone;
141 }
142
143 144 145 146 147 148 149 150 151
152 private function send($phones, $message, $type = 'sms') {
153 $baseURL = self::getSMSCURL();
154 if (!$baseURL) return false;
155 if (!$phones) return false;
156 if (!$message) return false;
157
158 $url = $baseURL . '&phones=' . urlencode($phones) . '&mes=' . urlencode($message);
159 if ($type == 'voice') {
160 $url .= '&call=1';
161 }
162
163 $request = new RestfulService($url, 0);
164
165
166 $rs = $request->request('', "GET", null, null, array(CURLOPT_TIMEOUT => 5));
167
168
169 $error = '';
170 if ($rs->isError()) {
171 $error = sprintf(_t('SMSCOrderNotification.ErrorMesssage', 'Error Messsage'), $phones, ($rs->getStatusCode() . ' - ' . $rs->getStatusDescription()));
172 } elseif (substr($rs->getBody(), 0, 5) == 'ERROR') {
173 $errorNo = (int)substr($rs->getBody(), 8, 1);
174 $error = sprintf(_t('SMSCOrderNotification.ErrorMesssage', 'Error Messsage'), $phones, ((isset(self::$smscErrors[$errorNo])) ? self::$smscErrors[$errorNo] : $rs));
175 }
176 if ($error) {
177 $conf = SiteConfig::current_site_config();
178 $em = new Email(Email::getAdminEmail(), $conf->CartAdminEmail(), _t('SMSCOrderNotification.SendError','Send Error'), $error);
179 $em->send();
180 }
181 return true;
182 }
183 }
184
185
186 class SMSCOrderNotification_SiteConfig extends SiteConfigDecorator {
187
188 static function smsNotificationTypes() {
189 $types = array();
190 $types['sms'] = _t('SMSCOrderNotification_SiteConfig.SMS', 'SMS');
191 $types['voice'] = _t('SMSCOrderNotification_SiteConfig.Voice', 'Voice');
192 return $types;
193 }
194
195 function () {
196 return array(
197 'db' => array(
198 'ClientSMSCOrderNotificationActive' => 'Boolean',
199 'ManagerSMSCOrderNotificationActive' => 'Boolean',
200 'SMSCLogin' => 'Varchar(255)',
201 'SMSCPassword' => 'Varchar(255)',
202 'SenderName' => 'Varchar(255)',
203 'ManagerPhones' => 'Varchar(255)',
204 'ManagerNotificationType' => "Enum('sms,voice')",
205 'ClientNotificationType' => "Enum('sms,voice')",
206 )
207 );
208 }
209
210 public function updateCMSFields(FieldSet &$fields) {
211 $tab = self::get_config_tab($fields, 'OrderNotifications.SMSCOrderNotification');
212
213 $tab->push(new CheckboxField('ClientSMSCOrderNotificationActive', $this->owner->FieldLabel('ClientSMSCOrderNotificationActive')));
214 $tab->push(new DropdownField('ClientNotificationType', $this->owner->FieldLabel('ClientNotificationType'), self::smsNotificationTypes()));
215 $tab->push(new CheckboxField('ManagerSMSCOrderNotificationActive', $this->owner->FieldLabel('ManagerSMSCOrderNotificationActive')));
216 $tab->push(new DropdownField('ManagerNotificationType', $this->owner->FieldLabel('ManagerNotificationType'), self::smsNotificationTypes()));
217 $tab->push(new TextField('ManagerPhones', $this->owner->FieldLabel('ManagerPhones')));
218
219 $tab->push(new HeaderField('SMSCParams', _t('SMSCOrderNotification_SiteConfig.SMSC_Params', 'SMSC Params')));
220 $tab->push(new TextField('SMSCLogin', $this->owner->FieldLabel('SMSCLogin')));
221 $tab->push(new PasswordField('SMSCPassword', $this->owner->FieldLabel('SMSCPassword')));
222 $tab->push(new TextField('SenderName', $this->owner->FieldLabel('SenderName')));
223 }
224 }
225
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-