From d298cffb224c47cdbd18cb59e2fe2b4b6783ada3 Mon Sep 17 00:00:00 2001 From: Kocmonavtik <61938582+Kocmonavtik@users.noreply.github.com> Date: Mon, 18 Mar 2024 14:00:13 +0300 Subject: [PATCH] =?UTF-8?q?ref#=2094173=20=D0=94=D0=BE=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B0=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D1=83=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D0=BA=D0=B5=20=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B5=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F.=20(#339)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- intaro.retailcrm/install/index.php | 41 +-- intaro.retailcrm/install/step1.php | 13 +- intaro.retailcrm/install/step3.php | 35 -- intaro.retailcrm/lang/en/install/index.php | 1 + intaro.retailcrm/lang/en/options.php | 4 +- intaro.retailcrm/lang/ru/install/index.php | 2 +- intaro.retailcrm/lang/ru/options.php | 4 +- intaro.retailcrm/lib/component/constants.php | 16 + intaro.retailcrm/options.php | 357 +++++++++---------- 9 files changed, 225 insertions(+), 248 deletions(-) diff --git a/intaro.retailcrm/install/index.php b/intaro.retailcrm/install/index.php index 284773b2..07606fa1 100644 --- a/intaro.retailcrm/install/index.php +++ b/intaro.retailcrm/install/index.php @@ -61,7 +61,6 @@ class intaro_retailcrm extends CModule public $CRM_ORDER_LAST_ID = 'order_last_id'; public $CRM_ORDER_PROPS = 'order_props'; public $CRM_LEGAL_DETAILS = 'legal_details'; - public $CRM_CUSTOM_FIELDS = 'custom_fields'; public $CRM_CONTRAGENT_TYPE = 'contragent_type'; public $CRM_ORDER_DISCHARGE = 'order_discharge'; public $CRM_ORDER_FAILED_IDS = 'order_failed_ids'; @@ -217,9 +216,7 @@ class intaro_retailcrm extends CModule $type['NAME'] = $APPLICATION->ConvertCharset((string)$field, 'utf-8', SITE_CHARSET); $type['ID'] = (string)$field['id']; - if ($field['group'] == 'custom') { - $arResult['customFields'][] = $type; - } elseif (!$field['group']) { + if (!$field['group']) { $arResult['orderProps'][] = $type; } else { $groups = explode(',', (string) $field['group']); @@ -485,6 +482,14 @@ class intaro_retailcrm extends CModule } try { + $credentials = $this->RETAIL_CRM_API->getCredentials()->getResponseBody(); + $residualRight = array_diff(Constants::REQUIRED_API_SCOPES, $credentials['scopes']); + + if (count($residualRight) !== 0) { + throw new \InvalidArgumentException(sprintf(GetMessage('ERR_403'), implode(', ', $residualRight))); + } + + $arResult['orderTypesList'] = $this->RETAIL_CRM_API->orderTypesList()->orderTypes; $arResult['deliveryTypesList'] = $this->RETAIL_CRM_API->deliveryTypesList()->deliveryTypes; $arResult['deliveryServicesList'] = $this->RETAIL_CRM_API->deliveryServicesList()->deliveryServices; @@ -498,7 +503,8 @@ class intaro_retailcrm extends CModule $e->getCode() . ': ' . $e->getMessage() ); } catch (\InvalidArgumentException $e) { - $arResult['errCode'] = 'ERR_METHOD_NOT_FOUND'; + $arResult['errCode'] = $e->getMessage(); + $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), $this->INSTALL_PATH . '/step1.php' ); @@ -764,9 +770,6 @@ class intaro_retailcrm extends CModule if ($orderProps = COption::GetOptionString($this->OLD_MODULE_ID, $this->CRM_ORDER_PROPS, 0)) { $arResult['ORDER_PROPS'] = unserialize($orderProps); } - if ($customFields = COption::GetOptionString($this->OLD_MODULE_ID, $this->CRM_CUSTOM_FIELDS, 0)) { - $arResult['CUSTOM_FIELDS'] = unserialize($customFields); - } if ($legalDetails = COption::GetOptionString($this->OLD_MODULE_ID, $this->CRM_LEGAL_DETAILS, 0)) { $arResult['LEGAL_DETAILS'] = unserialize($legalDetails); } @@ -864,15 +867,6 @@ class intaro_retailcrm extends CModule $legalDetailsArr[$orderType['ID']] = $_legalDetailsArr; } - $customFieldsArr = []; - foreach ($orderTypesList as $orderType) { - $_customFieldsArr = []; - foreach ($arResult['customFields'] as $custom) { - $_customFieldsArr[$custom['ID']] = htmlspecialchars(trim($_POST['custom-fields-' . $custom['ID'] . '-' . $orderType['ID']])); - } - $customFieldsArr[$orderType['ID']] = $_customFieldsArr; - } - //contragents type list $contragentTypeArr = []; foreach ($orderTypesList as $orderType) { @@ -881,7 +875,6 @@ class intaro_retailcrm extends CModule COption::SetOptionString($this->MODULE_ID, $this->CRM_ADDRESS_OPTIONS, serialize($addressDetailOptions)); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_PROPS, serialize(RCrmActions::clearArr($orderPropsArr))); - COption::SetOptionString($this->MODULE_ID, $this->CRM_CUSTOM_FIELDS, serialize(RCrmActions::clearArr($customFieldsArr))); COption::SetOptionString($this->MODULE_ID, $this->CRM_LEGAL_DETAILS, serialize(RCrmActions::clearArr($legalDetailsArr))); COption::SetOptionString($this->MODULE_ID, $this->CRM_CONTRAGENT_TYPE, serialize(RCrmActions::clearArr($contragentTypeArr))); @@ -1296,7 +1289,6 @@ class intaro_retailcrm extends CModule COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR); COption::RemoveOption($this->MODULE_ID, $this->CRM_LEGAL_DETAILS); COption::RemoveOption($this->MODULE_ID, $this->CRM_CONTRAGENT_TYPE); - COption::RemoveOption($this->MODULE_ID, $this->CRM_CUSTOM_FIELDS); COption::RemoveOption($this->MODULE_ID, $this->CRM_SITES_LIST); COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE); COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS); @@ -1508,11 +1500,14 @@ class intaro_retailcrm extends CModule { global $APPLICATION; - $client = new Client($api_host . '/api/'.self::V5, ['apiKey' => $api_key]); + $client = new Client($api_host . '/api', ['apiKey' => $api_key]); $result = []; try { - $siteResponse = $client->makeRequest('/reference/sites', 'GET'); + $credentials = $client->makeRequest('/credentials', 'GET')->getResponseBody(); + $residualRight = array_diff(Constants::REQUIRED_API_SCOPES, $credentials['scopes']); + + $siteResponse = $client->makeRequest('/v5/reference/sites', 'GET'); $bitrixSites = RCrmActions::getSitesList(); $currencySites = RCrmActions::getCurrencySites(); } catch (CurlException $e) { @@ -1527,7 +1522,7 @@ class intaro_retailcrm extends CModule } // Проверка, что был получен корректный ответ - if (isset($siteResponse) && $siteResponse->getStatusCode() === 200) { + if (isset($siteResponse) && $siteResponse->getStatusCode() === 200 && count($residualRight) === 0) { $sites = $siteResponse->sites ?? null; if ($sites === null) { @@ -1555,7 +1550,7 @@ class intaro_retailcrm extends CModule $result['sitesList'] = $APPLICATION->ConvertCharsetArray($sites, 'utf-8', SITE_CHARSET); } } else { - $result['errCode'] = 'ERR_METHOD_NOT_FOUND'; + $result['errCode'] = sprintf(GetMessage('ERR_403'), implode(', ', $residualRight)); } return $result; diff --git a/intaro.retailcrm/install/step1.php b/intaro.retailcrm/install/step1.php index 60f8295f..4a84799e 100644 --- a/intaro.retailcrm/install/step1.php +++ b/intaro.retailcrm/install/step1.php @@ -1,8 +1,15 @@ $arResult['errCode'], 'HTML' => true]); + } + } $MODULE_ID = 'intaro.retailcrm'; $CRM_API_HOST_OPTION = 'api_host'; @@ -53,4 +60,4 @@ $arResult['API_KEY'] = COption::GetOptionString($MODULE_ID, $CRM_API_KEY_OPTION) - \ No newline at end of file + diff --git a/intaro.retailcrm/install/step3.php b/intaro.retailcrm/install/step3.php index 0b900313..5f80208a 100644 --- a/intaro.retailcrm/install/step3.php +++ b/intaro.retailcrm/install/step3.php @@ -284,41 +284,6 @@ if (isset($arResult['ORDER_PROPS'])) { - - - 0):?> - - - - - - - - - - - - - - - - - - - diff --git a/intaro.retailcrm/lang/en/install/index.php b/intaro.retailcrm/lang/en/install/index.php index f9b50076..fe8e0a65 100644 --- a/intaro.retailcrm/lang/en/install/index.php +++ b/intaro.retailcrm/lang/en/install/index.php @@ -18,3 +18,4 @@ $MESS ['DATE_TIMEZONE_ERR'] = 'Timezone is not specified in php settings.'; $MESS ['SALE_VERSION_ERR'] = '\'Online-store\' module version must be higher than 16.'; $MESS['UF_SUBSCRIBE_USER_EMAIL_TITLE'] = 'Agree to receive promotional newsletters'; $MESS ['CRM_SITES_ERROR'] = 'Failed to get list of CRM stores, please try another API key or contact RetailCRM support.'; +$MESS ['ERR_403'] = 'Insufficient permissions for API key. The module must be added for it to work correctly: %s'; diff --git a/intaro.retailcrm/lang/en/options.php b/intaro.retailcrm/lang/en/options.php index bcf743dd..9ac239eb 100644 --- a/intaro.retailcrm/lang/en/options.php +++ b/intaro.retailcrm/lang/en/options.php @@ -27,8 +27,10 @@ $MESS ['ORDER_NUMBER'] = 'Order numbers: '; $MESS ['ORDER_UPLOAD_INFO'] = 'Click "Start uploading" to upload all the orders . Or list the required order IDs separated by commas, intervals or dashes. For example: 1, 3, 5-10, 12, 13... etc.'; $MESS ['ICRM_OPTIONS_SUBMIT_TITLE'] = 'Save settings'; $MESS ['ICRM_OPTIONS_SUBMIT_VALUE'] = 'Save'; +$MESS ['ERR_403_LABEL'] = 'The module must be added for it to work correctly: %s'; $MESS ['ERR_404'] = 'CRM address may be incorrect.'; -$MESS ['ERR_403'] = 'Wrong apiKey.'; +$MESS ['ERR_403'] = 'Insufficient permissions for API key. %s'; +$MESS ['ERR_403_CUSTOM'] = 'Not enough permissions for API key!'; $MESS ['ERR_JSON'] = 'Invalid data was received from CRM, check data books settings'; $MESS ['ERR_0'] = 'Server connection timeout error.'; $MESS ['ICRM_OPTIONS_OK'] = 'Changes are successfully saved.'; diff --git a/intaro.retailcrm/lang/ru/install/index.php b/intaro.retailcrm/lang/ru/install/index.php index ff079619..385abebc 100644 --- a/intaro.retailcrm/lang/ru/install/index.php +++ b/intaro.retailcrm/lang/ru/install/index.php @@ -56,4 +56,4 @@ $MESS ['UF_LP_ID_INTARO_TITLE'] = 'Номер аккаунта в програм $MESS['IBLOCK_NOT_SELECTED'] = 'Не выбрано ни одного инфоблока для экспорта'; $MESS['TARGET_DIR_DOESNT_EXIST'] = 'Целевая директория не существует'; $MESS['UF_SUBSCRIBE_USER_EMAIL_TITLE'] = 'Согласен на рекламно-информационные рассылки'; - +$MESS ['ERR_403'] = 'Недостаточно прав для API ключа. Для корректной работы модуля необходимо добавить: %s'; diff --git a/intaro.retailcrm/lang/ru/options.php b/intaro.retailcrm/lang/ru/options.php index 818d4087..0605b7fe 100644 --- a/intaro.retailcrm/lang/ru/options.php +++ b/intaro.retailcrm/lang/ru/options.php @@ -40,8 +40,10 @@ $MESS ['CRM_STORE'] = 'CRM магазин: '; $MESS ['ICRM_OPTIONS_SUBMIT_TITLE'] = 'Сохранить настройки'; $MESS ['ICRM_OPTIONS_SUBMIT_VALUE'] = 'Сохранить'; +$MESS ['ERR_403_LABEL'] = 'Для корректной работы модуля необходимо добавить: %s'; $MESS ['ERR_404'] = 'Возможно не верно введен адрес CRM.'; -$MESS ['ERR_403'] = 'Неверный apiKey.'; +$MESS ['ERR_403'] = 'Недостаточно прав для API ключа. %s'; +$MESS ['ERR_403_CUSTOM'] = 'Недостаточно прав для API ключа!'; $MESS ['ERR_JSON'] = 'Получены некорректные данные из CRM, проверьте данные справочников в настройках'; $MESS ['ERR_0'] = 'Превышено время ожидания ответа от сервера.'; $MESS ['ICRM_OPTIONS_OK'] = 'Изменения успешно сохранены.'; diff --git a/intaro.retailcrm/lib/component/constants.php b/intaro.retailcrm/lib/component/constants.php index 58898a9e..1763b715 100644 --- a/intaro.retailcrm/lib/component/constants.php +++ b/intaro.retailcrm/lib/component/constants.php @@ -111,4 +111,20 @@ class Constants public const SYNC_INTEGRATION_PAYMENT = 'sync_integration_payment'; public const CRM_PART_SUBSTITUTED_PAYMENT_CODE = '-not-integration'; public const CRM_SUBSTITUTION_PAYMENT_LIST = 'substitution_payment'; + public const REQUIRED_API_SCOPES = [ + 'order_read' => 'order_read', + 'order_write' => 'order_write', + 'customer_read' => 'customer_read', + 'customer_write' => 'customer_write', + 'store_read' => 'store_read', + 'store_write' => 'store_write', + 'reference_read' => 'reference_read', + 'reference_write' => 'reference_write', + 'integration_read' => 'integration_read', + 'integration_write' => 'integration_write', + ]; + public const REQUIRED_API_SCOPES_CUSTOM = [ + 'custom_fields_read' => 'custom_fields_read', + 'custom_fields_write' => 'custom_fields_write' + ]; } diff --git a/intaro.retailcrm/options.php b/intaro.retailcrm/options.php index b7511160..569ad4e2 100644 --- a/intaro.retailcrm/options.php +++ b/intaro.retailcrm/options.php @@ -88,6 +88,7 @@ if (!empty($_GET['ok']) && $_GET['ok'] === 'Y') { } $arResult = []; +$enabledCustom = false; if (file_exists($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/intaro.retailcrm/classes/general/config/options.xml')) { $options = simplexml_load_file($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/intaro.retailcrm/classes/general/config/options.xml'); @@ -102,9 +103,7 @@ if (file_exists($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/intaro.retailcrm/cl $type["NAME"] = $APPLICATION->ConvertCharset((string) $field, 'utf-8', SITE_CHARSET); $type["ID"] = (string) $field["id"]; - if ($field["group"] === 'custom') { - $arResult['customFields'][] = $type; - } elseif (!$field["group"]) { + if (!$field["group"]) { $arResult['orderProps'][] = $type; } else { $groups = explode(",", (string) $field["group"]); @@ -400,16 +399,6 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { $legalDetailsArr[$orderType['ID']] = $_legalDetailsArr; } - $customFieldsArr = []; - - foreach ($orderTypesList as $orderType) { - $_customFieldsArr = []; - foreach ($arResult['customFields'] as $custom) { - $_customFieldsArr[$custom['ID']] = htmlspecialchars(trim($_POST['custom-fields-' . $custom['ID'] . '-' . $orderType['ID']])); - } - $customFieldsArr[$orderType['ID']] = $_customFieldsArr; - } - //contragents type list $contragentTypeArr = []; @@ -818,11 +807,6 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { $CRM_LEGAL_DETAILS, serialize(RCrmActions::clearArr(is_array($legalDetailsArr) ? $legalDetailsArr : [])) ); - COption::SetOptionString( - $mid, - $CRM_CUSTOM_FIELDS, - serialize(RCrmActions::clearArr(is_array($customFieldsArr) ? $customFieldsArr : [])) - ); COption::SetOptionString( $mid, $CRM_ORDER_NUMBERS, @@ -1079,6 +1063,19 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { // Prepare crm lists try { + $credentialsApi = $api->getCredentials()->getResponseBody(); + $requiredApiScopes = Constants::REQUIRED_API_SCOPES; + + if (ConfigProvider::getCustomFieldsStatus() === 'Y') { + $requiredApiScopes = array_merge($requiredApiScopes, Constants::REQUIRED_API_SCOPES_CUSTOM); + } + + $residualRight = array_diff($requiredApiScopes, $credentialsApi['scopes']); + + if (count($residualRight) !== 0) { + throw new InvalidArgumentException(sprintf(GetMessage('ERR_403_LABEL'), implode(', ', $residualRight))); + } + $arResult['orderTypesList'] = $api->orderTypesList()->orderTypes; $arResult['deliveryTypesList'] = $api->deliveryTypesList()->deliveryTypes; $arResult['deliveryServicesList'] = $api->deliveryServicesList()->deliveryServices; @@ -1089,16 +1086,22 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { $arResult['sitesList'] = $APPLICATION->ConvertCharsetArray($api->sitesList()->sites, 'utf-8', SITE_CHARSET); $arResult['inventoriesList'] = $APPLICATION->ConvertCharsetArray($api->storesList()->stores, 'utf-8', SITE_CHARSET); $arResult['priceTypeList'] = $APPLICATION->ConvertCharsetArray($api->pricesTypes()->priceTypes, 'utf-8', SITE_CHARSET); - $arResult['crmCustomOrderFields'] = $APPLICATION->ConvertCharsetArray( + $arResult['crmCustomOrderFields'] = []; + $arResult['crmCustomUserFields'] = []; + + if (count(array_diff(Constants::REQUIRED_API_SCOPES_CUSTOM, $credentialsApi['scopes'])) === 0) { + $arResult['crmCustomOrderFields'] = $APPLICATION->ConvertCharsetArray( $api->customFieldsList(['entity' => 'order', 'type' => ['string','text', 'numeric', 'boolean', 'date']], 250)->customFields, 'utf-8', SITE_CHARSET - ); - $arResult['crmCustomUserFields'] = $APPLICATION->ConvertCharsetArray( + ); + $arResult['crmCustomUserFields'] = $APPLICATION->ConvertCharsetArray( $api->customFieldsList(['entity' => 'customer', 'type' => ['string', 'text', 'integer', 'numeric', 'boolean', 'date']], 250)->customFields, 'utf-8', SITE_CHARSET - ); + ); + $enabledCustom = true; + } $orderMethods = []; $getOrderMethods = $api->orderMethodsList(); @@ -1123,7 +1126,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { echo CAdminMessage::ShowMessage(GetMessage('ERR_' . $e->getCode())); } catch (InvalidArgumentException $e) { $badKey = true; - echo CAdminMessage::ShowMessage(GetMessage('ERR_403')); + echo CAdminMessage::ShowMessage(['MESSAGE' => sprintf(GetMessage('ERR_403'), $e->getMessage()), 'HTML' => true]); } catch (\RetailCrm\Exception\InvalidJsonException $e) { $badJson = true; echo CAdminMessage::ShowMessage(GetMessage('ERR_JSON')); @@ -2201,34 +2204,6 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) { - 0): ?> - - - - - - - - - - - - - - - - - - @@ -2519,152 +2494,166 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
> -
+ +
- - +
+ + + + + + + + + + + + $crmField) {?> + + + + + + +
+ +
+ +
+ + +      + +   + +
+ +
+ + + - - + + - - + + $crmField) {?> - - + + + + + +
- +
- +
-
+ + +      + +   + +
+ + + +
+
+
- $mass) {?> - - $prop) {?> - - - - - - - -      - -   - - - - - - - -
- - - - - +
+ + + - - - - - - - - $crmField) {?> - - - - - - -
- -
- -
- - -      - -   - -
+