From 7f697652958f30e184d3d7ad4eec4a5fde19aab8 Mon Sep 17 00:00:00 2001 From: dkorol Date: Tue, 5 Apr 2016 16:49:56 +0300 Subject: [PATCH 01/11] parsing opencart version from index.php define procedure --- system/cron/dispatch.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/system/cron/dispatch.php b/system/cron/dispatch.php index 8f29648..47e3144 100644 --- a/system/cron/dispatch.php +++ b/system/cron/dispatch.php @@ -3,13 +3,18 @@ // Ensure $cli_action is set if (!isset($cli_action)) { echo 'ERROR: $cli_action must be set in calling script.'; - $log->write('ERROR: $cli_action must be set in calling script.'); http_response_code(400); exit; } // Version -define('VERSION', '1.5.6'); +$version = '1.5.6'; +$indexFile = file_get_contents(realpath(dirname(__FILE__)) . '/../../index.php'); +preg_match("/define\([\s]*['\"]VERSION['\"][\s]*,[\s]*['\"](.*)['\"][\s]*\)[\s]*;/mi", $indexFile, $versionMatches); +if(isset($versionMatches[1])) { + $version = $versionMatches[1]; +} +define('VERSION', $version); // Configuration (note we're using the admin config) require_once(realpath(dirname(__FILE__)) . '/../../admin/config.php'); From a87af86a69da9cfac236af1cf45b086193cbfc2f Mon Sep 17 00:00:00 2001 From: dkorol Date: Wed, 6 Apr 2016 12:34:40 +0300 Subject: [PATCH 02/11] bugfixes --- admin/model/retailcrm/icml.php | 4 +++- system/cron/dispatch.php | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) mode change 100644 => 100755 admin/model/retailcrm/icml.php mode change 100644 => 100755 system/cron/dispatch.php diff --git a/admin/model/retailcrm/icml.php b/admin/model/retailcrm/icml.php old mode 100644 new mode 100755 index 8887493..bfdf129 --- a/admin/model/retailcrm/icml.php +++ b/admin/model/retailcrm/icml.php @@ -63,6 +63,8 @@ class ModelRetailcrmIcml extends Model { $categories = $this->model_catalog_category->getCategories(array()); foreach($categories as $category) { + $category = $this->model_catalog_category->getCategory($category['category_id']); + $e = $this->eCategories->appendChild( $this->dd->createElement( 'category', $category['name'] @@ -118,7 +120,7 @@ class ModelRetailcrmIcml extends Model if (!empty($categories)) { foreach ($categories as $category) { - $e->appendChild($this->dd->createElement('category')) + $e->appendChild($this->dd->createElement('categoryId')) ->appendChild( $this->dd->createTextNode($category) ); diff --git a/system/cron/dispatch.php b/system/cron/dispatch.php old mode 100644 new mode 100755 index 47e3144..e7ccfdb --- a/system/cron/dispatch.php +++ b/system/cron/dispatch.php @@ -106,6 +106,10 @@ $request = new Request(); $registry->set('request', $request); $response = new Response(); $response->addHeader('Content-Type: text/html; charset=utf-8'); + +$cache = new Cache('file'); +$registry->set('cache', $cache); + $registry->set('response', $response); $session = new Session(); $registry->set('session', $session); From ec022db8b543039b0babf5f790da589a04d26958 Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 7 Apr 2016 13:03:09 +0300 Subject: [PATCH 03/11] added comparing version, because model sale/customer from 2.1 version comes to customer/customer --- admin/model/retailcrm/history.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 2552c72..3765da0 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -9,7 +9,11 @@ class ModelRetailcrmHistory extends Model $this->load->model('setting/setting'); $this->load->model('setting/store'); $this->load->model('sale/order'); - $this->load->model('sale/customer'); + if (version_compare(VERSION, '2.1.0.0', '>=')) { + $this->load->model('customer/customer'); + } else { + $this->load->model('sale/customer'); + } $this->load->model('retailcrm/references'); $this->load->model('catalog/product'); $this->load->model('localisation/zone'); @@ -281,13 +285,25 @@ class ModelRetailcrmHistory extends Model ), ); - $this->model_sale_customer->addCustomer($cData); + if (version_compare(VERSION, '2.1.0.0', '>=')) { + $this->model_customer_customer->addCustomer($cData); + } else { + $this->model_sale_customer->addCustomer($cData); + } if (!empty($order['email'])) { - $tryToFind = $this->model_sale_customer->getCustomerByEmail($order['email']); + if (version_compare(VERSION, '2.1.0.0', '>=')) { + $tryToFind = $this->model_customer_customer->getCustomerByEmail($order['email']); + } else { + $tryToFind = $this->model_sale_customer->getCustomerByEmail($order['email']); + } $customer_id = $tryToFind['customer_id']; } else { - $last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); + if (version_compare(VERSION, '2.1.0.0', '>=')) { + $last = $this->model_customer_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); + } else { + $last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); + } $customer_id = $last[0]['customer_id']; } From 7cceb9852c3aaf5e920d47e12515908c5145ef60 Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 7 Apr 2016 13:57:21 +0300 Subject: [PATCH 04/11] language loading fix --- system/cron/dispatch.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/system/cron/dispatch.php b/system/cron/dispatch.php index e7ccfdb..4869ec5 100755 --- a/system/cron/dispatch.php +++ b/system/cron/dispatch.php @@ -113,15 +113,21 @@ $registry->set('cache', $cache); $registry->set('response', $response); $session = new Session(); $registry->set('session', $session); -$languages = array(); +$languages = array(); $query = $db->query("SELECT * FROM " . DB_PREFIX . "language"); foreach ($query->rows as $result) { $languages[$result['code']] = $result; } -$config->set('config_language_id', $languages[$config->get('config_admin_language')]['language_id']); -$language = new Language($languages[$config->get('config_admin_language')]['directory']); -$language->load($languages[$config->get('config_admin_language')]['filename']); + +$adminLanguageCode = $config->get('config_admin_language'); +$config->set('config_language_id', $languages[$adminLanguageCode]['language_id']); +$language = new Language($languages[$adminLanguageCode]['directory']); +if(isset($languages[$adminLanguageCode]['filename'])) { + $language->load($languages[$adminLanguageCode]['filename']); +} else { + $language->load($languages[$adminLanguageCode]['directory']); +} $registry->set('language', $language); $document = new Document(); From a4261d544c1e255e32b99aa57f2f823ab1e49bdc Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 7 Apr 2016 15:31:04 +0300 Subject: [PATCH 05/11] bugfixes --- admin/model/retailcrm/order.php | 25 +++++++++++++------------ catalog/model/retailcrm/order.php | 14 ++++++-------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index cf89d9a..808642a 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -18,9 +18,6 @@ class ModelRetailcrmOrder extends Model { $order = array(); - $payment_code = $order_data['payment_code']; - $delivery_code = $order_data['shipping_code']; - $customers = $this->retailcrm->customersList( array( 'name' => $order_data['telephone'], @@ -44,30 +41,34 @@ class ModelRetailcrmOrder extends Model { $order['customerComment'] = $order_data['comment']; $deliveryCost = 0; - $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; + $altTotals = isset($order_data['order_total']) ? $order_data['order_total'] : ""; + $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $altTotals ; - foreach ($orderTotals as $totals) { - if ($totals['code'] == 'shipping') { - $deliveryCost = $totals['value']; + if (!empty($orderTotals)) { + foreach ($orderTotals as $totals) { + if ($totals['code'] == 'shipping') { + $deliveryCost = $totals['value']; + } } } $order['createdAt'] = date('Y-m-d H:i:s'); + + $payment_code = $order_data['payment_code']; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; - $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - + $delivery_code = $order_data['shipping_code']; $order['delivery'] = array( 'code' => $settings['retailcrm_delivery'][$delivery_code], 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], 'city' => $order_data['shipping_city'], - 'country' => $order_data['shipping_country_id'], - 'region' => $order_data['shipping_zone_id'], + 'countryIso' => $order_data['shipping_iso_code_2'], + 'region' => $order_data['shipping_zone'], 'text' => implode(', ', array( $order_data['shipping_postcode'], - $country, + (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '', $order_data['shipping_city'], $order_data['shipping_address_1'], $order_data['shipping_address_2'] diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index f2df393..8cd772d 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -18,9 +18,6 @@ class ModelRetailcrmOrder extends Model { $order = array(); - $payment_code = $order_data['payment_code']; - $delivery_code = $order_data['shipping_code']; - $customers = $this->retailcrm->customersList( array( 'name' => $order_data['telephone'], @@ -56,21 +53,22 @@ class ModelRetailcrmOrder extends Model { } $order['createdAt'] = date('Y-m-d H:i:s'); + + $payment_code = $order_data['payment_code']; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; - $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - + $delivery_code = $order_data['shipping_code']; $order['delivery'] = array( 'code' => $settings['retailcrm_delivery'][$delivery_code], 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], 'city' => $order_data['shipping_city'], - 'country' => $order_data['shipping_country_id'], - 'region' => $order_data['shipping_zone_id'], + 'countryIso' => $order_data['shipping_iso_code_2'], + 'region' => $order_data['shipping_zone'], 'text' => implode(', ', array( $order_data['shipping_postcode'], - $country, + (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '', $order_data['shipping_city'], $order_data['shipping_address_1'], $order_data['shipping_address_2'] From 0b5b07a19c8d7aba50f0ce11a6307da4606b1ac1 Mon Sep 17 00:00:00 2001 From: dkorol Date: Fri, 8 Apr 2016 14:59:15 +0300 Subject: [PATCH 06/11] fixed uploading to crm when order changed --- admin/controller/module/retailcrm.php | 8 ++- catalog/controller/module/retailcrm.php | 28 ++++++++ catalog/model/retailcrm/order.php | 87 ++++++++++++++++++++++++- 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 015338f..85e4bd4 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -37,6 +37,13 @@ class ControllerModuleRetailcrm extends Controller 'post.order.add', 'module/retailcrm/order_create' ); + + $this->model_extension_event + ->addEvent( + 'retailcrm', + 'post.order.history.add', + 'module/retailcrm/order_edit' + ); } } @@ -278,7 +285,6 @@ class ControllerModuleRetailcrm extends Controller */ public function order_create($order_id) { - $this->load->model('checkout/order'); $this->load->model('account/order'); diff --git a/catalog/controller/module/retailcrm.php b/catalog/controller/module/retailcrm.php index bdd9fea..089a74f 100644 --- a/catalog/controller/module/retailcrm.php +++ b/catalog/controller/module/retailcrm.php @@ -42,4 +42,32 @@ class ControllerModuleRetailcrm extends Controller $this->model_retailcrm_order->sendToCrm($data, $data['order_id']); } } + + public function order_edit($order_id) { + $this->load->model('checkout/order'); + $this->load->model('account/order'); + + $data = $this->model_checkout_order->getOrder($order_id); + + if($data['order_status_id'] == 0) return; + + $data['products'] = $this->model_account_order->getOrderProducts($order_id); + + if (!isset($data['fromApi'])) { + $this->load->model('setting/setting'); + $status = $this->model_setting_setting->getSetting('retailcrm'); + + if ($data['order_status_id'] > 0) { + $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; + } + + $data['totals'][] = array( + 'code' => 'shipping', + 'value' => isset($this->session->data['shipping_method']) ? $this->session->data['shipping_method']['cost'] : '' + ); + + $this->load->model('retailcrm/order'); + $this->model_retailcrm_order->changeInCrm($data, $data['order_id']); + } + } } diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index 8cd772d..ac01816 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -57,9 +57,17 @@ class ModelRetailcrmOrder extends Model { $payment_code = $order_data['payment_code']; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; + // Совместимость с 1.5.5, когда этот метод вызывается из model/checkout/order->createOrder(), а не из controller/module/retailcrm->order_create() + if(!isset($order_data['shipping_iso_code_2']) && isset($order_data['shipping_country_id'])) { + $this->load->model('localisation/country'); + $shipping_country = $this->model_localisation_country->getCountry($order_data['shipping_country_id']); + $order_data['shipping_iso_code_2'] = $shipping_country['iso_code_2']; + } + + $delivery_code = $order_data['shipping_code']; $order['delivery'] = array( - 'code' => $settings['retailcrm_delivery'][$delivery_code], + 'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '', 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], @@ -94,5 +102,82 @@ class ModelRetailcrmOrder extends Model { $this->retailcrm->ordersCreate($order); } } + + public function changeInCrm($order_data, $order_id) + { + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { + require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + + $this->retailcrm = new RetailcrmProxy( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'logs/retailcrm.log' + ); + + $order = array(); + + $payment_code = $order_data['payment_code']; + $delivery_code = $order_data['shipping_code']; + + $order['externalId'] = $order_id; + $order['firstName'] = $order_data['firstname']; + $order['lastName'] = $order_data['lastname']; + $order['email'] = $order_data['email']; + $order['phone'] = $order_data['telephone']; + $order['customerComment'] = $order_data['comment']; + + $deliveryCost = 0; + $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; + + foreach ($orderTotals as $totals) { + if ($totals['code'] == 'shipping') { + $deliveryCost = $totals['value']; + } + } + + $order['createdAt'] = date('Y-m-d H:i:s'); + $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; + + $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; + + $order['delivery'] = array( + 'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '', + 'cost' => $deliveryCost, + 'address' => array( + 'index' => $order_data['shipping_postcode'], + 'city' => $order_data['shipping_city'], + 'country' => $order_data['shipping_country_id'], + 'region' => $order_data['shipping_zone_id'], + 'text' => implode(', ', array( + $order_data['shipping_postcode'], + $country, + $order_data['shipping_city'], + $order_data['shipping_address_1'], + $order_data['shipping_address_2'] + )) + ) + ); + + $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + + foreach ($orderProducts as $product) { + $order['items'][] = array( + 'productId' => $product['product_id'], + 'productName' => $product['name'], + 'initialPrice' => $product['price'], + 'quantity' => $product['quantity'], + ); + } + + if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { + $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; + } + + $this->retailcrm->ordersEdit($order); + } + } } From 1ffb7387dcd9857feb52af151601703010bec01c Mon Sep 17 00:00:00 2001 From: dkorol Date: Fri, 8 Apr 2016 16:39:51 +0300 Subject: [PATCH 07/11] bugfix --- admin/model/retailcrm/history.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 3765da0..6472a71 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -124,7 +124,7 @@ class ModelRetailcrmHistory extends Model $data['payment_address'] = '0'; $data['payment_firstname'] = $order['firstName']; $data['payment_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; - $data['payment_address_1'] = $order['customer']['address']['text']; + $data['payment_address_1'] = isset($order['customer']['address']) ? $order['customer']['address']['text'] : ''; $data['payment_address_2'] = ''; $data['payment_company'] = ''; $data['payment_company_id'] = ''; @@ -162,6 +162,7 @@ class ModelRetailcrmHistory extends Model $data['shipping'] = $this->delivery[$order['delivery']['code']]; $data['shipping_method'] = $this->ocDelivery[$data['shipping']]; $data['shipping_code'] = $this->delivery[$order['delivery']['code']]; + $data['payment'] = $this->payment[$order['paymentType']]; $data['payment_method'] = $this->ocPayment[$data['payment']]; $data['payment_code'] = $this->payment[$order['paymentType']]; From 76dc6535cc290d14e50b9419779e18320e9d36f9 Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 14 Apr 2016 13:55:11 +0300 Subject: [PATCH 08/11] history for opencart >=2 --- admin/controller/module/retailcrm.php | 0 admin/model/retailcrm/history.php | 26 +- catalog/controller/module/retailcrm.php | 0 catalog/model/retailcrm/order.php | 4 + system/cron/getmyip.php | 2 + .../library/retailcrm/OpencartApiClient.php | 327 ++++++++++++++++++ 6 files changed, 354 insertions(+), 5 deletions(-) mode change 100644 => 100755 admin/controller/module/retailcrm.php mode change 100644 => 100755 admin/model/retailcrm/history.php mode change 100644 => 100755 catalog/controller/module/retailcrm.php mode change 100644 => 100755 catalog/model/retailcrm/order.php create mode 100644 system/cron/getmyip.php create mode 100755 system/library/retailcrm/OpencartApiClient.php diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php old mode 100644 new mode 100755 diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php old mode 100644 new mode 100755 index 6472a71..579ab4b --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -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)); diff --git a/catalog/controller/module/retailcrm.php b/catalog/controller/module/retailcrm.php old mode 100644 new mode 100755 diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php old mode 100644 new mode 100755 index ac01816..5b62c88 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -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'); diff --git a/system/cron/getmyip.php b/system/cron/getmyip.php new file mode 100644 index 0000000..10db1bf --- /dev/null +++ b/system/cron/getmyip.php @@ -0,0 +1,2 @@ +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); + } +} \ No newline at end of file From d862d98001e06bd520994c56a00f4567947b40f9 Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 14 Apr 2016 14:28:23 +0300 Subject: [PATCH 09/11] fix --- system/library/retailcrm/OpencartApiClient.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index 294f980..f0d007b 100755 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -77,13 +77,6 @@ class OpencartApiClient { 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); @@ -213,7 +206,7 @@ class OpencartApiClient { ); $g = $this->request('order/edit', array('order_id' => $order_id), $order); - var_dump($a, $b, $c, $d, $e, $f, $g); + //var_dump($a, $b, $c, $d, $e, $f, $g); } public function addOrder($data) { @@ -295,7 +288,7 @@ class OpencartApiClient { ); $h = $this->request('order/add', array(), $order); - var_dump($a, $b, $c, $d, $e, $f, $g, $h); + //var_dump($a, $b, $c, $d, $e, $f, $g, $h); } private function getInnerIpAddr() { From 3ede32ff5165f3edca7d239ffef30b8e0972727c Mon Sep 17 00:00:00 2001 From: dkorol Date: Fri, 15 Apr 2016 12:02:42 +0300 Subject: [PATCH 10/11] fixes --- admin/model/retailcrm/history.php | 2 +- .../library/retailcrm/OpencartApiClient.php | 34 ++++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 579ab4b..c5871dd 100755 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -7,7 +7,7 @@ class ModelRetailcrmHistory extends Model private $opencartApiClient; public function request() - { + { $this->load->model('setting/setting'); $this->load->model('setting/store'); if(version_compare(VERSION, '2.0.0', '>=')) { diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index f0d007b..676a5fa 100755 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -146,7 +146,7 @@ class OpencartApiClient { 'telephone' => !empty($data['telephone']) ? $data['telephone'] : '0000', 'fax' => $data['fax'], ); - $a = $this->request('customer', array(), $customer); + $this->request('customer', array(), $customer); $products = array(); foreach ($data['order_product'] as $order_product) { @@ -155,7 +155,7 @@ class OpencartApiClient { 'quantity' => $order_product['quantity'] ); } - $b = $this->request('cart/add', array(), array('product' => $products)); + $this->request('cart/add', array(), array('product' => $products)); $payment_address = array( 'payment_address' => $data['payment_address'], @@ -169,13 +169,13 @@ class OpencartApiClient { '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/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); + $this->request('payment/method', array(), $payment_method); $shipping_address = array( 'shipping_address' => $data['shipping_address'], @@ -189,13 +189,13 @@ class OpencartApiClient { '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/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('shipping/method', array(), $shipping_method); $order = array( 'shipping_method' => $data['shipping_code'], @@ -204,9 +204,7 @@ class OpencartApiClient { '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); + $this->request('order/edit', array('order_id' => $order_id), $order); } public function addOrder($data) { @@ -227,7 +225,7 @@ class OpencartApiClient { 'telephone' => $data['telephone'], 'fax' => $data['fax'], ); - $b = $this->request('customer', array(), $customer); + $this->request('customer', array(), $customer); $products = array(); foreach($data['order_product'] as $product) { @@ -237,7 +235,7 @@ class OpencartApiClient { ); $products[] = $product; } - $c = $this->request('cart/add', array(), array('product' => $products)); + $this->request('cart/add', array(), array('product' => $products)); $payment_address = array( 'payment_address' => $data['payment_address'], @@ -251,7 +249,7 @@ class OpencartApiClient { 'country_id' => $data['payment_country_id'], 'zone_id' => $data['payment_zone_id'], ); - $d = $this->request('payment/address', array(), $payment_address); + $this->request('payment/address', array(), $payment_address); $shipping_address = array( 'shipping_address' => $data['shipping_address'], @@ -265,19 +263,19 @@ class OpencartApiClient { '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/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('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); + $this->request('payment/method', array(), $payment_method); $order = array( 'shipping_method' => $data['shipping_code'], @@ -286,9 +284,7 @@ class OpencartApiClient { '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); + $this->request('order/add', array(), $order); } private function getInnerIpAddr() { @@ -317,4 +313,4 @@ class OpencartApiClient { return curl_exec($curl); } -} \ No newline at end of file +} From 3b184b14e716d81aa1c6a6b7fca53f0f551cdf78 Mon Sep 17 00:00:00 2001 From: dkorol Date: Fri, 15 Apr 2016 12:15:57 +0300 Subject: [PATCH 11/11] fixes --- admin/model/retailcrm/history.php | 1 - admin/model/retailcrm/order.php | 1 - catalog/model/retailcrm/order.php | 1 - system/cron/getmyip.php | 2 +- 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index c5871dd..aaed8f9 100755 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -343,7 +343,6 @@ class ModelRetailcrmHistory extends Model $data['payment_city'] = !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city']; $data['payment_postcode'] = !empty($order['customer']['address']['index']) ? $order['customer']['address']['index'] : $order['delivery']['address']['index']; - $region = ''; if (!empty($order['delivery']['address']['region']) && is_int($order['delivery']['address']['region'])) { diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 808642a..8f40478 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -172,4 +172,3 @@ class ModelRetailcrmOrder extends Model { } } } - diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index 5b62c88..e3fed74 100755 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -184,4 +184,3 @@ class ModelRetailcrmOrder extends Model { } } } - diff --git a/system/cron/getmyip.php b/system/cron/getmyip.php index 10db1bf..439471b 100644 --- a/system/cron/getmyip.php +++ b/system/cron/getmyip.php @@ -1,2 +1,2 @@