BxPaymentSubscriptions.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 BxPaymentSubscriptions extends BxBaseModPaymentSubscriptions
  12. {
  13. function __construct()
  14. {
  15. $this->MODULE = 'bx_payment';
  16. parent::__construct();
  17. }
  18. /*
  19. * Service methods
  20. */
  21. /**
  22. * @page service Service Calls
  23. * @section bx_payment Payment
  24. * @subsection bx_payment-page_blocks Page Blocks
  25. * @subsubsection bx_payment-get_block_list_my get_block_list_my
  26. *
  27. * @code bx_srv('bx_payment', 'get_block_list_my', [...], 'Subscriptions'); @endcode
  28. *
  29. * Get page block with a list of currently logged in member's subscriptions.
  30. *
  31. * @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.
  32. *
  33. * @see BxPaymentSubscriptions::serviceGetBlockListMy
  34. */
  35. /**
  36. * @ref bx_payment-get_block_list_my "get_block_list_my"
  37. */
  38. public function serviceGetBlockListMy()
  39. {
  40. return $this->_getBlock('list_my');
  41. }
  42. /**
  43. * @page service Service Calls
  44. * @section bx_payment Payment
  45. * @subsection bx_payment-page_blocks Page Blocks
  46. * @subsubsection bx_payment-get_block_list_all get_block_list_all
  47. *
  48. * @code bx_srv('bx_payment', 'get_block_list_all', [...], 'Subscriptions'); @endcode
  49. *
  50. * Get page block with a list of all subscriptions. It's available for authorized members only.
  51. *
  52. * @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.
  53. *
  54. * @see BxPaymentSubscriptions::serviceGetBlockListAll
  55. */
  56. /**
  57. * @ref bx_payment-get_block_list_all "get_block_list_all"
  58. */
  59. public function serviceGetBlockListAll()
  60. {
  61. return $this->_getBlock('list_all');
  62. }
  63. /**
  64. * @page service Service Calls
  65. * @section bx_payment Payment
  66. * @subsection bx_payment-page_blocks Page Blocks
  67. * @subsubsection bx_payment-get_block_history get_block_history
  68. *
  69. * @code bx_srv('bx_payment', 'get_block_history', [...], 'Subscriptions'); @endcode
  70. *
  71. * Get page block with a list of payments related to subscriptions of currently logged in member.
  72. *
  73. * @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.
  74. *
  75. * @see BxPaymentSubscriptions::serviceGetBlockHistory
  76. */
  77. /**
  78. * @ref bx_payment-get_block_history "get_block_history"
  79. */
  80. public function serviceGetBlockHistory()
  81. {
  82. return $this->_getBlock('history');
  83. }
  84. /**
  85. * @page service Service Calls
  86. * @section bx_payment Payment
  87. * @subsection bx_payment-purchase_processing Purchase Processing
  88. * @subsubsection bx_payment-subscribe subscribe
  89. *
  90. * @code bx_srv('bx_payment', 'subscribe', [...], 'Subscriptions'); @endcode
  91. *
  92. * Initialize subscription for specified item.
  93. *
  94. * @param $iSellerId integer value with seller ID.
  95. * @param $sSellerProvider string value with a name of payment provider to be used for processing. Empty value means that payment provider selector should be shown.
  96. * @param $iModuleId integer value with module ID.
  97. * @param $iItemId integer value with item ID.
  98. * @param $iItemCount integer value with a number of items for purchasing. It's equal to 1 in case of subscription.
  99. * @param $sRedirect (optional) string value with redirect URL, if it's needed.
  100. * @return an array with special format which describes the result of operation.
  101. *
  102. * @see BxPaymentSubscriptions::serviceSubscribe
  103. */
  104. /**
  105. * @ref bx_payment-subscribe "subscribe"
  106. */
  107. public function serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect = '')
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. $iClientId = $this->_oModule->getProfileId();
  111. $mixedResult = $this->_oModule->checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount);
  112. if($mixedResult !== true)
  113. return $mixedResult;
  114. $aSellerProviders = $this->_oModule->_oDb->getVendorInfoProvidersRecurring($iSellerId);
  115. if(empty($aSellerProviders))
  116. return array('code' => 5, 'message' => _t($CNF['T']['ERR_NOT_ACCEPT_PAYMENTS']));
  117. $aCartItem = array($iSellerId, $iModuleId, $iItemId, $iItemCount);
  118. $sCartItem = $this->_oModule->_oConfig->descriptorA2S($aCartItem);
  119. if(empty($sSellerProvider)) {
  120. $sId = $this->_oModule->_oConfig->getHtmlIds('cart', 'providers_select') . BX_PAYMENT_TYPE_RECURRING;
  121. $sTitle = _t($CNF['T']['POPUP_PROVIDERS_SELECT']);
  122. return array('popup' => array(
  123. 'html' => BxTemplStudioFunctions::getInstance()->popupBox($sId, $sTitle, $this->_oModule->_oTemplate->displayProvidersSelector($aCartItem, $aSellerProviders, $sRedirect)),
  124. 'options' => array('closeOnOuterClick' => true)
  125. ));
  126. }
  127. $aProvider = $aSellerProviders[$sSellerProvider];
  128. $mixedResult = $this->_oModule->serviceInitializeCheckout(BX_PAYMENT_TYPE_RECURRING, $iSellerId, $aProvider['name'], array($sCartItem), $sRedirect);
  129. if(is_string($mixedResult))
  130. return array('code' => 6, 'message' => _t($mixedResult));
  131. return $mixedResult;
  132. }
  133. public function cancel($iPendingId)
  134. {
  135. $aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iPendingId));
  136. if(empty($aSubscription) || !is_array($aSubscription))
  137. return true;
  138. $aOrder = $this->_oModule->_oDb->getOrderSubscription(array('type' => 'id', 'id' => $iPendingId));
  139. if(empty($aOrder) || !is_array($aOrder))
  140. return false;
  141. $iSellerId = (int)$aOrder['seller_id'];
  142. $oProvider = $this->_oModule->getObjectProvider($aOrder['provider'], $iSellerId);
  143. if($oProvider === false || !$oProvider->isActive())
  144. return false;
  145. if(!$oProvider->cancelRecurring($iPendingId, $aSubscription['customer_id'], $aSubscription['subscription_id']))
  146. return false;
  147. list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $this->_oModule->_oConfig->descriptorS2A($aOrder['items']);
  148. if(!$this->_oModule->callCancelSubscriptionItem((int)$iModuleId, array($aOrder['client_id'], $iSellerId, $iItemId, $iItemCount, $aOrder['order'])))
  149. return false;
  150. if(!$this->_oModule->_oDb->deleteSubscription($aSubscription['id'], 'cancel'))
  151. return false;
  152. return true;
  153. }
  154. protected function _getBlock($sType)
  155. {
  156. $CNF = &$this->_oModule->_oConfig->CNF;
  157. $sMethod = 'displayBlockSbs' . bx_gen_method_name($sType);
  158. if(!method_exists($this->_oModule->_oTemplate, $sMethod))
  159. return array(
  160. 'content' => MsgBox(_t('_Empty'))
  161. );
  162. $iUserId = $this->_oModule->getProfileId();
  163. if(empty($iUserId))
  164. return array(
  165. 'content' => MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']))
  166. );
  167. $this->_oModule->setSiteSubmenu('menu_dashboard', 'system', 'dashboard-subscriptions');
  168. $sBlockSubmenu = $this->_oModule->_oConfig->getObject('menu_sbs_submenu');
  169. $oBlockSubmenu = BxDolMenu::getObjectInstance($sBlockSubmenu);
  170. if($oBlockSubmenu)
  171. $oBlockSubmenu->setSelected($this->MODULE, 'sbs-' . str_replace('_', '-', $sType));
  172. return array(
  173. 'content' => $this->_oModule->_oTemplate->$sMethod($iUserId),
  174. 'menu' => $this->_oModule->_oConfig->getObject('menu_sbs_submenu')
  175. );
  176. }
  177. }
  178. /** @} */