17 private $handlerClassName;
18 private array $handlerDescription = [];
19 private array $handlerModeList = [];
21 private function checkParams(array $fields):
Sale\Result
25 if (empty($fields[
'ACTION_FILE']))
29 'actionFile not found',
35 if (!empty($fields[
'PS_MODE']))
37 $this->loadHandlerModeList($fields[
'ACTION_FILE']);
38 if (!array_key_exists($fields[
'PS_MODE'], $this->handlerModeList))
42 "psMode \"{$fields['PS_MODE']}\" not available",
52 public function run(array $fields)
56 $checkParamsResult = $this->checkParams($fields);
57 if (!$checkParamsResult->isSuccess())
59 $this->
addErrors($checkParamsResult->getErrors());
63 $createPaySystemResult = $this->createPaySystem($fields);
64 if (!$createPaySystemResult->isSuccess())
66 $this->
addErrors($createPaySystemResult->getErrors());
71 'ID' => $createPaySystemResult->getId(),
75 private function createPaySystem(array $fields):
Sale\
Result
79 $name = $fields[
'NAME'] ??
'';
82 $name = $this->getDefaultPaySystemName($fields[
'ACTION_FILE'], $fields[
'PS_MODE'] ??
null);
85 $actionFile = $fields[
'ACTION_FILE'];
86 $psMode = $fields[
'PS_MODE'] ??
'';
91 'ACTION_FILE' => $actionFile,
93 'NEW_WINDOW' => $fields[
'NEW_WINDOW'] ?:
'N',
94 'ACTIVE' => $fields[
'ACTIVE'] ?:
'Y',
95 'DESCRIPTION' => $fields[
'DESCRIPTION'] ??
'',
96 'XML_ID' => $fields[
'XML_ID'] ?? Sale\PaySystem\Manager::generateXmlId(),
97 'ENTITY_REGISTRY_TYPE' => $fields[
'ENTITY_REGISTRY_TYPE'] ?? Sale\Registry::REGISTRY_TYPE_ORDER
100 if (isset($fields[
'ENTITY_REGISTRY_TYPE']))
102 $paySystemParams[
'ENTITY_REGISTRY_TYPE'] = $fields[
'ENTITY_REGISTRY_TYPE'];
105 if (isset($fields[
'LOGOTYPE']))
107 $paySystemParams[
'LOGOTIP'] = self::saveFile($fields[
'LOGOTYPE']);
111 $documentRoot = Main\Application::getDocumentRoot();
115 $image =
'/bitrix/images/sale/sale_payments/' . $actionFile .
'/' . $psMode .
'.png';
116 if (Main\
IO\File::isFileExists($documentRoot . $image))
118 $paySystemParams[
'LOGOTIP'] = \CFile::MakeFileArray($image);
122 if (!isset($paySystemParams[
'LOGOTIP']))
124 $image =
'/bitrix/images/sale/sale_payments/' . $actionFile .
'.png';
125 if (Main\
IO\File::isFileExists($documentRoot . $image))
127 $paySystemParams[
'LOGOTIP'] = \CFile::MakeFileArray($image);
131 if (isset($paySystemParams[
'LOGOTIP']))
133 $paySystemParams[
'LOGOTIP'][
'MODULE_ID'] =
'sale';
134 \CFile::SaveForDB($paySystemParams,
'LOGOTIP',
'sale/paysystem/logotip');
138 $addResult = Sale\PaySystem\Manager::add($paySystemParams);
139 if ($addResult->isSuccess())
141 $id = $addResult->getId();
142 Sale\PaySystem\Manager::update(
145 'PARAMS' => serialize(
147 'BX_PAY_SYSTEM_ID' => $id,
150 'PAY_SYSTEM_ID' => $id,
154 $personTypeId = $fields[
'PERSON_TYPE_ID'] ?? 0;
156 if (isset($fields[
'SETTINGS']) && is_array($fields[
'SETTINGS']))
158 foreach ($fields[
'SETTINGS'] as $key => $value)
160 Sale\BusinessValue::setMapping(
162 Sale\PaySystem\Service::PAY_SYSTEM_PREFIX . $id,
165 'PROVIDER_KEY' => $value[
'TYPE'] ??
'',
166 'PROVIDER_VALUE' => $value[
'VALUE'] ??
'',
172 if ($personTypeId > 0)
174 static::savePersonTypeId($id, $personTypeId);
181 $result->addErrors($addResult->getErrors());
187 private function getDefaultPaySystemName(
string $actionFile, ?
string $psMode): string
189 $this->loadHandlerDescription($actionFile, $psMode);
191 $name = $this->handlerDescription[
'NAME'] ??
'';
195 $this->loadHandlerModeList($actionFile);
197 $psModeName = $this->handlerModeList[$psMode] ??
'';
199 $name .=
": $psModeName";
202 return $name ?:
'untitled';
205 private function includeHandler(
string $actionFile): void
207 if ($this->handlerClassName)
212 [$this->handlerClassName] = Sale\PaySystem\Manager::includeHandler($actionFile);
215 private function loadHandlerDescription(
string $actionFile, ?
string $psMode): void
217 if ($this->handlerDescription)
222 $this->handlerDescription = Sale\PaySystem\Manager::getHandlerDescription($actionFile, $psMode);
225 private function loadHandlerModeList(
string $actionFile): void
227 if ($this->handlerModeList)
232 $this->includeHandler($actionFile);
234 $this->handlerModeList = $this->handlerClassName::getHandlerModeList();
237 private static function saveFile($fileContent)
239 $file = \CRestUtil::saveFile($fileContent);
242 $file[
'MODULE_ID'] =
'sale';
243 return \CFile::SaveFile($file,
'sale');
249 private static function savePersonTypeId($serviceId, $personTypeId): void
253 'SERVICE_ID' => $serviceId,
254 'SERVICE_TYPE' => Sale\Services\Base\RestrictionManager::SERVICE_TYPE_PAYMENT,
255 '=CLASS_NAME' =>
'\\'.Sale\Services\PaySystem\Restrictions\PersonType::class
259 $serviceRestrictionIterator = Sale\Internals\ServiceRestrictionTable::getList($params);
260 if ($serviceRestrictionData = $serviceRestrictionIterator->fetch())
262 $restrictionId = $serviceRestrictionData[
'ID'];
270 'SERVICE_ID' => $serviceId,
271 'SERVICE_TYPE' => Sale\Services\Base\RestrictionManager::SERVICE_TYPE_PAYMENT,
273 'PARAMS' => [
'PERSON_TYPE_ID' => [$personTypeId]]
276 Sale\Services\PaySystem\Restrictions\PersonType::save($fields, $restrictionId);