Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
addpaysystemaction.php
1<?php
2
4
7
15{
17 private $handlerClassName;
18 private array $handlerDescription = [];
19 private array $handlerModeList = [];
20
21 private function checkParams(array $fields): Sale\Result
22 {
23 $result = new Sale\Result();
24
25 if (empty($fields['ACTION_FILE']))
26 {
27 $result->addError(
28 new Main\Error(
29 'actionFile not found',
31 )
32 );
33 }
34
35 if (!empty($fields['PS_MODE']))
36 {
37 $this->loadHandlerModeList($fields['ACTION_FILE']);
38 if (!array_key_exists($fields['PS_MODE'], $this->handlerModeList))
39 {
40 $result->addError(
41 new Main\Error(
42 "psMode \"{$fields['PS_MODE']}\" not available",
44 )
45 );
46 }
47 }
48
49 return $result;
50 }
51
52 public function run(array $fields)
53 {
54 $result = [];
55
56 $checkParamsResult = $this->checkParams($fields);
57 if (!$checkParamsResult->isSuccess())
58 {
59 $this->addErrors($checkParamsResult->getErrors());
60 return $result;
61 }
62
63 $createPaySystemResult = $this->createPaySystem($fields);
64 if (!$createPaySystemResult->isSuccess())
65 {
66 $this->addErrors($createPaySystemResult->getErrors());
67 return $result;
68 }
69
70 return [
71 'ID' => $createPaySystemResult->getId(),
72 ];
73 }
74
75 private function createPaySystem(array $fields): Sale\Result
76 {
77 $result = new Sale\Result();
78
79 $name = $fields['NAME'] ?? '';
80 if (empty($name))
81 {
82 $name = $this->getDefaultPaySystemName($fields['ACTION_FILE'], $fields['PS_MODE'] ?? null);
83 }
84
85 $actionFile = $fields['ACTION_FILE'];
86 $psMode = $fields['PS_MODE'] ?? '';
87
88 $paySystemParams = [
89 'NAME' => $name,
90 'PSA_NAME' => $name,
91 'ACTION_FILE' => $actionFile,
92 'PS_MODE' => $psMode,
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
98 ];
99
100 if (isset($fields['ENTITY_REGISTRY_TYPE']))
101 {
102 $paySystemParams['ENTITY_REGISTRY_TYPE'] = $fields['ENTITY_REGISTRY_TYPE'];
103 }
104
105 if (isset($fields['LOGOTYPE']))
106 {
107 $paySystemParams['LOGOTIP'] = self::saveFile($fields['LOGOTYPE']);
108 }
109 else
110 {
111 $documentRoot = Main\Application::getDocumentRoot();
112
113 if ($psMode)
114 {
115 $image = '/bitrix/images/sale/sale_payments/' . $actionFile . '/' . $psMode . '.png';
116 if (Main\IO\File::isFileExists($documentRoot . $image))
117 {
118 $paySystemParams['LOGOTIP'] = \CFile::MakeFileArray($image);
119 }
120 }
121
122 if (!isset($paySystemParams['LOGOTIP']))
123 {
124 $image = '/bitrix/images/sale/sale_payments/' . $actionFile . '.png';
125 if (Main\IO\File::isFileExists($documentRoot . $image))
126 {
127 $paySystemParams['LOGOTIP'] = \CFile::MakeFileArray($image);
128 }
129 }
130
131 if (isset($paySystemParams['LOGOTIP']))
132 {
133 $paySystemParams['LOGOTIP']['MODULE_ID'] = 'sale';
134 \CFile::SaveForDB($paySystemParams, 'LOGOTIP', 'sale/paysystem/logotip');
135 }
136 }
137
138 $addResult = Sale\PaySystem\Manager::add($paySystemParams);
139 if ($addResult->isSuccess())
140 {
141 $id = $addResult->getId();
142 Sale\PaySystem\Manager::update(
143 $id,
144 [
145 'PARAMS' => serialize(
146 [
147 'BX_PAY_SYSTEM_ID' => $id,
148 ]
149 ),
150 'PAY_SYSTEM_ID' => $id,
151 ]
152 );
153
154 $personTypeId = $fields['PERSON_TYPE_ID'] ?? 0;
155
156 if (isset($fields['SETTINGS']) && is_array($fields['SETTINGS']))
157 {
158 foreach ($fields['SETTINGS'] as $key => $value)
159 {
160 Sale\BusinessValue::setMapping(
161 $key,
162 Sale\PaySystem\Service::PAY_SYSTEM_PREFIX . $id,
163 $personTypeId,
164 [
165 'PROVIDER_KEY' => $value['TYPE'] ?? '',
166 'PROVIDER_VALUE' => $value['VALUE'] ?? '',
167 ]
168 );
169 }
170 }
171
172 if ($personTypeId > 0)
173 {
174 static::savePersonTypeId($id, $personTypeId);
175 }
176
177 $result->setId($id);
178 }
179 else
180 {
181 $result->addErrors($addResult->getErrors());
182 }
183
184 return $result;
185 }
186
187 private function getDefaultPaySystemName(string $actionFile, ?string $psMode): string
188 {
189 $this->loadHandlerDescription($actionFile, $psMode);
190
191 $name = $this->handlerDescription['NAME'] ?? '';
192
193 if ($psMode)
194 {
195 $this->loadHandlerModeList($actionFile);
196
197 $psModeName = $this->handlerModeList[$psMode] ?? '';
198
199 $name .= ": $psModeName";
200 }
201
202 return $name ?: 'untitled';
203 }
204
205 private function includeHandler(string $actionFile): void
206 {
207 if ($this->handlerClassName)
208 {
209 return;
210 }
211
212 [$this->handlerClassName] = Sale\PaySystem\Manager::includeHandler($actionFile);
213 }
214
215 private function loadHandlerDescription(string $actionFile, ?string $psMode): void
216 {
217 if ($this->handlerDescription)
218 {
219 return;
220 }
221
222 $this->handlerDescription = Sale\PaySystem\Manager::getHandlerDescription($actionFile, $psMode);
223 }
224
225 private function loadHandlerModeList(string $actionFile): void
226 {
227 if ($this->handlerModeList)
228 {
229 return;
230 }
231
232 $this->includeHandler($actionFile);
233
234 $this->handlerModeList = $this->handlerClassName::getHandlerModeList();
235 }
236
237 private static function saveFile($fileContent)
238 {
239 $file = \CRestUtil::saveFile($fileContent);
240 if ($file)
241 {
242 $file['MODULE_ID'] = 'sale';
243 return \CFile::SaveFile($file, 'sale');
244 }
245
246 return null;
247 }
248
249 private static function savePersonTypeId($serviceId, $personTypeId): void
250 {
251 $params = [
252 'filter' => [
253 'SERVICE_ID' => $serviceId,
254 'SERVICE_TYPE' => Sale\Services\Base\RestrictionManager::SERVICE_TYPE_PAYMENT,
255 '=CLASS_NAME' => '\\'.Sale\Services\PaySystem\Restrictions\PersonType::class
256 ]
257 ];
258
259 $serviceRestrictionIterator = Sale\Internals\ServiceRestrictionTable::getList($params);
260 if ($serviceRestrictionData = $serviceRestrictionIterator->fetch())
261 {
262 $restrictionId = $serviceRestrictionData['ID'];
263 }
264 else
265 {
266 $restrictionId = 0;
267 }
268
269 $fields = [
270 'SERVICE_ID' => $serviceId,
271 'SERVICE_TYPE' => Sale\Services\Base\RestrictionManager::SERVICE_TYPE_PAYMENT,
272 'SORT' => 100,
273 'PARAMS' => ['PERSON_TYPE_ID' => [$personTypeId]]
274 ];
275
276 Sale\Services\PaySystem\Restrictions\PersonType::save($fields, $restrictionId);
277 }
278}
addErrors(array $errors)
Definition action.php:213