BxDolPayments.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolPayments extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oDb;
  12. protected $_aObjects;
  13. protected $_sActive;
  14. protected function __construct()
  15. {
  16. parent::__construct();
  17. $this->_oDb = new BxDolPaymentsQuery();
  18. $this->_aObjects = $this->_oDb->getObjects();
  19. $this->_sActive = getParam('sys_default_payment');
  20. }
  21. static public function getInstance()
  22. {
  23. if(!isset($GLOBALS['bxDolClasses']['BxDolPayments']))
  24. $GLOBALS['bxDolClasses']['BxDolPayments'] = new BxDolPayments();
  25. return $GLOBALS['bxDolClasses']['BxDolPayments'];
  26. }
  27. public function setActive($sActive)
  28. {
  29. $this->_sActive = $sActive;
  30. }
  31. public function getActive()
  32. {
  33. return $this->_sActive;
  34. }
  35. public function getActiveObject()
  36. {
  37. if(!$this->isActive())
  38. return null;
  39. return BxDolModule::getInstance($this->_sActive);
  40. }
  41. public function isActive()
  42. {
  43. if(empty($this->_sActive))
  44. return false;
  45. if(!BxDolModuleQuery::getInstance()->isModuleByName($this->_sActive))
  46. return false;
  47. return true;
  48. }
  49. public function isCreditsOnly()
  50. {
  51. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'is_credits_only'))
  52. return false;
  53. return bx_srv($this->_sActive, 'is_credits_only');
  54. }
  55. public function getDetailsUrl()
  56. {
  57. if(!BxDolRequest::serviceExists($this->_sActive, 'get_details_url', 'Details'))
  58. return '';
  59. return bx_srv($this->_sActive, 'get_details_url', array(), 'Details');
  60. }
  61. public function getCurrencyInfo($iVendorId = 0)
  62. {
  63. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_currency_info'))
  64. return [];
  65. $aSrvParams = [$iVendorId];
  66. return bx_srv($this->_sActive, 'get_currency_info', $aSrvParams);
  67. }
  68. public function getCurrencyCode($iVendorId = 0)
  69. {
  70. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_currency_code'))
  71. return '';
  72. $aSrvParams = [$iVendorId];
  73. return bx_srv($this->_sActive, 'get_currency_code', $aSrvParams);
  74. }
  75. public function getCurrencySign($iVendorId = 0)
  76. {
  77. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_currency_sign'))
  78. return '';
  79. $aSrvParams = [$iVendorId];
  80. return bx_srv($this->_sActive, 'get_currency_sign', $aSrvParams);
  81. }
  82. public function convert($fAmount, $sCurrencyFrom, $sCurrencyTo)
  83. {
  84. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'convert'))
  85. return false;
  86. $aSrvParams = [$fAmount, $sCurrencyFrom, $sCurrencyTo];
  87. return bx_srv($this->_sActive, 'convert', $aSrvParams);
  88. }
  89. public function isAcceptingPayments($iVendorId, $sPaymentType = '')
  90. {
  91. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'is_accepting_payments'))
  92. return false;
  93. $aSrvParams = array($iVendorId, $sPaymentType);
  94. return bx_srv($this->_sActive, 'is_accepting_payments', $aSrvParams);
  95. }
  96. /**
  97. * @deprecated since version 11.0.0
  98. *
  99. * @see BxDolPayments::isProviderOptions
  100. */
  101. public function isPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  102. {
  103. return $this->isProviderOptions($iVendorId, $sVendorProvider);
  104. }
  105. /**
  106. * @deprecated since version 11.0.0
  107. *
  108. * @see BxDolPayments::getProviderOptions
  109. */
  110. public function getPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  111. {
  112. return $this->getProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  113. }
  114. public function isProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  115. {
  116. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'is_provider_options'))
  117. return false;
  118. $aSrvParams = array($iVendorId, $sVendorProvider, $sPaymentType);
  119. return bx_srv($this->_sActive, 'is_provider_options', $aSrvParams);
  120. }
  121. public function getProvider($sProvider, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  122. {
  123. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_provider'))
  124. return false;
  125. $aSrvParams = array($sProvider, $mixedVendorId);
  126. return bx_srv($this->_sActive, 'get_provider', $aSrvParams);
  127. }
  128. public function getProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  129. {
  130. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_provider_options'))
  131. return false;
  132. $aSrvParams = array($iVendorId, $sVendorProvider, $sPaymentType);
  133. return bx_srv($this->_sActive, 'get_provider_options', $aSrvParams);
  134. }
  135. public function getModulesWithPayments()
  136. {
  137. if(empty($this->_sActive) || !BxDolRequest::serviceExists($this->_sActive, 'get_modules_with_payments'))
  138. return false;
  139. return bx_srv($this->_sActive, 'get_modules_with_payments', array());
  140. }
  141. public function getProductsNames($iVendor = 0, $iLimit = 1000)
  142. {
  143. $aRet = array();
  144. $aModules = $this->getModulesWithPayments();
  145. foreach ($aModules as $sModule) {
  146. if(!BxDolRequest::serviceExists($sModule, 'get_products_names'))
  147. continue;
  148. $aKeys = bx_srv($sModule, 'get_products_names', array($iVendor, $iLimit));
  149. if (!$aKeys)
  150. continue;
  151. $aValues = array_fill(0, count($aKeys), $sModule);
  152. $aRet = array_merge($aRet, array_combine($aKeys, $aValues));
  153. }
  154. return $aRet;
  155. }
  156. public function getPayments()
  157. {
  158. $aPayments = array(
  159. '' => _t('_Select_one')
  160. );
  161. foreach($this->_aObjects as $aObject) {
  162. if(empty($aObject) || !is_array($aObject))
  163. continue;
  164. $aPayments[$aObject['object']] = _t($aObject['title']);
  165. }
  166. return $aPayments;
  167. }
  168. public function updateDependentModules($sModule = 'all', $bInstall = true)
  169. {
  170. if(!BxDolRequest::serviceExists($this->_sActive, 'update_dependent_modules'))
  171. return;
  172. bx_srv($this->_sActive, 'update_dependent_modules', array($sModule, $bInstall));
  173. }
  174. public function getProvidersCart($iVendorId)
  175. {
  176. if(!BxDolRequest::serviceExists($this->_sActive, 'get_providers_cart'))
  177. return array();
  178. $aSrvParams = array($iVendorId);
  179. return bx_srv($this->_sActive, 'get_providers_cart', $aSrvParams);
  180. }
  181. public function getOption($sOption)
  182. {
  183. if(!BxDolRequest::serviceExists($this->_sActive, 'get_option'))
  184. return '';
  185. return bx_srv($this->_sActive, 'get_option', array($sOption));
  186. }
  187. public function getOrdersUrl()
  188. {
  189. if(!BxDolRequest::serviceExists($this->_sActive, 'get_orders_url', 'Orders'))
  190. return '';
  191. return bx_srv($this->_sActive, 'get_orders_url', array(), 'Orders');
  192. }
  193. public function getOrdersCount($sType)
  194. {
  195. if(!BxDolRequest::serviceExists($this->_sActive, 'get_orders_count', 'Orders'))
  196. return array();
  197. $aSrvParams = array($sType);
  198. return bx_srv($this->_sActive, 'get_orders_count', $aSrvParams, 'Orders');
  199. }
  200. public function getOrdersInfo($aConditions)
  201. {
  202. if(!BxDolRequest::serviceExists($this->_sActive, 'get_orders_info', 'Orders'))
  203. return array();
  204. $aSrvParams = array($aConditions);
  205. return bx_srv($this->_sActive, 'get_orders_info', $aSrvParams, 'Orders');
  206. }
  207. public function getPendingOrdersUrl()
  208. {
  209. if(!BxDolRequest::serviceExists($this->_sActive, 'get_pending_orders_url', 'Orders'))
  210. return '';
  211. return bx_srv($this->_sActive, 'get_pending_orders_url', array(), 'Orders');
  212. }
  213. public function getPendingOrdersInfo($aConditions)
  214. {
  215. if(!BxDolRequest::serviceExists($this->_sActive, 'get_pending_orders_info', 'Orders'))
  216. return array();
  217. $aSrvParams = array($aConditions);
  218. return bx_srv($this->_sActive, 'get_pending_orders_info', $aSrvParams, 'Orders');
  219. }
  220. public function getSubscriptionOrdersInfo($aConditions)
  221. {
  222. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscription_orders_info', 'Subscriptions'))
  223. return array();
  224. $aSrvParams = array($aConditions);
  225. return bx_srv($this->_sActive, 'get_subscription_orders_info', $aSrvParams, 'Subscriptions');
  226. }
  227. public function getSubscriptionsInfo($aConditions, $bCheckInProvider = false)
  228. {
  229. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscriptions_info', 'Subscriptions'))
  230. return array();
  231. $aSrvParams = array($aConditions, $bCheckInProvider);
  232. return bx_srv($this->_sActive, 'get_subscriptions_info', $aSrvParams, 'Subscriptions');
  233. }
  234. public function getInvoicesUrl()
  235. {
  236. if(!BxDolRequest::serviceExists($this->_sActive, 'get_invoices_url', 'Commissions'))
  237. return '';
  238. return bx_srv($this->_sActive, 'get_invoices_url', array(), 'Commissions');
  239. }
  240. public function getInvoicesCount($sType)
  241. {
  242. if(!BxDolRequest::serviceExists($this->_sActive, 'get_invoices_count', 'Commissions'))
  243. return array();
  244. $aSrvParams = array($sType);
  245. return bx_srv($this->_sActive, 'get_invoices_count', $aSrvParams, 'Commissions');
  246. }
  247. public function getCartUrl($iVendor = 0)
  248. {
  249. if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_url', 'Cart'))
  250. return '';
  251. return bx_srv($this->_sActive, 'get_cart_url', array($iVendor), 'Cart');
  252. }
  253. public function getCartItemsCount()
  254. {
  255. if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_items_count', 'Cart'))
  256. return 0;
  257. $aSrvParams = array();
  258. return bx_srv($this->_sActive, 'get_cart_items_count', $aSrvParams, 'Cart');
  259. }
  260. public function getCartItems($iVendorId, $iModuleId)
  261. {
  262. if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_items', 'Cart'))
  263. return 0;
  264. $aSrvParams = array($iVendorId, $iModuleId);
  265. return bx_srv($this->_sActive, 'get_cart_items', $aSrvParams, 'Cart');
  266. }
  267. public function getCartItemDescriptor($iVendorId, $iModuleId, $iItemId, $iItemCount = false)
  268. {
  269. if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_item_descriptor', 'Cart'))
  270. return '';
  271. $aSrvParams = array($iVendorId, $iModuleId, $iItemId, $iItemCount);
  272. return bx_srv($this->_sActive, 'get_cart_item_descriptor', $aSrvParams, 'Cart');
  273. }
  274. /**
  275. * Returns cart JavaScript code which should be included in the page to make "Add To Cart" and "Subscribe" buttons work properly.
  276. */
  277. public function getCartJs($sType = '', $iVendorId = 0)
  278. {
  279. if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_js', 'Cart'))
  280. return '';
  281. $aSrvParams = array($sType, $iVendorId);
  282. return bx_srv($this->_sActive, 'get_cart_js', $aSrvParams, 'Cart');
  283. }
  284. /**
  285. * Adds an item to cart in background mode.
  286. */
  287. public function addToCart($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $aCustom = array())
  288. {
  289. if(!BxDolRequest::serviceExists($this->_sActive, 'add_to_cart', 'Cart'))
  290. return array();
  291. $aSrvParams = array($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $aCustom);
  292. return bx_srv($this->_sActive, 'add_to_cart', $aSrvParams, 'Cart');
  293. }
  294. /**
  295. * Returns "Add To Cart" JavaScript code to use in onclick attribute.
  296. */
  297. public function getAddToCartJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $aCustom = array())
  298. {
  299. if(!BxDolRequest::serviceExists($this->_sActive, 'get_add_to_cart_js', 'Cart'))
  300. return array();
  301. $aSrvParams = array($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect, $aCustom);
  302. return bx_srv($this->_sActive, 'get_add_to_cart_js', $aSrvParams, 'Cart');
  303. }
  304. /**
  305. * Returns the whole "Add To Cart" link including HTML tag BUTTON and cart JavaScript code.
  306. */
  307. public function getAddToCartLink($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $aCustom = array())
  308. {
  309. if(!BxDolRequest::serviceExists($this->_sActive, 'get_add_to_cart_link', 'Cart'))
  310. return '';
  311. $aSrvParams = array($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect, $aCustom);
  312. return bx_srv($this->_sActive, 'get_add_to_cart_link', $aSrvParams, 'Cart');
  313. }
  314. /**
  315. * Subscribe an item in background mode.
  316. */
  317. public function subscribeWithAddons($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount = 1, $sItemAddons = '', $sRedirect = '', $aCustom = array())
  318. {
  319. if(!BxDolRequest::serviceExists($this->_sActive, 'subscribe_with_addons', 'Subscriptions'))
  320. return array();
  321. $aSrvParams = array($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount, $sItemAddons, $sRedirect, $aCustom);
  322. return bx_srv($this->_sActive, 'subscribe_with_addons', $aSrvParams, 'Subscriptions');
  323. }
  324. public function getSubscriptionsUrl()
  325. {
  326. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscriptions_url', 'Subscriptions'))
  327. return '';
  328. return bx_srv($this->_sActive, 'get_subscriptions_url', array(), 'Subscriptions');
  329. }
  330. public function getSubscribeUrl($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount = 1)
  331. {
  332. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_url', 'Subscriptions'))
  333. return '';
  334. return bx_srv($this->_sActive, 'get_subscribe_url', array($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount), 'Subscriptions');
  335. }
  336. /**
  337. * Returns "Subscribe" JavaScript code to use in onclick attribute.
  338. */
  339. public function getSubscribeJs($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount = 1, $sRedirect = '', $aCustom = array())
  340. {
  341. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_js', 'Subscriptions'))
  342. return array();
  343. $aSrvParams = array($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  344. return bx_srv($this->_sActive, 'get_subscribe_js', $aSrvParams, 'Subscriptions');
  345. }
  346. /**
  347. * Returns "Subscribe" JavaScript code to use in onclick attribute.
  348. */
  349. public function getSubscribeJsWithAddons($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount = 1, $sItemAddons = '', $sRedirect = '', $aCustom = array())
  350. {
  351. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_js_with_addons', 'Subscriptions'))
  352. return array();
  353. $aSrvParams = array($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount, $sItemAddons, $sRedirect, $aCustom);
  354. return bx_srv($this->_sActive, 'get_subscribe_js_with_addons', $aSrvParams, 'Subscriptions');
  355. }
  356. /**
  357. * Returns the whole "Subscribe" link including HTML tag BUTTON and cart JavaScript code.
  358. */
  359. public function getSubscribeLink($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount = 1, $sRedirect = '', $aCustom = array())
  360. {
  361. if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_link', 'Subscriptions'))
  362. return '';
  363. $aSrvParams = array($iVendorId, $sVendorProvider, $mixedModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  364. return bx_srv($this->_sActive, 'get_subscribe_link', $aSrvParams, 'Subscriptions');
  365. }
  366. public function sendSubscriptionExpirationLetters($iPendingId, $sOrderId)
  367. {
  368. if(!BxDolRequest::serviceExists($this->_sActive, 'send_subscription_expiration_letters', 'Subscriptions'))
  369. return '';
  370. $aSrvParams = array($iPendingId, $sOrderId);
  371. return bx_srv($this->_sActive, 'send_subscription_expiration_letters', $aSrvParams, 'Subscriptions');
  372. }
  373. public function authorizeCheckoutSingle($iVendorId, $sProvider, $aItems = array())
  374. {
  375. if(!BxDolRequest::serviceExists($this->_sActive, 'authorize_checkout'))
  376. return '';
  377. $aSrvParams = array(BX_PAYMENT_TYPE_SINGLE, $iVendorId, $sProvider, $aItems);
  378. return bx_srv($this->_sActive, 'authorize_checkout', $aSrvParams);
  379. }
  380. public function authorizeCheckoutRecurring($iVendorId, $sProvider, $aItems = array())
  381. {
  382. if(!BxDolRequest::serviceExists($this->_sActive, 'authorize_checkout'))
  383. return '';
  384. $aSrvParams = array(BX_PAYMENT_TYPE_RECURRING, $iVendorId, $sProvider, $aItems);
  385. return bx_srv($this->_sActive, 'authorize_checkout', $aSrvParams);
  386. }
  387. public function captureAuthorizedCheckoutSingle($sOrder)
  388. {
  389. if(!BxDolRequest::serviceExists($this->_sActive, 'capture_authorized_checkout'))
  390. return '';
  391. $aSrvParams = array(BX_PAYMENT_TYPE_SINGLE, $sOrder);
  392. return bx_srv($this->_sActive, 'capture_authorized_checkout', $aSrvParams);
  393. }
  394. public function captureAuthorizedCheckoutRecurring($sOrder)
  395. {
  396. if(!BxDolRequest::serviceExists($this->_sActive, 'capture_authorized_checkout'))
  397. return '';
  398. $aSrvParams = array(BX_PAYMENT_TYPE_RECURRING, $sOrder);
  399. return bx_srv($this->_sActive, 'capture_authorized_checkout', $aSrvParams);
  400. }
  401. public function initializeCheckout($iVendorId, $sProvider, $aItems = array())
  402. {
  403. if(!BxDolRequest::serviceExists($this->_sActive, 'initialize_checkout'))
  404. return '';
  405. $aSrvParams = array($iVendorId, $sProvider, $aItems);
  406. return bx_srv($this->_sActive, 'initialize_checkout', $aSrvParams);
  407. }
  408. public function cancelSubscription($sOrderId)
  409. {
  410. if(!BxDolRequest::serviceExists($this->_sActive, 'cancel', 'Subscriptions'))
  411. return false;
  412. $aSrvParams = [$sOrderId];
  413. return bx_srv($this->_sActive, 'cancel', $aSrvParams, 'Subscriptions');
  414. }
  415. public function processOrder($iSellerId, $iClientId, $iModuleId, $aItems, $sType, $sOrder)
  416. {
  417. if(!BxDolRequest::serviceExists($this->_sActive, 'process_order', 'Orders'))
  418. return false;
  419. $aSrvParams = array($iSellerId, $iClientId, $iModuleId, $aItems, $sType, $sOrder);
  420. return bx_srv($this->_sActive, 'process_order', $aSrvParams, 'Orders');
  421. }
  422. public function processOrderByPending($iPendingId, $sOrder)
  423. {
  424. if(!BxDolRequest::serviceExists($this->_sActive, 'process_order_by_pending', 'Orders'))
  425. return false;
  426. $aSrvParams = array($iPendingId, $sOrder);
  427. return bx_srv($this->_sActive, 'process_order_by_pending', $aSrvParams, 'Orders');
  428. }
  429. public function refundOrder($sOrder)
  430. {
  431. if(!BxDolRequest::serviceExists($this->_sActive, 'refund_order', 'Orders'))
  432. return false;
  433. $aSrvParams = array($sOrder);
  434. return bx_srv($this->_sActive, 'refund_order', $aSrvParams, 'Orders');
  435. }
  436. public function generateLicense()
  437. {
  438. if(!BxDolRequest::serviceExists($this->_sActive, 'generate_license'))
  439. return '';
  440. $aSrvParams = array();
  441. return bx_srv($this->_sActive, 'generate_license', $aSrvParams, 'Module', true);
  442. }
  443. }
  444. /** @} */