BxPaymentOrders.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 BxPaymentOrders extends BxBaseModPaymentOrders
  12. {
  13. protected $_sLangsPrefix;
  14. function __construct()
  15. {
  16. $this->MODULE = 'bx_payment';
  17. parent::__construct();
  18. $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
  19. }
  20. /*
  21. * Service methods
  22. */
  23. /**
  24. * @page service Service Calls
  25. * @section bx_payment Payment
  26. * @subsection bx_payment-page_blocks Page Blocks
  27. * @subsubsection bx_payment-get_block_orders get_block_orders
  28. *
  29. * @code bx_srv('bx_payment', 'get_block_orders', [...], 'Orders'); @endcode
  30. *
  31. * Get page block with a list of orders represented as table.
  32. *
  33. * @param $sType string value with order type (processed or pending).
  34. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  35. *
  36. * @see BxPaymentOrders::serviceGetBlockOrders
  37. */
  38. /**
  39. * @ref bx_payment-get_block_orders "get_block_orders"
  40. */
  41. public function serviceGetBlockOrders($sType = '', $iUserId = BX_PAYMENT_EMPTY_ID)
  42. {
  43. if(empty($sType) && bx_get('type') !== false)
  44. $sType = bx_process_input(bx_get('type'));
  45. if(empty($sType) || !in_array($sType, array(BX_PAYMENT_ORDERS_TYPE_PENDING, BX_PAYMENT_ORDERS_TYPE_PROCESSED)))
  46. $sType = BX_PAYMENT_ORDERS_TYPE_PROCESSED;
  47. if(!$this->_oModule->isLogged())
  48. return ($sMsg = _t($this->_sLangsPrefix . 'err_required_login')) && $this->_bIsApi ? [bx_api_get_msg ($sMsg)] : [
  49. 'content' => MsgBox($sMsg)
  50. ];
  51. $iUserId = $iUserId != BX_PAYMENT_EMPTY_ID ? $iUserId : $this->_oModule->getProfileId();
  52. if($sType == BX_PAYMENT_ORDERS_TYPE_PROCESSED)
  53. $this->_oModule->_oDb->updateOrdersProcessed(array('new' => 0), array('seller_id' => $iUserId, 'new' => 1));
  54. $this->_oModule->setSiteSubmenu('menu_dashboard', 'system', 'dashboard-orders');
  55. $sBlockTitle = _t($this->_sLangsPrefix . 'page_block_title_orders_' . $sType);
  56. $sBlockContent = $this->_oModule->_oTemplate->displayBlockOrders($sType, $iUserId);
  57. $sBlockSubmenu = $this->_oModule->_oConfig->getObject('menu_orders_submenu');
  58. $oBlockSubmenu = BxDolMenu::getObjectInstance($sBlockSubmenu);
  59. if($oBlockSubmenu)
  60. $oBlockSubmenu->setSelected($this->MODULE, 'orders-' . $sType);
  61. return $this->_bIsApi ? [
  62. 'title' => $sBlockTitle,
  63. 'content' => !$sBlockContent ? [bx_api_get_msg(_t($this->_sLangsPrefix . 'msg_no_results'))] : [bx_api_get_block('grid', $sBlockContent)],
  64. 'menu' => $oBlockSubmenu->getCodeAPI()
  65. ] : [
  66. 'title' => $sBlockTitle,
  67. 'content' => !$sBlockContent ? MsgBox(_t($this->_sLangsPrefix . 'msg_no_results')) : $sBlockContent,
  68. 'menu' => $oBlockSubmenu
  69. ];
  70. }
  71. /**
  72. * @page service Service Calls
  73. * @section bx_payment Payment
  74. * @subsection bx_payment-other Other
  75. * @subsubsection bx_payment-get_orders_info get_orders_info
  76. *
  77. * @code bx_srv('bx_payment', 'get_orders_info', [...], 'Orders'); @endcode
  78. *
  79. * Get transaction(s) which meets all requirements.
  80. *
  81. * @param $aConditions an array of pears('key' => 'value'). Available keys are the following:
  82. * a. license - internal license (string)
  83. * b. client_id - client's ID (integer)
  84. * c. seller_id - seller's ID (integer)
  85. * d. module_id - modules's where the purchased product is located. (integer)
  86. * e. item_id - item id in the database. (integer)
  87. * f. date - the date when the payment was processed(UNIXTIME STAMP)
  88. * @return an array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
  89. *
  90. * @see BxPaymentOrders::serviceGetOrdersInfo
  91. */
  92. /**
  93. * @ref bx_payment-get_orders_info "get_orders_info"
  94. */
  95. public function serviceGetOrdersInfo($aConditions)
  96. {
  97. if(empty($aConditions) || !is_array($aConditions))
  98. return array();
  99. return $this->_oModule->_oDb->getOrderProcessed(array('type' => 'mixed', 'conditions' => $aConditions));
  100. }
  101. /**
  102. * @page service Service Calls
  103. * @section bx_payment Payment
  104. * @subsection bx_payment-other Other
  105. * @subsubsection bx_payment-get_pending_orders_info get_pending_orders_info
  106. *
  107. * @code bx_srv('bx_payment', 'get_pending_orders_info', [...], 'Orders'); @endcode
  108. *
  109. * Get pending transaction(s) which meets all requirements.
  110. *
  111. * @param $aConditions an array of pears('key' => 'value'). The most useful keys are the following:
  112. * a. client_id - client's ID (integer)
  113. * b. seller_id - seller's ID (integer)
  114. * c. type - transaction type: single or recurring (string)
  115. * d. amount - transaction amount (float)
  116. * e. order - order ID received from payment provider (string)
  117. * f. provider - payment provider name (string)
  118. * g. date - the date when the payment was established(UNIXTIME STAMP)
  119. * h. processed - whether the payment was processed or not (integer, 0 or 1)
  120. * @return an array of pending transactions. Each transaction has full info(client ID, seller ID, type, date and so on)
  121. *
  122. * @see BxPaymentOrders::serviceGetPendingOrdersInfo
  123. */
  124. /**
  125. * @ref bx_payment-get_pending_orders_info "get_pending_orders_info"
  126. */
  127. public function serviceGetPendingOrdersInfo($aConditions)
  128. {
  129. if(empty($aConditions) || !is_array($aConditions))
  130. return array();
  131. return $this->_oModule->_oDb->getOrderPending(array('type' => 'mixed', 'conditions' => $aConditions));
  132. }
  133. /**
  134. * @page service Service Calls
  135. * @section bx_payment Payment
  136. * @subsection bx_payment-other Other
  137. * @subsubsection bx_payment-get_transactions_after get_transactions_after
  138. *
  139. * @code bx_srv('bx_payment', 'get_transactions_after', [0, 500], 'Orders'); @endcode
  140. *
  141. * Get processed transactions a page at a time, in the order they were registered, to read
  142. * the whole payment history in steps. Pass the id of the last transaction of the previous
  143. * page to get the next one.
  144. *
  145. * @param $iAfterId integer value, the transactions with a greater ID are returned (0 for the first page).
  146. * @param $iLimit integer value, transactions per page (at most 5000).
  147. * @return an array of transactions ordered by ID. Each has the transaction's ID, pending_id, client_id, seller_id, author_id, module_id, item_id, item_count, amount, currency and date, and its order's type, provider, order, pending_currency, pending_amount and data (the serialized currency snapshot).
  148. *
  149. * @see BxPaymentOrders::serviceGetTransactionsAfter
  150. */
  151. /**
  152. * @ref bx_payment-get_transactions_after "get_transactions_after"
  153. */
  154. public function serviceGetTransactionsAfter($iAfterId = 0, $iLimit = 500)
  155. {
  156. return $this->_oModule->_oDb->getTransactionsAfter((int)$iAfterId, (int)$iLimit);
  157. }
  158. public function serviceProcessOrder($iSellerId, $iClientId, $iModuleId, $aItems, $sType, $sOrder)
  159. {
  160. $mixedResult = $this->addOrder(array(
  161. 'client_id' => $iClientId,
  162. 'seller_id' => $iSellerId,
  163. 'provider' => 'manual',
  164. 'type' => $sType,
  165. 'order' => $sOrder,
  166. 'error_code' => 0,
  167. 'error_msg' => 'Manually processed',
  168. 'module_id' => $iModuleId,
  169. 'items' => $aItems
  170. ), true);
  171. if(is_array($mixedResult))
  172. return false;
  173. return $this->_oModule->registerPayment((int)$mixedResult);
  174. }
  175. public function serviceProcessOrderByPending($iPendingId, $sOrder)
  176. {
  177. $this->_oModule->_oDb->updateOrderPending($iPendingId, array(
  178. 'order' => $sOrder,
  179. 'error_code' => 0,
  180. 'error_msg' => 'Manually processed'
  181. ));
  182. return $this->_oModule->registerPayment($iPendingId);
  183. }
  184. public function serviceRefundOrder($sOrder)
  185. {
  186. $aPending = $this->_oModule->_oDb->getOrderPending(['type' => 'order', 'order' => $sOrder]);
  187. if(empty($aPending) || !is_array($aPending))
  188. return false;
  189. return $this->_oModule->refundPayment($aPending);
  190. }
  191. public function addOrder($aData, $bForce = false)
  192. {
  193. $iSellerId = isset($aData['seller_id']) ? (int)$aData['seller_id'] : $this->_oModule->getProfileId();
  194. if($iSellerId == $aData['client_id'])
  195. return ['msg' => $this->_sLangsPrefix . 'err_self_purchase'];
  196. $sOrder = trim($aData['order']);
  197. if(empty($sOrder))
  198. return ['msg' => $this->_sLangsPrefix . 'form_processed_input_order_err'];
  199. $aItems = [];
  200. $fItemsPrice = 0;
  201. foreach($aData['items'] as $aItem) {
  202. if(!$bForce && !$this->_oModule->isAllowedSell(['module_id' => $aData['module_id'], 'item_id' => $aItem['id']], true))
  203. continue;
  204. $aItems[] = [
  205. 'author_id' => $iSellerId,
  206. 'module_id' => $aData['module_id'],
  207. 'id' => $aItem['id'],
  208. 'quantity' => $aItem['quantity']
  209. ];
  210. $fItemsPrice += $this->_oModule->_oConfig->getPrice($aData['type'], $aItem) * $aItem['quantity'];
  211. }
  212. $iPendingId = $this->_oModule->_oDb->insertOrderPending($aData['client_id'], $aData['type'], $aData['provider'], [
  213. 'vendor_id' => $iSellerId,
  214. 'vendor_currency_code' => $this->_oModule->getVendorCurrencyCode($iSellerId),
  215. 'items_price' => $fItemsPrice,
  216. 'items' => $aItems
  217. ]);
  218. $this->_oModule->_oDb->updateOrderPending($iPendingId, [
  219. 'order' => $sOrder,
  220. 'error_code' => $aData['error_code'],
  221. 'error_msg' => $aData['error_msg']
  222. ]);
  223. return (int)$iPendingId;
  224. }
  225. public function getOrder($sType, $iId)
  226. {
  227. return $this->_oModule->_oTemplate->displayOrder($sType, $iId);
  228. }
  229. public function cancel($sType, $iOrderId)
  230. {
  231. $sMethodName = 'getOrder' . bx_gen_method_name($sType);
  232. $aOrder = $this->_oModule->_oDb->$sMethodName(array('type' => 'id', 'id' => $iOrderId));
  233. if(empty($aOrder) || !is_array($aOrder))
  234. return true;
  235. if($sType == BX_PAYMENT_ORDERS_TYPE_PROCESSED && !$this->_oModule->callUnregisterCartItem((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aOrder['order'], $aOrder['license'])))
  236. return false;
  237. $sMethodName = 'deleteOrder' . bx_gen_method_name($sType);
  238. if(!$this->_oModule->_oDb->$sMethodName($iOrderId))
  239. return false;
  240. $this->_oModule->alert('cancel_order', $aOrder['id'], $aOrder['seller_id'], array(
  241. 'type' => $sType,
  242. 'order' => $aOrder
  243. ));
  244. return true;
  245. }
  246. }
  247. /** @} */