From 0854d361551212ba77d9a9be6a4a72723b67d69c Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 3 Sep 2018 15:57:58 +0300 Subject: [PATCH 1/4] Send to crm marketplace activate/deactivate information --- .../controller/extension/module/retailcrm.php | 140 +++++++++++++++--- .../library/retailcrm/RetailcrmApiClient4.php | 36 ++++- .../library/retailcrm/RetailcrmApiClient5.php | 42 +++++- 3 files changed, 188 insertions(+), 30 deletions(-) diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 8a11213..1c00161 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -50,13 +50,32 @@ public function install() */ public function uninstall() { - $this->uninstall_collector(); $this->load->model('setting/setting'); - $this->model_setting_setting->editSetting( - $this->moduleTitle, - array($this->moduleTitle . '_status' => 0) - ); + $settings = $this->model_setting_setting->getSetting('retailcrm_setting'); + + if (!empty($settings)) { + $clientId = $settings['retailcrm_setting_client_id']; + + $this->integrationModule( + $this->retailcrm->getApiClient( + $settings['retailcrm_setting_url'], + $settings['retailcrm_setting_key'], + $settings['retailcrm_setting_version'] + ), + $clientId, + $settings['retailcrm_setting_version'], + false + ); + + $this->uninstall_collector(); + $this->model_setting_setting->editSetting( + $this->moduleTitle, + array($this->moduleTitle . '_status' => 0) + ); + } + $this->model_setting_setting->deleteSetting('retailcrm_history'); + $this->model_setting_setting->deleteSetting('retailcrm_setting'); $this->deleteEvents(); } @@ -66,7 +85,7 @@ public function uninstall() * @return void */ public function install_collector() - { + { $collector = $this->getCollectorTitle(); $this->loadModels(); $this->load->model('setting/setting'); @@ -80,7 +99,7 @@ public function install_collector() * @return void */ public function uninstall_collector() - { + { $collector = $this->getCollectorTitle(); $this->loadModels(); $this->load->model('setting/setting'); @@ -174,7 +193,7 @@ public function index() } $this->model_setting_setting->editSetting( - 'retailcrm_history', + 'retailcrm_history', array( 'retailcrm_history_orders' => isset($sinceIdOrders) ? $sinceIdOrders : 1, 'retailcrm_history_customers' => isset($sinceIdCustomers) ? $sinceIdCustomers : 1, @@ -184,6 +203,36 @@ public function index() } } + $retailcrm_setting = $this->model_setting_setting->getSetting('retailcrm_setting'); + + if (!$retailcrm_setting) { + $clientId = hash('md5', date('Y-m-d H:i:s')); + $api = $this->retailcrm->getApiClient( + $this->request->post[$this->moduleTitle . '_url'], + $this->request->post[$this->moduleTitle . '_apikey'], + $this->request->post[$this->moduleTitle . '_apiversion'] + ); + + $result = $this->integrationModule( + $api, + $clientId, + $this->request->post[$this->moduleTitle . '_apiversion'] + ); + + if ($result === true) { + $this->model_setting_setting->editSetting( + 'retailcrm_setting', + array( + 'retailcrm_setting_active_in_crm' => true, + 'retailcrm_setting_client_id' => $clientId, + 'retailcrm_setting_url' => $this->request->post[$this->moduleTitle . '_url'], + 'retailcrm_setting_key' => $this->request->post[$this->moduleTitle . '_apikey'], + 'retailcrm_setting_version' => $this->request->post[$this->moduleTitle . '_apiversion'] + ) + ); + } + } + $this->session->data['success'] = $this->language->get('text_success'); $redirect = $this->url->link( 'extension/module/retailcrm', $this->tokenTitle . '=' . $this->session->data[$this->tokenTitle], @@ -282,7 +331,7 @@ public function index() ->getOrderStatuses(); $_data['payments'] = $this->model_extension_retailcrm_references ->getPaymentTypes(); - + if ($apiVersion == 'v5') { $_data['customFields'] = $this->model_extension_retailcrm_references ->getCustomFields(); @@ -464,7 +513,7 @@ public function icml() * @return void */ public function customer_edit($route, $customer) - { + { $this->load->model('localisation/country'); $this->load->model('localisation/zone'); $this->load->model('customer/customer'); @@ -487,7 +536,7 @@ public function customer_edit($route, $customer) 'postcode' => $address['postcode'], 'iso_code_2' => $country['iso_code_2'], 'zone' => $zone['name'] - ); + ); } $this->load->model('extension/retailcrm/customer'); @@ -654,7 +703,7 @@ private function validate() /** * Clear retailcrm log file - * + * * @return void */ public function clear_retailcrm() @@ -672,7 +721,7 @@ public function clear_retailcrm() /** * Clear opencart API log file - * + * * @return void */ public function clear_opencart() @@ -690,7 +739,7 @@ public function clear_opencart() /** * Method for load models - * + * * @return void */ private function loadModels() @@ -716,7 +765,7 @@ private function loadModels() /** * Get collector module name - * + * * @return string */ private function getCollectorTitle() @@ -732,7 +781,7 @@ private function getCollectorTitle() /** * Check file size - * + * * @return string */ private function checkLogFile($file) @@ -754,7 +803,7 @@ private function checkLogFile($file) /** * Add events to db - * + * * @return void */ private function addEvents() @@ -806,7 +855,7 @@ private function addEvents() /** * Check events in db - * + * * @return boolean */ private function checkEvents() @@ -826,7 +875,7 @@ private function checkEvents() /** * Delete events from db - * + * * @return void */ private function deleteEvents() @@ -839,4 +888,57 @@ private function deleteEvents() $this->{'model_' . $this->modelEvent}->deleteEventByCode($this->moduleTitle); } } + + /** + * Activate/deactivate module in marketplace retailCRM + * + * @param \RetailcrmProxy $apiClient + * @param string $clientId + * @param string $api_version + * @param boolean $active + * + * @return boolean + */ + private function integrationModule($apiClient, $clientId, $api_version, $active = true) + { + $scheme = isset($this->request->server['HTTPS']) ? 'https://' : 'http://'; + $logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5af48736c6a0c-opencart-seeklogo.com.svg'; + $code = 'opencart'; + $name = 'Opencart'; + $accountUrl = $scheme . $this->request->server['HTTP_HOST'] . '/admin/index.php?route=extension/module/retailcrm'; + + if ($api_version == 'v4') { + $configuration = array( + 'name' => $name, + 'code' => $code, + 'logo' => $logo, + 'configurationUrl' => $accountUrl, + 'active' => $active + ); + + $response = $apiClient->marketplaceSettingsEdit($configuration); + } else { + $configuration = array( + 'clientId' => $clientId, + 'code' => $code, + 'integrationCode' => $code, + 'active' => $active, + 'name' => $name, + 'logo' => $logo, + 'accountUrl' => $accountUrl + ); + + $response = $apiClient->integrationModulesEdit($configuration); + } + + if (!$response) { + return false; + } + + if ($response->isSuccessful()) { + return true; + } + + return false; + } } diff --git a/src/upload/system/library/retailcrm/RetailcrmApiClient4.php b/src/upload/system/library/retailcrm/RetailcrmApiClient4.php index 1436175..98a3f98 100644 --- a/src/upload/system/library/retailcrm/RetailcrmApiClient4.php +++ b/src/upload/system/library/retailcrm/RetailcrmApiClient4.php @@ -35,20 +35,20 @@ public function __construct($url, $apiKey, $version = null, $site = null) if ('/' !== $url[strlen($url) - 1]) { $url .= '/'; } - + $url = $version == null ? $url . 'api' : $url . 'api/' . $version; $this->client = new RetailcrmHttpClient($url, array('apiKey' => $apiKey)); $this->siteCode = $site; } - + /** * Returns api versions list - * + * * @throws \RetailCrm\Exception\InvalidJsonException * @throws \RetailCrm\Exception\CurlException * @throws \InvalidArgumentException - * + * * @return ApiResponse */ public function apiVersions() @@ -1699,6 +1699,34 @@ public function statisticUpdate() ); } + /** + * Edit marketplace configuration + * + * @param array $configuration + * + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * @throws \InvalidArgumentException + * + * @return ApiResponse + */ + public function marketplaceSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + $code = $configuration['code']; + + return $this->client->makeRequest( + "/marketplace/external/setting/$code/edit", + RetailcrmHttpClient::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + /** * Return current site * diff --git a/src/upload/system/library/retailcrm/RetailcrmApiClient5.php b/src/upload/system/library/retailcrm/RetailcrmApiClient5.php index 1ea4619..d657a58 100644 --- a/src/upload/system/library/retailcrm/RetailcrmApiClient5.php +++ b/src/upload/system/library/retailcrm/RetailcrmApiClient5.php @@ -41,21 +41,21 @@ public function __construct($url, $apiKey, $version = null, $site = null) $this->client = new RetailcrmHttpClient($url, array('apiKey' => $apiKey)); $this->siteCode = $site; } - + /** * Returns api versions list - * + * * @throws \RetailCrm\Exception\InvalidJsonException * @throws \RetailCrm\Exception\CurlException * @throws \InvalidArgumentException - * + * * @return ApiResponse */ public function apiVersions() { return $this->client->makeRequest('/api-versions', RetailcrmHttpClient::METHOD_GET); } - + /** * Returns users list * @@ -190,7 +190,7 @@ public function customFieldsList(array $filter = array(), $limit = null, $page = $parameters ); } - + /** * Create custom field * @@ -223,7 +223,7 @@ public function customFieldsCreate($entity, $customField) array('customField' => json_encode($customField)) ); } - + /** * Edit custom field * @@ -730,7 +730,7 @@ public function ordersPaymentDelete($id) 'Parameter `id` must be set' ); } - + return $this->client->makeRequest( sprintf('/orders/payments/%s/delete', $id), RetailcrmHttpClient::METHOD_POST @@ -2324,6 +2324,34 @@ public function telephonyCallManager($phone, $details) ); } + /** + * Edit module configuration + * + * @param array $configuration + * + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * @throws \InvalidArgumentException + * + * @return ApiResponse + */ + public function integrationModulesEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + $code = $configuration['code']; + + return $this->client->makeRequest( + "/integration-modules/$code/edit", + RetailcrmHttpClient::METHOD_POST, + array('integrationModule' => json_encode($configuration)) + ); + } + /** * Update CRM basic statistic * From 273499acdce84c9c0c2478adc231c243a886bac7 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 11:30:05 +0300 Subject: [PATCH 2/4] Update generation clientId --- .../admin/controller/extension/module/retailcrm.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 1c00161..c45b048 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -206,7 +206,7 @@ public function index() $retailcrm_setting = $this->model_setting_setting->getSetting('retailcrm_setting'); if (!$retailcrm_setting) { - $clientId = hash('md5', date('Y-m-d H:i:s')); + $clientId = uniqid(); $api = $this->retailcrm->getApiClient( $this->request->post[$this->moduleTitle . '_url'], $this->request->post[$this->moduleTitle . '_apikey'], @@ -903,14 +903,14 @@ private function integrationModule($apiClient, $clientId, $api_version, $active { $scheme = isset($this->request->server['HTTPS']) ? 'https://' : 'http://'; $logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5af48736c6a0c-opencart-seeklogo.com.svg'; - $code = 'opencart'; + $integrationCode = 'opencart'; $name = 'Opencart'; - $accountUrl = $scheme . $this->request->server['HTTP_HOST'] . '/admin/index.php?route=extension/module/retailcrm'; + $accountUrl = $scheme . $this->request->server['HTTP_HOST'] . '/admin'; if ($api_version == 'v4') { $configuration = array( 'name' => $name, - 'code' => $code, + 'code' => $integrationCode . '-' . $clientId, 'logo' => $logo, 'configurationUrl' => $accountUrl, 'active' => $active @@ -920,8 +920,8 @@ private function integrationModule($apiClient, $clientId, $api_version, $active } else { $configuration = array( 'clientId' => $clientId, - 'code' => $code, - 'integrationCode' => $code, + 'code' => $integrationCode . '-' . $clientId, + 'integrationCode' => $integrationCode, 'active' => $active, 'name' => $name, 'logo' => $logo, From 75aed59403a6b13d80236285aee90abfc5f78859 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 11:33:35 +0300 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a68b3..99656b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v.3.1.3 +* Добавлена активация модуля в маркетплейсе retailCRM + ## v.3.1.2 * Добавлен перевод на испанский язык * Переделан перевод на английский язык From 0787d4a997bc2b7b5439e6e071c06878ea922f78 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 11:33:49 +0300 Subject: [PATCH 4/4] Update VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index ef538c2..ff365e0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.2 +3.1.3