1
0

BxBaseModPaymentModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 BasePayment Base classes for Payment like modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModPaymentModule extends BxBaseModGeneralModule
  12. {
  13. protected $_sLangsPrefix;
  14. public function __construct($aModule)
  15. {
  16. parent::__construct($aModule);
  17. $this->_oConfig->init($this->_oDb);
  18. $this->_sLangsPrefix = $this->_oConfig->getPrefix('langs');
  19. }
  20. public function serviceGetSafeServices()
  21. {
  22. return array();
  23. }
  24. /**
  25. * @page service Service Calls
  26. * @section bx_base_payment Base Payment
  27. * @subsection bx_base_payment-integration Integration
  28. * @subsubsection bx_base_payment-update_dependent_modules update_dependent_modules
  29. *
  30. * @code bx_srv('bx_payment', 'update_dependent_modules', [...]); @endcode
  31. *
  32. * Get payments dependent modules and save them.
  33. *
  34. * @param $sModule (optional) string value with module name. All modules are used by default.
  35. * @param $bInstall (optional) boolean value determining whether the install or uninstall operation is performed.
  36. *
  37. * @see BxBaseModPaymentModule::serviceUpdateDependentModules
  38. */
  39. /**
  40. * @ref bx_base_payment-update_dependent_modules "update_dependent_modules"
  41. */
  42. public function serviceUpdateDependentModules($sModule = 'all', $bInstall = true)
  43. {
  44. $aModules = $sModule == 'all' ? $this->_oDb->getModulesBy(array('type' => 'modules'), false) : array($this->_oDb->getModuleByName($sModule, false));
  45. foreach($aModules as $aModule) {
  46. if(empty($aModule) || empty($aModule['name']))
  47. continue;
  48. $mixedData = $this->callGetPaymentData($aModule['name']);
  49. if($mixedData === false)
  50. continue;
  51. $sMethodName = $bInstall ? 'insertModule' : 'deleteModule';
  52. $this->_oDb->$sMethodName($mixedData);
  53. }
  54. }
  55. /**
  56. * @page service Service Calls
  57. * @section bx_base_payment Base Payment
  58. * @subsection bx_base_payment-other Other
  59. * @subsubsection bx_base_payment-generate_license generate_license
  60. *
  61. * @code bx_srv('bx_payment', 'generate_license'); @endcode
  62. *
  63. * Generate license.
  64. *
  65. * @return a string with license.
  66. *
  67. * @see BxBaseModPaymentModule::serviceGenerateLicense
  68. */
  69. /**
  70. * @ref bx_base_payment-generate_license "generate_license"
  71. */
  72. public function serviceGenerateLicense()
  73. {
  74. return $this->_oConfig->getLicense();
  75. }
  76. /**
  77. * @page service Service Calls
  78. * @section bx_base_payment Base Payment
  79. * @subsection bx_base_payment-other Other
  80. * @subsubsection bx_base_payment-get_currency_info get_currency_info
  81. *
  82. * @code bx_srv('bx_payment', 'get_currency_info', [...]); @endcode
  83. *
  84. * Get default currency info (sign and code).
  85. *
  86. * @return an array with currency info.
  87. *
  88. * @see BxBaseModPaymentModule::serviceGetCurrencyInfo
  89. */
  90. /**
  91. * @ref bx_base_payment-get_currency_info "get_currency_info"
  92. */
  93. public function serviceGetCurrencyInfo($iVendorId = 0)
  94. {
  95. if((int)$iVendorId != 0) {
  96. $aVendorInfo = $this->getVendorInfo ($iVendorId);
  97. return [
  98. 'sign' => $aVendorInfo['currency_sign'],
  99. 'code' => $aVendorInfo['currency_code']
  100. ];
  101. }
  102. return [
  103. 'sign' => $this->_oConfig->getDefaultCurrencySign(),
  104. 'code' => $this->_oConfig->getDefaultCurrencyCode()
  105. ];
  106. }
  107. public function serviceGetCurrencyCode($iVendorId = 0)
  108. {
  109. if((int)$iVendorId != 0)
  110. return $this->getVendorCurrencyCode($iVendorId);
  111. return $this->_oConfig->getDefaultCurrencyCode();
  112. }
  113. public function serviceGetCurrencySign($iVendorId = 0)
  114. {
  115. if((int)$iVendorId != 0)
  116. return $this->getVendorCurrencySign($iVendorId);
  117. return $this->_oConfig->getDefaultCurrencySign();
  118. }
  119. /**
  120. * @page service Service Calls
  121. * @section bx_base_payment Base Payment
  122. * @subsection bx_base_payment-other Other
  123. * @subsubsection bx_base_payment-get_option get_option
  124. *
  125. * @code bx_srv('bx_payment', 'get_option', [...]); @endcode
  126. *
  127. * Get value of payment provider configuration option.
  128. *
  129. * @param $sOption string value with option name.
  130. * @return string with option value.
  131. *
  132. * @see BxBaseModPaymentModule::serviceGetOption
  133. */
  134. /**
  135. * @ref bx_base_payment-get_option "get_option"
  136. */
  137. public function serviceGetOption($sOption)
  138. {
  139. $sMethod = 'get' . bx_gen_method_name($sOption);
  140. if(method_exists($this->_oConfig, $sMethod))
  141. return $this->_oConfig->$sMethod();
  142. return $this->_oDb->getParam($this->_oConfig->getPrefix('options') . $sOption);
  143. }
  144. /**
  145. * @page service Service Calls
  146. * @section bx_base_payment Base Payment
  147. * @subsection bx_base_payment-other Other
  148. * @subsubsection bx_base_payment-get_provider get_provider
  149. *
  150. * @code bx_srv('bx_payment', 'get_provider', [...]); @endcode
  151. *
  152. * Get payment provider object by its name and vendor ID.
  153. *
  154. * @param $sProvider string value with provider name.
  155. * @param $mixedVendorId mixed value with vendor ID.
  156. * @return provider object, instance of BxBaseModPaymentProvider.
  157. *
  158. * @see BxBaseModPaymentModule::serviceGetProvider
  159. */
  160. /**
  161. * @ref bx_base_payment-get_provider "get_provider"
  162. */
  163. public function serviceGetProvider($sProvider, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  164. {
  165. return $this->getObjectProvider($sProvider, $mixedVendorId);
  166. }
  167. /**
  168. * @page service Service Calls
  169. * @section bx_base_payment Base Payment
  170. * @subsection bx_base_payment-other Other
  171. * @subsubsection bx_base_payment-get_providers_cart get_providers_cart
  172. *
  173. * @code bx_srv('bx_payment', 'get_providers_cart', [...]); @endcode
  174. *
  175. * Get list of available payment providers which can process single time payments via Shopping Cart.
  176. *
  177. * @param $iVendorId integer value with vendor ID.
  178. * @return an array with special format.
  179. *
  180. * @see BxBaseModPaymentModule::serviceGetProvidersCart
  181. */
  182. /**
  183. * @ref bx_base_payment-get_providers_cart "get_providers_cart"
  184. */
  185. public function serviceGetProvidersCart($iVendorId)
  186. {
  187. $aVendorProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  188. $aResult = array();
  189. foreach($aVendorProviders as $aProvider) {
  190. $aProvider['caption_cart'] = _t($this->_sLangsPrefix . 'txt_cart_' . $aProvider['name']);
  191. $aResult[] = $aProvider;
  192. }
  193. return $aResult;
  194. }
  195. public function _isModeratorForProfile($isPerformAction = false, $iProfileId = false)
  196. {
  197. return BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR], $iProfileId) || (($oProfile = BxDolProfile::getInstance()) !== false && isAdmin($oProfile->getAccountId()));
  198. }
  199. public function _isAdministratorForProfile($isPerformAction = false, $iProfileId = false)
  200. {
  201. return BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_ADMINISTRATOR], $iProfileId) || (($oProfile = BxDolProfile::getInstance()) !== false && isAdmin($oProfile->getAccountId()));
  202. }
  203. public function isSingleSeller()
  204. {
  205. return $this->_oConfig->isSingleSeller();
  206. }
  207. public function getVendorInfo($iUserId)
  208. {
  209. $CNF = &$this->_oConfig->CNF;
  210. $sCode = $sSign = '';
  211. if(!$this->_oConfig->isSingleSeller() && ($oProvider = $this->getObjectProvider($CNF['PROVIDER_GENERIC'], $iUserId)) !== false) {
  212. $sCode = $oProvider->getOption('currency_code');
  213. $sSign = $this->_oConfig->retrieveCurrencySign($sCode);
  214. }
  215. if(empty($sCode) || empty($sSign)) {
  216. $sCode = $this->_oConfig->getDefaultCurrencyCode();
  217. $sSign = $this->_oConfig->getDefaultCurrencySign();
  218. }
  219. return array_merge($this->getProfileInfo($iUserId), [
  220. 'currency_code' => $sCode,
  221. 'currency_sign' => $sSign
  222. ]);
  223. }
  224. public function getVendorCurrencyCode($iUserId)
  225. {
  226. $CNF = &$this->_oConfig->CNF;
  227. $sCode = '';
  228. if(!$this->_oConfig->isSingleSeller() && ($oProvider = $this->getObjectProvider($CNF['PROVIDER_GENERIC'], $iUserId)) !== false)
  229. $sCode = $oProvider->getOption('currency_code');
  230. if(empty($sCode))
  231. $sCode = $this->_oConfig->getDefaultCurrencyCode();
  232. return $sCode;
  233. }
  234. public function getVendorCurrencySign($iUserId)
  235. {
  236. $CNF = &$this->_oConfig->CNF;
  237. $sSign = '';
  238. if(!$this->_oConfig->isSingleSeller() && ($oProvider = $this->getObjectProvider($CNF['PROVIDER_GENERIC'], $iUserId)) !== false)
  239. $sSign = $this->_oConfig->retrieveCurrencySign($oProvider->getOption('currency_code'));
  240. if(empty($sSign))
  241. $sSign = $this->_oConfig->getDefaultCurrencySign();
  242. return $sSign;
  243. }
  244. public function getObjectJoin()
  245. {
  246. $sClassName = $this->_oConfig->getClassPrefix() . 'Join';
  247. if(!isset($GLOBALS['bxDolClasses'][$sClassName])) {
  248. bx_import('Join', $this->_aModule);
  249. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
  250. }
  251. return $GLOBALS['bxDolClasses'][$sClassName];
  252. }
  253. public function getObjectCart()
  254. {
  255. $sClassName = $this->_oConfig->getClassPrefix() . 'Cart';
  256. if(!isset($GLOBALS['bxDolClasses'][$sClassName])) {
  257. bx_import('Cart', $this->_aModule);
  258. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
  259. }
  260. return $GLOBALS['bxDolClasses'][$sClassName];
  261. }
  262. public function getObjectOrders()
  263. {
  264. $sClassName = $this->_oConfig->getClassPrefix() . 'Orders';
  265. if(!isset($GLOBALS['bxDolClasses'][$sClassName])) {
  266. bx_import('Orders', $this->_aModule);
  267. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
  268. }
  269. return $GLOBALS['bxDolClasses'][$sClassName];
  270. }
  271. public function getObjectDetails()
  272. {
  273. $sClassName = $this->_oConfig->getClassPrefix() . 'Details';
  274. if(!isset($GLOBALS['bxDolClasses'][$sClassName])) {
  275. bx_import('Details', $this->_aModule);
  276. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
  277. }
  278. return $GLOBALS['bxDolClasses'][$sClassName];
  279. }
  280. public function getObjectSubscriptions()
  281. {
  282. $sClassName = $this->_oConfig->getClassPrefix() . 'Subscriptions';
  283. if(!isset($GLOBALS['bxDolClasses'][$sClassName])) {
  284. bx_import('Subscriptions', $this->_aModule);
  285. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
  286. }
  287. return $GLOBALS['bxDolClasses'][$sClassName];
  288. }
  289. public function getObjectProvider($sProvider, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  290. {
  291. $aProvider = $this->_oDb->getProviders(array('type' => 'by_name', 'name' => $sProvider));
  292. if(empty($aProvider) || !is_array($aProvider) || empty($aProvider['class_name']))
  293. return false;
  294. if(is_numeric($mixedVendorId) && (int)$mixedVendorId != BX_PAYMENT_EMPTY_ID) {
  295. $aProvider['vendor'] = (int)$mixedVendorId;
  296. $aProvider['options'] = $this->_oDb->getOptions((int)$mixedVendorId, $aProvider['id']);
  297. }
  298. $sClassPath = !empty($aProvider['class_file']) ? BX_DIRECTORY_PATH_ROOT . $aProvider['class_file'] : $this->_oConfig->getClassPath() . $aProvider['class_name'] . '.php';
  299. if(!file_exists($sClassPath))
  300. return false;
  301. require_once($sClassPath);
  302. return new $aProvider['class_name']($aProvider);
  303. }
  304. /**
  305. * Method to fire alert on behalf of both 'system' and currently active payment module.
  306. * It's needed for general events like: Finalize Checkout, Register Payment, Refund Payment, etc.
  307. */
  308. public function alert($sAction, $iObjectId, $iSender = false, $aExtras = array())
  309. {
  310. $sSystem = 'system';
  311. $sModule = $this->getName();
  312. foreach([$sSystem, $sModule] as $sUnit)
  313. /**
  314. * @hooks
  315. * @hookdef hook-system-bx_payment_action 'system', '{payment_action}' - hook after some payment related action happened
  316. * - $unit_name - equals `system`
  317. * - $action - action (Finalize Checkout, Register Payment, Refund Payment, etc)
  318. * - $object_id - object id, not used in most cases
  319. * - $sender_id - action performer profile id
  320. * - $extra_params - array of additional params including currently active payment processing module name
  321. * @hook @ref hook-system-bx_payment_action
  322. */
  323. /**
  324. * @hooks
  325. * @hookdef hook-bx_base_payment-bx_payment_action '{payment_module_name}', '{payment_action}' - hook after some payment related action happened
  326. * It's equivalent to @ref hook-system-bx_payment_action
  327. * except `module` parameter in $extra_params is missing
  328. * @hook @ref hook-bx_base_payment-bx_payment_action
  329. */
  330. bx_alert($sUnit, $sAction, $iObjectId, $iSender, ($sUnit == $sSystem ? array_merge(['module' => $sModule], $aExtras) : $aExtras));
  331. }
  332. public function callGetPaymentData($mixedModule)
  333. {
  334. $sMethod = 'get_payment_data';
  335. if(!BxDolRequest::serviceExists($mixedModule, $sMethod))
  336. return false;
  337. return BxDolService::call($mixedModule, $sMethod);
  338. }
  339. public function callGetCartItem($mixedModule, $aParams)
  340. {
  341. return BxDolService::call($mixedModule, 'get_cart_item', $aParams);
  342. }
  343. public function callGetCartItems($mixedModule, $aParams)
  344. {
  345. return BxDolService::call($mixedModule, 'get_cart_items', $aParams);
  346. }
  347. public function callAuthorizeCartItem($mixedModule, $aParams)
  348. {
  349. return BxDolService::call($mixedModule, 'authorize_cart_item', $aParams);
  350. }
  351. public function callRegisterCartItem($mixedModule, $aParams)
  352. {
  353. return BxDolService::call($mixedModule, 'register_cart_item', $aParams);
  354. }
  355. public function callReregisterCartItem($mixedModule, $aParams)
  356. {
  357. return BxDolService::call($mixedModule, 'reregister_cart_item', $aParams);
  358. }
  359. public function callAuthorizeSubscriptionItem($mixedModule, $aParams)
  360. {
  361. return BxDolService::call($mixedModule, 'authorize_subscription_item', $aParams);
  362. }
  363. public function callRegisterSubscriptionItem($mixedModule, $aParams)
  364. {
  365. return BxDolService::call($mixedModule, 'register_subscription_item', $aParams);
  366. }
  367. public function callReregisterSubscriptionItem($mixedModule, $aParams)
  368. {
  369. return BxDolService::call($mixedModule, 'reregister_subscription_item', $aParams);
  370. }
  371. public function callUnregisterCartItem($mixedModule, $aParams)
  372. {
  373. return BxDolService::call($mixedModule, 'unregister_cart_item', $aParams);
  374. }
  375. public function callUnregisterSubscriptionItem($mixedModule, $aParams)
  376. {
  377. return BxDolService::call($mixedModule, 'unregister_subscription_item', $aParams);
  378. }
  379. public function callCancelSubscriptionItem($mixedModule, $aParams)
  380. {
  381. return BxDolService::call($mixedModule, 'cancel_subscription_item', $aParams);
  382. }
  383. public function log($mixedContents, $sSection = '', $sTitle = '', $iLevel = BX_LOG_DEBUG)
  384. {
  385. if(is_array($mixedContents))
  386. $mixedContents = var_export($mixedContents, true);
  387. else if(is_object($mixedContents))
  388. $mixedContents = json_encode($mixedContents);
  389. if(empty($sSection))
  390. $sSection = "Core";
  391. $sTitle .= "\n";
  392. bx_log('sys_payments', ":\n[" . $sSection . "] " . $sTitle . $mixedContents, $iLevel);
  393. }
  394. }
  395. /** @} */