BxPaymentGridCart.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 BxPaymentGridCart extends BxBaseModPaymentGridCarts
  12. {
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. $this->MODULE = 'bx_payment';
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_bSelectAll = true;
  18. }
  19. public function getCodeAPI($bForceReturn = false)
  20. {
  21. $aData = parent::getCodeAPI($bForceReturn);
  22. $aData['settings']['field_id'] = 'id';
  23. foreach ($aData['data'] as &$aRow){
  24. $aRow['id'] = $aRow['checkbox']['data'];
  25. $aRow['title']['value'] = strip_tags($aRow['title']['value']);
  26. $aRow['price_single']['value'] = str_replace('&#36; ','$', $aRow['price_single']['value']);
  27. }
  28. return $aData;
  29. }
  30. public function performActionQuantitySub()
  31. {
  32. $sId = $this->_getItemId();
  33. list($iVendorId, $iModuleId, $iItemId, $iItemCount) = $this->_oModule->_oConfig->descriptorS2A($sId);
  34. if($iItemCount == 1)
  35. return echoJson([]);
  36. $aResult = $this->_oModule->getObjectCart()->serviceAddToCart($iVendorId, $iModuleId, $iItemId, -1);
  37. if($aResult['code'] != 0 && isset($aResult['message']))
  38. return echoJson(['msg' => $aResult['message']]);
  39. return echoJson(['grid' => $this->getCode(false), 'blink' => $sId]);
  40. }
  41. public function performActionQuantityAdd()
  42. {
  43. $sId = $this->_getItemId();
  44. list($iVendorId, $iModuleId, $iItemId, $iItemCount) = $this->_oModule->_oConfig->descriptorS2A($sId);
  45. $aResult = $this->_oModule->getObjectCart()->serviceAddToCart($iVendorId, $iModuleId, $iItemId, 1);
  46. if($aResult['code'] != 0 && isset($aResult['message']))
  47. return echoJson(['msg' => $aResult['message']]);
  48. return echoJson(['grid' => $this->getCode(false), 'blink' => $sId]);
  49. }
  50. public function performActionCheckout()
  51. {
  52. $aParams = array(
  53. 'seller_id' => bx_process_input(bx_get('seller_id'), BX_DATA_INT),
  54. 'provider' => bx_process_input(bx_get('provider')),
  55. 'items' => bx_process_input(bx_get('ids'))
  56. );
  57. if(empty($aParams['seller_id']) || empty($aParams['provider']))
  58. return $this->_bIsApi ? [] : echoJson([]);
  59. if(empty($aParams['items']) || !is_array($aParams['items']))
  60. return ($sMsg = _t('_bx_payment_err_nothing_selected')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  61. $oProvider = $this->_oModule->getObjectProvider($aParams['provider'], $aParams['seller_id']);
  62. if($oProvider !== false) {
  63. if(method_exists($oProvider, 'overwriteCheckoutParamsSingle') && ($aResults = $oProvider->overwriteCheckoutParamsSingle($aParams, $this)))
  64. return $this->_bIsApi ? $aResults : echoJson($aResults);
  65. if(method_exists($oProvider, 'getCheckoutParamsSingle'))
  66. $aParams = $oProvider->getCheckoutParamsSingle($aParams, $this);
  67. }
  68. if($this->_bIsApi) {
  69. $sItems = $this->_oModule->_oConfig->descriptorsA2S($aParams['items']);
  70. $sRedirect = $this->_oModule->_oConfig->urlEncode($this->_oModule->_oConfig->getUrl('URL_HISTORY'));
  71. return [
  72. 'url' => '/api.php?r=' . $this->MODULE . '/initialize_checkout_api&params[]=' . implode('&params[]=', [BX_PAYMENT_TYPE_SINGLE, $aParams['seller_id'], $aParams['provider'], $sItems, $sRedirect])
  73. ];
  74. }
  75. else
  76. echoJson([
  77. 'eval' => $this->_oModule->_oConfig->getJsObject('cart') . '.onCartCheckout(oData);',
  78. 'link' => bx_append_url_params($this->_oModule->_oConfig->getUrl('URL_CART_CHECKOUT'), $aParams)
  79. ]);
  80. }
  81. protected function _getCellTitle($mixedValue, $sKey, $aField, $aRow)
  82. {
  83. if($this->_bIsApi)
  84. return [
  85. 'type' => 'link',
  86. 'name' => $sKey,
  87. 'data' => [
  88. 'url' => bx_api_get_relative_url($aRow['url']),
  89. 'text' => $aRow['title']
  90. ]
  91. ];
  92. return parent::_getCellDefault($this->_oModule->_oTemplate->displayLink('link', [
  93. 'href' => $aRow['url'],
  94. 'title' => bx_html_attribute($aRow['title']),
  95. 'content' => $aRow['title']
  96. ]), $sKey, $aField, $aRow);
  97. }
  98. protected function _getCellPriceSingle($mixedValue, $sKey, $aField, $aRow)
  99. {
  100. return parent::_getCellDefault(_t_format_currency_ext($mixedValue, ['sign' => $this->_sCurrencySign]), $sKey, $aField, $aRow);
  101. }
  102. protected function _getCellQuantity($mixedValue, $sKey, $aField, $aRow)
  103. {
  104. if(!$this->_bIsApi)
  105. $mixedValue = $this->_oModule->_oTemplate->parseHtmlByName('cart_quantity.html', [
  106. 'sub' => $this->_getActionQuantity('sub', $aRow),
  107. 'quantity' => $mixedValue,
  108. 'add' => $this->_getActionQuantity('add', $aRow)
  109. ]);
  110. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  111. }
  112. protected function _getActionQuantitySub ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  113. {
  114. return '';
  115. }
  116. protected function _getActionQuantityAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  117. {
  118. return '';
  119. }
  120. protected function _getActionQuantity($sAction, &$aRow)
  121. {
  122. $sAction = 'quantity_' . $sAction;
  123. $aAction = array_merge($this->_aOptions['actions_single'][$sAction], ['attr' => [
  124. 'bx_grid_action_single' => $sAction,
  125. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  126. 'bx_grid_action_confirm' => $this->_aOptions['actions_single'][$sAction]['confirm'],
  127. 'bx_grid_action_reset_paginate' => 0
  128. ]]);
  129. return $this->_getActionDefault('single', $sAction, $aAction, false, false, $aRow);
  130. }
  131. protected function _getActionCheckout ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  132. {
  133. return '';
  134. }
  135. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  136. {
  137. $CNF = &$this->_oModule->_oConfig->CNF;
  138. $sActions = '';
  139. if($sType == 'bulk' && !empty($this->_aQueryAppend['seller_id'])) {
  140. $sActionName = 'checkout';
  141. $iClientId = (int)$this->_aQueryAppend['client_id'];
  142. $iSellerId = (int)$this->_aQueryAppend['seller_id'];
  143. $aCartInfo = $this->_oModule->getObjectCart()->getInfo(BX_PAYMENT_TYPE_SINGLE, $iClientId, $iSellerId);
  144. $bCreditsOnly = $this->_oModule->_oConfig->isCreditsOnly();
  145. $bPayForCredits = true;
  146. if(!empty($aCartInfo['items']) && is_array($aCartInfo['items']))
  147. foreach($aCartInfo['items'] as $aItem) {
  148. $aModule = $this->_oModule->_oDb->getModuleById((int)$aItem['module_id']);
  149. if($aModule['name'] != $CNF['MODULE_CREDITS']) {
  150. $bPayForCredits = false;
  151. break;
  152. }
  153. }
  154. $aProviders = $this->_oModule->_oDb->getVendorInfoProvidersSingle($iSellerId);
  155. foreach($aProviders as $aProvider) {
  156. $bProviderCredits = $aProvider['name'] == $CNF['OBJECT_PP_CREDITS'];
  157. /*
  158. * Hide all non-Credits payment providers when 'credits only' mode is enabled
  159. * and purchasing items aren't credits.
  160. */
  161. if($bCreditsOnly && !$bProviderCredits && !$bPayForCredits)
  162. continue;
  163. /*
  164. * Hide Credits payment provider when paying for credits.
  165. */
  166. if($bProviderCredits && $bPayForCredits)
  167. continue;
  168. $sAction = $this->_getActionDefault($sType, $sActionName, array(
  169. 'title'=> _t('_bx_payment_grid_action_title_crt_checkout', _t($CNF['T']['TXT_CART_PROVIDER'] . $aProvider['name'])),
  170. 'icon' => '',
  171. 'icon_only' => 0,
  172. 'confirm' => 0,
  173. 'attr' => array(
  174. 'bx_grid_action_' . $sType => $sActionName,
  175. 'bx_grid_action_append' => json_encode(array('provider' => $aProvider['name'])),
  176. 'bx_grid_action_confirm' => 0,
  177. 'bx_grid_action_reset_paginate' => 0,
  178. )
  179. ), $isSmall, $isDisabled, $aRow);
  180. $oProvider = $this->_oModule->getObjectProvider($aProvider['name'], $iSellerId);
  181. if($oProvider !== false && method_exists($oProvider, 'getButtonSingle')) {
  182. $aParams = array(
  183. 'sObjNameGrid' => $this->getObject(),
  184. 'aCartInfo' => $aCartInfo,
  185. 'sAction' => $sAction
  186. );
  187. if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
  188. $aTitles = array();
  189. foreach ($aCartInfo['items'] as $aItem)
  190. $aTitles[] = $aItem['title'];
  191. $aParams = array_merge($aParams, array(
  192. 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
  193. 'sItemTitle' => implode(', ', $aTitles)
  194. ));
  195. }
  196. $mixedButton = $oProvider->getButtonSingle($iClientId, $iSellerId, $aParams);
  197. if($mixedButton !== false)
  198. $sAction = $mixedButton;
  199. }
  200. $sActions .= $sAction;
  201. }
  202. }else{
  203. if (bx_is_api())
  204. return parent::_getActions ($sType, $sActionData, $isSmall, $isDisabled, $isPermanentState, $aRow);
  205. }
  206. return $sActions . parent::_getActions ($sType, $sActionData, $isSmall, $isDisabled, $isPermanentState, $aRow);
  207. }
  208. protected function _getActionsAPI($sType)
  209. {
  210. if($sType != 'bulk' || empty($this->_aQueryAppend['seller_id']))
  211. return parent::_getActionsAPI($sType);
  212. $CNF = &$this->_oModule->_oConfig->CNF;
  213. $iClientId = (int)$this->_aQueryAppend['client_id'];
  214. $iSellerId = (int)$this->_aQueryAppend['seller_id'];
  215. $aCartInfo = $this->_oModule->getObjectCart()->getInfo(BX_PAYMENT_TYPE_SINGLE, $iClientId, $iSellerId);
  216. $bCreditsOnly = $this->_oModule->_oConfig->isCreditsOnly();
  217. $bPayForCredits = true;
  218. if(!empty($aCartInfo['items']) && is_array($aCartInfo['items']))
  219. foreach($aCartInfo['items'] as $aItem) {
  220. $aModule = $this->_oModule->_oDb->getModuleById((int)$aItem['module_id']);
  221. if($aModule['name'] != $CNF['MODULE_CREDITS']) {
  222. $bPayForCredits = false;
  223. break;
  224. }
  225. }
  226. $aActions = [];
  227. $aProviders = $this->_oModule->_oDb->getVendorInfoProvidersSingle($iSellerId);
  228. foreach($aProviders as $aProvider) {
  229. $bProviderCredits = $aProvider['name'] == $CNF['OBJECT_PP_CREDITS'];
  230. /*
  231. * Hide all non-Credits payment providers when 'credits only' mode is enabled
  232. * and purchasing items aren't credits.
  233. */
  234. if($bCreditsOnly && !$bProviderCredits && !$bPayForCredits)
  235. continue;
  236. /*
  237. * Hide Credits payment provider when paying for credits.
  238. */
  239. if($bProviderCredits && $bPayForCredits)
  240. continue;
  241. $aActions[$aProvider['name']] = [
  242. 'payment_type' => BX_PAYMENT_TYPE_SINGLE,
  243. 'name' => $aProvider['name'],
  244. 'title'=> _t('_bx_payment_grid_action_title_crt_checkout', _t($CNF['T']['TXT_CART_PROVIDER'] . $aProvider['name'])),
  245. 'icon' => '',
  246. 'icon_only' => 0,
  247. 'confirm' => 0,
  248. ];
  249. }
  250. return $aActions;
  251. }
  252. protected function _getDataArray($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  253. {
  254. if(empty($this->_aQueryAppend['client_id']) || empty($this->_aQueryAppend['seller_id']))
  255. return array();
  256. $aCart = $this->_oCart->getInfo(BX_PAYMENT_TYPE_SINGLE, $this->_aQueryAppend['client_id'], $this->_aQueryAppend['seller_id']);
  257. if(empty($aCart) || empty($aCart['items']) || !is_array($aCart['items']))
  258. return array();
  259. $this->_aOptions['source'] = array();
  260. foreach($aCart['items'] as $aCartItem) {
  261. $this->_aOptions['source'][] = array_merge($aCartItem, [
  262. 'descriptor' => $this->_oModule->_oConfig->descriptorA2S(array($aCartItem['author_id'], $aCartItem['module_id'], $aCartItem['id'], $aCartItem['quantity'])),
  263. 'description' => strip_tags($aCartItem['description']),
  264. 'currency_code' => $aCart['vendor_currency_code'],
  265. 'currency_sign' => $aCart['vendor_currency_sign']
  266. ]);
  267. }
  268. return parent::_getDataArray($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  269. }
  270. protected function _delete($mixedId)
  271. {
  272. list($iVendorId, $iModuleId, $iItemId) = $this->_oModule->_oConfig->descriptorS2A($mixedId);
  273. if(!$this->_oModule->isSingleSeller() && (int)$iVendorId != (int)$this->_aQueryAppend['seller_id'])
  274. return false;
  275. return $this->_oCart->serviceDeleteFromCart($iVendorId, $iModuleId, $iItemId);
  276. }
  277. protected function _getItemId()
  278. {
  279. if(($aIds = bx_get('ids')) && is_array($aIds))
  280. return array_shift($aIds);
  281. if(($sId = bx_get('id')))
  282. return $sId;
  283. return false;
  284. }
  285. }
  286. /** @} */