history for opencart >=2

This commit is contained in:
dkorol 2016-04-14 13:55:11 +03:00
parent acd3591b28
commit 76dc6535cc
6 changed files with 354 additions and 5 deletions

0
admin/controller/module/retailcrm.php Normal file → Executable file
View File

24
admin/model/retailcrm/history.php Normal file → Executable file
View File

@ -4,10 +4,15 @@ class ModelRetailcrmHistory extends Model
{
protected $createResult;
private $opencartApiClient;
public function request()
{
$this->load->model('setting/setting');
$this->load->model('setting/store');
if(version_compare(VERSION, '2.0.0', '>=')) {
$this->load->model('user/api');
}
$this->load->model('sale/order');
if (version_compare(VERSION, '2.1.0.0', '>=')) {
$this->load->model('customer/customer');
@ -32,6 +37,10 @@ class ModelRetailcrmHistory extends Model
return false;
}
if(version_compare(VERSION, '2.0.0', '>=')) {
$this->opencartApiClient = new OpencartApiClient($this->registry);
}
$crm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
@ -100,7 +109,6 @@ class ModelRetailcrmHistory extends Model
if (!empty($this->createResult['orders'])) {
$crm->ordersFixExternalIds($this->createResult['orders']);
}
}
protected function updateOrders($orders)
@ -117,7 +125,7 @@ class ModelRetailcrmHistory extends Model
$data['firstname'] = $order['firstName'];
$data['lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$data['email'] = $order['email'];
$data['telephone'] = (!empty($order['phone']['number'])) ? $order['phone']['number'] : ' ';
$data['telephone'] = (!empty($order['phone'])) ? $order['phone'] : '';
$data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : '';
$data['fax'] = '';
@ -239,7 +247,11 @@ class ModelRetailcrmHistory extends Model
$data['order_status_id'] = $tmpOrder['order_status_id'];
}
$this->model_sale_order->editOrder($order['externalId'], $data);
if(version_compare(VERSION, '2.0.0', '>=')) {
$this->opencartApiClient->editOrder($order['externalId'], $data);
} else {
$this->model_sale_order->editOrder($order['externalId'], $data);
}
}
}
@ -431,7 +443,11 @@ class ModelRetailcrmHistory extends Model
$data['fromApi'] = true;
$data['order_status_id'] = 1;
$this->model_sale_order->addOrder($data);
if(version_compare(VERSION, '2.0.0', '>=')) {
$this->opencartApiClient->addOrder($data);
} else {
$this->model_sale_order->addOrder($data);
}
$last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1, 'start' => 0));

0
catalog/controller/module/retailcrm.php Normal file → Executable file
View File

4
catalog/model/retailcrm/order.php Normal file → Executable file
View File

@ -4,6 +4,8 @@ class ModelRetailcrmOrder extends Model {
public function sendToCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
@ -105,6 +107,8 @@ class ModelRetailcrmOrder extends Model {
public function changeInCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');

2
system/cron/getmyip.php Normal file
View File

@ -0,0 +1,2 @@
<?php
die($_SERVER['REMOTE_ADDR']);

View File

@ -0,0 +1,327 @@
<?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;
}
public function request($method, $getParams, $postParams) {
$opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId);
if(version_compare(VERSION, '2.1.0', '>=') && !empty($this->apiToken)) {
$getParams['token'] = $this->apiToken;
}
$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);
//$json = curl_exec($curl);
//var_dump($json);
//var_dump(curl_getinfo($curl));
//if(isset($json['error']))
//return false;
curl_close($curl);
return $json;
}
private function auth() {
$apiUsers = $this->model_user_api->getApis();
$api = array();
foreach ($apiUsers as $apiUser) {
if($apiUser['status'] == 1) {
if(version_compare(VERSION, '2.1.0', '>=')) {
$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;
}
}
if(!isset($api['api_id']))
return false;
if(version_compare(VERSION, '2.1.0', '>=')) {
$alreadyBinded = false;
$innerIp = $this->getInnerIpAddr();
$apiIps = $this->model_user_api->getApiIps($api['api_id']);
foreach($apiIps as $apiIp) {
if($apiIp['ip'] == $innerIp)
$alreadyBinded = true;
}
if(!$alreadyBinded) {
$this->model_user_api->addApiIp($api['api_id'], $innerIp);
}
}
$apiAnswer = $this->request('login', array(), $apiUser);
if(version_compare(VERSION, '2.1.0', '>=')) {
$this->apiToken = $apiAnswer['token'];
}
return $apiAnswer;
}
public function editOrder($order_id, $data) {
$data['telephone'] = trim($data['telephone']);
$customer = array(
'currency' => isset($data['currency']) ? $data['currency'] : '',
'customer' => $data['customer'],
'customer_id' => $data['customer_id'],
'customer_group_id' => $data['customer_group_id'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'telephone' => !empty($data['telephone']) ? $data['telephone'] : '0000',
'fax' => $data['fax'],
);
$a = $this->request('customer', array(), $customer);
$products = array();
foreach ($data['order_product'] as $order_product) {
$products[] = array(
'product_id' => $order_product['product_id'],
'quantity' => $order_product['quantity']
);
}
$b = $this->request('cart/add', array(), array('product' => $products));
$payment_address = array(
'payment_address' => $data['payment_address'],
'firstname' => $data['payment_firstname'],
'lastname' => $data['payment_lastname'],
'company' => $data['payment_company'],
'address_1'=> $data['payment_address_1'],
'address_2' => $data['payment_address_2'],
'city' => !empty($data['payment_city']) ? $data['payment_city'] : 'none',
'postcode' => $data['payment_postcode'],
'country_id' => $data['payment_country_id'],
'zone_id' => !empty($data['payment_zone_id']) ? $data['payment_zone_id'] : 0,
);
$c = $this->request('payment/address', array(), $payment_address);
$this->request('payment/methods', array(), array());
$payment_method = array(
'payment_method' => $data['payment_code']
);
$d = $this->request('payment/method', array(), $payment_method);
$shipping_address = array(
'shipping_address' => $data['shipping_address'],
'firstname' => $data['shipping_firstname'],
'lastname' => $data['shipping_lastname'],
'company' => $data['shipping_company'],
'address_1' => $data['shipping_address_1'],
'address_2' => $data['shipping_address_2'],
'city' => !empty($data['shipping_city']) ? $data['shipping_city'] : 'none',
'postcode' => $data['shipping_postcode'],
'country_id' => $data['shipping_country_id'],
'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0,
);
$e = $this->request('shipping/address', array(), $shipping_address);
$this->request('shipping/methods', array(), array());
$shipping_method = array(
'shipping_method' => $data['shipping_code']
);
$f = $this->request('shipping/method', array(), $shipping_method);
$order = array(
'shipping_method' => $data['shipping_code'],
'payment_method' => $data['payment_code'],
'order_status_id' => $data['order_status_id'],
'comment' => $data['comment'],
'affiliate_id' => $data['affiliate_id'],
);
$g = $this->request('order/edit', array('order_id' => $order_id), $order);
var_dump($a, $b, $c, $d, $e, $f, $g);
}
public function addOrder($data) {
$currency = $this->getCookieValue('currency');
if($currency) {
$a = $this->request('currency', array(), array('currency' => $currency));
}
$customer = array(
'store_id' => $data['store_id'],
'currency' => $currency != false ? $currency : '',
'customer' => $data['customer'],
'customer_id' => $data['customer_id'],
'customer_group_id' => $data['customer_group_id'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'telephone' => $data['telephone'],
'fax' => $data['fax'],
);
$b = $this->request('customer', array(), $customer);
$products = array();
foreach($data['order_product'] as $product) {
$product = array(
'product_id' => $product['product_id'],
'quantity' => $product['quantity'],
);
$products[] = $product;
}
$c = $this->request('cart/add', array(), array('product' => $products));
$payment_address = array(
'payment_address' => $data['payment_address'],
'firstname' => $data['payment_firstname'],
'lastname' => $data['payment_lastname'],
'company' => $data['payment_company'],
'address_1' => $data['payment_address_1'],
'address_2' => $data['payment_address_2'],
'city' => $data['payment_city'],
'postcode' => $data['payment_postcode'],
'country_id' => $data['payment_country_id'],
'zone_id' => $data['payment_zone_id'],
);
$d = $this->request('payment/address', array(), $payment_address);
$shipping_address = array(
'shipping_address' => $data['shipping_address'],
'firstname' => $data['shipping_firstname'],
'lastname' => $data['shipping_lastname'],
'company' => $data['shipping_company'],
'address_1' => $data['shipping_address_1'],
'address_2' => $data['shipping_address_2'],
'city' => $data['shipping_city'],
'postcode' => $data['shipping_postcode'],
'country_id' => $data['shipping_country_id'],
'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0,
);
$e = $this->request('shipping/address', array(), $shipping_address);
$this->request('shipping/methods', array(), array());
$shipping_method = array(
'shipping_method' => $data['shipping_code']
);
$f = $this->request('shipping/method', array(), $shipping_method);
$this->request('payment/methods', array(), array());
$payment_method = array(
'payment_method' => $data['payment_code']
);
$g = $this->request('payment/method', array(), $payment_method);
$order = array(
'shipping_method' => $data['shipping_code'],
'payment_method' => $data['payment_code'],
'order_status_id' => $data['order_status_id'],
'comment' => $data['comment'],
'affiliate_id' => $data['affiliate_id'],
);
$h = $this->request('order/add', array(), $order);
var_dump($a, $b, $c, $d, $e, $f, $g, $h);
}
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);
}
}