36 private const ERROR_HANDLER_ADD =
'ERROR_HANDLER_ADD';
37 private const ERROR_HANDLER_UPDATE =
'ERROR_HANDLER_UPDATE';
38 private const ERROR_HANDLER_DELETE =
'ERROR_HANDLER_DELETE';
49 private const ALLOWED_PAYSYSTEM_FIELDS = [
50 'ID',
'PERSON_TYPE_ID',
'NAME',
'PSA_NAME',
'SORT',
'DESCRIPTION',
'ACTION_FILE',
'RESULT_FILE',
51 'NEW_WINDOW',
'TARIF',
'PS_MODE',
'HAVE_PAYMENT',
'HAVE_ACTION',
'HAVE_RESULT',
'HAVE_PREPAY',
52 'HAVE_PRICE',
'HAVE_RESULT_RECEIVE',
'ENCODING',
'LOGOTIP',
'ACTIVE',
'ALLOW_EDIT_PAYMENT',
53 'IS_CASH',
'AUTO_CHANGE_1C',
'CAN_PRINT_CHECK',
'ENTITY_REGISTRY_TYPE',
'XML_ID'
58 if (!Main\Loader::includeModule(
'rest'))
63 if (empty($fields[
'APP_ID']) || empty($fields[
'CLEAN']) || $fields[
'CLEAN'] !==
true)
68 $app = Rest\AppTable::getByClientId($fields[
'APP_ID']);
74 $restHandlerResult = Internals\PaySystemRestHandlersTable::getList([
75 'select' => [
'ID',
'CODE'],
77 '=APP_ID' => $app[
'CLIENT_ID'],
80 while ($restHandler = $restHandlerResult->fetch())
85 '=ACTION_FILE' => $restHandler[
'CODE'],
88 while ($paySystem = $paySystemResult->fetch())
93 Internals\PaySystemRestHandlersTable::delete($restHandler[
'ID']);
104 'sale.paysystem.handler.add' => [__CLASS__,
'addHandler'],
105 'sale.paysystem.handler.update' => [__CLASS__,
'updateHandler'],
106 'sale.paysystem.handler.delete' => [__CLASS__,
'deleteHandler'],
107 'sale.paysystem.handler.list' => [__CLASS__,
'getHandlerList'],
109 'sale.paysystem.add' => [__CLASS__,
'addPaySystem'],
110 'sale.paysystem.update' => [__CLASS__,
'updatePaySystem'],
111 'sale.paysystem.delete' => [__CLASS__,
'deletePaySystem'],
112 'sale.paysystem.list' => [__CLASS__,
'getPaySystemList'],
114 'sale.paysystem.settings.get' => [__CLASS__,
'getSettings'],
115 'sale.paysystem.settings.update' => [__CLASS__,
'updateSettings'],
117 'sale.paysystem.settings.invoice.get' => [__CLASS__,
'getSettingsByInvoice'],
118 'sale.paysystem.settings.payment.get' => [__CLASS__,
'getSettingsByPayment'],
120 'sale.paysystem.pay.invoice' => [__CLASS__,
'payInvoice'],
121 'sale.paysystem.pay.payment' => [__CLASS__,
'payPayment'],
133 public static function addPaySystem(array $params, $n, \CRestServer $server)
135 static::checkPaySystemPermission();
137 $params = self::preparePaySystemParams($params, $server);
139 if (!isset($params[
'ENTITY_REGISTRY_TYPE']))
141 if (IsModuleInstalled(
'crm'))
143 $params[
'ENTITY_REGISTRY_TYPE'] = REGISTRY_TYPE_CRM_INVOICE;
151 static::checkParamsBeforePaySystemAdd($params);
154 'NAME' => $params[
'NAME'],
155 'PSA_NAME' => $params[
'NAME'],
156 'NEW_WINDOW' => $params[
'NEW_WINDOW'] ?:
'N',
157 'ACTIVE' => $params[
'ACTIVE'] ?:
'N',
158 'PERSON_TYPE_ID' => $params[
'PERSON_TYPE_ID'],
159 'ACTION_FILE' => $params[
'BX_REST_HANDLER'],
160 'HAVE_PREPAY' =>
'N',
161 'HAVE_RESULT' =>
'N',
162 'HAVE_ACTION' =>
'N',
163 'HAVE_PAYMENT' =>
'N',
164 'HAVE_RESULT_RECEIVE' =>
'Y',
165 'ENTITY_REGISTRY_TYPE' => $params[
'ENTITY_REGISTRY_TYPE'],
166 'DESCRIPTION' => $params[
'DESCRIPTION'],
167 'XML_ID' => $params[
'XML_ID'],
170 if (isset($params[
'LOGOTIP']))
172 $fields[
'LOGOTIP'] = self::saveFile($params[
'LOGOTIP']);
176 if ($result->isSuccess())
178 $id = $result->getId();
181 foreach ($params[
'SETTINGS'] as $key => $value)
186 $params[
'PERSON_TYPE_ID'],
188 'PROVIDER_KEY' => $value[
'TYPE'],
189 'PROVIDER_VALUE' => $value[
'VALUE']
194 if ($fields[
'PERSON_TYPE_ID'] > 0)
196 static::savePersonTypeId($id, $params[
'PERSON_TYPE_ID']);
199 static::logAnalytics(
200 'addPaySystem' . $params[
'ENTITY_REGISTRY_TYPE'],
201 $params[
'BX_REST_HANDLER'],
202 $params[
'PERSON_TYPE_ID'],
209 $error = implode(
"\n", $result->getErrorMessages());
220 if (empty($params[
'BX_REST_HANDLER']))
222 throw new RestException(
'Parameter BX_REST_HANDLER is not defined', self::ERROR_CHECK_FAILURE);
225 $handlerData = self::getHandlerData($params[
'BX_REST_HANDLER']);
228 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
231 if ($params[
'APP_ID'] && !empty($handlerData[
'APP_ID']) && $handlerData[
'APP_ID'] !== $params[
'APP_ID'])
236 $dbRes = Internals\PersonTypeTable::getList([
238 '=ID' => $params[
'PERSON_TYPE_ID'],
239 '=ENTITY_REGISTRY_TYPE' => $params[
'ENTITY_REGISTRY_TYPE'],
242 if (!$dbRes->fetch())
244 throw new RestException(
'Incorrect person type id!', self::ERROR_PERSON_TYPE_NOT_FOUND);
252 private static function savePersonTypeId($serviceId, $personTypeId)
256 "SERVICE_ID" => $serviceId,
257 "SERVICE_TYPE" => Restrictions\Manager::SERVICE_TYPE_PAYMENT,
258 "=CLASS_NAME" =>
'\\'.Restrictions\PersonType::class
262 $dbRes = Internals\ServiceRestrictionTable::getList($params);
263 if ($data = $dbRes->fetch())
265 $restrictionId = $data[
'ID'];
273 "SERVICE_ID" => $serviceId,
274 "SERVICE_TYPE" => Restrictions\Manager::SERVICE_TYPE_PAYMENT,
276 "PARAMS" => array(
'PERSON_TYPE_ID' => array($personTypeId))
279 Restrictions\PersonType::save($fields, $restrictionId);
290 static::checkPaySystemPermission();
292 $params = self::preparePaySystemParams($params, $server);
294 static::checkParamsBeforePaySystemUpdate($params);
297 if (isset($params[
'FIELDS'][
'NAME']))
299 $fields[
'NAME'] = $params[
'FIELDS'][
'NAME'];
302 if (isset($params[
'FIELDS'][
'DESCRIPTION']))
304 $fields[
'DESCRIPTION'] = $params[
'FIELDS'][
'DESCRIPTION'];
307 if (isset($params[
'FIELDS'][
'NEW_WINDOW']))
309 $fields[
'NEW_WINDOW'] = $params[
'FIELDS'][
'NEW_WINDOW'];
312 if (isset($params[
'FIELDS'][
'ACTIVE']))
314 $fields[
'ACTIVE'] = $params[
'FIELDS'][
'ACTIVE'];
317 if (isset($params[
'FIELDS'][
'PERSON_TYPE_ID']))
319 $fields[
'PERSON_TYPE_ID'] = $params[
'FIELDS'][
'PERSON_TYPE_ID'];
322 if (isset($params[
'FIELDS'][
'BX_REST_HANDLER']))
324 $fields[
'ACTION_FILE'] = $params[
'FIELDS'][
'BX_REST_HANDLER'];
327 if (isset($params[
'FIELDS'][
'LOGOTIP']))
329 $fields[
'LOGOTIP'] = self::saveFile($params[
'FIELDS'][
'LOGOTIP']);
334 if ($fields[
'PERSON_TYPE_ID'] > 0)
336 static::savePersonTypeId($params[
'ID'], $fields[
'PERSON_TYPE_ID']);
339 return $result->isSuccess();
349 $handlerList = Manager::getHandlerList();
351 $handler = $params[
'FIELDS'][
'BX_REST_HANDLER'];
352 if (!isset($handlerList[
'USER'][$handler]) && !isset($handlerList[
'SYSTEM'][$handler]))
354 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
359 'ID' => $params[
'ID']
363 $data = $dbRes->fetch();
366 throw new RestException(
'Pay system not found', self::ERROR_PAY_SYSTEM_NOT_FOUND);
369 if (!self::hasAccessToPaySystem($data, $params[
'APP_ID']))
374 $dbRes = Internals\PersonTypeTable::getList([
376 '=ID' => $params[
'FIELDS'][
'PERSON_TYPE_ID'],
377 '=ENTITY_REGISTRY_TYPE' => $data[
'ENTITY_REGISTRY_TYPE'],
380 if (!$dbRes->fetch())
382 throw new RestException(
'Incorrect person type id!', self::ERROR_PERSON_TYPE_NOT_FOUND);
395 static::checkPaySystemPermission();
397 $params = self::preparePaySystemParams($params, $server);
399 static::checkParamsBeforeSettingsUpdate($params);
401 foreach ($params[
'SETTINGS'] as $field => $value)
406 $params[
'PERSON_TYPE_ID'],
408 'PROVIDER_KEY' => $value[
'TYPE'],
409 'PROVIDER_VALUE' => $value[
'VALUE']
413 if (!$result->isSuccess())
428 if (!isset($params[
'ID']))
430 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
436 throw new RestException(
'Pay system not found', static::ERROR_CHECK_FAILURE);
439 if (!isset($params[
'SETTINGS']) || empty($params[
'SETTINGS']))
441 throw new RestException(
'Parameter SETTINGS is not defined or empty', self::ERROR_HANDLER_NOT_FOUND);
444 if (!self::hasAccessToPaySystem($item, $params[
'APP_ID']))
456 public static function getSettings(array $params, $n, \CRestServer $server)
458 static::checkPaySystemPermission();
460 $params = self::preparePaySystemParams($params, $server);
462 static::checkParamsBeforeSettingsGet($params);
469 foreach ($codes as $field => $value)
474 $params[
'PERSON_TYPE_ID']
478 'TYPE' => $mapping[
'PROVIDER_KEY'],
479 'VALUE' => $mapping[
'PROVIDER_VALUE']
493 if (!isset($params[
'PERSON_TYPE_ID']))
495 throw new RestException(
'Parameter PERSON_TYPE_ID is not defined', self::ERROR_CHECK_FAILURE);
498 if (!isset($params[
'ID']))
500 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
506 throw new RestException(
'Pay system not found', static::ERROR_CHECK_FAILURE);
509 if (!self::hasAccessToPaySystem($item, $params[
'APP_ID']))
521 static::checkPaySystemPermission();
523 $params = self::preparePaySystemParams($params, $server);
525 static::checkParamsBeforePaySystemDelete($params);
529 return $result->isSuccess();
542 throw new RestException(
'Pay system not found', self::ERROR_PAY_SYSTEM_NOT_FOUND);
545 if (!self::hasAccessToPaySystem($data, $params[
'APP_ID']))
558 public static function addHandler(array $params, $n, \CRestServer $server)
560 static::checkPaySystemPermission();
562 $params = self::prepareHandlerParams($params, $server);
564 self::checkParamsOnAddHandler($params);
567 'NAME' => $params[
'NAME'],
568 'CODE' => $params[
'CODE'],
569 'SORT' => $params[
'SORT'] ?: 100,
570 'SETTINGS' => $params[
'SETTINGS'],
571 'APP_ID' => $params[
'APP_ID'],
574 $result = Internals\PaySystemRestHandlersTable::add($data);
575 if ($result->isSuccess())
577 return $result->getId();
580 $error = implode(
"\n", $result->getErrorMessages());
588 private static function checkParamsOnAddHandler(array $params)
590 if (!isset($params[
'NAME']))
592 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
595 if (!isset($params[
'CODE']))
597 throw new RestException(
'Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
600 if (!isset($params[
'SETTINGS']))
602 throw new RestException(
'Parameter SETTINGS is not defined', self::ERROR_CHECK_FAILURE);
605 if (!isset($params[
'SETTINGS'][
'CODES']))
607 throw new RestException(
'Parameter SETTINGS[CODES] is not defined', self::ERROR_CHECK_FAILURE);
611 empty($params[
'SETTINGS'][
'FORM_DATA'])
612 && empty($params[
'SETTINGS'][
'CHECKOUT_DATA'])
613 && empty($params[
'SETTINGS'][
'IFRAME_DATA'])
616 throw new RestException(
617 'Parameter SETTINGS[FORM_DATA] or SETTINGS[CHECKOUT_DATA] or SETTINGS[IFRAME_DATA] is not defined',
618 self::ERROR_CHECK_FAILURE
623 !empty($params[
'SETTINGS'][
'FORM_DATA'])
624 && empty($params[
'SETTINGS'][
'FORM_DATA'][
'ACTION_URI'])
627 throw new RestException(
'Parameter SETTINGS[FORM_DATA][ACTION_URI] is not defined', self::ERROR_CHECK_FAILURE);
631 !empty($params[
'SETTINGS'][
'CHECKOUT_DATA'])
632 && empty($params[
'SETTINGS'][
'CHECKOUT_DATA'][
'ACTION_URI'])
635 throw new RestException(
'Parameter SETTINGS[IFRAME_DATA][ACTION_URI] is not defined', self::ERROR_CHECK_FAILURE);
639 !empty($params[
'SETTINGS'][
'IFRAME_DATA'])
640 && empty($params[
'SETTINGS'][
'IFRAME_DATA'][
'ACTION_URI'])
643 throw new RestException(
'Parameter SETTINGS[IFRAME_DATA][ACTION_URI] is not defined', self::ERROR_CHECK_FAILURE);
647 isset($params[
'SETTINGS'][
'CLIENT_TYPE'])
651 throw new RestException(
'Parameter value SETTINGS[CLIENT_TYPE] is invalid', self::ERROR_CHECK_FAILURE);
654 $dbRes = Internals\PaySystemRestHandlersTable::getList([
656 '=CODE' => $params[
'CODE']
661 throw new RestException(
'Handler already exists!', self::ERROR_HANDLER_ALREADY_EXIST);
672 public static function updateHandler(array $params, $n, \CRestServer $server)
674 static::checkPaySystemPermission();
676 $params = self::prepareHandlerParams($params, $server);
678 self::checkParamsOnUpdateHandler($params);
680 $result = Internals\PaySystemRestHandlersTable::update($params[
'ID'], $params[
'FIELDS']);
681 if ($result->isSuccess())
686 $error = implode(
"\n", $result->getErrorMessages());
695 private static function checkParamsOnUpdateHandler(array $params)
697 if (!isset($params[
'FIELDS']))
699 throw new RestException(
'Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
703 isset($params[
'SETTINGS'][
'CLIENT_TYPE'])
707 throw new RestException(
'Parameter value SETTINGS[CLIENT_TYPE] is invalid', self::ERROR_CHECK_FAILURE);
710 $paySystemRestHandlers = Internals\PaySystemRestHandlersTable::getList(array(
712 'ID' => $params[
'ID']
715 if (!$paySystemRestHandlers)
717 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
720 if ($params[
'APP_ID'] && !empty($paySystemRestHandlers[
'APP_ID']) && $paySystemRestHandlers[
'APP_ID'] !== $params[
'APP_ID'])
722 throw new AccessException();
733 public static function deleteHandler(array $params, $n, \CRestServer $server)
735 static::checkPaySystemPermission();
737 $params = self::prepareHandlerParams($params, $server);
739 self::checkParamsOnDeleteHandler($params);
741 $result = Internals\PaySystemRestHandlersTable::delete($params[
'ID']);
742 if ($result->isSuccess())
747 $error = implode(
"\n", $result->getErrorMessages());
755 private static function checkParamsOnDeleteHandler($params): void
757 $dbRes = Internals\PaySystemRestHandlersTable::getList(array(
759 'ID' => $params[
'ID']
762 $data = $dbRes->fetch();
765 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
768 if ($params[
'APP_ID'] && !empty($data[
'APP_ID']) && $data[
'APP_ID'] !== $params[
'APP_ID'])
773 $dbRes =
Manager::getList(array(
'filter' => array(
'ACTION_FILE' => $data[
'CODE'])));
776 throw new RestException(
'Pay system with handler '.ToUpper($data[
'CODE']).
' exists!', self::ERROR_PAY_SYSTEM_DELETE);
788 static::checkPaySystemPermission();
791 $dbRes = Internals\PaySystemRestHandlersTable::getList([
792 'select' => [
'ID',
'NAME',
'CODE',
'SORT',
'SETTINGS'],
794 while ($item = $dbRes->fetch())
810 static::checkPaySystemPermission();
811 $params = self::prepareIncomingParams($params);
812 self::checkParamsBeforePaySystemListGet($params);
815 isset($params[
'SELECT']) && is_array($params[
'SELECT'])
816 ? array_flip(self::prepareIncomingParams(array_flip($params[
'SELECT'])))
817 : self::ALLOWED_PAYSYSTEM_FIELDS
821 $filterFromParams = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
822 if ($filterFromParams)
824 $incomingFieldsMap = self::getIncomingFieldsMap();
825 foreach ($filterFromParams as $rawName => $value)
827 $filterField = \CSqlUtil::GetFilterOperation($rawName);
828 $fieldName = $incomingFieldsMap[$filterField[
'FIELD']] ?? $filterField[
'FIELD'];
829 $filter[$filterField[
'OPERATION'] . $fieldName] = $value;
834 isset($params[
'ORDER']) && is_array($params[
'ORDER'])
835 ? self::prepareIncomingParams($params[
'ORDER'])
845 while ($item = $dbRes->fetch())
847 $result[] = self::prepareOutcomingFields($item);
857 private static function checkParamsBeforePaySystemListGet(array $params)
859 $select = isset($params[
'SELECT']) && is_array($params[
'SELECT']) ? $params[
'SELECT'] : [];
862 $select = array_flip(self::prepareIncomingParams(array_flip($select)));
863 $diffSelect = array_diff($select, self::ALLOWED_PAYSYSTEM_FIELDS);
867 throw new RestException(implode(
', ', $diffSelect) .
' not allowed for select');
871 $filter = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
875 foreach ($filter as $rawName => $value)
877 $filterField = \CSqlUtil::GetFilterOperation($rawName);
878 if (isset($filterField[
'FIELD']))
880 $filterFields[] = $filterField[
'FIELD'];
884 $filterFields = array_flip(self::prepareIncomingParams(array_flip($filterFields)));
885 $diffFilter = array_diff($filterFields, self::ALLOWED_PAYSYSTEM_FIELDS);
888 throw new RestException(implode(
', ', $diffFilter) .
' not allowed for filter');
893 isset($params[
'ORDER']) && is_array($params[
'ORDER'])
894 ? self::prepareIncomingParams($params[
'ORDER'])
899 $diffOrder = array_diff(array_keys($order), self::ALLOWED_PAYSYSTEM_FIELDS);
902 throw new RestException(implode(
', ', $diffOrder) .
' not allowed for order');
914 public static function getSettingsByInvoice(array $params, $n, \CRestServer $server)
916 static::checkOrderPermission();
918 $params = self::preparePaySystemParams($params, $server);
920 self::checkParamsBeforeSettingsByInvoiceGet($params);
922 if (isset($params[
'PAY_SYSTEM_ID']))
928 $dbRes =
Manager::getList(array(
'filter' => array(
'=ACTION_FILE' => $params[
'BX_REST_HANDLER'])));
929 $item = $dbRes->fetch();
932 throw new RestException(
'Pay system with handler '.$params[
'BX_REST_HANDLER'].
' not found', self::ERROR_PAY_SYSTEM_NOT_FOUND);
935 $service =
new Service($item);
938 $invoice = Invoice\Invoice::load($params[
'INVOICE_ID']);
941 $paymentCollection = $invoice->getPaymentCollection();
942 if ($paymentCollection)
945 foreach ($paymentCollection as $payment)
947 if (!$payment->isInner())
949 return $service->getParamsBusValue($payment);
955 throw new RestException(
'Invoice #'.$params[
'INVOICE_ID'].
' not found', self::ERROR_INTERNAL_INVOICE_NOT_FOUND);
965 public static function getSettingsByPayment(array $params, $n, \CRestServer $server)
967 static::checkOrderPermission();
969 $params = self::preparePaySystemParams($params, $server);
971 self::checkParamsBeforeSettingsByPaymentGet($params);
973 list($orderId, $paymentId) = Manager::getIdsByPayment($params[
'PAYMENT_ID']);
978 $orderClassName = $registry->getOrderClassName();
979 $order = $orderClassName::load($orderId);
982 $paymentCollection = $order->getPaymentCollection();
985 $payment = $paymentCollection->getItemById($paymentId);
989 return $service->getParamsBusValue($payment);
992 throw new RestException(
'Order #'.$orderId.
' not found', self::ERROR_INTERNAL_ORDER_NOT_FOUND);
1002 public static function payInvoice(array $params, $n, \CRestServer $server)
1004 if (!Main\Loader::includeModule(
'crm'))
1006 throw new RestException(
'Pay invoice is not supported!', self::ERROR_PAY_INVOICE_NOT_SUPPORTED);
1009 static::checkOrderPermission();
1011 $params = self::prepareIncomingParams($params);
1013 self::checkParamsForInvoice($params);
1015 $dbRes = Invoice\Payment::getList(array(
1016 'select' => array(
'ID'),
1018 'ORDER_ID' => $params[
'INVOICE_ID'],
1023 $payment = $dbRes->fetch();
1026 throw new RestException(
'Invoice #'.$params[
'INVOICE_ID'].
' not found', self::ERROR_INTERNAL_INVOICE_NOT_FOUND);
1029 $params[
'PAYMENT_ID'] = $payment[
'ID'];
1032 '=ENTITY_REGISTRY_TYPE' => REGISTRY_TYPE_CRM_INVOICE
1035 if (isset($params[
'PAY_SYSTEM_ID']))
1037 $filter[
'=ID'] = $params[
'PAY_SYSTEM_ID'];
1041 $filter[
'=ACTION_FILE'] = $params[
'BX_REST_HANDLER'];
1048 $item = $dbRes->fetch();
1051 throw new RestException(
'Pay system not found', static::ERROR_PROCESS_REQUEST_RESULT);
1054 $params[
'PAY_SYSTEM_ID'] = $item[
'ID'];
1056 return self::payPaymentInternal($params, $server);
1065 public static function payPayment(array $params, $n, \CRestServer $server)
1067 static::checkOrderPermission();
1069 $params = self::prepareIncomingParams($params);
1071 self::checkParamsForPayment($params);
1073 return self::payPaymentInternal($params, $server);
1082 private static function payPaymentInternal(array $params, \CRestServer $restServer)
1084 $context = Main\Context::getCurrent();
1085 $server = $context->getServer();
1087 $request =
new Main\HttpRequest($server, array(), $params, array(), array());
1091 $result = $service->processRequest($request);
1092 if (!$result->isSuccess())
1094 $error = join(
"\n", $result->getErrorMessages());
1095 throw new RestException($error, static::ERROR_PROCESS_REQUEST_RESULT);
1098 static::logAnalytics(
1099 'payPayment' . $service->getField(
'ENTITY_REGISTRY_TYPE'),
1100 $service->getField(
'ACTION_FILE'),
1101 $service->getField(
'PERSON_TYPE_ID'),
1113 private static function arrayChangeKeyCaseRecursive(array $data, $case = CASE_UPPER)
1115 return array_map(
static function ($item) use ($case) {
1116 if (is_array($item))
1118 $item = self::arrayChangeKeyCaseRecursive($item, $case);
1121 }, array_change_key_case($data, $case));
1128 private static function prepareIncomingParams(array $data): array
1130 return self::replaceIncomingKeys(self::arrayChangeKeyCaseRecursive($data));
1138 private static function prepareHandlerParams(array $data, \CRestServer $server): array
1140 $data = self::replaceIncomingKeys(array_change_key_case($data, CASE_UPPER));
1141 $data[
'APP_ID'] = $server->getClientId();
1146 private static function preparePaySystemParams(array $data, \CRestServer $server): array
1148 $data = self::prepareIncomingParams($data);
1149 $data[
'APP_ID'] = $server->getClientId();
1158 private static function checkParamsForInvoice(array $params)
1160 if (!isset($params[
'BX_REST_HANDLER']) && !isset($params[
'PAY_SYSTEM_ID']))
1162 throw new RestException(
'Empty field BX_REST_HANDLER and PAY_SYSTEM_ID', self::ERROR_CHECK_FAILURE);
1165 if (isset($params[
'PAY_SYSTEM_ID']))
1170 throw new RestException(
'Pay system with ID='.$params[
'PAY_SYSTEM_ID'].
' not found', static::ERROR_CHECK_FAILURE);
1174 if (isset($params[
'BX_REST_HANDLER']))
1176 $dbRes = Internals\PaySystemRestHandlersTable::getList(array(
1178 '=CODE' => $params[
'BX_REST_HANDLER']
1181 if (!$dbRes->fetch())
1183 throw new RestException(
'Incorrect rest handler code', static::ERROR_CHECK_FAILURE);
1187 if (empty($params[
'INVOICE_ID']))
1189 throw new RestException(
'Empty field INVOICE_ID!', self::ERROR_CHECK_FAILURE);
1198 private static function checkParamsBeforeSettingsByInvoiceGet(array $params)
1200 if (!isset($params[
'BX_REST_HANDLER']) && !isset($params[
'PAY_SYSTEM_ID']))
1202 throw new RestException(
'Empty field BX_REST_HANDLER and PAY_SYSTEM_ID', self::ERROR_CHECK_FAILURE);
1205 if (empty($params[
'INVOICE_ID']))
1207 throw new RestException(
'Empty field INVOICE_ID', self::ERROR_CHECK_FAILURE);
1210 if (isset($params[
'PAY_SYSTEM_ID']))
1215 throw new RestException(
'Pay system with ID='.$params[
'PAY_SYSTEM_ID'].
' not found', static::ERROR_CHECK_FAILURE);
1218 if (!self::hasAccessToPaySystem($data, $params[
'APP_ID']))
1220 throw new AccessException();
1224 if (isset($params[
'BX_REST_HANDLER']))
1226 $dbRes = Internals\PaySystemRestHandlersTable::getList(array(
1228 '=CODE' => $params[
'BX_REST_HANDLER']
1232 $handlerData = $dbRes->fetch();
1235 throw new RestException(
'Incorrect rest handler code', static::ERROR_CHECK_FAILURE);
1238 if ($params[
'APP_ID'] && !empty($handlerData[
'APP_ID']) && $handlerData[
'APP_ID'] !== $params[
'APP_ID'])
1240 throw new AccessException();
1250 private static function checkParamsForPayment(array $params)
1252 if (empty($params[
'PAY_SYSTEM_ID']))
1254 throw new RestException(
'Empty field PAY_SYSTEM_ID!', self::ERROR_CHECK_FAILURE);
1260 throw new RestException(
'Pay system not found', static::ERROR_CHECK_FAILURE);
1263 if (empty($params[
'PAYMENT_ID']))
1265 throw new RestException(
'Empty field PAYMENT_ID', self::ERROR_CHECK_FAILURE);
1271 $paymentClassName = $registry->getPaymentClassName();
1272 $dbRes = $paymentClassName::getList([
1273 'select' => [
'ID',
'PAY_SYSTEM_ID'],
1275 '=ID' => $params[
'PAYMENT_ID'],
1276 '=PAY_SYSTEM_ID' => $params[
'PAY_SYSTEM_ID']
1280 if (!$dbRes->fetch())
1282 throw new RestException(
1284 . $params[
'PAYMENT_ID']
1285 .
' and PAY_SYSTEM_ID='.$params[
'PAY_SYSTEM_ID']
1286 .
' not found', self::ERROR_CHECK_FAILURE
1296 private static function checkParamsBeforeSettingsByPaymentGet(array $params)
1298 if (empty($params[
'PAY_SYSTEM_ID']))
1300 throw new RestException(
'Empty field PAY_SYSTEM_ID!', self::ERROR_CHECK_FAILURE);
1303 if (empty($params[
'PAYMENT_ID']))
1305 throw new RestException(
'Empty field PAYMENT_ID', self::ERROR_CHECK_FAILURE);
1311 throw new RestException(
'Pay system not found', static::ERROR_CHECK_FAILURE);
1314 if (!self::hasAccessToPaySystem($item, $params[
'APP_ID']))
1316 throw new AccessException();
1322 $paymentClassName = $registry->getPaymentClassName();
1323 $dbRes = $paymentClassName::getList([
1324 'select' => [
'ID',
'PAY_SYSTEM_ID'],
1326 '=ID' => $params[
'PAYMENT_ID'],
1327 '=PAY_SYSTEM_ID' => $params[
'PAY_SYSTEM_ID']
1331 if (!$dbRes->fetch())
1333 throw new RestException(
1335 . $params[
'PAYMENT_ID']
1336 .
' and PAY_SYSTEM_ID='.$params[
'PAY_SYSTEM_ID']
1337 .
' not found', self::ERROR_CHECK_FAILURE
1348 global $APPLICATION;
1350 if (IsModuleInstalled(
'intranet') && Main\Loader::includeModule(
'crm'))
1352 $CCrmInvoice = new \CCrmInvoice();
1353 if ($CCrmInvoice->cPerms->HavePerm(
'INVOICE', BX_CRM_PERM_NONE,
'WRITE')
1354 && $CCrmInvoice->cPerms->HavePerm(
'INVOICE', BX_CRM_PERM_NONE,
'ADD')
1362 $saleModulePermissions = $APPLICATION->GetGroupRight(
"sale");
1364 if($saleModulePermissions ==
"D")
1377 \Bitrix\Sale\Helpers\Rest\AccessChecker::checkAccessPermission();
1384 private static function saveFile($fileContent)
1386 $file = \CRestUtil::saveFile($fileContent);
1389 $file[
'MODULE_ID'] =
'sale';
1390 return \CFile::SaveFile($file,
'sale');
1399 private static function getIncomingFieldsMap(): array
1402 'LOGOTYPE' =>
'LOGOTIP',
1403 'TARIFF' =>
'TARIF',
1410 private static function getOutcomingFieldsMap(): array
1413 'LOGOTIP' =>
'LOGOTYPE',
1414 'TARIF' =>
'TARIFF',
1422 private static function prepareOutcomingFields(array $data): array
1424 return self::replaceOutcomingKeys($data);
1431 private static function replaceIncomingKeys(array $data): array
1433 return self::replaceKeys($data, self::getIncomingFieldsMap());
1440 private static function replaceOutcomingKeys(array $data): array
1442 return self::replaceKeys($data, self::getOutcomingFieldsMap());
1450 private static function replaceKeys(array $data, array $map): array
1452 foreach ($map as $key => $newKey)
1454 if (array_key_exists($key, $data))
1456 $data[$newKey] = $data[$key];
1460 if (isset($data[
'FIELDS']) && array_key_exists($key, $data[
'FIELDS']))
1462 $data[
'FIELDS'][$newKey] = $data[
'FIELDS'][$key];
1463 unset($data[
'FIELDS'][$key]);
1470 private static function logAnalytics($action, $handler, $personType, \CRestServer $restServer) : bool
1474 if ($restServer->getAuthType() === \
Bitrix\Rest\OAuth\Auth::AUTH_TYPE)
1476 $app = \Bitrix\Rest\AppTable::getByClientId($restServer->getClientId());
1479 $code = $app[
'CODE'];
1485 $code = $restServer->getPasswordId();
1491 $tag = uniqid($code,
true);
1518 private static function hasAccessToPaySystem(array $paySystemData,
string $appId =
null): bool
1520 $handlerCode = $paySystemData[
'ACTION_FILE'];
1523 $handlerData = self::getHandlerData($handlerCode);
1524 if ($appId && !empty($handlerData[
'APP_ID']) && $handlerData[
'APP_ID'] !== $appId)
1537 private static function getHandlerData(
string $code): ?array
1539 static $result = [];
1541 if (!empty($result[$code]))
1543 return $result[$code];
1546 $handlerData = Internals\PaySystemRestHandlersTable::getList([
1547 'filter' => [
'CODE' => $code],
1550 if (is_array($handlerData))
1552 $result[$code] = $handlerData;
1555 return $result[$code] ??
null;