PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/general/pay_system.php

https://gitlab.com/alexprowars/bitrix
PHP | 410 lines | 336 code | 67 blank | 7 comment | 56 complexity | c2f0ff576ed788dda4445961bedb99bf MD5 | raw file
  1. <?php
  2. use Bitrix\Sale\PaySystem;
  3. use Bitrix\Sale\Internals\PaySystemActionTable;
  4. IncludeModuleLangFile(__FILE__);
  5. /** @deprecated */
  6. class CAllSalePaySystem
  7. {
  8. public static function DoProcessOrder(&$arOrder, $paySystemId, &$arErrors)
  9. {
  10. if (intval($paySystemId) > 0)
  11. {
  12. $arPaySystem = array();
  13. $dbPaySystem = CSalePaySystem::GetList(
  14. array("SORT" => "ASC", "PSA_NAME" => "ASC"),
  15. array(
  16. "ACTIVE" => "Y",
  17. "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"],
  18. "PSA_HAVE_PAYMENT" => "Y"
  19. )
  20. );
  21. while ($arPaySystem = $dbPaySystem->Fetch())
  22. {
  23. if ($arPaySystem["ID"] == $paySystemId)
  24. {
  25. $arOrder["PAY_SYSTEM_ID"] = $paySystemId;
  26. $arOrder["PAY_SYSTEM_PRICE"] = CSalePaySystemsHelper::getPSPrice(
  27. $arPaySystem,
  28. $arOrder["ORDER_PRICE"],
  29. $arOrder["PRICE_DELIVERY"],
  30. $arOrder["DELIVERY_LOCATION"]
  31. );
  32. break;
  33. }
  34. }
  35. if (empty($arPaySystem))
  36. {
  37. $arErrors[] = array("CODE" => "CALCULATE", "TEXT" => GetMessage('SKGPS_PS_NOT_FOUND'));
  38. }
  39. }
  40. }
  41. public static function DoLoadPaySystems($personType, $deliveryId = 0, $arDeliveryMap = null)
  42. {
  43. $arResult = array();
  44. $arFilter = array(
  45. "ACTIVE" => "Y",
  46. "PERSON_TYPE_ID" => $personType,
  47. "PSA_HAVE_PAYMENT" => "Y"
  48. );
  49. // $arDeliveryMap = array(array($deliveryId => 8), array($deliveryId => array(34, 22)), ...)
  50. if (is_array($arDeliveryMap) && (count($arDeliveryMap) > 0))
  51. {
  52. foreach ($arDeliveryMap as $val)
  53. {
  54. if (is_array($val[$deliveryId]))
  55. {
  56. foreach ($val[$deliveryId] as $v)
  57. $arFilter["ID"][] = $v;
  58. }
  59. elseif (intval($val[$deliveryId]) > 0)
  60. $arFilter["ID"][] = $val[$deliveryId];
  61. }
  62. }
  63. $dbPaySystem = CSalePaySystem::GetList(
  64. array("SORT" => "ASC", "PSA_NAME" => "ASC"),
  65. $arFilter
  66. );
  67. while ($arPaySystem = $dbPaySystem->GetNext())
  68. $arResult[$arPaySystem["ID"]] = $arPaySystem;
  69. return $arResult;
  70. }
  71. public static function GetByID($id, $personTypeId = 0)
  72. {
  73. $id = (int)$id;
  74. $personTypeId = (int)$personTypeId;
  75. if ($personTypeId > 0)
  76. {
  77. $select = array_merge(array('ID', 'NAME', 'DESCRIPTION', 'ACTIVE', 'SORT'), self::getAliases());
  78. $dbRes = PaySystem\Manager::getList(array(
  79. 'select' => $select,
  80. 'filter' => array('ID' => $id)
  81. ));
  82. }
  83. else
  84. {
  85. $dbRes = PaySystemActionTable::getById($id);
  86. }
  87. if ($result = $dbRes->fetch())
  88. {
  89. $map = CSalePaySystemAction::getOldToNewHandlersMap();
  90. $key = array_search($result['ACTION_FILE'], $map);
  91. if ($key !== false)
  92. $result['ACTION_FILE'] = $key;
  93. return $result;
  94. }
  95. return false;
  96. }
  97. protected static function getAliases()
  98. {
  99. $aliases = array(
  100. "PSA_ID" => 'ID',
  101. "PSA_ACTION_FILE" => 'ACTION_FILE',
  102. "PSA_RESULT_FILE" => 'RESULT_FILE',
  103. "PSA_NEW_WINDOW" => 'NEW_WINDOW',
  104. "PSA_PERSON_TYPE_ID" => 'PERSON_TYPE_ID',
  105. "PSA_PARAMS" => 'PARAMS',
  106. "PSA_TARIF" => 'TARIF',
  107. "PSA_HAVE_PAYMENT" => 'HAVE_PAYMENT',
  108. "PSA_HAVE_ACTION" => 'HAVE_ACTION',
  109. "PSA_HAVE_RESULT" => 'HAVE_RESULT',
  110. "PSA_HAVE_PREPAY" => 'HAVE_PREPAY',
  111. "PSA_HAVE_RESULT_RECEIVE" => 'HAVE_RESULT_RECEIVE',
  112. "PSA_ENCODING" => 'ENCODING',
  113. "PSA_LOGOTIP" => 'LOGOTIP'
  114. );
  115. return $aliases;
  116. }
  117. public static function CheckFields($ACTION, &$arFields)
  118. {
  119. global $DB, $USER;
  120. if ((is_set($arFields, "NAME") || $ACTION=="ADD") && $arFields["NAME"] == '')
  121. {
  122. $GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGPS_EMPTY_NAME"), "ERROR_NO_NAME");
  123. return false;
  124. }
  125. if (is_set($arFields, "ACTIVE") && $arFields["ACTIVE"]!="Y")
  126. $arFields["ACTIVE"] = "N";
  127. if (is_set($arFields, "SORT") && intval($arFields["SORT"])<=0)
  128. $arFields["SORT"] = 100;
  129. return True;
  130. }
  131. public static function Update($id, $arFields)
  132. {
  133. if (isset($arFields['LID']))
  134. unset($arFields['LID']);
  135. if (isset($arFields['CURRENCY']))
  136. unset($arFields['CURRENCY']);
  137. $id = (int)$id;
  138. if (!CSalePaySystem::CheckFields("UPDATE", $arFields))
  139. return false;
  140. return CSalePaySystemAction::Update($id, $arFields);
  141. }
  142. public static function Delete($id)
  143. {
  144. $id = (int)$id;
  145. $dbRes = PaySystemActionTable::getById($id);
  146. if (!$dbRes->fetch())
  147. {
  148. $GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGPS_ORDERS_TO_PAYSYSTEM"), "ERROR_ORDERS_TO_PAYSYSTEM");
  149. return false;
  150. }
  151. $dbRes = PaySystem\Manager::delete($id);
  152. return $dbRes->isSuccess();
  153. }
  154. public static function getNewIdsFromOld($ids, $personTypeId = null)
  155. {
  156. $dbRes = PaySystem\Manager::getList(array(
  157. 'select' => array('ID'),
  158. 'filter' => array('PAY_SYSTEM_ID' => $ids)
  159. ));
  160. $data = array();
  161. while ($ps = $dbRes->fetch())
  162. {
  163. if (!is_null($personTypeId))
  164. {
  165. $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
  166. 'filter' => array(
  167. 'SERVICE_ID' => $ps['ID'],
  168. 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT,
  169. '=CLASS_NAME' => '\\'.\Bitrix\Sale\Services\PaySystem\Restrictions\PersonType::class
  170. )
  171. ));
  172. while ($restriction = $dbRestriction->fetch())
  173. {
  174. if (!in_array($personTypeId, $restriction['PARAMS']['PERSON_TYPE_ID']))
  175. continue(2);
  176. }
  177. }
  178. $data[] = $ps['ID'];
  179. }
  180. return $data;
  181. }
  182. public static function getPaySystemPersonTypeIds($paySystemId)
  183. {
  184. $data = array();
  185. $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
  186. 'filter' => array(
  187. 'SERVICE_ID' => $paySystemId,
  188. 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT,
  189. '=CLASS_NAME' => '\\'.\Bitrix\Sale\Services\PaySystem\Restrictions\PersonType::class
  190. )
  191. ));
  192. while ($restriction = $dbRestriction->fetch())
  193. $data = array_merge($data, $restriction['PARAMS']['PERSON_TYPE_ID']);
  194. return $data;
  195. }
  196. public static function GetList($arOrder = array("SORT" => "ASC", "NAME" => "ASC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
  197. {
  198. if (array_key_exists("PSA_PERSON_TYPE_ID", $arFilter))
  199. {
  200. $arFilter['PERSON_TYPE_ID'] = $arFilter['PSA_PERSON_TYPE_ID'];
  201. unset($arFilter["PSA_PERSON_TYPE_ID"]);
  202. }
  203. $salePaySystemFields = array('ID', 'NAME', 'ACTIVE', 'SORT', 'DESCRIPTION');
  204. $ignoredFields = array('LID', 'CURRENCY', 'PERSON_TYPE_ID');
  205. if (!$arSelectFields)
  206. {
  207. $select = array('ID', 'NAME', 'ACTIVE', 'SORT', 'DESCRIPTION');
  208. }
  209. else
  210. {
  211. $select = array();
  212. foreach ($arSelectFields as $key => $field)
  213. {
  214. if (in_array($field, $ignoredFields))
  215. continue;
  216. $select[$key] = self::getAlias($field);
  217. }
  218. }
  219. $filter = array();
  220. foreach ($arFilter as $key => $value)
  221. {
  222. if (in_array($key, $ignoredFields))
  223. continue;
  224. $filter[self::getAlias($key)] = $value;
  225. }
  226. if (isset($arFilter['PERSON_TYPE_ID']))
  227. $select = array_merge($select, array('PSA_ID' => 'ID', 'PSA_NAME', 'ACTION_FILE', 'RESULT_FILE', 'NEW_WINDOW', 'PERSON_TYPE_ID', 'PARAMS', 'TARIF', 'HAVE_PAYMENT', 'HAVE_ACTION', 'HAVE_RESULT', 'HAVE_PREPAY', 'HAVE_RESULT_RECEIVE', 'ENCODING', 'LOGOTIP'));
  228. if (in_array('PARAMS', $select) && !array_key_exists('PSA_ID', $select))
  229. $select['PSA_ID'] = 'ID';
  230. if (in_array('PARAMS', $select) && !in_array('PERSON_TYPE_ID', $select))
  231. $select[] = 'PERSON_TYPE_ID';
  232. $order = array();
  233. foreach ($arOrder as $key => $value)
  234. $order[self::getAlias($key)] = $value;
  235. $groupBy = array();
  236. if ($arGroupBy !== false)
  237. {
  238. $arGroupBy = !is_array($arGroupBy) ? array($arGroupBy) : $arGroupBy;
  239. foreach ($arGroupBy as $key => $value)
  240. $groupBy[$key] = self::getAlias($value);
  241. }
  242. $dbRes = PaySystem\Manager::getList(
  243. array(
  244. 'select' => $select,
  245. 'filter' => $filter,
  246. 'order' => $order,
  247. 'group' => $groupBy,
  248. )
  249. );
  250. $limit = null;
  251. if (is_array($arNavStartParams) && isset($arNavStartParams['nTopCount']))
  252. {
  253. if ($arNavStartParams['nTopCount'] > 0)
  254. $limit = $arNavStartParams['nTopCount'];
  255. }
  256. $result = array();
  257. while ($data = $dbRes->fetch())
  258. {
  259. if ($limit !== null && !$limit)
  260. break;
  261. $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
  262. 'filter' => array(
  263. 'SERVICE_ID' => $data['ID'],
  264. 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT
  265. )
  266. ));
  267. while ($restriction = $dbRestriction->fetch())
  268. {
  269. if (!CSalePaySystemAction::checkRestriction($restriction, $arFilter))
  270. continue(2);
  271. }
  272. if (isset($data['ACTION_FILE']))
  273. {
  274. $oldHandler = array_search($data['ACTION_FILE'], CSalePaySystemAction::getOldToNewHandlersMap());
  275. if ($oldHandler !== false)
  276. $data['ACTION_FILE'] = $oldHandler;
  277. }
  278. if (array_key_exists('PARAMS', $data))
  279. {
  280. $params = CSalePaySystemAction::getParamsByConsumer('PAYSYSTEM_'.$data['PSA_ID'], $data['PERSON_TYPE_ID']);
  281. $params['BX_PAY_SYSTEM_ID'] = array('TYPE' => '', 'VALUE' => $data['PSA_ID']);
  282. $data['PARAMS'] = serialize($params);
  283. }
  284. foreach ($data as $key => $value)
  285. {
  286. if (!in_array($key, $salePaySystemFields))
  287. {
  288. $newKey = self::getAliasBack($key);
  289. if ($newKey != $key)
  290. {
  291. $data[$newKey] = $value;
  292. unset($data[$key]);
  293. }
  294. }
  295. }
  296. $result[] = $data;
  297. $limit--;
  298. }
  299. $dbRes = new \CDBResult();
  300. $dbRes->InitFromArray($result);
  301. return $dbRes;
  302. }
  303. private static function getAlias($key)
  304. {
  305. $prefix = '';
  306. $pos = mb_strpos($key, 'PSA_');
  307. if ($pos > 0)
  308. {
  309. $prefix = mb_substr($key, 0, $pos);
  310. $key = mb_substr($key, $pos);
  311. }
  312. $aliases = self::getAliases();
  313. if (isset($aliases[$key]))
  314. $key = $aliases[$key];
  315. return $prefix.$key;
  316. }
  317. private static function getAliasBack($value)
  318. {
  319. $aliases = self::getAliases();
  320. $result = array_search($value, $aliases);
  321. return $result !== false ? $result : $value;
  322. }
  323. /**
  324. * @param $arFields
  325. * @return bool|int
  326. * @throws Exception
  327. */
  328. public static function Add($arFields)
  329. {
  330. if (isset($arFields['LID']))
  331. unset($arFields['LID']);
  332. if (isset($arFields['CURRENCY']))
  333. unset($arFields['CURRENCY']);
  334. if (!CSalePaySystem::CheckFields("ADD", $arFields))
  335. return false;
  336. return CSalePaySystemAction::add($arFields);
  337. }
  338. }