|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of RussianPost SDK package. |
|
5
|
|
|
* |
|
6
|
|
|
* © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Appwilio\RussianPostSDK\Tracking; |
|
13
|
|
|
|
|
14
|
|
|
use Appwilio\RussianPostSDK\Tracking\Single\TrackingResponse; |
|
15
|
|
|
use Appwilio\RussianPostSDK\Tracking\Single\AuthorizationHeader; |
|
16
|
|
|
use Appwilio\RussianPostSDK\Tracking\Single\PostalOrderEventsForMail; |
|
17
|
|
|
use Appwilio\RussianPostSDK\Tracking\Exceptions\SingleAccessException; |
|
18
|
|
|
use Appwilio\RussianPostSDK\Tracking\Single\PostalOrderEventsForMailInput; |
|
19
|
|
|
|
|
20
|
|
|
class SingleAccessClient |
|
21
|
|
|
{ |
|
22
|
|
|
public const LANG_ENG = 'ENG'; |
|
23
|
|
|
public const LANG_RUS = 'RUS'; |
|
24
|
|
|
|
|
25
|
|
|
public const HISTORY_OPERATIONS = 0; |
|
26
|
|
|
public const HISTORY_NOTIFICATIONS = 1; |
|
27
|
|
|
|
|
28
|
|
|
protected const LINK_URL = 'https://www.pochta.ru/tracking'; |
|
29
|
|
|
protected const WSDL_URL = 'https://tracking.russianpost.ru/rtm34?wsdl'; |
|
30
|
|
|
|
|
31
|
|
|
protected const XML_NS_HISTORY = 'http://russianpost.org/operationhistory'; |
|
32
|
|
|
protected const XML_NS_DATA = 'http://russianpost.org/operationhistory/data'; |
|
33
|
|
|
|
|
34
|
|
|
/** @var AuthorizationHeader */ |
|
35
|
|
|
protected $auth; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \SoapClient */ |
|
38
|
|
|
protected $client; |
|
39
|
|
|
|
|
40
|
|
|
protected $options = [ |
|
41
|
|
|
'soap_version' => SOAP_1_2, |
|
42
|
|
|
'trace' => 1, |
|
43
|
|
|
'classmap' => [ |
|
44
|
|
|
'Address' => Single\Address::class, |
|
45
|
|
|
'Country' => Single\Country::class, |
|
46
|
|
|
'Rtm02Parameter' => Single\Parameter::class, |
|
47
|
|
|
'UserParameters' => Single\UserParameters::class, |
|
48
|
|
|
'ItemParameters' => Single\ItemParameters::class, |
|
49
|
|
|
'PostalOrderEvent' => Single\PostalOrderEvent::class, |
|
50
|
|
|
'AddressParameters' => Single\AddressParameters::class, |
|
51
|
|
|
'FinanceParameters' => Single\FinanceParameters::class, |
|
52
|
|
|
'AuthorizationHeader' => Single\AuthorizationHeader::class, |
|
53
|
|
|
'OperationParameters' => Single\OperationParameters::class, |
|
54
|
|
|
'OperationHistoryData' => Single\OperationHistoryData::class, |
|
55
|
|
|
'OperationHistoryRecord' => Single\OperationHistoryRecord::class, |
|
56
|
|
|
'PostalOrderEventsForMail' => Single\PostalOrderEventsForMail::class, |
|
57
|
|
|
'getOperationHistoryResponse' => Single\TrackingResponse::class, |
|
58
|
|
|
'PostalOrderEventsForMailInput' => Single\PostalOrderEventsForMailInput::class, |
|
59
|
|
|
// Mai – не опечатка |
|
60
|
|
|
'PostalOrderEventsForMaiOutput' => Single\PostalOrderEventsForMailOutput::class, |
|
61
|
|
|
'PostalOrderEventsForMailResponse' => Single\CashOnDeliveryResponse::class, |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
public function __construct(string $login, string $password) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->auth = new AuthorizationHeader($login, $password); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getTrackingUrl(string $number): ?string |
|
71
|
|
|
{ |
|
72
|
|
|
return self::LINK_URL.'#'.$number; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getTrackingEvents( |
|
76
|
|
|
string $track, |
|
77
|
|
|
string $language = self::LANG_RUS, |
|
78
|
|
|
int $type = self::HISTORY_OPERATIONS |
|
79
|
|
|
): TrackingResponse |
|
80
|
|
|
{ |
|
81
|
|
|
$arguments = $this->assembleTrackingRequestArguments($track, $language, $type); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
return $this->getClient()->{'getOperationHistory'}($arguments); |
|
85
|
|
|
} catch (\SoapFault $e) { |
|
86
|
|
|
$detail = get_object_vars($e->{'detail'}); |
|
87
|
|
|
|
|
88
|
|
|
throw new SingleAccessException(key($detail).': '.current($detail), $e->getCode(), $e); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getCashOnDeliveryEvents(string $track, string $language = self::LANG_RUS) |
|
93
|
|
|
{ |
|
94
|
|
|
$arguments = $this->assembleCashOnDeliveryRequestArguments($track, $language); |
|
95
|
|
|
|
|
96
|
|
|
return $this->getClient()->{'PostalOrderEventsForMail'}($arguments); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function assembleTrackingRequestArguments(string $track, string $language, int $type): \SoapVar |
|
100
|
|
|
{ |
|
101
|
|
|
return new \SoapVar([ |
|
102
|
|
|
new \SoapVar([ |
|
103
|
|
|
new \SoapVar($track, XSD_STRING, null, null, 'Barcode', self::XML_NS_DATA), |
|
104
|
|
|
new \SoapVar($type, XSD_STRING, null, null, 'MessageType', self::XML_NS_DATA), |
|
105
|
|
|
new \SoapVar($language, XSD_STRING, null, null, 'Language', self::XML_NS_DATA), |
|
106
|
|
|
], SOAP_ENC_OBJECT, null, null, 'OperationHistoryRequest', self::XML_NS_DATA), |
|
107
|
|
|
new \SoapVar($this->auth, SOAP_ENCODED, null, null, 'AuthorizationHeader', self::XML_NS_DATA), |
|
108
|
|
|
], SOAP_ENC_OBJECT); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function assembleCashOnDeliveryRequestArguments(string $code, string $language): \SoapVar |
|
112
|
|
|
{ |
|
113
|
|
|
$request = new PostalOrderEventsForMail( |
|
114
|
|
|
new PostalOrderEventsForMailInput($code, $language), |
|
115
|
|
|
$this->auth |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return new \SoapVar( |
|
119
|
|
|
$request, SOAP_ENC_OBJECT, null, null, 'PostalOrderEventsForMail', self::XML_NS_HISTORY |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function getClient(): \SoapClient |
|
124
|
|
|
{ |
|
125
|
|
|
if (! $this->client) { |
|
126
|
|
|
$this->client = new \SoapClient(self::WSDL_URL, $this->options); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->client; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|