BxPaymentModule.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 Payment Payment
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_PAYMENT_ORDERS_TYPE_PENDING', 'pending');
  12. define('BX_PAYMENT_ORDERS_TYPE_PROCESSED', 'processed');
  13. define('BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION', 'subscription');
  14. define('BX_PAYMENT_ORDERS_TYPE_HISTORY', 'history');
  15. define('BX_PAYMENT_EMPTY_ID', 0);
  16. define('BX_PAYMENT_RESULT_SUCCESS', 0);
  17. /**
  18. * Payment module by BoonEx
  19. *
  20. * This module is needed to work with payment providers and organize the process
  21. * of some item purchasing. Shopping Cart and Orders Manager are included.
  22. *
  23. * Integration notes:
  24. * To integrate your module with this one, you need:
  25. * 1. Get 'Add To Cart' button using serviceGetAddToCartLink service.
  26. * 2. Add info about your module in the 'bx_pmt_modules' table.
  27. * 3. Realize the following service methods in your Module class.
  28. * a. serviceGetItems($iSellerId) - Is used in Orders Administration to get all products of the requested seller(vendor).
  29. * b. serviceGetCartItem($iClientId, $iItemId) - Is used in Shopping Cart to get one product by specified id.
  30. * c. serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Register purchased product.
  31. * d. serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Unregister the product purchased earlier.
  32. * @see You may see an example of integration in Membership module.
  33. *
  34. *
  35. * Profile's Wall:
  36. * no spy events
  37. *
  38. *
  39. *
  40. * Spy:
  41. * no spy events
  42. *
  43. *
  44. *
  45. * Memberships/ACL:
  46. * Doesn't depend on user's membership.
  47. *
  48. *
  49. *
  50. * Service methods:
  51. *
  52. * Is used to get "Add to cart" link for some item(s) in your module.
  53. * @see BxPmtModule::serviceGetAddToCartLink
  54. * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
  55. *
  56. * Check transaction(s) in database which satisty all conditions.
  57. * @see BxPmtModule::serviceGetOrdersInfo
  58. * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
  59. *
  60. * Get total count of items in Shopping Cart.
  61. * @see BxPmtModule::serviceGetCartItemsCount
  62. * BxDolService::call('payment', 'get_cart_items_count', array($iUserId, $iOldCount));
  63. * @note is needed for internal usage(integration with member tool bar).
  64. *
  65. * Get Shopping cart content.
  66. * @see BxPmtModule::serviceGetCartItems
  67. * BxDolService::call('payment', 'get_cart_items');
  68. * @note is needed for internal usage(integration with member tool bar).
  69. *
  70. *
  71. * Alerts:
  72. * no alerts available
  73. *
  74. */
  75. class BxPaymentModule extends BxBaseModPaymentModule
  76. {
  77. protected $_iUserId;
  78. protected $_aOrderTypes;
  79. function __construct($aModule)
  80. {
  81. parent::__construct($aModule);
  82. $this->_iUserId = $this->getProfileId();
  83. $this->_aOrderTypes = array(
  84. BX_PAYMENT_ORDERS_TYPE_PENDING,
  85. BX_PAYMENT_ORDERS_TYPE_PROCESSED,
  86. BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION,
  87. BX_PAYMENT_ORDERS_TYPE_HISTORY
  88. );
  89. }
  90. /**
  91. * Manage Orders Methods
  92. */
  93. public function actionGetClients()
  94. {
  95. $sTerm = bx_get('term');
  96. $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  97. echoJson($aResult);
  98. }
  99. public function actionGetItems($sType, $iModuleId)
  100. {
  101. $iSellerId = $this->getProfileId();
  102. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  103. $aResult = array('code' => 0);
  104. if(is_array($aItems) && !empty($aItems))
  105. $aResult['data'] = $this->_oTemplate->displayItems($sType, $aItems);
  106. echoJson($aResult);
  107. }
  108. /**
  109. * Payment Details Methods
  110. */
  111. public function serviceIsAcceptingPayments($iVendorId, $sPaymentType = '')
  112. {
  113. $bResult = false;
  114. switch($sPaymentType) {
  115. case BX_PAYMENT_TYPE_SINGLE:
  116. $aProvidersCart = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  117. $bResult = !empty($aProvidersCart);
  118. break;
  119. case BX_PAYMENT_TYPE_RECURRING:
  120. $aProvidersSubscription = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  121. $bResult = !empty($aProvidersSubscription);
  122. break;
  123. default:
  124. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  125. $bResult = !empty($aProviders);
  126. }
  127. return $bResult;
  128. }
  129. public function serviceIsPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  130. {
  131. $aProvider = $this->serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType);
  132. return $aProvider !== false;
  133. }
  134. public function serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  135. {
  136. $aProviders = array();
  137. switch($sPaymentType) {
  138. case BX_PAYMENT_TYPE_SINGLE:
  139. $aProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  140. break;
  141. case BX_PAYMENT_TYPE_RECURRING:
  142. $aProviders = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  143. break;
  144. default:
  145. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  146. }
  147. return !empty($aProviders) && !empty($aProviders[$sVendorProvider]) && is_array(($aProviders[$sVendorProvider])) ? $aProviders[$sVendorProvider] : false;
  148. }
  149. public function serviceGetOptionsDefaultCurrencyCode()
  150. {
  151. $CNF = &$this->_oConfig->CNF;
  152. $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES']);
  153. $aResult = array();
  154. foreach($aCurrencies as $sKey => $sValue)
  155. $aResult[] = array(
  156. 'key' => $sKey,
  157. 'value' => $sValue
  158. );
  159. return $aResult;
  160. }
  161. public function serviceGetOptionsSiteAdmin()
  162. {
  163. $aResult = array(
  164. array('key' => '', 'value' => _t('_Select_one'))
  165. );
  166. $aIds = $this->_oDb->getAdminsIds();
  167. foreach($aIds as $iId) {
  168. $aUser = $this->getProfileInfo($iId);
  169. $aResult[] = array(
  170. 'key' => $iId,
  171. 'value' => $aUser['name']
  172. );
  173. }
  174. return $aResult;
  175. }
  176. /**
  177. * Cart Processing Methods
  178. */
  179. public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount)
  180. {
  181. $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount);
  182. echoJson($aResult);
  183. }
  184. /**
  185. * Isn't used yet.
  186. */
  187. public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
  188. {
  189. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId);
  190. echoJson($aResult);
  191. }
  192. /**
  193. * Isn't used yet.
  194. */
  195. public function actionEmptyCart($iSellerId)
  196. {
  197. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId);
  198. echoJson($aResult);
  199. }
  200. public function actionSubscribe()
  201. {
  202. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  203. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  204. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  205. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  206. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  207. if(empty($iItemCount))
  208. $iItemCount = 1;
  209. $aResult = $this->getObjectCart()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount);
  210. if($aResult['code'] != 0)
  211. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  212. if(empty($aResult['redirect']) && !empty($aResult['message']))
  213. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  214. header('Location: ' . $aResult['redirect']);
  215. exit;
  216. }
  217. public function actionSubscribeJson()
  218. {
  219. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  220. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  221. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  222. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  223. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  224. if(empty($iItemCount))
  225. $iItemCount = 1;
  226. $sRedirect = bx_process_input(bx_get('redirect'));
  227. $aResult = $this->getObjectCart()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect);
  228. echoJson($aResult);
  229. }
  230. public function actionSubscriptionGetDetails($iId)
  231. {
  232. $aResult = $this->_oTemplate->displaySubscriptionGetDetails($iId);
  233. echoJson($aResult);
  234. }
  235. public function actionSubscriptionChangeDetails($iId)
  236. {
  237. $aResult = $this->_oTemplate->displaySubscriptionChangeDetails($iId);
  238. echoJson($aResult);
  239. }
  240. public function actionSubscriptionGetBilling($iId)
  241. {
  242. $aResult = $this->_oTemplate->displaySubscriptionGetBilling($iId);
  243. echoJson($aResult);
  244. }
  245. public function actionSubscriptionChangeBilling($iId)
  246. {
  247. $aResult = $this->_oTemplate->displaySubscriptionChangeBilling($iId);
  248. echoJson($aResult);
  249. }
  250. public function actionSubscriptionCancelation($iId)
  251. {
  252. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  253. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  254. if(empty($aPending) && !is_array($aPending))
  255. return echoJson($aResult);
  256. $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
  257. if(empty($aSubscription) && !is_array($aSubscription))
  258. return echoJson($aResult);
  259. $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
  260. if(!$oRecipient)
  261. return echoJson($aResult);
  262. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
  263. 'sibscription_id' => $aSubscription['subscription_id'],
  264. 'sibscription_customer' => $aSubscription['customer_id'],
  265. 'sibscription_date' => bx_time_js($aSubscription['date'], BX_FORMAT_DATE, true)
  266. ), 0, (int)$aPending['client_id']);
  267. $sEmail = '';
  268. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  269. if($oProvider !== false && $oProvider->isActive())
  270. $sEmail = $oProvider->getOption('cancellation_email');
  271. if(empty($sEmail))
  272. $sEmail = $oRecipient->getAccountObject()->getEmail();
  273. if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  274. return echoJson($aResult);
  275. echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
  276. }
  277. /**
  278. * Payment Processing Methods
  279. */
  280. public function actionInitializeCheckout($sType)
  281. {
  282. if(!$this->isLogged())
  283. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  284. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  285. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  286. $sProvider = bx_process_input(bx_get('provider'));
  287. $aItems = bx_process_input(bx_get('items'));
  288. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  289. if($mixedResult !== true)
  290. return $this->_oTemplate->displayPageCodeError($mixedResult);
  291. }
  292. header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
  293. exit;
  294. }
  295. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '')
  296. {
  297. if(!is_array($aItems))
  298. $aItems = array($aItems);
  299. $iSellerId = (int)$iSellerId;
  300. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  301. return $this->_sLangsPrefix . 'err_unknown_vendor';
  302. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  303. if($oProvider === false || !$oProvider->isActive())
  304. return $this->_sLangsPrefix . 'err_incorrect_provider';
  305. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  306. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  307. return $this->_sLangsPrefix . 'err_empty_order';
  308. /*
  309. * Process FREE (price = 0) items for LOGGED IN members
  310. * WITHOUT processing via payment provider.
  311. */
  312. $bProcessedFree = false;
  313. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  314. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  315. foreach($aInfo['items'] as $iIndex => $aItem)
  316. if((int)$aInfo['client_id'] != 0 && (float)$aItem[$sKeyPriceSingle] == 0 && (float)$aItem[$sKeyPriceRecurring] == 0) {
  317. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense()));
  318. if(is_array($aItemInfo) && !empty($aItemInfo))
  319. $bProcessedFree = true;
  320. $aInfo['items_count'] -= 1;
  321. unset($aInfo['items'][$iIndex]);
  322. $sCartItems = $this->_oDb->getCartItems($aInfo['client_id']);
  323. $sCartItems = trim(preg_replace("'" . $this->_oConfig->descriptorA2S(array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity'])) . ":?'", "", $sCartItems), ":");
  324. $this->_oDb->setCartItems($aInfo['client_id'], $sCartItems);
  325. }
  326. if(empty($aInfo['items']))
  327. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  328. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo);
  329. if(empty($iPendingId))
  330. return $this->_sLangsPrefix . 'err_access_db';
  331. /*
  332. * Perform Join WITHOUT processing via payment provider
  333. * if a client ISN'T logged in and has only ONE FREE item in the card.
  334. */
  335. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  336. reset($aInfo['items']);
  337. $aItem = current($aInfo['items']);
  338. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  339. $this->_oDb->updateOrderPending($iPendingId, array(
  340. 'order' => $this->_oConfig->getLicense(),
  341. 'error_code' => '1',
  342. 'error_msg' => ''
  343. ));
  344. $this->getObjectJoin()->performJoin($iPendingId);
  345. }
  346. }
  347. return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
  348. }
  349. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  350. {
  351. $aData = &$_REQUEST;
  352. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  353. if($oProvider === false || !$oProvider->isActive())
  354. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  355. $aResult = $oProvider->finalizeCheckout($aData);
  356. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  357. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  358. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  359. $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
  360. if($bTypeRecurring)
  361. $this->registerSubscription($aPending, array(
  362. 'customer_id' => $aResult['customer_id'],
  363. 'subscription_id' => $aResult['subscription_id']
  364. ));
  365. $this->onPaymentRegisterBefore($aPending);
  366. //--- Check "Pay Before Join" situation
  367. if((int)$aPending['client_id'] == 0)
  368. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  369. //--- Register payment for purchased items in associated modules
  370. if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
  371. $this->registerPayment($aPending);
  372. bx_alert($this->getName(), 'finalize_checkout', 0, bx_get_logged_profile_id(), array(
  373. 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
  374. 'provider' => $oProvider,
  375. 'message' => &$aResult['message'],
  376. ));
  377. if($oProvider->needRedirect()) {
  378. header('Location: ' . $oProvider->getReturnUrl());
  379. exit;
  380. }
  381. if(!empty($aResult['redirect'])) {
  382. header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
  383. exit;
  384. }
  385. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  386. }
  387. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  388. {
  389. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  390. if($oProvider === false || !$oProvider->isActive())
  391. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  392. $aResult = $oProvider->finalizedCheckout();
  393. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  394. }
  395. public function actionNotify($sProvider, $mixedVendorId = "")
  396. {
  397. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  398. if($oProvider === false || !$oProvider->isActive())
  399. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  400. $oProvider->notify();
  401. }
  402. public function onProfileJoin($iProfileId)
  403. {
  404. $this->getObjectJoin()->onProfileJoin($iProfileId);
  405. }
  406. public function onProfileDelete($iProfileId)
  407. {
  408. $this->_oDb->onProfileDelete($iProfileId);
  409. }
  410. public function isAllowedSell($aItem, $bPerform = false)
  411. {
  412. $iUserId = (int)$this->getProfileId();
  413. if(!$iUserId)
  414. return false;
  415. $aItemInfo = $this->callGetCartItem($aItem['module_id'], array($aItem['item_id']));
  416. if(empty($aItemInfo))
  417. return false;
  418. if(isAdmin())
  419. return true;
  420. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  421. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  422. return true;
  423. return $aCheckResult[CHECK_ACTION_MESSAGE];
  424. }
  425. public function registerPayment($mixedPending)
  426. {
  427. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  428. if(empty($aPending) || !is_array($aPending))
  429. return false;
  430. $sType = $aPending['type'];
  431. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  432. if($bTypeSingle && (int)$aPending['processed'] == 1)
  433. return true;
  434. $iClientId = (int)$aPending['client_id'];
  435. $sLicense = $this->_oConfig->getLicense();
  436. $sCartItems = '';
  437. if($bTypeSingle)
  438. $sCartItems = $this->_oDb->getCartItems($iClientId);
  439. $bResult = false;
  440. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  441. foreach($aItems as $aItem) {
  442. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  443. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense));
  444. if(empty($aItemInfo) || !is_array($aItemInfo))
  445. continue;
  446. $this->_oDb->insertOrderProcessed(array(
  447. 'pending_id' => $aPending['id'],
  448. 'client_id' => $aPending['client_id'],
  449. 'seller_id' => $aPending['seller_id'],
  450. 'module_id' => $aItem['module_id'],
  451. 'item_id' => $aItem['item_id'],
  452. 'item_count' => $aItem['item_count'],
  453. 'amount' => $aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  454. 'license' => $sLicense,
  455. ));
  456. if($bTypeSingle)
  457. $sCartItems = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $sCartItems), ":");
  458. $bResult = true;
  459. }
  460. if($bTypeSingle)
  461. $this->_oDb->setCartItems($iClientId, $sCartItems);
  462. if($bResult) {
  463. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  464. $this->onPaymentRegister($aPending);
  465. }
  466. return $bResult;
  467. }
  468. public function refundPayment($mixedPending)
  469. {
  470. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  471. if(empty($aPending) || !is_array($aPending))
  472. return false;
  473. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  474. $iCanceled = 0;
  475. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  476. foreach($aOrders as $aOrder) {
  477. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  478. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  479. if(!$bResult)
  480. continue;
  481. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  482. $iCanceled++;
  483. }
  484. if($iCanceled != count($aOrders))
  485. return false;
  486. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  487. if($bResult)
  488. $this->onPaymentRefund($aPending);
  489. return $bResult;
  490. }
  491. public function registerSubscription($aPending, $aParams = array())
  492. {
  493. $this->_oDb->insertSubscription(array(
  494. 'pending_id' => $aPending['id'],
  495. 'customer_id' => $aParams['customer_id'],
  496. 'subscription_id' => $aParams['subscription_id']
  497. ));
  498. $this->onSubscriptionCreate($aPending);
  499. }
  500. public function updateSubscription($aPending, $aParams = array())
  501. {
  502. $this->_oDb->updateSubscription($aParams, array(
  503. 'pending_id' => $aPending['id']
  504. ));
  505. $this->onSubscriptionUpdate($aPending);
  506. }
  507. public function cancelSubscription($mixedPending)
  508. {
  509. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  510. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING)
  511. return false;
  512. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  513. foreach($aItems as $aItem)
  514. $this->callCancelSubscriptionItem((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order']));
  515. $this->onSubscriptionCancel($aPending);
  516. }
  517. public function onPaymentRegisterBefore($aPending, $aResult = array())
  518. {
  519. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  520. bx_alert('system', 'before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  521. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  522. }
  523. public function onPaymentRegister($aPending, $aResult = array())
  524. {
  525. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  526. bx_alert('system', 'register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  527. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  528. }
  529. public function onPaymentRefund($aPending, $aResult = array())
  530. {
  531. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  532. bx_alert('system', 'refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  533. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  534. }
  535. public function onSubscriptionCreate($aPending, $aResult = array())
  536. {
  537. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  538. bx_alert('system', 'create_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  539. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  540. }
  541. public function onSubscriptionUpdate($aPending, $aResult = array())
  542. {
  543. //--- 'System' -> 'Update Subscription' for Alerts Engine ---//
  544. bx_alert('system', 'update_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  545. //--- 'System' -> 'Update Subscription' for Alerts Engine ---//
  546. }
  547. public function onSubscriptionCancel($aPending, $aResult = array())
  548. {
  549. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  550. bx_alert('system', 'cancel_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  551. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  552. }
  553. public function setSiteSubmenu($sSubmenu, $sSelected)
  554. {
  555. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  556. if(!$oSiteSubmenu)
  557. return;
  558. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  559. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  560. if(!$oModuleSubmenu)
  561. return;
  562. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu, array('title' => _t('_bx_payment'), 'link' => '','icon' => ''));
  563. $oModuleSubmenu->setSelected($this->_aModule['name'], $sSelected);
  564. }
  565. }
  566. /** @} */