Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/wbs24.ozonapinew/lib/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew; use Bitrix\Main\SystemException; class Api { use Exception; // trait protected $main; protected $moduleId; protected $wrappers; protected $debug = false; protected $accountIndex; protected $OrdersState; protected $DeliveryServices; protected $requestBase; protected $requestAllOrders; protected $requestOrderStatus; protected $numberOfDaysToProcessOrders; protected $isHttps; protected $testMode; protected $saveApiLog; protected $param; protected $limit; protected $offset; public function __construct($objects = []) { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); $this->accountIndex = $this->wrappers->Option->getAccountIndex(); $this->OrdersState = $object['OrdersState'] ?? new OrdersState($this->accountIndex); $this->DeliveryServices = $object['DeliveryServices'] ?? new DeliveryServices($this->accountIndex); $this->ErrorProcessing = $object['ErrorProcessing'] ?? new ErrorProcessing($objects); $this->numberOfDaysToProcessOrders = $this->wrappers->Option->get($this->moduleId, 'numberOfDaysToProcessOrders'); $this->isHttps = $this->wrappers->Option->get($this->moduleId, 'isHttps'); $this->testMode = $this->wrappers->Option->get($this->moduleId, 'testMode'); $this->saveApiLog = $this->wrappers->Option->get($this->moduleId, 'saveApiLog'); if ($this->saveApiLog == 'Y') $this->debug = true; // изначальные настройки лимита на получение товаров и позиции смещения $this->limit = 50; $this->offset = 0; $this->setRequestsUrls(); } protected function setRequestsUrls() { $this->requestAllOrders = $this->getRequestUrl('/v3/posting/fbs/list'); $this->requestOrderStatus = $this->getRequestUrl('/v3/posting/fbs/get'); } protected function getRequestUrl($apiMethod) { return $this->getRequestBase().$apiMethod.$this->getExtOfRequestUrl(); } protected function getRequestBase() { $baseUrl = 'https://api-seller.ozon.ru'; if ($this->testMode == 'Y') { $domain = $this->wrappers->Option->get('main', 'server_name'); $baseUrl = 'http'.($this->isHttps == 'Y' ? 's' : '').'://'.$domain.'/bitrix/tools/'.$this->moduleId.'/test'; } return $baseUrl; } protected function getExtOfRequestUrl() { return ($this->testMode == 'Y') ? '.php' : ''; } public function getAllOrders() { $allOrders = []; do { $orders = $this->getOrders(); $ordersResult = $orders['result']['postings'] ?? []; $isHasNext = $orders['result']['has_next'] ?? false; $allOrders = array_merge($allOrders, $ordersResult); if ($isHasNext == true) { $this->offset += $this->limit; } } while ($isHasNext == true); return $allOrders; } public function getOrders() { $this->checkSinceAndToVariables(); $requestBody = $this->createRequestBody($this->requestAllOrders); $orders = $this->createRequest($this->requestAllOrders, $requestBody); return $orders ?? false; } public function getShippingInformation($externalId) { $requestBody = $this->createRequestBody($this->requestOrderStatus, $externalId); $shippingInformation = $this->createRequest($this->requestOrderStatus, $requestBody); return $shippingInformation['result'] ?? []; } protected function checkSinceAndToVariables() { $param = [ 'since' => date('Y-m-d', strtotime('-1 days')), 'to' => date('Y-m-d'), ]; $this->param = $param; return true; } protected function createRequestBody($url, $externalId = null, $prepareInfo = null) { $arrayOfBodiesToRequest = [ $this->requestAllOrders => [ 'requestBody' => [ 'dir' => 'ASC', 'filter' => [ 'since' => $this->param['since'] . 'T00:00:00.000Z', 'to' => $this->param['to'] . 'T23:59:59.000Z', ], 'limit' => $this->limit, 'offset' => $this->offset, ], ], $this->requestOrderStatus => [ 'requestBody' => [ 'posting_number' => $externalId, 'with' => [ 'product_exemplars' => true, ], ], ], ]; foreach ($arrayOfBodiesToRequest as $requestName => $requestBody) { if (strpos($url, $requestName) !== false) { return $requestBody['requestBody']; } } } protected function createRequest($url, $data = [], $getPdf = false) { try { $apiKey = trim($this->wrappers->Option->get($this->moduleId, 'apiKey')); $clientId = trim($this->wrappers->Option->get($this->moduleId, 'clientId')); if (!$apiKey || !$clientId) return; $headers = [ 'Client-Id: ' . $clientId, 'Api-Key: ' . $apiKey, ]; if (!empty($data)) $headers[] = 'Content-Type: application/json'; $request = json_encode($data); if ($this->debug) { $this->createReport('api_log.txt', 'request to '.$url.' (account '.$this->accountIndex.'): '.$request); } $curl = $this->wrappers->Curl->curl_init($url); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_HEADER, false); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_POST, true); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_POSTFIELDS, empty($data) ? '' : $request); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_TIMEOUT, 30); $this->wrappers->Curl->curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = $this->wrappers->Curl->curl_exec($curl); $error = $this->wrappers->Curl->curl_error($curl); $this->wrappers->Curl->curl_close($curl); if ($error) { $result = 'cURL Error: ' . $error; } else { if ($getPdf) { $json = json_decode($response, true); if (empty($json['message'])) { $result = $response; $log = 'PDF file'; } else { $log = $response; } } else { $result = json_decode($response, true); $log = $response; } if ($this->debug) $this->createReport('api_log.txt', 'response: '.$log); } } catch (SystemException $exception) { $this->exceptionHandler($exception); } $baseUrl = $this->getRequestBase(); $this->ErrorProcessing->verifyErrors($data, $result, $url, $baseUrl); return $result; } }