1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use SoapClient; |
7
|
|
|
use Radowoj\Yaah\Constants\Sysvars; |
8
|
|
|
|
9
|
|
|
class Client |
10
|
|
|
{ |
11
|
|
|
const WSDL_PRODUCTION = 'https://webapi.allegro.pl/service.php?wsdl'; |
12
|
|
|
const WSDL_SANDBOX = 'https://webapi.allegro.pl.webapisandbox.pl/service.php?wsdl'; |
13
|
|
|
|
14
|
|
|
protected $config = null; |
15
|
|
|
|
16
|
|
|
protected $soapClient = null; |
17
|
|
|
|
18
|
|
|
protected $allegroSessionHandle = null; |
19
|
|
|
|
20
|
|
|
protected $allegroUserId = null; |
21
|
|
|
|
22
|
|
|
protected $localVersionKey = null; |
23
|
|
|
|
24
|
|
|
public function __construct(Config $config) |
25
|
|
|
{ |
26
|
|
|
$this->config = $config; |
27
|
|
|
|
28
|
|
|
$this->soapClient = new SoapClient( |
29
|
|
|
$config->getIsSandbox() |
30
|
|
|
? self::WSDL_SANDBOX |
31
|
|
|
: self::WSDL_PRODUCTION |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->login(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
protected function getLocalVersionKey() |
39
|
|
|
{ |
40
|
|
|
if (is_null($this->localVersionKey)) { |
41
|
|
|
$response = $this->soapClient->doQuerySysStatus([ |
42
|
|
|
'sysvar' => Sysvars::SYSVAR_CATEGORY_TREE, |
43
|
|
|
'countryId' => $this->config->getCountryCode(), |
44
|
|
|
'webapiKey' => $this->config->getApiKey(), |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
if (!isset($response->verKey)) { |
48
|
|
|
throw new Exception("Invalid WebAPI doQuerySysStatus() response: " . print_r($response, 1)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->localVersionKey = $response->verKey; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->localVersionKey; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getWebApiRequest($data) |
58
|
|
|
{ |
59
|
|
|
return array_merge($data, [ |
60
|
|
|
'webapiKey' => $this->config->getApiKey(), |
61
|
|
|
'localVersion' => $this->getLocalVersionKey(), |
62
|
|
|
|
63
|
|
|
//for some methods... |
64
|
|
|
'countryId' => $this->config->getCountryCode(), |
65
|
|
|
'sessionId' => $this->allegroSessionHandle, |
66
|
|
|
|
67
|
|
|
//...for some other methods |
68
|
|
|
'countryCode' => $this->config->getCountryCode(), |
69
|
|
|
'sessionHandle' => $this->allegroSessionHandle, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function login() |
74
|
|
|
{ |
75
|
|
|
$data = [ |
76
|
|
|
'userLogin' => $this->config->getLogin() |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
if ($this->config->getPasswordHash()) { |
80
|
|
|
$data['userHashPassword'] = $this->config->getPasswordHash(); |
81
|
|
|
$response = $this->soapClient->doLoginEnc($this->getWebApiRequest($data)); |
82
|
|
|
} else { |
83
|
|
|
//ugly non-encrypted way, but sometimes sandbox fails to accept correct hashed login data |
84
|
|
|
$data['userPassword'] = $this->config->getPasswordPlain(); |
85
|
|
|
$response = $this->soapClient->doLogin($this->getWebApiRequest($data)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//validate basic response properties |
89
|
|
|
if (!is_object($response) || !isset($response->sessionHandlePart) || !isset($response->userId)) { |
90
|
|
|
throw new Exception("Invalid WebAPI doLoginEnc() response: " . print_r($response, 1)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->allegroSessionHandle = $response->sessionHandlePart; |
94
|
|
|
$this->allegroUserId = $response->userId; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
public function __call($name, $args) |
99
|
|
|
{ |
100
|
|
|
//prefix with WebAPI "do" prefix |
101
|
|
|
$name = 'do' . ucfirst($name); |
102
|
|
|
|
103
|
|
|
$request = $this->getWebApiRequest( |
104
|
|
|
array_key_exists(0, $args) ? $args[0] : [] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
return $this->soapClient->{$name}($request); |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
throw new Exception('WebAPI exception: ' . $e->getMessage()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|