24 private const ERROR_HANDLER_ALREADY_EXISTS =
'ERROR_HANDLER_ALREADY_EXIST';
25 private const ERROR_HANDLER_NOT_FOUND =
'ERROR_HANDLER_NOT_FOUND';
26 private const ERROR_HANDLER_UPDATE =
'ERROR_HANDLER_UPDATE';
27 private const ERROR_HANDLER_DELETE =
'ERROR_HANDLER_DELETE';
33 private static function checkParamsBeforeAddHandler($params)
35 if (empty($params[
'CODE']))
37 throw new RestException(
'Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
40 if (empty($params[
'NAME']))
42 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
45 if (empty($params[
'SETTINGS']) || !is_array($params[
'SETTINGS']))
47 throw new RestException(
'Parameter SETTINGS is not defined or empty', self::ERROR_CHECK_FAILURE);
50 self::checkHandlerSettingsBeforeAdd($params[
'SETTINGS']);
54 '=CODE' => $params[
'CODE']
59 throw new RestException(
'Handler already exists!', self::ERROR_HANDLER_ALREADY_EXISTS);
68 private static function checkParamsBeforeUpdateHandler($params)
70 if (empty($params[
'ID']))
72 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
82 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
85 if ($params[
'APP_ID'] && !empty($handler[
'APP_ID']) && $handler[
'APP_ID'] !== $params[
'APP_ID'])
90 if (empty($params[
'FIELDS']) || !is_array($params[
'FIELDS']))
92 throw new RestException(
'Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
95 if (isset($params[
'FIELDS'][
'SETTINGS']))
97 self::checkHandlerSettingsBeforeUpdate($params[
'FIELDS'][
'SETTINGS']);
106 private static function checkParamsBeforeDeleteHandler($params)
108 if (empty($params[
'ID']))
110 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
115 'ID' => $params[
'ID']
120 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
123 if ($params[
'APP_ID'] && !empty($handler[
'APP_ID']) && $handler[
'APP_ID'] !== $params[
'APP_ID'])
129 'select' => [
'ID',
'HANDLER',
'SETTINGS'],
131 '=HANDLER' =>
'\\'.CashboxRest::class,
136 while ($cashbox = $cashboxListResult->fetch())
138 if ($cashbox[
'SETTINGS'][
'REST'][
'REST_CODE'] === $handler[
'CODE'])
140 $cashboxIdList[] = $cashbox[
'ID'];
147 'There are cashboxes with this handler: '.implode(
', ', $cashboxIdList),
148 self::ERROR_CHECK_FAILURE
156 private static function checkHandlerSettingsBeforeAdd($settings):
void
158 self::checkRequiredSettingsFields($settings, [
'PRINT_URL',
'CHECK_URL',
'CONFIG']);
159 self::checkSettingsFieldValues($settings, [
'HTTP_VERSION',
'CONFIG']);
165 private static function checkHandlerSettingsBeforeUpdate($settings):
void
167 self::checkSettingsFieldValues($settings, [
'PRINT_URL',
'CHECK_URL',
'CONFIG',
'HTTP_VERSION']);
175 private static function checkRequiredSettingsFields(array $settings, array $requiredFields):
void
177 foreach ($requiredFields as $fieldName)
179 if (empty($settings[$fieldName]))
181 throw new RestException(
'Parameter SETTINGS[' . $fieldName .
'] is not defined', self::ERROR_CHECK_FAILURE);
191 private static function checkSettingsFieldValues(array $settings, array $fields):
void
193 foreach ($fields as $fieldName)
195 if ($fieldName ===
'HTTP_VERSION' && array_key_exists(
'HTTP_VERSION', $settings))
197 $version = $settings[
'HTTP_VERSION'];
199 $version !== Main\Web\HttpClient::HTTP_1_0
200 && $version !== Main\Web\HttpClient::HTTP_1_1
203 throw new RestException(
'The value of SETTINGS[HTTP_VERSION] is not valid', self::ERROR_CHECK_FAILURE);
206 elseif ($fieldName ===
'CONFIG' && array_key_exists(
'CONFIG', $settings))
208 self::checkSettingsConfig($settings[
'CONFIG']);
210 elseif (array_key_exists($fieldName, $settings) && empty($settings[$fieldName]))
212 throw new RestException(
'The value of SETTINGS[' . $fieldName .
'] is not valid', self::ERROR_CHECK_FAILURE);
221 private static function checkSettingsConfig(array $config):
void
223 foreach ($config as $group => $block)
225 foreach ($block[
'ITEMS'] as $code => $item)
229 \Bitrix\Sale\Internals\Input\Manager::getEditHtml(
'SETTINGS['.$group.
']['.$code.
']', $item);
233 throw new RestException(
'The config provided in SETTINGS[CONFIG] is not valid', self::ERROR_CHECK_FAILURE);
244 private static function mergeHandlerSettings($cashboxId, array $newSettings): array
247 'select' => [
'SETTINGS'],
248 'filter' => [
'=ID' => $cashboxId],
257 $existingSettings = $dbResult[
'SETTINGS'];
258 if (!$existingSettings)
263 return array_replace_recursive($existingSettings, $newSettings);
271 public static function addHandler($params, $page, \CRestServer $server)
273 Helpers\Rest\AccessChecker::checkAccessPermission();
275 self::checkParamsBeforeAddHandler($params);
277 if (!isset($params[
'SETTINGS'][
'SUPPORTS_FFD105']))
279 $params[
'SETTINGS'][
'SUPPORTS_FFD105'] =
'N';
283 'NAME' => $params[
'NAME'],
284 'CODE' => $params[
'CODE'],
285 'SORT' => $params[
'SORT'] ?: 100,
286 'SETTINGS' => $params[
'SETTINGS'],
287 'APP_ID' => $params[
'APP_ID'],
290 if ($result->isSuccess())
292 return $result->getId();
307 Helpers\Rest\AccessChecker::checkAccessPermission();
309 self::checkParamsBeforeUpdateHandler($params);
311 $handlerFields = $params[
'FIELDS'];
312 if ($handlerFields[
'SETTINGS'])
314 $handlerFields[
'SETTINGS'] = self::mergeHandlerSettings($params[
'ID'], $handlerFields[
'SETTINGS']);
318 if ($result->isSuccess())
323 $errors = implode(
"\n", $result->getErrorMessages());
324 throw new RestException($errors, self::ERROR_HANDLER_UPDATE);
334 Helpers\Rest\AccessChecker::checkAccessPermission();
336 self::checkParamsBeforeDeleteHandler($params);
339 if ($result->isSuccess())
344 $errors = implode(
"\n", $result->getErrorMessages());
345 throw new RestException($errors, self::ERROR_HANDLER_DELETE);
353 Helpers\Rest\AccessChecker::checkAccessPermission();
358 'select' => [
'ID',
'NAME',
'CODE',
'SORT',
'SETTINGS'],
360 while ($item = $dbRes->fetch())
static includeModule($moduleName)
static getList(array $parameters=array())
static update($primary, array $data)