1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
client.php
См. документацию.
1<?
2
4
14
15Loc::loadMessages(__FILE__);
16
17class Client
18{
19 const SERVICE_HOST = 'https://properties.bitrix24.tech';
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
230 $additionalParams['client_id'] = $this->accessSettings['client_id'];
231 $additionalParams['client_secret'] = $this->accessSettings['client_secret'];
232 if($licenseCheck)
233 $additionalParams['key'] = static::getLicenseHash();
234
235 $http = new HttpClient(array('socketTimeout' => $this->httpTimeout));
236 $result = $http->post(
237 static::SERVICE_HOST.static::REST_URI.$methodName,
238 $additionalParams
239 );
240
241 if($result === false)
242 {
243 $httpErrors = $http->getError();
244 foreach ($httpErrors as $errorCode => $errorText)
245 {
246 $this->errorCollection->add(array(new Error($errorText, $errorCode)));
247 }
248 return false;
249 }
250
251 $answer = $this->prepareAnswer($result);
252
253 if(!is_array($answer) || count($answer) == 0)
254 {
255 $this->errorCollection->add(array(new Error('Malformed answer from service: '.$http->getStatus().' '.$result, static::ERROR_SERVICE_UNAVAILABLE)));
256 return false;
257 }
258
259 if(array_key_exists('error', $answer))
260 {
261 if($answer['error'] === 'verification_needed' && !$licenseCheck)
262 {
263 return $this->call($methodName, $additionalParams, true);
264 }
265 else if(($answer['error'] === 'ACCESS_DENIED' || $answer['error'] === 'Invalid client')
266 && !$clearAccessSettings)
267 {
268 return $this->call($methodName, $additionalParams, true, true);
269 }
270
271 $this->errorCollection->add(array(new Error($answer['error_description'], $answer['error'])));
272 return false;
273 }
274
275 if($answer['result'] == false)
276 {
277 $this->errorCollection->add(array(new Error(Loc::getMessage('SALE_PROPERTIES_ERROR_NOTHING_FOUND'), static::ERROR_NOTHING_FOUND)));
278 }
279
280 return $answer['result'];
281 }
282
288 protected function prepareAnswer($result)
289 {
290 try
291 {
292 return Json::decode($result);
293 }
294 catch (ArgumentException $e)
295 {
296 return false;
297 }
298 }
299
304 protected function register()
305 {
306 $httpClient = new HttpClient();
307
308 $queryParams = array(
309 "key" => static::getLicenseHash(),
310 "scope" => static::SCOPE,
311 "redirect_uri" => static::getRedirectUri(),
312 );
313
314 $result = $httpClient->post(static::SERVICE_HOST.static::REGISTER_URI, $queryParams);
315
316 if($result === false)
317 {
318 $this->errorCollection->add(array(new Error($result["error"], static::ERROR_SERVICE_UNAVAILABLE)));
319 return false;
320 }
321
322 $result = Json::decode($result);
323 if($result["error"])
324 {
325 $this->errorCollection->add(array(new Error($result["error"], static::ERROR_WRONG_LICENSE)));
326 return false;
327 }
328 else
329 {
330 return $result;
331 }
332 }
333
339 protected static function setAccessSettings(array $params)
340 {
341 Option::set('sale', static::SERVICE_ACCESS_OPTION, serialize($params));
342 }
343
348 protected function getAccessSettings()
349 {
350 $accessSettings = Option::get('sale', static::SERVICE_ACCESS_OPTION);
351
352 if($accessSettings != '')
353 {
354 return unserialize($accessSettings, ["allowed_classes" => false]);
355 }
356 else
357 {
358 if($accessSettings = $this->register())
359 {
361 return $accessSettings;
362 }
363 else
364 {
365 return false;
366 }
367 }
368 }
369
374 public function clearAccessSettings()
375 {
376 Option::set('sale', static::SERVICE_ACCESS_OPTION, null);
377 }
378
383 protected static function getRedirectUri()
384 {
385 $request = Context::getCurrent()->getRequest();
386
387 $host = $request->getHttpHost();
388 $isHttps = $request->isHttps();
389
390 return ($isHttps ? 'https' : 'http').'://'.$host."/";
391 }
392
397 public function getErrors()
398 {
399 return $this->errorCollection->toArray();
400 }
401
406 protected static function getLicenseHash()
407 {
408 return md5(LICENSE_KEY);
409 }
410}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
Определения error.php:15
Определения json.php:9
uaSearchByName($name, $limit, $offset=0)
Определения client.php:181
const ERROR_NOTHING_FOUND
Определения client.php:39
getByInn($inn, $showTerminated=false)
Определения client.php:70
const METHOD_COMMON_GET_BY_INN
Определения client.php:24
uaGetByEdrpou($edrpou)
Определения client.php:116
const METHOD_IP_SEARCH_BY_NAME
Определения client.php:27
const SERVICE_ACCESS_OPTION
Определения client.php:23
const METHOD_COMMON_GET_BY_OGRN
Определения client.php:25
static setAccessSettings(array $params)
Определения client.php:339
static getLicenseHash()
Определения client.php:406
const ERROR_WRONG_LICENSE
Определения client.php:37
const METHOD_ORGANIZATION_SEARCH_BY_NAME
Определения client.php:26
const METHOD_UA_GET_FO_BY_ID
Определения client.php:30
const METHOD_UA_GET_BY_EDRPOU
Определения client.php:28
const ERROR_SERVICE_UNAVAILABLE
Определения client.php:38
const METHOD_UA_SEARCH_FO_BY_NAME
Определения client.php:32
searchOrganizationByName($name, $limit, $offset=0)
Определения client.php:82
const METHOD_IS_SERVICE_ONLINE
Определения client.php:34
const METHOD_UA_GET_UO_BY_ID
Определения client.php:29
const METHOD_UA_SEARCH_BY_NAME
Определения client.php:33
static getRedirectUri()
Определения client.php:383
call($methodName, $additionalParams=null, $licenseCheck=false, $clearAccessSettings=false)
Определения client.php:212
searchIpByName($name, $secondName, $lastName, $limit, $offset=0)
Определения client.php:100
getByOgrn($ogrn, $showTerminated=false)
Определения client.php:60
uaSearchUoByName($name, $limit, $offset=0)
Определения client.php:148
uaSearchFoByName($name, $limit, $offset=0)
Определения client.php:164
prepareAnswer($result)
Определения client.php:288
const METHOD_UA_SEARCH_UO_BY_NAME
Определения client.php:31
const METHOD_COMMON_GET_BY_BIC
Определения client.php:35
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
const LICENSE_KEY($show_sql_stat=='Y')
Определения start.php:84
$name
Определения menu_edit.php:35
Определения culture.php:9
$host
Определения mysql_to_pgsql.php:32
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799