PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/lib/paysystem/baseservicehandler.php

https://gitlab.com/alexprowars/bitrix
PHP | 453 lines | 266 code | 69 blank | 118 comment | 35 complexity | 83297a4ff5c5f1701cc2da497865a81c MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\PaySystem;
  3. use Bitrix\Main\Application;
  4. use Bitrix\Main\Context;
  5. use Bitrix\Main\Text;
  6. use Bitrix\Main\Error;
  7. use Bitrix\Main\IO;
  8. use Bitrix\Main\Localization\Loc;
  9. use Bitrix\Main\Request;
  10. use Bitrix\Sale\BusinessValue;
  11. use Bitrix\Sale\Payment;
  12. Loc::loadMessages(__FILE__);
  13. abstract class BaseServiceHandler
  14. {
  15. const STREAM = 1;
  16. const STRING = 2;
  17. const TEST_URL = 'test';
  18. const ACTIVE_URL = 'active';
  19. protected $handlerType = '';
  20. protected $service = null;
  21. protected $extraParams = array();
  22. protected $initiateMode = self::STREAM;
  23. /** @var bool */
  24. protected $isClone = false;
  25. /**
  26. * @param Payment $payment
  27. * @param Request|null $request
  28. * @return ServiceResult
  29. */
  30. abstract public function initiatePay(Payment $payment, Request $request = null);
  31. /**
  32. * BaseServiceHandler constructor.
  33. * @param $type
  34. * @param Service $service
  35. */
  36. public function __construct($type, Service $service)
  37. {
  38. $this->handlerType = $type;
  39. $this->service = $service;
  40. }
  41. /**
  42. * @param Payment|null $payment
  43. * @param string $template
  44. * @return ServiceResult
  45. */
  46. public function showTemplate(Payment $payment = null, $template = '')
  47. {
  48. $result = new ServiceResult();
  49. global $APPLICATION, $USER, $DB;
  50. $templatePath = $this->searchTemplate($template);
  51. if ($templatePath != '' && IO\File::isFileExists($templatePath))
  52. {
  53. $params = array_merge($this->getParamsBusValue($payment), $this->getExtraParams());
  54. if ($this->initiateMode == self::STREAM)
  55. {
  56. require($templatePath);
  57. if ($this->service->getField('ENCODING') != '')
  58. {
  59. define("BX_SALE_ENCODING", $this->service->getField('ENCODING'));
  60. AddEventHandler('main', 'OnEndBufferContent', array($this, 'OnEndBufferContent'));
  61. }
  62. }
  63. elseif ($this->initiateMode == self::STRING)
  64. {
  65. ob_start();
  66. $content = require($templatePath);
  67. $buffer = ob_get_contents();
  68. if ($buffer <> '')
  69. $content = $buffer;
  70. if ($this->service->getField('ENCODING') != '')
  71. {
  72. $encoding = Context::getCurrent()->getCulture()->getCharset();
  73. $content = Text\Encoding::convertEncoding($content, $encoding, $this->service->getField('ENCODING'));
  74. }
  75. $result->setTemplate($content);
  76. ob_end_clean();
  77. }
  78. }
  79. else
  80. {
  81. $result->addError(new Error(Loc::getMessage('SALE_PS_BASE_SERVICE_TEMPLATE_ERROR')));
  82. }
  83. return $result;
  84. }
  85. /**
  86. * @param string $template
  87. * @return string
  88. */
  89. private function searchTemplate($template)
  90. {
  91. $documentRoot = Application::getDocumentRoot();
  92. $siteTemplate = \CSite::GetCurTemplate();
  93. $template = Manager::sanitize($template);
  94. $handlerName = static::getName();
  95. $folders = array();
  96. $folders[] = '/local/templates/'.$siteTemplate.'/payment/'.$handlerName.'/template';
  97. if ($siteTemplate !== '.default')
  98. $folders[] = '/local/templates/.default/payment/'.$handlerName.'/template';
  99. $folders[] = '/bitrix/templates/'.$siteTemplate.'/payment/'.$handlerName.'/template';
  100. if ($siteTemplate !== '.default')
  101. $folders[] = '/bitrix/templates/.default/payment/'.$handlerName.'/template';
  102. $baseFolders = Manager::getHandlerDirectories();
  103. $folders[] = $baseFolders[$this->handlerType].$handlerName.'/template';
  104. foreach ($folders as $folder)
  105. {
  106. $templatePath = $documentRoot.$folder.'/'.$template.'.php';
  107. if (IO\File::isFileExists($templatePath))
  108. return $templatePath;
  109. }
  110. return '';
  111. }
  112. /**
  113. * @param Payment $payment
  114. * @return array
  115. */
  116. public function getParamsBusValue(Payment $payment = null)
  117. {
  118. $params = array();
  119. $codes = $this->getBusinessCodes();
  120. if ($codes)
  121. {
  122. foreach ($codes as $code)
  123. $params[$code] = $this->getBusinessValue($payment, $code);
  124. }
  125. return $params;
  126. }
  127. /**
  128. * @return mixed|string
  129. */
  130. static protected function getName()
  131. {
  132. return Manager::getFolderFromClassName(get_called_class());
  133. }
  134. /**
  135. * @param Payment $payment
  136. * @param $code
  137. * @return mixed
  138. */
  139. protected function getBusinessValue(Payment $payment = null, $code)
  140. {
  141. $value = BusinessValue::getValueFromProvider($payment, $code, $this->service->getConsumerName());
  142. if (is_string($value))
  143. {
  144. $value = trim($value);
  145. }
  146. return $value;
  147. }
  148. /**
  149. * @return array
  150. */
  151. public function getDescription()
  152. {
  153. $data = array();
  154. $documentRoot = Application::getDocumentRoot();
  155. $dirs = Manager::getHandlerDirectories();
  156. $handlerDir = $dirs[$this->handlerType];
  157. $file = $documentRoot.$handlerDir.static::getName().'/.description.php';
  158. if (IO\File::isFileExists($file))
  159. {
  160. require $file;
  161. }
  162. if (isset($data["CODES"]) && is_array($data["CODES"]))
  163. {
  164. $data["CODES"] = $this->filterDescriptionCodes($data["CODES"]);
  165. }
  166. return $data;
  167. }
  168. /**
  169. * @param $codes
  170. * @return array
  171. */
  172. protected function filterDescriptionCodes($codes)
  173. {
  174. $psMode = $this->service->getField("PS_MODE");
  175. return array_filter($codes, static function ($code) use ($psMode) {
  176. if (!isset($code["HANDLER_MODE"]))
  177. {
  178. return true;
  179. }
  180. if (isset($code["HANDLER_MODE"]) && !is_array($code["HANDLER_MODE"]))
  181. {
  182. trigger_error("HANDLER_MODE must be an array", E_USER_WARNING);
  183. return false;
  184. }
  185. return in_array($psMode, $code["HANDLER_MODE"], true);
  186. });
  187. }
  188. /**
  189. * @return array
  190. */
  191. protected function getBusinessCodes()
  192. {
  193. static $data = array();
  194. if (!$data)
  195. {
  196. $result = $this->getDescription();
  197. if ($result['CODES'])
  198. $data = array_keys($result['CODES']);
  199. }
  200. return $data;
  201. }
  202. /**
  203. * @return array
  204. */
  205. protected function getExtraParams()
  206. {
  207. return $this->extraParams;
  208. }
  209. /**
  210. * @param array $values
  211. */
  212. public function setExtraParams(array $values)
  213. {
  214. $this->extraParams = $values;
  215. }
  216. /**
  217. * @return array
  218. */
  219. public abstract function getCurrencyList();
  220. /**
  221. * The type of client that the handler can work with
  222. *
  223. * If, depending on PS_MODE, the handler can work with different types of clients,
  224. * then you can implement validation in a similar way:
  225. *
  226. * ```php
  227. * public function getClientType($psMode)
  228. * {
  229. * if ($psMode === self::MODE_ALFABANK)
  230. * {
  231. * return ClientType::B2B;
  232. * }
  233. *
  234. * return ClientType::B2C;
  235. * }
  236. * ```
  237. *
  238. * @param string $psMode pay system type
  239. * @return string
  240. */
  241. public function getClientType($psMode)
  242. {
  243. return ClientType::DEFAULT;
  244. }
  245. /**
  246. * @param Payment $payment
  247. * @return ServiceResult
  248. */
  249. public function creditNoDemand(Payment $payment)
  250. {
  251. return new ServiceResult();
  252. }
  253. /**
  254. * @param Payment $payment
  255. * @return ServiceResult
  256. */
  257. public function debitNoDemand(Payment $payment)
  258. {
  259. return new ServiceResult();
  260. }
  261. /**
  262. * @return array
  263. */
  264. public static function getHandlerModeList()
  265. {
  266. return array();
  267. }
  268. /**
  269. * @param int $mode
  270. */
  271. public function setInitiateMode($mode)
  272. {
  273. $this->initiateMode = $mode;
  274. }
  275. /**
  276. * @param Payment $payment
  277. * @param string $action
  278. * @return string
  279. */
  280. protected function getUrl(Payment $payment = null, $action)
  281. {
  282. $urlList = $this->getUrlList();
  283. if (isset($urlList[$action]))
  284. {
  285. $url = $urlList[$action];
  286. if (is_array($url))
  287. {
  288. if ($this->isTestMode($payment) && isset($url[self::TEST_URL]))
  289. return $url[self::TEST_URL];
  290. else
  291. return $url[self::ACTIVE_URL];
  292. }
  293. else
  294. {
  295. return $url;
  296. }
  297. }
  298. return '';
  299. }
  300. /**
  301. * @param Payment $payment
  302. * @return bool
  303. */
  304. protected function isTestMode(Payment $payment = null)
  305. {
  306. return false;
  307. }
  308. /**
  309. * @return array
  310. */
  311. protected function getUrlList()
  312. {
  313. return array();
  314. }
  315. /**
  316. * @param \SplObjectStorage $cloneEntity
  317. *
  318. * @return BaseServiceHandler
  319. */
  320. public function createClone(\SplObjectStorage $cloneEntity)
  321. {
  322. if ($this->isClone() && $cloneEntity->contains($this))
  323. {
  324. return $cloneEntity[$this];
  325. }
  326. $serviceHandlerClone = clone $this;
  327. $serviceHandlerClone->isClone = true;
  328. if (!$cloneEntity->contains($this))
  329. {
  330. $cloneEntity[$this] = $serviceHandlerClone;
  331. }
  332. if ($this->service)
  333. {
  334. if ($cloneEntity->contains($this->service))
  335. {
  336. $serviceHandlerClone->service = $cloneEntity[$this->service];
  337. }
  338. }
  339. return $serviceHandlerClone;
  340. }
  341. /**
  342. * @return bool
  343. */
  344. public function isClone()
  345. {
  346. return $this->isClone;
  347. }
  348. /**
  349. * @return string
  350. */
  351. public function getHandlerType()
  352. {
  353. return $this->handlerType;
  354. }
  355. /**
  356. * @param $content
  357. */
  358. public function OnEndBufferContent(&$content)
  359. {
  360. if (
  361. strpos($content, 'charset=') !== false
  362. && strpos($content, "charset=".SITE_CHARSET) !== false
  363. )
  364. {
  365. header("Content-Type: text/html; charset=".BX_SALE_ENCODING);
  366. $content = Text\Encoding::convertEncoding($content, SITE_CHARSET, BX_SALE_ENCODING);
  367. $content = str_replace("charset=".SITE_CHARSET, "charset=".BX_SALE_ENCODING, $content);
  368. }
  369. }
  370. /**
  371. * @return array
  372. */
  373. public function getDemoParams()
  374. {
  375. return array();
  376. }
  377. /**
  378. * @return bool
  379. */
  380. public function isTuned()
  381. {
  382. return true;
  383. }
  384. }