BxPaymentModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Payment Payment
  7. * @ingroup TridentModules
  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($iVendorId) - 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($iVendorId, $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($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($aItems);
  106. echoJson($aResult);
  107. }
  108. /**
  109. * Payment Details Methods
  110. */
  111. public function serviceGetOptionsSiteAdmin()
  112. {
  113. $aResult = array(
  114. array('key' => '', 'value' => _t('_Select_one'))
  115. );
  116. $aIds = $this->_oDb->getAdminsIds();
  117. foreach($aIds as $iId) {
  118. $aUser = $this->getProfileInfo($iId);
  119. $aResult[] = array(
  120. 'key' => $iId,
  121. 'value' => $aUser['name']
  122. );
  123. }
  124. return $aResult;
  125. }
  126. /**
  127. * Check transaction(s) in database which satisty all conditions.
  128. *
  129. * @param array $aConditions an array of pears('key' => 'value'). Available keys are the following:
  130. * a. order_id - internal order ID (string)
  131. * b. client_id - client's ID (integer)
  132. * c. seller_id - seller's ID (integer)
  133. * d. module_id - modules's where the purchased product is located. (integer)
  134. * e. item_id - item id in the database. (integer)
  135. * f. date - the date when the payment was processed(UNIXTIME STAMP)
  136. *
  137. * @return array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
  138. */
  139. public function serviceGetTransactionsInfo($aConditions)
  140. {
  141. return $this->_oDb->getOrderProcessed(array('type' => 'mixed', 'conditions' => $aConditions));
  142. }
  143. /**
  144. * Cart Processing Methods
  145. */
  146. public function actionCartSubmit()
  147. {
  148. if(!$this->isLogged())
  149. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  150. if(bx_get('bx-payment-delete') !== false && bx_get('items') !== false){
  151. $aItems = bx_get('items');
  152. foreach($aItems as $sItem) {
  153. list($iVendorId, $iModuleId, $iItemId, $iItemCount) = $this->_oConfig->descriptorS2A($sItem);
  154. $this->getObjectCart()->serviceDeleteFromCart($iVendorId, $iModuleId, $iItemId);
  155. }
  156. }
  157. else if(bx_get('bx-payment-checkout') !== false && bx_get('items') !== false) {
  158. $iVendorId = (int)bx_get('vendor_id');
  159. $sProvider = bx_process_input(bx_get('provider'));
  160. $aItems = bx_get('items');
  161. $sError = $this->serviceInitializeCheckout($iVendorId, $sProvider, $aItems);
  162. if(!empty($sError))
  163. return $this->_oTemplate->displayPageCodeError($sError, false);
  164. }
  165. header('Location: ' . $this->_oConfig->getUrl('cart'));
  166. exit;
  167. }
  168. public function actionAddToCart($iVendorId, $iModuleId, $iItemId, $iItemCount)
  169. {
  170. $aResult = $this->getObjectCart()->serviceAddToCart($iVendorId, $iModuleId, $iItemId, $iItemCount);
  171. echoJson($aResult);
  172. }
  173. /**
  174. * Isn't used yet.
  175. */
  176. public function actionDeleteFromCart($iVendorId, $iModuleId, $iItemId)
  177. {
  178. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iVendorId, $iModuleId, $iItemId);
  179. echoJson($aResult);
  180. }
  181. /**
  182. * Isn't used yet.
  183. */
  184. public function actionEmptyCart($iVendorId)
  185. {
  186. $aResult = $this->getObjectCart()->serviceDeleteFromCart($this->_iUserId, $iVendorId);
  187. echoJson($aResult);
  188. }
  189. /**
  190. * Payment Processing Methods
  191. */
  192. public function serviceInitializeCheckout($iVendorId, $sProvider, $aItems = array())
  193. {
  194. if(!is_array($aItems))
  195. $aItems = array($aItems);
  196. $iVendorId = (int)$iVendorId;
  197. if($iVendorId == BX_PAYMENT_EMPTY_ID)
  198. return MsgBox(_t($this->_sLangsPrefix . 'err_unknown_vendor'));
  199. $oProvider = $this->getObjectProvider($sProvider, $iVendorId);
  200. if($oProvider === false)
  201. return MsgBox(_t($this->_sLangsPrefix . 'err_incorrect_provider'));
  202. $aInfo = $this->getObjectCart()->getInfo($this->_iUserId, $iVendorId, $aItems);
  203. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  204. return MsgBox(_t($this->_sLangsPrefix . 'err_empty_order'));
  205. /*
  206. * Process FREE (price = 0) items for LOGGED IN members
  207. * WITHOUT processing via payment provider.
  208. */
  209. $bProcessedFree = false;
  210. foreach($aInfo['items'] as $iIndex => $aItem)
  211. if((int)$aInfo['client_id'] != 0 && (float)$aItem['price'] == 0) {
  212. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense()));
  213. if(is_array($aItemInfo) && !empty($aItemInfo))
  214. $bProcessedFree = true;
  215. $aInfo['items_count'] -= 1;
  216. unset($aInfo['items'][$iIndex]);
  217. $sCartItems = $this->_oDb->getCartItems($aInfo['client_id']);
  218. $sCartItems = trim(preg_replace("'" . $this->_oConfig->descriptorA2S(array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity'])) . ":?'", "", $sCartItems), ":");
  219. $this->_oDb->setCartItems($aInfo['client_id'], $sCartItems);
  220. }
  221. if(empty($aInfo['items']))
  222. return MsgBox(_t($this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order')));
  223. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sProvider, $aInfo);
  224. if(empty($iPendingId))
  225. return MsgBox(_t($this->_sLangsPrefix . 'err_access_db'));
  226. /*
  227. * Perform Join WITHOUT processing via payment provider
  228. * if a client ISN'T logged in and has only ONE FREE item in the card.
  229. */
  230. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  231. reset($aInfo['items']);
  232. $aItem = current($aInfo['items']);
  233. if(!empty($aItem) && $aItem['price'] == 0) {
  234. $this->_oDb->updateOrderPending($iPendingId, array(
  235. 'order' => $this->_oConfig->getLicense(),
  236. 'error_code' => '1',
  237. 'error_msg' => ''
  238. ));
  239. $this->getObjectJoin()->performJoin($iPendingId);
  240. }
  241. }
  242. $sError = $oProvider->initializeCheckout($iPendingId, $aInfo);
  243. if(!empty($sError))
  244. return MsgBox($sError);
  245. return true;
  246. }
  247. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  248. {
  249. $aData = &$_REQUEST;
  250. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  251. if($oProvider === false)
  252. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  253. $aResult = $oProvider->finalizeCheckout($aData);
  254. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  255. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  256. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  257. //--- Check "Pay Before Join" situation
  258. if((int)$aPending['client_id'] == 0)
  259. $this->getObjectJoin()->performJoin((int)$aPending['id'], $aResult);
  260. //--- Register payment for purchased items in associated modules
  261. $this->getObjectCart()->updateInfo($aPending);
  262. if($oProvider->needRedirect()) {
  263. header('Location: ' . $this->_oConfig->getUrl('return'));
  264. exit;
  265. }
  266. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  267. }
  268. public function actionCheckoutFinished($sProvider, $mixedVendorId = "")
  269. {
  270. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  271. if($oProvider === false)
  272. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  273. $aResult = $oProvider->checkoutFinished();
  274. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  275. exit;
  276. }
  277. public function onProfileJoin($iProfileId)
  278. {
  279. $this->getObjectJoin()->onProfileJoin($iProfileId);
  280. }
  281. public function onProfileDelete($iProfileId)
  282. {
  283. $this->_oDb->onProfileDelete($iProfileId);
  284. }
  285. public function isAllowedSell($aItem, $bPerform = false)
  286. {
  287. $iUserId = (int)$this->getProfileId();
  288. if(!$iUserId)
  289. return false;
  290. $aItemInfo = $this->callGetCartItem($aItem['module_id'], array($aItem['item_id']));
  291. if(empty($aItemInfo))
  292. return false;
  293. if(isAdmin())
  294. return true;
  295. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  296. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  297. return true;
  298. return $aCheckResult[CHECK_ACTION_MESSAGE];
  299. }
  300. }
  301. /** @} */