1
0

BxPaymentModule.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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::serviceGetTransactionsInfo
  58. * BxDolService::call('payment', 'get_transactions_info', array($aConditions));
  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 serviceGetOptionsSiteAdmin()
  150. {
  151. $aResult = array(
  152. array('key' => '', 'value' => _t('_Select_one'))
  153. );
  154. $aIds = $this->_oDb->getAdminsIds();
  155. foreach($aIds as $iId) {
  156. $aUser = $this->getProfileInfo($iId);
  157. $aResult[] = array(
  158. 'key' => $iId,
  159. 'value' => $aUser['name']
  160. );
  161. }
  162. return $aResult;
  163. }
  164. /**
  165. * Check transaction(s) in database which satisty all conditions.
  166. *
  167. * @param array $aConditions an array of pears('key' => 'value'). Available keys are the following:
  168. * a. license - internal license (string)
  169. * b. client_id - client's ID (integer)
  170. * c. seller_id - seller's ID (integer)
  171. * d. module_id - modules's where the purchased product is located. (integer)
  172. * e. item_id - item id in the database. (integer)
  173. * f. date - the date when the payment was processed(UNIXTIME STAMP)
  174. *
  175. * @return array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
  176. */
  177. public function serviceGetTransactionsInfo($aConditions)
  178. {
  179. return $this->_oDb->getOrderProcessed(array('type' => 'mixed', 'conditions' => $aConditions));
  180. }
  181. /**
  182. * Cart Processing Methods
  183. */
  184. public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount)
  185. {
  186. $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount);
  187. echoJson($aResult);
  188. }
  189. /**
  190. * Isn't used yet.
  191. */
  192. public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
  193. {
  194. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId);
  195. echoJson($aResult);
  196. }
  197. /**
  198. * Isn't used yet.
  199. */
  200. public function actionEmptyCart($iSellerId)
  201. {
  202. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId);
  203. echoJson($aResult);
  204. }
  205. public function actionSubscribe()
  206. {
  207. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  208. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  209. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  210. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  211. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  212. if(empty($iItemCount))
  213. $iItemCount = 1;
  214. $aResult = $this->getObjectCart()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount);
  215. if($aResult['code'] != 0)
  216. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  217. if(empty($aResult['redirect']) && !empty($aResult['message']))
  218. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  219. header('Location: ' . $aResult['redirect']);
  220. exit;
  221. }
  222. public function actionSubscribeJson()
  223. {
  224. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  225. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  226. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  227. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  228. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  229. if(empty($iItemCount))
  230. $iItemCount = 1;
  231. $aResult = $this->getObjectCart()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount);
  232. echoJson($aResult);
  233. }
  234. public function actionSubscriptionGetDetails($iId)
  235. {
  236. $aResult = $this->_oTemplate->displaySubscriptionGetDetails($iId);
  237. echoJson($aResult);
  238. }
  239. public function actionSubscriptionGetBilling($iId)
  240. {
  241. $aResult = $this->_oTemplate->displaySubscriptionGetBilling($iId);
  242. echoJson($aResult);
  243. }
  244. public function actionSubscriptionChangeBilling($iId)
  245. {
  246. $aResult = $this->_oTemplate->displaySubscriptionChangeBilling($iId);
  247. echoJson($aResult);
  248. }
  249. public function actionSubscriptionCancelation($iId)
  250. {
  251. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  252. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  253. if(empty($aPending) && !is_array($aPending))
  254. return echoJson($aResult);
  255. $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
  256. if(empty($aSubscription) && !is_array($aSubscription))
  257. return echoJson($aResult);
  258. $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
  259. if(!$oRecipient)
  260. return echoJson($aResult);
  261. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
  262. 'sibscription_id' => $aSubscription['subscription_id'],
  263. 'sibscription_customer' => $aSubscription['customer_id'],
  264. 'sibscription_date' => bx_time_js($aSubscription['date'], BX_FORMAT_DATE, true)
  265. ), 0, (int)$aPending['client_id']);
  266. if(!sendMail($oRecipient->getAccountObject()->getEmail(), $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  267. return echoJson($aResult);
  268. echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
  269. }
  270. /**
  271. * Payment Processing Methods
  272. */
  273. public function actionInitializeCheckout($sType)
  274. {
  275. if(!$this->isLogged())
  276. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  277. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  278. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  279. $sProvider = bx_process_input(bx_get('provider'));
  280. $aItems = bx_process_input(bx_get('items'));
  281. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  282. if($mixedResult !== true)
  283. return $this->_oTemplate->displayPageCodeError($mixedResult);
  284. }
  285. header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
  286. exit;
  287. }
  288. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array())
  289. {
  290. if(!is_array($aItems))
  291. $aItems = array($aItems);
  292. $iSellerId = (int)$iSellerId;
  293. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  294. return $this->_sLangsPrefix . 'err_unknown_vendor';
  295. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  296. if($oProvider === false || !$oProvider->isActive())
  297. return $this->_sLangsPrefix . 'err_incorrect_provider';
  298. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  299. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  300. return $this->_sLangsPrefix . 'err_empty_order';
  301. /*
  302. * Process FREE (price = 0) items for LOGGED IN members
  303. * WITHOUT processing via payment provider.
  304. */
  305. $bProcessedFree = false;
  306. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  307. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  308. foreach($aInfo['items'] as $iIndex => $aItem)
  309. if((int)$aInfo['client_id'] != 0 && (float)$aItem[$sKeyPriceSingle] == 0 && (float)$aItem[$sKeyPriceRecurring] == 0) {
  310. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense()));
  311. if(is_array($aItemInfo) && !empty($aItemInfo))
  312. $bProcessedFree = true;
  313. $aInfo['items_count'] -= 1;
  314. unset($aInfo['items'][$iIndex]);
  315. $sCartItems = $this->_oDb->getCartItems($aInfo['client_id']);
  316. $sCartItems = trim(preg_replace("'" . $this->_oConfig->descriptorA2S(array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity'])) . ":?'", "", $sCartItems), ":");
  317. $this->_oDb->setCartItems($aInfo['client_id'], $sCartItems);
  318. }
  319. if(empty($aInfo['items']))
  320. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  321. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo);
  322. if(empty($iPendingId))
  323. return $this->_sLangsPrefix . 'err_access_db';
  324. /*
  325. * Perform Join WITHOUT processing via payment provider
  326. * if a client ISN'T logged in and has only ONE FREE item in the card.
  327. */
  328. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  329. reset($aInfo['items']);
  330. $aItem = current($aInfo['items']);
  331. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  332. $this->_oDb->updateOrderPending($iPendingId, array(
  333. 'order' => $this->_oConfig->getLicense(),
  334. 'error_code' => '1',
  335. 'error_msg' => ''
  336. ));
  337. $this->getObjectJoin()->performJoin($iPendingId);
  338. }
  339. }
  340. return $oProvider->initializeCheckout($iPendingId, $aInfo);
  341. }
  342. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  343. {
  344. $aData = &$_REQUEST;
  345. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  346. if($oProvider === false || !$oProvider->isActive())
  347. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  348. $aResult = $oProvider->finalizeCheckout($aData);
  349. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  350. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  351. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  352. if($aPending['type'] == BX_PAYMENT_TYPE_RECURRING)
  353. $this->registerSubscription($aPending, array(
  354. 'customer_id' => $aResult['customer_id'],
  355. 'subscription_id' => $aResult['subscription_id']
  356. ));
  357. $this->onPaymentRegisterBefore($aPending);
  358. //--- Check "Pay Before Join" situation
  359. if((int)$aPending['client_id'] == 0)
  360. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  361. //--- Register payment for purchased items in associated modules
  362. if(!empty($aResult['paid']))
  363. $this->registerPayment($aPending);
  364. if($oProvider->needRedirect()) {
  365. header('Location: ' . $oProvider->getReturnUrl());
  366. exit;
  367. }
  368. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  369. }
  370. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  371. {
  372. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  373. if($oProvider === false || !$oProvider->isActive())
  374. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  375. $aResult = $oProvider->finalizedCheckout();
  376. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  377. }
  378. public function actionNotify($sProvider, $mixedVendorId = "")
  379. {
  380. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  381. if($oProvider === false || !$oProvider->isActive())
  382. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  383. $oProvider->notify();
  384. }
  385. public function onProfileJoin($iProfileId)
  386. {
  387. $this->getObjectJoin()->onProfileJoin($iProfileId);
  388. }
  389. public function onProfileDelete($iProfileId)
  390. {
  391. $this->_oDb->onProfileDelete($iProfileId);
  392. }
  393. public function isAllowedSell($aItem, $bPerform = false)
  394. {
  395. $iUserId = (int)$this->getProfileId();
  396. if(!$iUserId)
  397. return false;
  398. $aItemInfo = $this->callGetCartItem($aItem['module_id'], array($aItem['item_id']));
  399. if(empty($aItemInfo))
  400. return false;
  401. if(isAdmin())
  402. return true;
  403. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  404. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  405. return true;
  406. return $aCheckResult[CHECK_ACTION_MESSAGE];
  407. }
  408. public function registerPayment($mixedPending)
  409. {
  410. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  411. if(empty($aPending) || !is_array($aPending))
  412. return false;
  413. $sType = $aPending['type'];
  414. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  415. if($bTypeSingle && (int)$aPending['processed'] == 1)
  416. return true;
  417. $iClientId = (int)$aPending['client_id'];
  418. $sLicense = $this->_oConfig->getLicense();
  419. $sCartItems = '';
  420. if($bTypeSingle)
  421. $sCartItems = $this->_oDb->getCartItems($iClientId);
  422. $bResult = false;
  423. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  424. foreach($aItems as $aItem) {
  425. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  426. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense));
  427. if(empty($aItemInfo) || !is_array($aItemInfo))
  428. continue;
  429. $this->_oDb->insertOrderProcessed(array(
  430. 'pending_id' => $aPending['id'],
  431. 'client_id' => $aPending['client_id'],
  432. 'seller_id' => $aPending['seller_id'],
  433. 'module_id' => $aItem['module_id'],
  434. 'item_id' => $aItem['item_id'],
  435. 'item_count' => $aItem['item_count'],
  436. 'amount' => $aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  437. 'license' => $sLicense,
  438. ));
  439. if($bTypeSingle)
  440. $sCartItems = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $sCartItems), ":");
  441. $bResult = true;
  442. }
  443. if($bTypeSingle)
  444. $this->_oDb->setCartItems($iClientId, $sCartItems);
  445. if($bResult) {
  446. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  447. $this->onPaymentRegister($aPending);
  448. }
  449. return $bResult;
  450. }
  451. public function refundPayment($mixedPending)
  452. {
  453. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  454. if(empty($aPending) || !is_array($aPending))
  455. return false;
  456. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  457. $iCanceled = 0;
  458. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  459. foreach($aOrders as $aOrder) {
  460. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  461. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  462. if(!$bResult)
  463. continue;
  464. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  465. $iCanceled++;
  466. }
  467. if($iCanceled != count($aOrders))
  468. return false;
  469. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  470. if($bResult)
  471. $this->onPaymentRefund($aPending);
  472. return $bResult;
  473. }
  474. public function registerSubscription($aPending, $aParams = array())
  475. {
  476. $this->_oDb->insertSubscription(array(
  477. 'pending_id' => $aPending['id'],
  478. 'customer_id' => $aParams['customer_id'],
  479. 'subscription_id' => $aParams['subscription_id']
  480. ));
  481. $this->onSubscriptionCreate($aPending);
  482. }
  483. public function cancelSubscription($mixedPending)
  484. {
  485. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  486. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING)
  487. return false;
  488. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  489. foreach($aItems as $aItem)
  490. $this->callCancelSubscriptionItem((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order']));
  491. $this->onSubscriptionCancel($aPending);
  492. }
  493. public function onPaymentRegisterBefore($aPending, $aResult = array())
  494. {
  495. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  496. bx_alert('system', 'before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  497. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  498. }
  499. public function onPaymentRegister($aPending, $aResult = array())
  500. {
  501. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  502. bx_alert('system', 'register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  503. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  504. }
  505. public function onPaymentRefund($aPending, $aResult = array())
  506. {
  507. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  508. bx_alert('system', 'refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  509. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  510. }
  511. public function onSubscriptionCreate($aPending, $aResult = array())
  512. {
  513. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  514. bx_alert('system', 'create_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  515. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  516. }
  517. public function onSubscriptionCancel($aPending, $aResult = array())
  518. {
  519. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  520. bx_alert('system', 'cancel_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  521. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  522. }
  523. public function setSiteSubmenu($sSubmenu, $sSelected)
  524. {
  525. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  526. if(!$oSiteSubmenu)
  527. return;
  528. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  529. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  530. if(!$oModuleSubmenu)
  531. return;
  532. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu, array('title' => _t('_bx_payment'), 'link' => '','icon' => ''));
  533. $oModuleSubmenu->setSelected($this->_aModule['name'], $sSelected);
  534. }
  535. }
  536. /** @} */