Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseservicehandler.php
1<?php
3
13
14Loc::loadMessages(__FILE__);
15
16abstract class BaseServiceHandler
17{
18 const STREAM = 1;
19 const STRING = 2;
20
21 const TEST_URL = 'test';
22 const ACTIVE_URL = 'active';
23
24 protected $handlerType = '';
25
26 protected $service = null;
27
28 protected $extraParams = array();
30
32 protected $isClone = false;
33
34 private array $businessCodes = [];
35
41 abstract public function initiatePay(Payment $payment, Request $request = null);
42
48 public function __construct($type, Service $service)
49 {
50 $this->handlerType = $type;
51 $this->service = $service;
52 }
53
59 public function showTemplate(Payment $payment = null, $template = '')
60 {
61 $result = new ServiceResult();
62
63 global $APPLICATION, $USER, $DB;
64
65 $templatePath = $this->searchTemplate($template);
66
67 if ($templatePath != '' && IO\File::isFileExists($templatePath))
68 {
69 $params = array_merge($this->getParamsBusValue($payment), $this->getExtraParams());
70
71 if ($this->initiateMode == self::STREAM)
72 {
73 require($templatePath);
74
75 if ($this->service->getField('ENCODING') != '')
76 {
77 define("BX_SALE_ENCODING", $this->service->getField('ENCODING'));
78 AddEventHandler('main', 'OnEndBufferContent', array($this, 'OnEndBufferContent'));
79 }
80 }
81 elseif ($this->initiateMode == self::STRING)
82 {
83 ob_start();
84 $content = require($templatePath);
85
86 $buffer = ob_get_contents();
87 if ($buffer <> '')
88 $content = $buffer;
89
90 if ($this->service->getField('ENCODING') != '')
91 {
92 $encoding = Context::getCurrent()->getCulture()->getCharset();
93 $content = Text\Encoding::convertEncoding($content, $encoding, $this->service->getField('ENCODING'));
94 }
95
96 $result->setTemplate($content);
97 ob_end_clean();
98 }
99 }
100 else
101 {
102 $result->addError(new Error(Loc::getMessage('SALE_PS_BASE_SERVICE_TEMPLATE_ERROR')));
103 }
104
105 return $result;
106 }
107
112 private function searchTemplate($template)
113 {
114 $documentRoot = Application::getDocumentRoot();
115 $siteTemplate = \CSite::GetCurTemplate();
116 $template = Manager::sanitize($template);
117 $handlerName = static::getName();
118
119 $folders = array();
120
121 $folders[] = '/local/templates/' . $siteTemplate . '/payment/' . $handlerName . '/template';
122 if ($siteTemplate !== '.default')
123 {
124 $folders[] = '/local/templates/.default/payment/' . $handlerName . '/template';
125 }
126
127 $folders[] = '/bitrix/templates/' . $siteTemplate . '/payment/' . $handlerName . '/template';
128 if ($siteTemplate !== '.default')
129 {
130 $folders[] = '/bitrix/templates/.default/payment/' . $handlerName . '/template';
131 }
132
133 $baseFolders = Manager::getHandlerDirectories();
134 if (isset($baseFolders[$this->handlerType]))
135 {
136 $folders[] = $baseFolders[$this->handlerType] . $handlerName . '/template';
137 }
138
139 foreach ($folders as $folder)
140 {
141 $templatePath = $documentRoot . $folder . '/' . $template . '.php';
142
143 if (IO\File::isFileExists($templatePath))
144 {
145 return $templatePath;
146 }
147 }
148
149 return '';
150 }
151
152
157 public function getParamsBusValue(Payment $payment = null)
158 {
159 $params = array();
160 $codes = $this->getBusinessCodes();
161
162 if ($codes)
163 {
164 foreach ($codes as $code)
165 $params[$code] = $this->getBusinessValue($payment, $code);
166 }
167
168 return $params;
169 }
170
174 static protected function getName()
175 {
176 return Manager::getFolderFromClassName(get_called_class());
177 }
178
184 protected function getBusinessValue(Payment $payment = null, $code)
185 {
186 $value = BusinessValue::get($code, $this->service->getConsumerName(), $payment);
187 if (is_string($value))
188 {
189 $value = trim($value);
190 }
191
192 return $value;
193 }
194
198 public function getDescription()
199 {
200 $description = $this->includeDescription();
201
202 if (isset($description['CODES']) && is_array($description['CODES']))
203 {
204 $description['CODES'] = $this->filterDescriptionCodes($description['CODES']);
205 }
206
207 return $description;
208 }
209
214 protected function filterDescriptionCodes($codes)
215 {
216 $psMode = $this->service->getField('PS_MODE');
217
218 return array_filter(
219 $codes,
220 static function ($code, $key) use ($psMode)
221 {
222 if (!isset($code['HANDLER_MODE']))
223 {
224 return true;
225 }
226
227 if (!is_array($code['HANDLER_MODE']))
228 {
229 trigger_error('HANDLER_MODE must be an array', E_USER_WARNING);
230 return false;
231 }
232
233 return in_array($psMode, $code['HANDLER_MODE'], true);
234 },
235 ARRAY_FILTER_USE_BOTH
236 );
237 }
238
242 protected function getBusinessCodes()
243 {
244 if (!$this->businessCodes)
245 {
246 $description = $this->includeDescription();
247
248 if (isset($description['CODES']) && is_array($description['CODES']))
249 {
250 $this->businessCodes = array_keys($description['CODES']);
251 }
252 }
253
254 return $this->businessCodes;
255 }
256
260 protected function getExtraParams()
261 {
262 return $this->extraParams;
263 }
264
268 public function setExtraParams(array $values)
269 {
270 $this->extraParams = $values;
271 }
272
276 public abstract function getCurrencyList();
277
299 public function getClientType($psMode)
300 {
301 return ClientType::DEFAULT;
302 }
303
308 public function creditNoDemand(Payment $payment)
309 {
310 return new ServiceResult();
311 }
312
317 public function debitNoDemand(Payment $payment)
318 {
319 return new ServiceResult();
320 }
321
325 public static function getHandlerModeList()
326 {
327 return array();
328 }
329
333 public function setInitiateMode($mode)
334 {
335 $this->initiateMode = $mode;
336 }
337
343 protected function getUrl(Payment $payment = null, $action)
344 {
345 $urlList = $this->getUrlList();
346 if (isset($urlList[$action]))
347 {
348 $url = $urlList[$action];
349
350 if (is_array($url))
351 {
352 if ($this->isTestMode($payment) && isset($url[self::TEST_URL]))
353 return $url[self::TEST_URL];
354 else
355 return $url[self::ACTIVE_URL];
356 }
357 else
358 {
359 return $url;
360 }
361 }
362
363 return '';
364 }
365
370 protected function isTestMode(Payment $payment = null)
371 {
372 return false;
373 }
374
378 protected function getUrlList()
379 {
380 return array();
381 }
382
383
389 public function createClone(\SplObjectStorage $cloneEntity)
390 {
391 if ($this->isClone() && $cloneEntity->contains($this))
392 {
393 return $cloneEntity[$this];
394 }
395
396 $serviceHandlerClone = clone $this;
397 $serviceHandlerClone->isClone = true;
398
399 if (!$cloneEntity->contains($this))
400 {
401 $cloneEntity[$this] = $serviceHandlerClone;
402 }
403
404 if ($this->service)
405 {
406 if ($cloneEntity->contains($this->service))
407 {
408 $serviceHandlerClone->service = $cloneEntity[$this->service];
409 }
410 }
411
412 return $serviceHandlerClone;
413 }
414
418 public function isClone()
419 {
420 return $this->isClone;
421 }
422
426 public function getHandlerType()
427 {
428 return $this->handlerType;
429 }
430
434 public function OnEndBufferContent(&$content)
435 {
436 if (
437 strpos($content, 'charset=') !== false
438 && strpos($content, "charset=".SITE_CHARSET) !== false
439 )
440 {
441 header("Content-Type: text/html; charset=".BX_SALE_ENCODING);
442 $content = Text\Encoding::convertEncoding($content, SITE_CHARSET, BX_SALE_ENCODING);
443 $content = str_replace("charset=".SITE_CHARSET, "charset=".BX_SALE_ENCODING, $content);
444 }
445 }
446
450 public function getDemoParams()
451 {
452 return array();
453 }
454
458 public function isTuned()
459 {
460 return true;
461 }
462
463 protected function includeDescription(): array
464 {
465 $data = null;
466
467 $documentRoot = Application::getDocumentRoot();
469 $handlerDir = $dirs[$this->handlerType];
470 $file = $documentRoot . $handlerDir . static::getName() . '/.description.php';
471
472 if (IO\File::isFileExists($file))
473 {
474 require $file;
475 }
476
477 return (isset($data) && is_array($data)) ? $data : [];
478 }
479}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static get($codeKey, $consumerKey=null, $personTypeId=null, $providerInstance=null)
getUrl(Payment $payment=null, $action)
createClone(\SplObjectStorage $cloneEntity)
initiatePay(Payment $payment, Request $request=null)
showTemplate(Payment $payment=null, $template='')
getBusinessValue(Payment $payment=null, $code)
static getFolderFromClassName($className)
Definition manager.php:222