BxPaymentCart.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. class BxPaymentCart extends BxBaseModPaymentCart
  12. {
  13. function __construct()
  14. {
  15. $this->MODULE = 'bx_payment';
  16. parent::__construct();
  17. }
  18. /*
  19. * Service methods
  20. */
  21. public function serviceGetBlockCarts()
  22. {
  23. $CNF = &$this->_oModule->_oConfig->CNF;
  24. if(bx_get('seller_id') !== false)
  25. return '';
  26. $iUserId = $this->_oModule->getProfileId();
  27. if(empty($iUserId))
  28. return MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']));
  29. $this->_oModule->setSiteSubmenu('menu_cart_submenu', 'cart');
  30. return array(
  31. 'content' => $this->_oModule->_oTemplate->displayBlockCarts($iUserId)
  32. );
  33. }
  34. public function serviceGetBlockCart()
  35. {
  36. // Don't show the block at all if 'seller_id' not exists.
  37. if(bx_get('seller_id') === false)
  38. return '';
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. $iUserId = $this->_oModule->getProfileId();
  41. if(empty($iUserId))
  42. return MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']));
  43. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  44. if(empty($iSellerId))
  45. return MsgBox(_t($CNF['T']['ERR_UNKNOWN_VENDOR']));
  46. $this->_oModule->setSiteSubmenu('menu_cart_submenu', 'cart');
  47. $aSeller = $this->_oModule->getProfileInfo();
  48. return array(
  49. 'title' => _t($CNF['T']['BLOCK_TITLE_CART'], $aSeller['name']),
  50. 'content' => $this->_oModule->_oTemplate->displayBlockCart($iUserId, $iSellerId)
  51. );
  52. }
  53. public function serviceGetBlockCartHistory()
  54. {
  55. $CNF = &$this->_oModule->_oConfig->CNF;
  56. $iSellerId = bx_get('vendor') !== false ? (int)bx_get('vendor') : 0;
  57. $iUserId = $this->_oModule->getProfileId();
  58. if(empty($iUserId))
  59. return MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']));
  60. $this->_oModule->setSiteSubmenu('menu_cart_submenu', 'cart-history');
  61. return array(
  62. 'content' => $this->_oModule->_oTemplate->displayBlockHistory($iUserId, $iSellerId),
  63. );
  64. }
  65. public function serviceAddToCart($iSellerId, $mixedModuleId, $iItemId, $iItemCount)
  66. {
  67. $CNF = &$this->_oModule->_oConfig->CNF;
  68. $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
  69. $iClientId = $this->_oModule->getProfileId();
  70. $mixedResult = $this->_checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount);
  71. if($mixedResult !== true)
  72. return $mixedResult;
  73. $aSellerProviders = $this->_oModule->_oDb->getVendorInfoProvidersSingle($iSellerId);
  74. if(empty($aSellerProviders))
  75. return array('code' => 5, 'message' => _t($CNF['T']['ERR_NOT_ACCEPT_PAYMENTS']));
  76. $sCartItem = $this->_oModule->_oConfig->descriptorA2S(array($iSellerId, $iModuleId, $iItemId, $iItemCount));
  77. $sCartItems = $this->_oModule->_oDb->getCartItems($iClientId);
  78. if(strpos($sCartItems, $this->_oModule->_oConfig->descriptorA2S(array($iSellerId, $iModuleId, $iItemId))) !== false)
  79. $sCartItems = preg_replace_callback(
  80. "/" . $this->_oModule->_oConfig->descriptorA2S(array($iSellerId, $iModuleId, $iItemId, '([0-9])+')) . "/",
  81. create_function('$aMatches', 'return ' . $this->_oModule->_oConfig->descriptorA2S(array("'" . $iSellerId, $iModuleId, $iItemId, "' . (\$aMatches[1] + " . $iItemCount . ")")) . ';'),
  82. $sCartItems
  83. );
  84. else
  85. $sCartItems = empty($sCartItems) ? $sCartItem : $sCartItems . $this->_oModule->_oConfig->getDivider('DIVIDER_DESCRIPTORS') . $sCartItem;
  86. $this->_oModule->_oDb->setCartItems($iClientId, $sCartItems);
  87. $aInfo = $this->getInfo(BX_PAYMENT_TYPE_SINGLE, $iClientId);
  88. $iTotalQuantity = 0;
  89. foreach($aInfo as $aCart)
  90. $iTotalQuantity += $aCart['items_count'];
  91. return array(
  92. 'code' => 0,
  93. 'message' => _t($CNF['T']['MSG_ITEM_ADDED']),
  94. 'total_quantity' => $iTotalQuantity,
  95. //TODO: Update account submenu if it's needed.
  96. 'content' => '' //$this->_oModule->_oTemplate->displayToolbarSubmenu($aInfo)
  97. );
  98. }
  99. public function serviceDeleteFromCart($iSellerId, $iModuleId = 0, $iItemId = 0)
  100. {
  101. $CNF = &$this->_oModule->_oConfig->CNF;
  102. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  103. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  104. $iClientId = $this->_oModule->getProfileId();
  105. if(empty($iClientId))
  106. return array('code' => 2, 'message' => _t($CNF['T']['ERR_REQUIRED_LOGIN']));
  107. if(!empty($iModuleId) && !empty($iItemId))
  108. $sPattern = "'" . $iSellerId . "_" . $iModuleId . "_" . $iItemId . "_[0-9]+:?'";
  109. else
  110. $sPattern = "'" . $iSellerId . "_[0-9]+_[0-9]+_[0-9]+:?'";
  111. $sCartItems = $this->_oModule->_oDb->getCartItems($iClientId);
  112. $sCartItems = trim(preg_replace($sPattern, "", $sCartItems), ":");
  113. $this->_oModule->_oDb->setCartItems($iClientId, $sCartItems);
  114. return array('code' => 0, 'message' => _t($CNF['T']['MSG_ITEM_DELETED']));
  115. }
  116. public function serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount)
  117. {
  118. $CNF = &$this->_oModule->_oConfig->CNF;
  119. $iClientId = $this->_oModule->getProfileId();
  120. $mixedResult = $this->_checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount);
  121. if($mixedResult !== true)
  122. return $mixedResult;
  123. $aSellerProviders = $this->_oModule->_oDb->getVendorInfoProvidersRecurring($iSellerId);
  124. if(empty($aSellerProviders))
  125. return array('code' => 5, 'message' => _t($CNF['T']['ERR_NOT_ACCEPT_PAYMENTS']));
  126. $aCartItem = array($iSellerId, $iModuleId, $iItemId, $iItemCount);
  127. $sCartItem = $this->_oModule->_oConfig->descriptorA2S($aCartItem);
  128. if(empty($sSellerProvider)) {
  129. $sId = $this->_oModule->_oConfig->getHtmlIds('cart', 'providers_select') . BX_PAYMENT_TYPE_RECURRING;
  130. $sTitle = _t($CNF['T']['POPUP_PROVIDERS_SELECT']);
  131. return array('popup' => BxTemplStudioFunctions::getInstance()->popupBox($sId, $sTitle, $this->_oModule->_oTemplate->displayProvidersSelector($aCartItem, $aSellerProviders)));
  132. }
  133. $aProvider = $aSellerProviders[$sSellerProvider];
  134. $mixedResult = $this->_oModule->serviceInitializeCheckout(BX_PAYMENT_TYPE_RECURRING, $iSellerId, $aProvider['name'], array($sCartItem));
  135. if(is_string($mixedResult))
  136. return array('code' => 6, 'message' => _t($mixedResult));
  137. return $mixedResult;
  138. }
  139. public function getInfo($sType, $iUserId, $iSellerId = BX_PAYMENT_EMPTY_ID, $aItems = array())
  140. {
  141. if($iSellerId != BX_PAYMENT_EMPTY_ID && !empty($aItems))
  142. return $this->_getInfo($sType, $iUserId, $iSellerId, $this->_oModule->_oConfig->descriptorsM2A($aItems));
  143. $aContent = $this->_parseByVendor($iUserId);
  144. if($iSellerId != BX_PAYMENT_EMPTY_ID)
  145. return isset($aContent[$iSellerId]) ? $this->_getInfo($sType, $iUserId, $iSellerId, $aContent[$iSellerId]) : array();
  146. $aResult = array();
  147. foreach($aContent as $iSellerId => $aSellerItems)
  148. $aResult[$iSellerId] = $this->_getInfo($sType, $iUserId, $iSellerId, $aSellerItems);
  149. return $aResult;
  150. }
  151. /**
  152. * Enter description here...
  153. *
  154. * @param integer $iClientId client's ID
  155. * @param integer $iSellerId vendor's ID
  156. * @param array $aItems item descriptors(quaternions) from shopping cart.
  157. * @return array with full info about vendor and items.
  158. */
  159. protected function _getInfo($sType, $iClientId, $iSellerId, $aItems)
  160. {
  161. $iItemsCount = 0;
  162. $fItemsPrice = 0;
  163. $aItemsInfo = array();
  164. foreach($aItems as $aItem) {
  165. $aItemInfo = $this->_oModule->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id']));
  166. $aItemInfo['module_id'] = (int)$aItem['module_id'];
  167. $aItemInfo['quantity'] = (int)$aItem['item_count'];
  168. $iItemsCount += $aItem['item_count'];
  169. $fItemsPrice += $aItem['item_count'] * $this->_oModule->_oConfig->getPrice($sType, $aItemInfo);
  170. $aItemsInfo[] = $aItemInfo;
  171. }
  172. $aSeller = $this->_oModule->getVendorInfo((int)$iSellerId);
  173. return array(
  174. 'client_id' => $iClientId,
  175. 'vendor_id' => $aSeller['id'],
  176. 'vendor_name' => $aSeller['name'],
  177. 'vendor_link' => $aSeller['link'],
  178. 'vendor_icon' => $aSeller['icon'],
  179. 'vendor_unit' => $aSeller['unit'],
  180. 'vendor_currency_code' => $aSeller['currency_code'],
  181. 'vendor_currency_sign' => $aSeller['currency_sign'],
  182. 'items_count' => $iItemsCount,
  183. 'items_price' => $fItemsPrice,
  184. 'items' => $aItemsInfo
  185. );
  186. }
  187. protected function _checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount)
  188. {
  189. $CNF = &$this->_oModule->_oConfig->CNF;
  190. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  191. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  192. $iClientId = $this->_oModule->getProfileId();
  193. if(empty($iClientId)) {
  194. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  195. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  196. }
  197. if($iClientId == $iSellerId)
  198. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  199. $aSeller = $this->_oModule->getVendorInfo($iSellerId);
  200. if(!$aSeller['active'])
  201. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  202. return true;
  203. }
  204. }
  205. /** @} */