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;
|
2017-09-06 13:08:39 +03:00
|
|
|
|
$moduleTitle = $this->getModuleTitle();
|
|
|
|
|
$settings = $this->model_setting_setting->getSetting($moduleTitle);
|
|
|
|
|
$this->cookieFileName = $settings[$moduleTitle . '_apikey'];
|
2016-04-14 13:55:11 +03:00
|
|
|
|
|
|
|
|
|
$this->auth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getCookieValue($cookieName) {
|
2017-11-17 16:32:18 +03:00
|
|
|
|
if (!file_exists(DIR_APPLICATION . $this->cookieFileName . '.txt')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cookieFile = file_get_contents(DIR_APPLICATION . $this->cookieFileName . '.txt');
|
2016-04-14 13:55:11 +03:00
|
|
|
|
$cookieFile = explode("\n", $cookieFile);
|
|
|
|
|
|
|
|
|
|
$cookies = array();
|
|
|
|
|
foreach($cookieFile as $line) {
|
2018-02-26 13:22:13 +03:00
|
|
|
|
if(empty($line) OR $line{0} == '#') {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
continue;
|
2018-02-26 13:22:13 +03:00
|
|
|
|
}
|
2016-04-14 13:55:11 +03:00
|
|
|
|
|
|
|
|
|
$params = explode("\t", $line);
|
|
|
|
|
$cookies[$params[5]] = $params[6];
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:22:13 +03:00
|
|
|
|
if (isset($cookies[$cookieName])) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
return $cookies[$cookieName];
|
2018-02-26 13:22:13 +03:00
|
|
|
|
}
|
2016-04-14 13:55:11 +03:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:22:13 +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:22:13 +03:00
|
|
|
|
if ($this->apiToken !== false) {
|
2017-07-11 17:39:48 +03:00
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
2018-02-26 13:22:13 +03:00
|
|
|
|
$getParams['key'] = $this->apiToken['key'];
|
2017-07-11 17:39:48 +03:00
|
|
|
|
} else {
|
2018-02-26 13:22:13 +03:00
|
|
|
|
$getParams['key'] = $this->apiToken['key'];
|
|
|
|
|
$getParams['username'] = $this->apiToken['username'];
|
|
|
|
|
|
|
|
|
|
if (isset($this->session->data['user_token'])) {
|
|
|
|
|
$getParams['api_token'] = $this->session->data['user_token'];
|
|
|
|
|
} else {
|
|
|
|
|
$session = $this->registry->get('session');
|
|
|
|
|
$session->start();
|
|
|
|
|
$getParams['api_token'] = $session->getId();
|
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
}
|
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));
|
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
|
curl_setopt($curl, CURLOPT_COOKIEFILE, DIR_APPLICATION . $this->cookieFileName . '.txt');
|
|
|
|
|
curl_setopt($curl, CURLOPT_COOKIEJAR, DIR_APPLICATION . $this->cookieFileName . '.txt');
|
2016-04-14 13:55:11 +03:00
|
|
|
|
|
|
|
|
|
$json = json_decode(curl_exec($curl), true);
|
|
|
|
|
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
|
if (!$json && json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
|
if (isset($json['error'])) {
|
|
|
|
|
if (is_array($json['error'])) {
|
|
|
|
|
foreach ($json['error'] as $error) {
|
|
|
|
|
error_log(date('Y-m-d H:i:s') . " @ " . "[$method]" . ' - ' . $error . "\n", 3, DIR_LOGS . "opencartapi.log");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error_log(date('Y-m-d H:i:s') . " @ " . "[$method]" . ' - ' . $json['error'] . "\n", 3, DIR_LOGS . "opencartapi.log");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function auth() {
|
2018-01-23 16:34:20 +03:00
|
|
|
|
$this->load->model('user/api');
|
|
|
|
|
|
2016-04-14 13:55:11 +03:00
|
|
|
|
$apiUsers = $this->model_user_api->getApis();
|
|
|
|
|
|
|
|
|
|
$api = array();
|
2018-02-26 13:22:13 +03:00
|
|
|
|
|
2016-04-14 13:55:11 +03:00
|
|
|
|
foreach ($apiUsers as $apiUser) {
|
|
|
|
|
if($apiUser['status'] == 1) {
|
2017-07-11 17:39:48 +03:00
|
|
|
|
$api = $apiUser;
|
2016-04-14 13:55:11 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:22:13 +03:00
|
|
|
|
if (!isset($api['api_id'])) {
|
2016-04-14 13:55:11 +03:00
|
|
|
|
return false;
|
2017-03-22 11:51:48 +03:00
|
|
|
|
}
|
2016-04-14 13:55:11 +03:00
|
|
|
|
|
2018-02-26 13:22:13 +03:00
|
|
|
|
if (isset($api) && !empty($api)) {
|
|
|
|
|
$this->apiToken = $api;
|
2017-07-11 17:39:48 +03:00
|
|
|
|
} else {
|
2018-02-26 13:22:13 +03:00
|
|
|
|
$this->apiToken = false;
|
2017-07-11 17:39:48 +03:00
|
|
|
|
}
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 13:22:13 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get delivery types
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getDeliveryTypes()
|
|
|
|
|
{
|
|
|
|
|
return $this->request('retailcrm/getDeliveryTypes', array(), array());
|
2016-04-14 13:55:11 +03:00
|
|
|
|
}
|
2017-07-21 10:27:08 +03:00
|
|
|
|
|
2017-07-27 14:41:58 +03:00
|
|
|
|
/**
|
2018-02-26 13:22:13 +03:00
|
|
|
|
* Add history order
|
|
|
|
|
*
|
|
|
|
|
* @param int $order_id
|
|
|
|
|
* @param int $order_status_id
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2017-07-27 14:41:58 +03:00
|
|
|
|
*/
|
2018-02-26 13:22:13 +03:00
|
|
|
|
public function addHistory($order_id, $order_status_id)
|
|
|
|
|
{
|
|
|
|
|
$this->request('retailcrm/addOrderHistory', array(), array('order_id' => $order_id, 'order_status_id' => $order_status_id));
|
2017-07-21 10:27:08 +03:00
|
|
|
|
}
|
2017-09-06 13:08:39 +03:00
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get module name
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-09-06 13:08:39 +03:00
|
|
|
|
private function getModuleTitle()
|
|
|
|
|
{
|
|
|
|
|
if (version_compare(VERSION, '3.0', '<')){
|
|
|
|
|
$title = 'retailcrm';
|
|
|
|
|
} else {
|
|
|
|
|
$title = 'module_retailcrm';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
|
}
|
2016-04-15 12:02:42 +03:00
|
|
|
|
}
|