2016-04-14 13:55:11 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class OpencartApiClient {
|
|
|
|
|
|
|
|
|
|
private $opencartStoreId = 0;
|
|
|
|
|
private $cookieFileName;
|
|
|
|
|
private $registry;
|
|
|
|
|
private $apiToken;
|
|
|
|
|
|
|
|
|
|
/* Совместимость с объектами ОС, например $this->model_module_name */
|
|
|
|
|
public function __get($name) {
|
|
|
|
|
return $this->registry->get($name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __construct(Registry &$registry) {
|
|
|
|
|
$this->registry = $registry;
|
|
|
|
|
|
|
|
|
|
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
|
|
|
|
$this->cookieFileName = $settings['retailcrm_apikey'];
|
|
|
|
|
|
|
|
|
|
$this->auth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getCookieValue($cookieName) {
|
|
|
|
|
$cookieFile = file_get_contents(DIR_APPLICATION . '/' . $this->cookieFileName . '.txt');
|
|
|
|
|
$cookieFile = explode("\n", $cookieFile);
|
|
|
|
|
|
|
|
|
|
$cookies = array();
|
|
|
|
|
foreach($cookieFile as $line) {
|
|
|
|
|
if(empty($line) OR $line{0} == '#')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
$params = explode("\t", $line);
|
|
|
|
|
$cookies[$params[5]] = $params[6];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(isset($cookies[$cookieName]))
|
|
|
|
|
return $cookies[$cookieName];
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
|
private function request($method, $getParams, $postParams) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
$opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId);
|
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
|
if (version_compare(VERSION, '2.1.0', '>=') && !empty($this->apiToken)) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
$getParams['token'] = $this->apiToken;
|
2018-02-26 13:23:36 +03:00
|
|
|
|
} elseif (is_array($this->apiToken) && isset($this->apiToken['username'])) {
|
|
|
|
|
$getParams = $this->apiToken;
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$postParams['fromApi'] = true;
|
|
|
|
|
|
|
|
|
|
if ($opencartStoreInfo) {
|
|
|
|
|
$url = $opencartStoreInfo['ssl'];
|
|
|
|
|
} else {
|
|
|
|
|
$url = HTTPS_CATALOG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$curl = curl_init();
|
|
|
|
|
|
|
|
|
|
// Set SSL if required
|
|
|
|
|
if (substr($url, 0, 5) == 'https') {
|
|
|
|
|
curl_setopt($curl, CURLOPT_PORT, 443);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false);
|
|
|
|
|
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : ''));
|
|
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postParams));
|
|
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_COOKIEFILE, DIR_APPLICATION . '/' . $this->cookieFileName . '.txt');
|
|
|
|
|
curl_setopt($curl, CURLOPT_COOKIEJAR, DIR_APPLICATION . '/' . $this->cookieFileName . '.txt');
|
|
|
|
|
|
|
|
|
|
$json = json_decode(curl_exec($curl), true);
|
|
|
|
|
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function auth() {
|
|
|
|
|
$apiUsers = $this->model_user_api->getApis();
|
|
|
|
|
|
|
|
|
|
$api = array();
|
|
|
|
|
foreach ($apiUsers as $apiUser) {
|
|
|
|
|
if($apiUser['status'] == 1) {
|
2018-02-26 13:23:36 +03:00
|
|
|
|
if (version_compare(VERSION, '2.1.0', '>=')) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
$api = array(
|
|
|
|
|
'api_id' => $apiUser['api_id'],
|
|
|
|
|
'key' => $apiUser['key']
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$api = array(
|
|
|
|
|
'api_id' => $apiUser['api_id'],
|
|
|
|
|
'username' => $apiUser['username'],
|
|
|
|
|
'password' => $apiUser['password']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
|
if(!isset($api['api_id'])) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
|
if (isset($api['key'])) {
|
|
|
|
|
$this->apiToken = $api['key'];
|
|
|
|
|
} elseif (isset($api['username'])) {
|
|
|
|
|
$this->apiToken = $api;
|
|
|
|
|
} else {
|
|
|
|
|
$this->apiToken = false;
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
|
/**
|
|
|
|
|
* Add history order
|
|
|
|
|
*
|
|
|
|
|
* @param int $order_id
|
|
|
|
|
* @param int $order_status_id
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function addHistory($order_id, $order_status_id)
|
|
|
|
|
{
|
|
|
|
|
$this->request('retailcrm/addOrderHistory', array(), array('order_id' => $order_id, 'order_status_id' => $order_status_id));
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 11:24:35 +03:00
|
|
|
|
public function getDeliveryTypes() {
|
|
|
|
|
return $this->request('retailcrm/getDeliveryTypes', array(), array());
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 13:55:11 +03:00
|
|
|
|
private function getInnerIpAddr() {
|
|
|
|
|
$opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId);
|
|
|
|
|
|
|
|
|
|
if ($opencartStoreInfo) {
|
|
|
|
|
$url = $opencartStoreInfo['ssl'];
|
|
|
|
|
} else {
|
|
|
|
|
$url = HTTPS_CATALOG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$curl = curl_init();
|
|
|
|
|
|
|
|
|
|
// Set SSL if required
|
|
|
|
|
if (substr($url, 0, 5) == 'https') {
|
|
|
|
|
curl_setopt($curl, CURLOPT_PORT, 443);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false);
|
|
|
|
|
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url . 'system/cron/getmyip.php');
|
|
|
|
|
|
|
|
|
|
return curl_exec($curl);
|
|
|
|
|
}
|
2016-04-15 12:02:42 +03:00
|
|
|
|
}
|