From cbbfc67cd8f063de0c08d95820eed6fb44fb960f Mon Sep 17 00:00:00 2001 From: iyzoer Date: Thu, 27 Jul 2017 14:41:58 +0300 Subject: [PATCH] fixes for API v5 --- .../controller/extension/module/retailcrm.php | 2 +- catalog/model/extension/retailcrm/order.php | 21 +- .../library/retailcrm/OpencartApiClient.php | 4 + .../library/retailcrm/RetailcrmApiClient3.php | 88 +++--- .../library/retailcrm/RetailcrmApiClient4.php | 1 - .../library/retailcrm/RetailcrmApiClient5.php | 274 +++++++++++++++++- 6 files changed, 342 insertions(+), 48 deletions(-) diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index 121a2ee..13533e7 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -546,7 +546,7 @@ class ControllerExtensionModuleRetailcrm extends Controller $response = $this->retailcrm->statisticUpdate(); - if (!$response) { + if (!$response->isSuccessful()) { $this->_error['warning'] = $this->language->get('text_error_api'); } diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index f3623bf..34afee1 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -62,7 +62,22 @@ class ModelExtensionRetailcrmOrder extends Model { $response = $this->retailcrm->ordersEdit($order); if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { - $this->editPayment($order_data, $order_id); + $response_order = $this->retailcrm->ordersGet($order_id); + if ($response_order->isSuccessful()) $order_info = $response_order['order']; + + foreach ($order_info['payments'] as $payment_data) { + if ($payment_data['externalId'] == $order_id) { + $payment = $payment_data; + } + } + + if (isset($payment) && $payment['type'] != $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]) { + $response = $this->retailcrm->ordersPaymentDelete($payment['id']); + + if ($response->isSuccessful()) { + $this->createPayment($order_data, $order_id); + } + } } } @@ -103,11 +118,13 @@ class ModelExtensionRetailcrmOrder extends Model { } } - if (isset($couponTotal)) $order['discount'] = $couponTotal; $order['createdAt'] = $order_data['date_added']; if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { $order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code]; + if (isset($couponTotal)) $order['discount'] = $couponTotal; + } else { + if (isset($couponTotal)) $order['discountManualAmount'] = $couponTotal; } $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index 9e8ac65..47a8684 100644 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -309,6 +309,10 @@ class OpencartApiClient { return curl_exec($curl); } + /** + * Login api user for opencart version > 3.0 + * + */ private function apiLogin() { $this->load->model('user/api'); $registry = new Registry(); diff --git a/system/library/retailcrm/RetailcrmApiClient3.php b/system/library/retailcrm/RetailcrmApiClient3.php index 82631e0..7a6d7df 100644 --- a/system/library/retailcrm/RetailcrmApiClient3.php +++ b/system/library/retailcrm/RetailcrmApiClient3.php @@ -1,6 +1,14 @@ + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 */ class RetailcrmApiClient3 { @@ -38,7 +46,7 @@ class RetailcrmApiClient3 * * @param array $order * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersCreate(array $order, $site = null) { @@ -57,7 +65,7 @@ class RetailcrmApiClient3 * @param array $order * @param string $by * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersEdit(array $order, $by = 'externalId', $site = null) { @@ -86,7 +94,7 @@ class RetailcrmApiClient3 * * @param array $orders * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersUpload(array $orders, $site = null) { @@ -105,7 +113,7 @@ class RetailcrmApiClient3 * @param string $id * @param string $by (default: 'externalId') * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersGet($id, $by = 'externalId', $site = null) { @@ -124,7 +132,7 @@ class RetailcrmApiClient3 * @param int $limit (default: 100) * @param int $offset (default: 0) * @param bool $skipMyChanges (default: true) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersHistory( DateTime $startDate = null, @@ -160,7 +168,7 @@ class RetailcrmApiClient3 * @param array $filter (default: array()) * @param int $page (default: null) * @param int $limit (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersList(array $filter = array(), $page = null, $limit = null) { @@ -184,7 +192,7 @@ class RetailcrmApiClient3 * * @param array $ids (default: array()) * @param array $externalIds (default: array()) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersStatuses(array $ids = array(), array $externalIds = array()) { @@ -204,7 +212,7 @@ class RetailcrmApiClient3 * Save order IDs' (id and externalId) association in the CRM * * @param array $ids - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersFixExternalIds(array $ids) { @@ -223,7 +231,7 @@ class RetailcrmApiClient3 * @param array $filter (default: array()) * @param int $page (default: null) * @param int $limit (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) { @@ -247,7 +255,7 @@ class RetailcrmApiClient3 * * @param array $customer * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersCreate(array $customer, $site = null) { @@ -266,7 +274,7 @@ class RetailcrmApiClient3 * @param array $customer * @param string $by (default: 'externalId') * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersEdit(array $customer, $by = 'externalId', $site = null) { @@ -298,7 +306,7 @@ class RetailcrmApiClient3 * * @param array $customers * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersUpload(array $customers, $site = null) { @@ -317,7 +325,7 @@ class RetailcrmApiClient3 * @param string $id * @param string $by (default: 'externalId') * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersGet($id, $by = 'externalId', $site = null) { @@ -334,7 +342,7 @@ class RetailcrmApiClient3 * @param array $filter (default: array()) * @param int $page (default: null) * @param int $limit (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersList(array $filter = array(), $page = null, $limit = null) { @@ -357,7 +365,7 @@ class RetailcrmApiClient3 * Save customer IDs' (id and externalId) association in the CRM * * @param array $ids - * @return RetailcrmApiResponse + * @return ApiResponse */ public function customersFixExternalIds(array $ids) { @@ -377,7 +385,7 @@ class RetailcrmApiClient3 * @param int $page (default: null) * @param int $limit (default: null) * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) { @@ -401,7 +409,7 @@ class RetailcrmApiClient3 * * @param array $offers * @param string $site (default: null) - * @return RetailcrmApiResponse + * @return ApiResponse */ public function storeInventoriesUpload(array $offers, $site = null) { @@ -419,7 +427,7 @@ class RetailcrmApiClient3 /** * Returns deliveryServices list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function deliveryServicesList() { @@ -429,7 +437,7 @@ class RetailcrmApiClient3 /** * Returns deliveryTypes list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function deliveryTypesList() { @@ -439,7 +447,7 @@ class RetailcrmApiClient3 /** * Returns orderMethods list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function orderMethodsList() { @@ -449,7 +457,7 @@ class RetailcrmApiClient3 /** * Returns orderTypes list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function orderTypesList() { @@ -459,7 +467,7 @@ class RetailcrmApiClient3 /** * Returns paymentStatuses list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function paymentStatusesList() { @@ -469,7 +477,7 @@ class RetailcrmApiClient3 /** * Returns paymentTypes list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function paymentTypesList() { @@ -479,7 +487,7 @@ class RetailcrmApiClient3 /** * Returns productStatuses list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function productStatusesList() { @@ -489,7 +497,7 @@ class RetailcrmApiClient3 /** * Returns statusGroups list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function statusGroupsList() { @@ -499,7 +507,7 @@ class RetailcrmApiClient3 /** * Returns statuses list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function statusesList() { @@ -509,7 +517,7 @@ class RetailcrmApiClient3 /** * Returns sites list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function sitesList() { @@ -519,7 +527,7 @@ class RetailcrmApiClient3 /** * Returns stores list * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function storesList() { @@ -530,7 +538,7 @@ class RetailcrmApiClient3 * Edit deliveryService * * @param array $data delivery service data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function deliveryServicesEdit(array $data) { @@ -551,7 +559,7 @@ class RetailcrmApiClient3 * Edit deliveryType * * @param array $data delivery type data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function deliveryTypesEdit(array $data) { @@ -572,7 +580,7 @@ class RetailcrmApiClient3 * Edit orderMethod * * @param array $data order method data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function orderMethodsEdit(array $data) { @@ -593,7 +601,7 @@ class RetailcrmApiClient3 * Edit orderType * * @param array $data order type data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function orderTypesEdit(array $data) { @@ -614,7 +622,7 @@ class RetailcrmApiClient3 * Edit paymentStatus * * @param array $data payment status data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function paymentStatusesEdit(array $data) { @@ -635,7 +643,7 @@ class RetailcrmApiClient3 * Edit paymentType * * @param array $data payment type data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function paymentTypesEdit(array $data) { @@ -656,7 +664,7 @@ class RetailcrmApiClient3 * Edit productStatus * * @param array $data product status data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function productStatusesEdit(array $data) { @@ -677,7 +685,7 @@ class RetailcrmApiClient3 * Edit order status * * @param array $data status data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function statusesEdit(array $data) { @@ -698,7 +706,7 @@ class RetailcrmApiClient3 * Edit site * * @param array $data site data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function sitesEdit(array $data) { @@ -719,7 +727,7 @@ class RetailcrmApiClient3 * Edit store * * @param array $data site data - * @return RetailcrmApiResponse + * @return ApiResponse */ public function storesEdit(array $data) { @@ -743,7 +751,7 @@ class RetailcrmApiClient3 /** * Update CRM basic statistic * - * @return RetailcrmApiResponse + * @return ApiResponse */ public function statisticUpdate() { diff --git a/system/library/retailcrm/RetailcrmApiClient4.php b/system/library/retailcrm/RetailcrmApiClient4.php index eca5eb5..5a406f1 100644 --- a/system/library/retailcrm/RetailcrmApiClient4.php +++ b/system/library/retailcrm/RetailcrmApiClient4.php @@ -12,7 +12,6 @@ */ class RetailcrmApiClient4 { - const VERSION = 'v4'; protected $client; diff --git a/system/library/retailcrm/RetailcrmApiClient5.php b/system/library/retailcrm/RetailcrmApiClient5.php index 9b2057b..bf80d53 100644 --- a/system/library/retailcrm/RetailcrmApiClient5.php +++ b/system/library/retailcrm/RetailcrmApiClient5.php @@ -8,11 +8,10 @@ * @package RetailCrm * @author RetailCrm * @license https://opensource.org/licenses/MIT MIT License - * @link http://www.retailcrm.ru/docs/Developers/ApiVersion4 + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion5 */ class RetailcrmApiClient5 { - const VERSION = 'v5'; protected $client; @@ -95,6 +94,14 @@ class RetailcrmApiClient5 return $this->client->makeRequest("/users/$id", RetailcrmHttpClient::METHOD_GET); } + /** + * Change user status + * + * @param integer $id user ID + * @param string $status user status + * + * @return ApiResponse + */ public function usersStatus($id, $status) { $statuses = array("free", "busy", "dinner", "break"); @@ -112,6 +119,15 @@ class RetailcrmApiClient5 ); } + /** + * Get segments list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return ApiResponse + */ public function segmentsList(array $filter = array(), $limit = null, $page = null) { $parameters = array(); @@ -133,6 +149,15 @@ class RetailcrmApiClient5 ); } + /** + * Get custom fields list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return ApiResponse + */ public function customFieldsList(array $filter = array(), $limit = null, $page = null) { $parameters = array(); @@ -153,7 +178,15 @@ class RetailcrmApiClient5 $parameters ); } - + + /** + * Create custom field + * + * @param $entity + * @param $customField + * + * @return ApiResponse + */ public function customFieldsCreate($entity, $customField) { if (!count($customField) || @@ -178,7 +211,15 @@ class RetailcrmApiClient5 array('customField' => json_encode($customField)) ); } - + + /** + * Edit custom field + * + * @param $entity + * @param $customField + * + * @return ApiResponse + */ public function customFieldsEdit($entity, $customField) { if (!count($customField) || empty($customField['code'])) { @@ -200,6 +241,14 @@ class RetailcrmApiClient5 ); } + /** + * Get custom field + * + * @param $entity + * @param $code + * + * @return ApiResponse + */ public function customFieldsGet($entity, $code) { if (empty($code)) { @@ -220,6 +269,15 @@ class RetailcrmApiClient5 ); } + /** + * Get custom dictionaries list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return ApiResponse + */ public function customDictionariesList(array $filter = [], $limit = null, $page = null) { $parameters = []; @@ -241,6 +299,13 @@ class RetailcrmApiClient5 ); } + /** + * Create custom dictionary + * + * @param $customDictionary + * + * @return ApiResponse + */ public function customDictionariesCreate($customDictionary) { if (!count($customDictionary) || @@ -259,6 +324,13 @@ class RetailcrmApiClient5 ); } + /** + * Edit custom dictionary + * + * @param $customDictionary + * + * @return ApiResponse + */ public function customDictionariesEdit($customDictionary) { if (!count($customDictionary) || @@ -277,6 +349,13 @@ class RetailcrmApiClient5 ); } + /** + * Get custom dictionary + * + * @param $code + * + * @return ApiResponse + */ public function customDictionariesGet($code) { if (empty($code)) { @@ -528,6 +607,15 @@ class RetailcrmApiClient5 ); } + /** + * Combine orders + * + * @param string $technique + * @param array $order + * @param array $resultOrder + * + * @return ApiResponse + */ public function ordersCombine($order, $resultOrder, $technique = 'ours') { $techniques = array('ours', 'summ', 'theirs'); @@ -555,6 +643,17 @@ class RetailcrmApiClient5 ); } + /** + * Create an order payment + * + * @param array $payment order data + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ public function ordersPaymentCreate(array $payment) { if (!count($payment)) { @@ -570,6 +669,15 @@ class RetailcrmApiClient5 ); } + /** + * Edit an order payment + * + * @param array $payment order data + * @param string $by by key + * @param null $site site code + * + * @return ApiResponse + */ public function ordersPaymentEdit(array $payment, $by = 'externalId', $site = null) { if (!count($payment)) { @@ -596,6 +704,27 @@ class RetailcrmApiClient5 ); } + /** + * Edit an order payment + * + * @param string $id payment id + * + * @return ApiResponse + */ + public function ordersPaymentDelete($id) + { + if (!$id) { + throw new \InvalidArgumentException( + 'Parameter `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/payments/%s/delete', $id), + RetailcrmHttpClient::METHOD_POST + ); + } + /** * Returns filtered customers list * @@ -802,6 +931,14 @@ class RetailcrmApiClient5 ); } + /** + * Combine customers + * + * @param array $customers + * @param array $resultCustomer + * + * @return ApiResponse + */ public function customersCombine(array $customers, $resultCustomer) { @@ -821,6 +958,88 @@ class RetailcrmApiClient5 ); } + /** + * Returns filtered customers notes list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function customersNotesList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + return $this->client->makeRequest( + '/customers/notes', + RetailcrmHttpClient::METHOD_GET, + $parameters + ); + } + + /** + * Create customer note + * + * @param array $note (default: array()) + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function customersNotesCreate($note, $site = null) + { + if (empty($note['customer']['id']) && empty($note['customer']['externalId'])) { + throw new \InvalidArgumentException( + 'Customer identifier must be set' + ); + } + return $this->client->makeRequest( + '/customers/notes/create', + RetailcrmHttpClient::METHOD_POST, + $this->fillSite($site, array('note' => json_encode($note))) + ); + } + + /** + * Delete customer note + * + * @param integer $id + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function customersNotesDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException( + 'Note id must be set' + ); + } + return $this->client->makeRequest( + "/customers/notes/$id/delete", + RetailcrmHttpClient::METHOD_POST + ); + } + /** * Get orders assembly list * @@ -989,6 +1208,15 @@ class RetailcrmApiClient5 ); } + /** + * Get tasks list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return ApiResponse + */ public function tasksList(array $filter = array(), $limit = null, $page = null) { $parameters = array(); @@ -1010,6 +1238,15 @@ class RetailcrmApiClient5 ); } + /** + * Create task + * + * @param array $task + * @param null $site + * + * @return ApiResponse + * + */ public function tasksCreate($task, $site = null) { if (!count($task)) { @@ -1028,6 +1265,15 @@ class RetailcrmApiClient5 ); } + /** + * Edit task + * + * @param array $task + * @param null $site + * + * @return ApiResponse + * + */ public function tasksEdit($task, $site = null) { if (!count($task)) { @@ -1046,6 +1292,13 @@ class RetailcrmApiClient5 ); } + /** + * Get custom dictionary + * + * @param $id + * + * @return ApiResponse + */ public function tasksGet($id) { if (empty($id)) { @@ -1060,6 +1313,19 @@ class RetailcrmApiClient5 ); } + /** + * Get products groups + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ public function storeProductsGroups(array $filter = array(), $page = null, $limit = null) { $parameters = array();