Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rus_post.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
17class RusPost extends Base
18{
22 public function getClassTitle()
23 {
24 return Loc::getMessage("SALE_DELIVERY_TRACKING_RUS_POST_TITLE");
25 }
26
30 public function getClassDescription()
31 {
32 return Loc::getMessage(
33 "SALE_DELIVERY_TRACKING_RUS_POST_DESCRIPTION",
34 array(
35 '#A1#' => '<a href="https://tracking.pochta.ru/">',
36 '#A2#' => '</a>'
37 )
38 );
39 }
40
41
46 public function getStatus($trackingNumber)
47 {
48 $trackingNumber = trim($trackingNumber);
49 $result = new StatusResult();
50
51 if(!$this->checkTrackNumberFormat($trackingNumber))
52 $result->addError(new Error(Loc::getMessage('SALE_DELIVERY_TRACKING_RUS_POST_ERROR_TRNUM_FORMAT')));
53
54 if(empty($this->params['LOGIN']))
55 $result->addError(new Error(Loc::getMessage("SALE_DELIVERY_TRACKING_RUS_POST_LOGIN_ERROR")));
56
57 if(empty($this->params['PASSWORD']))
58 $result->addError(new Error(Loc::getMessage("SALE_DELIVERY_TRACKING_RUS_POST_PASSWORD_ERROR")));
59
60 if($result->isSuccess())
61 {
62 $t = new RusPostSingle(
63 $this->params['LOGIN'],
64 $this->params['PASSWORD']
65 );
66
67 $result = $t->getOperationHistory($trackingNumber);
68 }
69
70 return $result;
71 }
72
78 public function getStatuses(array $trackingNumbers)
79 {
80 $data = array();
81
82 foreach($trackingNumbers as $number)
83 $data[$number] = $this->getStatus($number);
84
85 return $data;
86 }
87
91 public function getParamsStructure()
92 {
93 return array(
94 "LOGIN" => array(
95 'TYPE' => 'STRING',
96 'LABEL' => Loc::getMessage("SALE_DELIVERY_TRACKING_RUS_POST_LOGIN")
97 ),
98 "PASSWORD" => array(
99 'TYPE' => 'STRING',
100 'LABEL' => Loc::getMessage("SALE_DELIVERY_TRACKING_RUS_POST_PASSWORD")
101 )
102 );
103 }
104
112 protected function checkTrackNumberFormat($trackNumber)
113 {
114 if(mb_strlen($trackNumber) == 13)
115 return preg_match('/^[A-Z]{2}?\d{9}?[A-Z]{2}$/i', $trackNumber) == 1;
116 elseif(mb_strlen($trackNumber) == 14)
117 return preg_match('/^\d{14}?$/', $trackNumber) == 1;
118 else
119 return false;
120 }
121
126 public function getTrackingUrl($trackingNumber = '')
127 {
128 return 'https://pochta.ru/tracking'.($trackingNumber <> '' ? '#'.$trackingNumber : '');
129 }
130}
131
137{
138 const LANG_RUS = "RUS";
139 const LANG_ENG = "ENG";
140
141 protected $client = null;
142 protected $lang = "";
143 protected $login = "";
144 protected $password = "";
145
146 protected static $url = 'https://tracking.russianpost.ru/rtm34';
147
153 public function __construct($login, $password, $lang = self::LANG_RUS)
154 {
155 $this->httpClient = new \Bitrix\Main\Web\HttpClient(array(
156 "version" => "1.1",
157 "socketTimeout" => 15,
158 "streamTimeout" => 15,
159 "redirect" => true,
160 "redirectMax" => 5,
161 ));
162
163 $this->httpClient->setHeader("Content-Type", "application/soap+xml; charset=utf-8");
164 $this->lang = $lang;
165 $this->login = $login;
166 $this->password = $password;
167 }
168
169 public function sendRequest($requestData)
170 {
171 $result = new Result();
172
173 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
174 $requestData = Encoding::convertEncoding($requestData, SITE_CHARSET, 'UTF-8');
175
176 $httpRes = $this->httpClient->post(self::$url, $requestData);
177 $errors = $this->httpClient->getError();
178
179 if (!$httpRes && !empty($errors))
180 {
181 $strError = "";
182
183 foreach($errors as $errorCode => $errMes)
184 $strError .= $errorCode.": ".$errMes;
185
186 $result->addError(new Error($strError));
187 }
188 else
189 {
190 $status = $this->httpClient->getStatus();
191
192 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
193 $httpRes = Encoding::convertEncoding($httpRes, 'UTF-8', SITE_CHARSET);
194
195 $objXML = new \CDataXML();
196 $objXML->LoadString($httpRes);
197 $data = $objXML->GetArray();
198 $result->addData($data);
199
200 if ($status != 200)
201 {
202 $result->addError(new Error(Loc::getMessage('SALE_DELIVERY_TRACKING_RUS_POST_ERROR_HTTP_STATUS').': '.$status));
203
204 if(!empty($data['Envelope']['#']['Body'][0]['#']['Fault'][0]['#']['Reason'][0]['#']['Text'][0]['#']))
205 $result->addError(new Error($data['Envelope']['#']['Body'][0]['#']['Fault'][0]['#']['Reason'][0]['#']['Text'][0]['#']));
206
207 if(!empty($data['Envelope']['#']['Body'][0]['#']['Fault'][0]['#']['Detail'][0]['#']['AuthorizationFaultReason'][0]['#']))
208 $result->addError(new Error($data['Envelope']['#']['Body'][0]['#']['Fault'][0]['#']['Detail'][0]['#']['AuthorizationFaultReason'][0]['#']));
209 }
210 }
211
212 return $result;
213 }
214
219 public function getOperationHistory($trackingNumber)
220 {
221 $result = new StatusResult();
222 $requestData = '
223 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:oper="http://russianpost.org/operationhistory" xmlns:data="http://russianpost.org/operationhistory/data" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
224 <soap:Header/>
225 <soap:Body>
226 <oper:getOperationHistory>
227 <data:OperationHistoryRequest>
228 <data:Barcode>'.$trackingNumber.'</data:Barcode>
229 <data:MessageType>0</data:MessageType>
230 <data:Language>'.$this->lang.'</data:Language>
231 </data:OperationHistoryRequest>
232 <data:AuthorizationHeader soapenv:mustUnderstand="1">
233 <data:login>'.$this->login.'</data:login>
234 <data:password>'.$this->password.'</data:password>
235 </data:AuthorizationHeader>
236 </oper:getOperationHistory>
237 </soap:Body>
238 </soap:Envelope>
239 ';
240
241 $res = $this->sendRequest($requestData);
242
243 if(!$res->isSuccess())
244 {
245 $result->addErrors($res->getErrors());
246 return $result;
247 }
248
249 $lastOperation = $this->getLastOperation($res->getData());
250
251 if(!$lastOperation)
252 {
253 $result->addError(new Error(Loc::getMessage('SALE_DELIVERY_TRACKING_RUS_POST_ERROR_LAST_OP')));
254 }
255 else
256 {
257 $result->status = $this->extractStatus($lastOperation);
258 $result->description = $this->createDescription($trackingNumber);
259 $lastOperationTS = $this->extractLastChangeDate($lastOperation);
260
261 if($lastOperationTS > 0)
262 $result->lastChangeTimestamp = $this->extractLastChangeDate($lastOperation);
263
264 $result->trackingNumber = $trackingNumber;
265 }
266
267 return $result;
268 }
269
274 protected function extractLastChangeDate($lastOperation)
275 {
276 if(empty($lastOperation['#']['OperationParameters'][0]['#']['OperDate'][0]['#']))
277 return 0;
278
279 $date = new \DateTime($lastOperation['#']['OperationParameters'][0]['#']['OperDate'][0]['#']);
280 return $date->getTimestamp();
281 }
282
287 protected function getLastOperation(array $answer)
288 {
289 $history = $answer['Envelope']['#']['Body'][0]['#']['getOperationHistoryResponse'][0]['#']['OperationHistoryData'][0]['#']['historyRecord'];
290
291 if(!is_array($history) || empty($history))
292 return null;
293
294 if(!$lastOperation = end($history))
295 return null;
296
297 return $lastOperation;
298 }
299
304 protected function createDescription($trackingNumber)
305 {
306 $link = 'https://pochta.ru/tracking#'.$trackingNumber;
307 return Loc::getMessage('SALE_DELIVERY_TRACKING_RUS_POST_STATUS_DESCR').': '.'<a href="'.$link.'">'.$link.'</a>';
308 }
309
314 protected function extractStatus(array $lastOperation)
315 {
316 if(!isset($lastOperation['#']['OperationParameters']['0']['#']['OperType']['0']['#']['Id']['0']['#']))
317 return Statuses::UNKNOWN;
318
319 if(!isset($lastOperation['#']['OperationParameters'][0]['#']['OperAttr'][0]['#']['Id'][0]['#']))
320 return Statuses::UNKNOWN;
321
322 $oper = $lastOperation['#']['OperationParameters'][0]['#']['OperType'][0]['#']['Id'][0]['#'];
323 $att = $lastOperation['#']['OperationParameters'][0]['#']['OperAttr'][0]['#']['Id'][0]['#'];
324
325 return $this->mapStatus($oper, $att);
326 }
327
334 protected function mapStatus($oper, $attr)
335 {
336 if($oper == '')
337 return Statuses::UNKNOWN;
338
339 /*
340 * if innerStatus1 != innerStatus2 != .......
341 *
342 * opCode1 => array (
343 * attrCode1 => innerStatus1
344 * attrCode2 => innerStatus2
345 * ...
346 * )
347 *
348 * if innerStatus1 == innerStatus2 == .......
349 *
350 * opCode => innerStatus
351 *
352 */
353 $rusPostStatuses = array(
355 2 => array(
356 1 => Statuses::HANDED,
358 3 => Statuses::HANDED,
360 5 => Statuses::HANDED,
361 6 => Statuses::HANDED,
363 8 => Statuses::HANDED,
365 10 => Statuses::HANDED,
366 11 => Statuses::HANDED,
367 12 => Statuses::HANDED,
368 ),
371 5 => array(
377 ),
378 6 => array(
384 ),
386 8 => array(
397 10 => Statuses::ARRIVED,
399 12 => Statuses::ARRIVED,
401 14 => Statuses::ARRIVED,
407
408 ),
412 12 => array(
422 10 => Statuses::PROBLEM,
423 11 => Statuses::ARRIVED,
424 12 => Statuses::PROBLEM,
425 13 => Statuses::PROBLEM,
426 14 => Statuses::PROBLEM,
427 15 => Statuses::ARRIVED,
428 16 => Statuses::PROBLEM,
429 17 => Statuses::ARRIVED,
430 18 => Statuses::ARRIVED,
431 19 => Statuses::PROBLEM,
432 20 => Statuses::PROBLEM,
433 21 => Statuses::PROBLEM,
434 22 => Statuses::ARRIVED,
435 23 => Statuses::PROBLEM,
436 24 => Statuses::PROBLEM,
437 25 => Statuses::ARRIVED,
438 26 => Statuses::PROBLEM,
439 27 => Statuses::ARRIVED,
440 28 => Statuses::PROBLEM,
441 ),
444 15 => Statuses::ARRIVED,
445 16 => Statuses::PROBLEM,
446 17 => Statuses::ARRIVED,
447 18 => Statuses::PROBLEM,
451 22 => Statuses::PROBLEM,
453 24 => Statuses::PROBLEM,
455 26 => Statuses::PROBLEM,
463 );
464
465 if(!isset($rusPostStatuses[$oper]))
466 return Statuses::UNKNOWN;
467
468 if(!is_array($rusPostStatuses[$oper]))
469 return $rusPostStatuses[$oper];
470
471 if($attr == '')
472 return Statuses::UNKNOWN;
473
474 if(!isset($rusPostStatuses[$oper][$attr]))
475 return Statuses::UNKNOWN;
476
477 return $rusPostStatuses[$oper][$attr];
478 }
479}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
Definition rus_post.php:18
getClassDescription()
Definition rus_post.php:30
getParamsStructure()
Definition rus_post.php:91
getClassTitle()
Definition rus_post.php:22
getTrackingUrl($trackingNumber='')
Definition rus_post.php:126
getStatuses(array $trackingNumbers)
Definition rus_post.php:78
checkTrackNumberFormat($trackNumber)
Definition rus_post.php:112
getStatus($trackingNumber)
Definition rus_post.php:46
Definition rus_post.php:137
const LANG_RUS
Definition rus_post.php:138
extractStatus(array $lastOperation)
Definition rus_post.php:314
mapStatus($oper, $attr)
Definition rus_post.php:334
getLastOperation(array $answer)
Definition rus_post.php:287
$password
Definition rus_post.php:144
sendRequest($requestData)
Definition rus_post.php:169
const LANG_ENG
Definition rus_post.php:139
__construct($login, $password, $lang=self::LANG_RUS)
Definition rus_post.php:153
$lang
Definition rus_post.php:142
createDescription($trackingNumber)
Definition rus_post.php:304
getOperationHistory($trackingNumber)
Definition rus_post.php:219
static $url
Definition rus_post.php:146
$client
Definition rus_post.php:141
$login
Definition rus_post.php:143
extractLastChangeDate($lastOperation)
Definition rus_post.php:274