Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?
2
4
14
15Loc::loadMessages(__FILE__);
16
17class Client
18{
19 const SERVICE_HOST = 'https://properties.bitrix.info';
20 const REST_URI = '/rest/';
21 const REGISTER_URI = '/oauth/register/';
22 const SCOPE = 'ps';
23 const SERVICE_ACCESS_OPTION = 'properties_service_access';
24 const METHOD_COMMON_GET_BY_INN = 'ps.common.getByInn';
25 const METHOD_COMMON_GET_BY_OGRN = 'ps.common.getByOgrn';
26 const METHOD_ORGANIZATION_SEARCH_BY_NAME = 'ps.organization.searchByName';
27 const METHOD_IP_SEARCH_BY_NAME = 'ps.ip.searchByName';
28 const METHOD_UA_GET_BY_EDRPOU = 'ps.ua.getByEdrpou';
29 const METHOD_UA_GET_UO_BY_ID = 'ps.ua.getUoById';
30 const METHOD_UA_GET_FO_BY_ID = 'ps.ua.getFoById';
31 const METHOD_UA_SEARCH_UO_BY_NAME = 'ps.ua.searchUoByName';
32 const METHOD_UA_SEARCH_FO_BY_NAME = 'ps.ua.searchFoByName';
33 const METHOD_UA_SEARCH_BY_NAME = 'ps.ua.searchByName';
34 const METHOD_IS_SERVICE_ONLINE = 'ps.common.isOnline';
35 const METHOD_COMMON_GET_BY_BIC = 'ps.bic.getByBic';
40
41 protected $httpTimeout = 5;
42 protected $accessSettings = null;
43
46
50 public function __construct()
51 {
52 $this->errorCollection = new ErrorCollection();
53 }
54
60 public function getByOgrn($ogrn, $showTerminated = false)
61 {
62 return $this->call(static::METHOD_COMMON_GET_BY_OGRN, array('ogrn' => $ogrn, 'showTerminated'=> $showTerminated));
63 }
64
70 public function getByInn($inn, $showTerminated = false)
71 {
72 return $this->call(static::METHOD_COMMON_GET_BY_INN, array('inn' => $inn, 'showTerminated' => $showTerminated));
73 }
74
82 public function searchOrganizationByName($name, $limit, $offset = 0)
83 {
84 return $this->call(static::METHOD_ORGANIZATION_SEARCH_BY_NAME, array(
85 'name' => $name,
86 'limit' => $limit,
87 'offset' => $offset
88 ));
89 }
90
100 public function searchIpByName($name, $secondName, $lastName, $limit, $offset = 0)
101 {
102 return $this->call(static::METHOD_IP_SEARCH_BY_NAME, array(
103 'name' => $name,
104 'second_name' => $secondName,
105 'last_name' => $lastName,
106 'limit' => $limit,
107 'offset' => $offset
108 ));
109 }
110
116 public function uaGetByEdrpou($edrpou)
117 {
118 return $this->call(static::METHOD_UA_GET_BY_EDRPOU, array('edrpou' => $edrpou));
119 }
120
126 public function uaGetUoById($id)
127 {
128 return $this->call(static::METHOD_UA_GET_UO_BY_ID, array('id' => $id));
129 }
130
136 public function uaGetFoById($id)
137 {
138 return $this->call(static::METHOD_UA_GET_FO_BY_ID, array('id' => $id));
139 }
140
148 public function uaSearchUoByName($name, $limit, $offset = 0)
149 {
150 return $this->call(static::METHOD_UA_SEARCH_UO_BY_NAME, array(
151 'name' => $name,
152 'limit' => $limit,
153 'offset' => $offset
154 ));
155 }
156
164 public function uaSearchFoByName($name, $limit, $offset = 0)
165 {
166 return $this->call(static::METHOD_UA_SEARCH_FO_BY_NAME, array(
167 'name' => $name,
168 'limit' => $limit,
169 'offset' => $offset
170 ));
171 }
172
181 public function uaSearchByName($name, $limit, $offset = 0)
182 {
183 return $this->call(static::METHOD_UA_SEARCH_BY_NAME, array(
184 'name' => $name,
185 'limit' => $limit,
186 'offset' => $offset
187 ));
188 }
189
190 public function getByBic($bic)
191 {
192 return $this->call(static::METHOD_COMMON_GET_BY_BIC, ['bic' => $bic]);
193 }
194
199 public function isServiceOnline()
200 {
201 return $this->call(static::METHOD_IS_SERVICE_ONLINE);
202 }
203
212 protected function call($methodName, $additionalParams = null, $licenseCheck = false, $clearAccessSettings = false)
213 {
214 $this->errorCollection->clear();
215
216 if($clearAccessSettings)
217 $this->clearAccessSettings();
218
219 if(is_null($this->accessSettings))
220 $this->accessSettings = $this->getAccessSettings();
221
222 if($this->accessSettings === false)
223 return false;
224
225 if(!is_array($additionalParams))
226 {
227 $additionalParams = array();
228 }
229 else
230 {
231 $additionalParams = Encoding::convertEncodingArray($additionalParams, LANG_CHARSET, "utf-8");
232 }
233
234 $additionalParams['client_id'] = $this->accessSettings['client_id'];
235 $additionalParams['client_secret'] = $this->accessSettings['client_secret'];
236 if($licenseCheck)
237 $additionalParams['key'] = static::getLicenseHash();
238
239 $http = new HttpClient(array('socketTimeout' => $this->httpTimeout));
240 $result = $http->post(
241 static::SERVICE_HOST.static::REST_URI.$methodName,
242 $additionalParams
243 );
244
245 if($result === false)
246 {
247 $httpErrors = $http->getError();
248 foreach ($httpErrors as $errorCode => $errorText)
249 {
250 $this->errorCollection->add(array(new Error($errorText, $errorCode)));
251 }
252 return false;
253 }
254
255 $answer = $this->prepareAnswer($result);
256
257 if(!is_array($answer) || count($answer) == 0)
258 {
259 $this->errorCollection->add(array(new Error('Malformed answer from service: '.$http->getStatus().' '.$result, static::ERROR_SERVICE_UNAVAILABLE)));
260 return false;
261 }
262
263 if(array_key_exists('error', $answer))
264 {
265 if($answer['error'] === 'verification_needed' && !$licenseCheck)
266 {
267 return $this->call($methodName, $additionalParams, true);
268 }
269 else if(($answer['error'] === 'ACCESS_DENIED' || $answer['error'] === 'Invalid client')
270 && !$clearAccessSettings)
271 {
272 return $this->call($methodName, $additionalParams, true, true);
273 }
274
275 $this->errorCollection->add(array(new Error($answer['error_description'], $answer['error'])));
276 return false;
277 }
278
279 if($answer['result'] == false)
280 {
281 $this->errorCollection->add(array(new Error(Loc::getMessage('SALE_PROPERTIES_ERROR_NOTHING_FOUND'), static::ERROR_NOTHING_FOUND)));
282 }
283
284 return $answer['result'];
285 }
286
292 protected function prepareAnswer($result)
293 {
294 try
295 {
296 return Json::decode($result);
297 }
298 catch (ArgumentException $e)
299 {
300 return false;
301 }
302 }
303
308 protected function register()
309 {
310 $httpClient = new HttpClient();
311
312 $queryParams = array(
313 "key" => static::getLicenseHash(),
314 "scope" => static::SCOPE,
315 "redirect_uri" => static::getRedirectUri(),
316 );
317
318 $result = $httpClient->post(static::SERVICE_HOST.static::REGISTER_URI, $queryParams);
319
320 if($result === false)
321 {
322 $this->errorCollection->add(array(new Error($result["error"], static::ERROR_SERVICE_UNAVAILABLE)));
323 return false;
324 }
325
326 $result = Json::decode($result);
327 if($result["error"])
328 {
329 $this->errorCollection->add(array(new Error($result["error"], static::ERROR_WRONG_LICENSE)));
330 return false;
331 }
332 else
333 {
334 return $result;
335 }
336 }
337
343 protected static function setAccessSettings(array $params)
344 {
345 Option::set('sale', static::SERVICE_ACCESS_OPTION, serialize($params));
346 }
347
352 protected function getAccessSettings()
353 {
354 $accessSettings = Option::get('sale', static::SERVICE_ACCESS_OPTION);
355
356 if($accessSettings != '')
357 {
358 return unserialize($accessSettings, ["allowed_classes" => false]);
359 }
360 else
361 {
362 if($accessSettings = $this->register())
363 {
365 return $accessSettings;
366 }
367 else
368 {
369 return false;
370 }
371 }
372 }
373
378 public function clearAccessSettings()
379 {
380 Option::set('sale', static::SERVICE_ACCESS_OPTION, null);
381 }
382
387 protected static function getRedirectUri()
388 {
389 $request = Context::getCurrent()->getRequest();
390
391 $host = $request->getHttpHost();
392 $isHttps = $request->isHttps();
393
394 return ($isHttps ? 'https' : 'http').'://'.$host."/";
395 }
396
401 public function getErrors()
402 {
403 return $this->errorCollection->toArray();
404 }
405
410 protected static function getLicenseHash()
411 {
412 return md5(LICENSE_KEY);
413 }
414}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
uaSearchByName($name, $limit, $offset=0)
Definition client.php:181
getByInn($inn, $showTerminated=false)
Definition client.php:70
static setAccessSettings(array $params)
Definition client.php:343
searchOrganizationByName($name, $limit, $offset=0)
Definition client.php:82
call($methodName, $additionalParams=null, $licenseCheck=false, $clearAccessSettings=false)
Definition client.php:212
searchIpByName($name, $secondName, $lastName, $limit, $offset=0)
Definition client.php:100
getByOgrn($ogrn, $showTerminated=false)
Definition client.php:60
uaSearchUoByName($name, $limit, $offset=0)
Definition client.php:148
uaSearchFoByName($name, $limit, $offset=0)
Definition client.php:164