BxPaymentModule.php 25 KB

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