BxAclModule.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 PaidLevels Paid Levels
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_ACL_LICENSE_TYPE_SINGLE', 'single'); //--- one-time payment license
  13. define('BX_ACL_LICENSE_TYPE_RECURRING', 'recurring'); //--- recurring payment license
  14. class BxAclModule extends BxDolModule
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * ACTION METHODS
  26. */
  27. public function actionCheckName()
  28. {
  29. $CNF = &$this->_oConfig->CNF;
  30. $sName = bx_process_input(bx_get('name'));
  31. if(empty($sName))
  32. return echoJson(array());
  33. $sResult = '';
  34. $iId = (int)bx_get('id');
  35. if(!empty($iId)) {
  36. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  37. if(strcmp($sName, $aPrice[$CNF['FIELD_NAME']]) == 0)
  38. $sResult = $sName;
  39. }
  40. echoJson(array(
  41. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  42. ));
  43. }
  44. /**
  45. * SERVICE METHODS
  46. */
  47. public function serviceGetSafeServices()
  48. {
  49. return array (
  50. 'GetViewUrl' => '',
  51. 'GetBlockView' => '',
  52. 'GetMembershipActions' => '',
  53. );
  54. }
  55. /**
  56. * @page service Service Calls
  57. * @section bx_acl Paid Levels
  58. * @subsection bx_acl-other Other
  59. * @subsubsection bx_acl-get_view_url get_view_url
  60. *
  61. * @code bx_srv('bx_acl', 'get_view_url', [...]); @endcode
  62. *
  63. * Get page URL with membership levels list.
  64. *
  65. * @return string with page URL.
  66. *
  67. * @see BxAclModule::serviceGetViewUrl
  68. */
  69. /**
  70. * @ref bx_acl-get_view_url "get_view_url"
  71. */
  72. public function serviceGetViewUrl()
  73. {
  74. $CNF = &$this->_oConfig->CNF;
  75. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']));
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section bx_acl Paid Levels
  80. * @subsection bx_acl-page_blocks Page Blocks
  81. * @subsubsection bx_acl-get_block_view get_block_view
  82. *
  83. * @code bx_srv('bx_acl', 'get_block_view', [...]); @endcode
  84. *
  85. * Get page block with a list of available ACL levels to purchase or get for Free. List is represented as table.
  86. *
  87. * @return HTML string with a list of ACL levels to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  88. *
  89. * @see BxAclModule::serviceGetBlockView
  90. */
  91. /**
  92. * @ref bx_acl-get_block_view "get_block_view"
  93. */
  94. public function serviceGetBlockView()
  95. {
  96. bx_require_authentication(false, false, $this->serviceGetViewUrl());
  97. $sGrid = $this->_oConfig->getGridObject('view');
  98. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  99. if(!$oGrid)
  100. return '';
  101. $this->_oTemplate->addCss(array('view.css'));
  102. return array(
  103. 'content' => $oGrid->getCode()
  104. );
  105. }
  106. /**
  107. * @page service Service Calls
  108. * @section bx_acl Paid Levels
  109. * @subsection bx_acl-page_blocks Page Blocks
  110. * @subsubsection bx_acl-get_membership_actions get_membership_actions
  111. *
  112. * @code bx_srv('bx_acl', 'get_membership_actions', [...]); @endcode
  113. *
  114. * Get page block with a list of current membership level's actions.
  115. *
  116. * @return HTML string with a list of actions to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  117. *
  118. * @see BxAclModule::serviceGetMembershipActions
  119. */
  120. /**
  121. * @ref bx_acl-get_membership_actions "get_membership_actions"
  122. */
  123. public function serviceGetMembershipActions($iProfileId)
  124. {
  125. if($iProfileId != $this->getUserId())
  126. return '';
  127. return $this->_oTemplate->displayMembershipActions($iProfileId);
  128. }
  129. /**
  130. * @page service Service Calls
  131. * @section bx_acl Paid Levels
  132. * @subsection bx_acl-payments Payments
  133. * @subsubsection bx_acl-get_payment_data get_payment_data
  134. *
  135. * @code bx_srv('bx_acl', 'get_payment_data', [...]); @endcode
  136. *
  137. * Get an array with module's description. Is needed for payments processing module.
  138. *
  139. * @return an array with module's description.
  140. *
  141. * @see BxAclModule::serviceGetPaymentData
  142. */
  143. /**
  144. * @ref bx_acl-get_payment_data "get_payment_data"
  145. */
  146. public function serviceGetPaymentData()
  147. {
  148. return $this->_aModule;
  149. }
  150. /**
  151. * @page service Service Calls
  152. * @section bx_acl Paid Levels
  153. * @subsection bx_acl-payments Payments
  154. * @subsubsection bx_acl-get_cart_item get_cart_item
  155. *
  156. * @code bx_srv('bx_acl', 'get_cart_item', [...]); @endcode
  157. *
  158. * Get an array with level's description. Is used in Shopping Cart in payments processing module.
  159. *
  160. * @param $mixedItemId level's ID or Unique Name.
  161. * @return an array with level's description. Empty array is returned if something is wrong.
  162. *
  163. * @see BxAclModule::serviceGetCartItem
  164. */
  165. /**
  166. * @ref bx_acl-get_cart_item "get_cart_item"
  167. */
  168. public function serviceGetCartItem($mixedItemId)
  169. {
  170. $CNF = &$this->_oConfig->CNF;
  171. if(!$mixedItemId)
  172. return array();
  173. if(is_numeric($mixedItemId))
  174. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => (int)$mixedItemId));
  175. else
  176. $aItem = $this->_oDb->getPrices(array('type' => 'by_name_full', 'value' => $mixedItemId));
  177. if(empty($aItem) || !is_array($aItem))
  178. return array();
  179. $sTitle = '';
  180. if((int)$aItem['period'] == 0)
  181. $sTitle = _t('_bx_acl_txt_cart_item_title_lifetime', _t($aItem['level_name']));
  182. else
  183. $sTitle = _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']);
  184. $sDescription = _t($aItem['level_description']);
  185. if(empty($sDescription))
  186. $sDescription = _t('_bx_acl_txt_cart_item_description', getParam('site_title'));
  187. return array (
  188. 'id' => $aItem['id'],
  189. 'author_id' => $this->_oConfig->getOwner(),
  190. 'name' => $aItem['name'],
  191. 'title' => $sTitle,
  192. 'description' => $sDescription,
  193. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW'])),
  194. 'price_single' => $aItem['price'],
  195. 'price_recurring' => $aItem['price'],
  196. 'period_recurring' => $aItem['period'],
  197. 'period_unit_recurring' => $aItem['period_unit'],
  198. 'trial_recurring' => $aItem['trial'],
  199. 'added' => $aItem['added'],
  200. );
  201. }
  202. /**
  203. * @page service Service Calls
  204. * @section bx_acl Paid Levels
  205. * @subsection bx_acl-payments Payments
  206. * @subsubsection bx_acl-get_cart_items get_cart_items
  207. *
  208. * @code bx_srv('bx_acl', 'get_cart_items', [...]); @endcode
  209. *
  210. * Get an array with levels' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  211. *
  212. * @param $iSellerId seller ID.
  213. * @return an array with levels' descriptions. Empty array is returned if something is wrong or seller doesn't have any paid level.
  214. *
  215. * @see BxAclModule::serviceGetCartItems
  216. */
  217. /**
  218. * @ref bx_acl-get_cart_items "get_cart_items"
  219. */
  220. public function serviceGetCartItems($iSellerId)
  221. {
  222. $CNF = &$this->_oConfig->CNF;
  223. $iSellerIdSetting = $this->_oConfig->getOwner();
  224. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  225. return array();
  226. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  227. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']));
  228. $aResult = array();
  229. foreach($aItems as $aItem) {
  230. $sTitle = '';
  231. if((int)$aItem['period'] == 0)
  232. $sTitle = _t('_bx_acl_txt_cart_item_title_lifetime', _t($aItem['level_name']));
  233. else
  234. $sTitle = _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']);
  235. $sDescription = _t($aItem['level_description']);
  236. if(empty($sDescription))
  237. $sDescription = _t('_bx_acl_txt_cart_item_description', getParam('site_title'));
  238. $aResult[] = array(
  239. 'id' => $aItem['id'],
  240. 'author_id' => $iSellerIdSetting,
  241. 'name' => $aItem['name'],
  242. 'title' => $sTitle,
  243. 'description' => $sDescription,
  244. 'url' => $sUrl,
  245. 'price_single' => $aItem['price'],
  246. 'price_recurring' => $aItem['price'],
  247. 'period_recurring' => $aItem['period'],
  248. 'period_unit_recurring' => $aItem['period_unit'],
  249. 'trial_recurring' => $aItem['trial']
  250. );
  251. }
  252. return $aResult;
  253. }
  254. /**
  255. * @page service Service Calls
  256. * @section bx_acl Paid Levels
  257. * @subsection bx_acl-payments Payments
  258. * @subsubsection bx_acl-register_cart_item register_cart_item
  259. *
  260. * @code bx_srv('bx_acl', 'register_cart_item', [...]); @endcode
  261. *
  262. * Register a processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was registered there.
  263. *
  264. * @param $iClientId client ID.
  265. * @param $iSellerId seller ID
  266. * @param $iItemId item ID.
  267. * @param $iItemCount the number of purchased items.
  268. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  269. * @param $sLicense license number genereted with payment processing module for internal usage
  270. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  271. *
  272. * @see BxAclModule::serviceRegisterCartItem
  273. */
  274. /**
  275. * @ref bx_acl-register_cart_item "register_cart_item"
  276. */
  277. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  278. {
  279. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  280. }
  281. /**
  282. * @page service Service Calls
  283. * @section bx_acl Paid Levels
  284. * @subsection bx_acl-payments Payments
  285. * @subsubsection bx_acl-register_subscription_item register_subscription_item
  286. *
  287. * @code bx_srv('bx_acl', 'register_subscription_item', [...]); @endcode
  288. *
  289. * Register a processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was registered there.
  290. *
  291. * @param $iClientId client ID.
  292. * @param $iSellerId seller ID
  293. * @param $iItemId item ID.
  294. * @param $iItemCount the number of purchased items.
  295. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  296. * @param $sLicense license number genereted with payment processing module for internal usage
  297. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  298. *
  299. * @see BxAclModule::serviceRegisterSubscriptionItem
  300. */
  301. /**
  302. * @ref bx_acl-register_subscription_item "register_subscription_item"
  303. */
  304. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  305. {
  306. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  307. }
  308. /**
  309. * @page service Service Calls
  310. * @section bx_acl Paid Levels
  311. * @subsection bx_acl-payments Payments
  312. * @subsubsection bx_acl-reregister_cart_item reregister_cart_item
  313. *
  314. * @code bx_srv('bx_acl', 'reregister_cart_item', [...]); @endcode
  315. *
  316. * Reregister a single time payment inside the Paid Levels module. Is called with payment processing module after the payment was reregistered there.
  317. *
  318. * @param $iClientId client ID.
  319. * @param $iSellerId seller ID
  320. * @param $iItemIdOld old item ID.
  321. * @param $iItemIdNew new item ID.
  322. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  323. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  324. *
  325. * @see BxAclModule::serviceReregisterCartItem
  326. */
  327. /**
  328. * @ref bx_acl-reregister_cart_item "reregister_cart_item"
  329. */
  330. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  331. {
  332. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_SINGLE);
  333. }
  334. /**
  335. * @page service Service Calls
  336. * @section bx_acl Paid Levels
  337. * @subsection bx_acl-payments Payments
  338. * @subsubsection bx_acl-reregister_subscription_item reregister_subscription_item
  339. *
  340. * @code bx_srv('bx_acl', 'reregister_subscription_item', [...]); @endcode
  341. *
  342. * Reregister a subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was reregistered there.
  343. *
  344. * @param $iClientId client ID.
  345. * @param $iSellerId seller ID
  346. * @param $iItemIdOld old item ID.
  347. * @param $iItemIdNew new item ID.
  348. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  349. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  350. *
  351. * @see BxAclModule::serviceReregisterSubscriptionItem
  352. */
  353. /**
  354. * @ref bx_acl-reregister_subscription_item "reregister_subscription_item"
  355. */
  356. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  357. {
  358. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_RECURRING);
  359. }
  360. /**
  361. * @page service Service Calls
  362. * @section bx_acl Paid Levels
  363. * @subsection bx_acl-payments Payments
  364. * @subsubsection bx_acl-unregister_cart_item unregister_cart_item
  365. *
  366. * @code bx_srv('bx_acl', 'unregister_cart_item', [...]); @endcode
  367. *
  368. * Unregister an earlier processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was unregistered there.
  369. *
  370. * @param $iClientId client ID.
  371. * @param $iSellerId seller ID
  372. * @param $iItemId item ID.
  373. * @param $iItemCount the number of purchased items.
  374. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  375. * @param $sLicense license number genereted with payment processing module for internal usage
  376. * @return boolean value determining where the payment was unregistered or not.
  377. *
  378. * @see BxAclModule::serviceUnregisterCartItem
  379. */
  380. /**
  381. * @ref bx_acl-unregister_cart_item "unregister_cart_item"
  382. */
  383. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  384. {
  385. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  386. }
  387. /**
  388. * @page service Service Calls
  389. * @section bx_acl Paid Levels
  390. * @subsection bx_acl-payments Payments
  391. * @subsubsection bx_acl-unregister_subscription_item unregister_subscription_item
  392. *
  393. * @code bx_srv('bx_acl', 'unregister_subscription_item', [...]); @endcode
  394. *
  395. * Unregister an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was unregistered there.
  396. *
  397. * @param $iClientId client ID.
  398. * @param $iSellerId seller ID
  399. * @param $iItemId item ID.
  400. * @param $iItemCount the number of purchased items.
  401. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  402. * @param $sLicense license number genereted with payment processing module for internal usage
  403. * @return boolean value determining where the subscription was unregistered or not.
  404. *
  405. * @see BxAclModule::serviceUnregisterSubscriptionItem
  406. */
  407. /**
  408. * @ref bx_acl-unregister_subscription_item "unregister_subscription_item"
  409. */
  410. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  411. {
  412. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  413. }
  414. /**
  415. * @page service Service Calls
  416. * @section bx_acl Paid Levels
  417. * @subsection bx_acl-payments Payments
  418. * @subsubsection bx_acl-cancel_subscription_item cancel_subscription_item
  419. *
  420. * @code bx_srv('bx_acl', 'cancel_subscription_item', [...]); @endcode
  421. *
  422. * Cancel an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was canceled there.
  423. *
  424. * @param $iClientId client ID.
  425. * @param $iSellerId seller ID
  426. * @param $iItemId item ID.
  427. * @param $iItemCount the number of purchased items.
  428. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  429. * @return boolean value determining where the subscription was canceled or not.
  430. *
  431. * @see BxAclModule::serviceCancelSubscriptionItem
  432. */
  433. /**
  434. * @ref bx_acl-cancel_subscription_item "cancel_subscription_item"
  435. */
  436. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  437. {
  438. //TODO: Do something if it's necessary.
  439. return true;
  440. }
  441. /**
  442. * @page service Service Calls
  443. * @section bx_acl Paid Levels
  444. * @subsection bx_acl-other Other
  445. * @subsubsection bx_acl-get_prices get_prices
  446. *
  447. * @code bx_srv('bx_acl', 'get_prices', [...]); @endcode
  448. *
  449. * Get array of available price options for membership levels
  450. *
  451. * @param $iLevelId membership level id
  452. * @param $bFreeUnlimitedOnly get unlimited free pricing options only
  453. * @return array of levels, or empty array of there are no any pricing options avaiulable
  454. *
  455. * @see BxAclModule::serviceGetPrices
  456. */
  457. /**
  458. * @ref bx_acl-get_prices "get_prices"
  459. */
  460. public function serviceGetPrices($iLevelId = 0, $bFreeUnlimitedOnly = false)
  461. {
  462. $aParams = array(
  463. 'type' => $iLevelId ? 'by_level_id' : 'all_full',
  464. 'value' => $iLevelId,
  465. );
  466. $aPrices = $this->_oDb->getPrices($aParams, false);
  467. return array_filter($aPrices, function ($r) use ($bFreeUnlimitedOnly) {
  468. return $bFreeUnlimitedOnly ? !$r['price'] && !$r['period'] : true;
  469. });
  470. }
  471. /**
  472. * @page service Service Calls
  473. * @section bx_acl Paid Levels
  474. * @subsection bx_acl-other Other
  475. * @subsubsection bx_acl-get_products_names get_products_names
  476. *
  477. * @code bx_srv('bx_acl', 'get_products_names', [...]); @endcode
  478. *
  479. * Get an array of products names.
  480. *
  481. * @param $iVendorId return products of 1 vendor only
  482. * @param $iLimit limit the result artay
  483. * @return an array of products names
  484. *
  485. * @see BxAclModule::serviceGetProductsNames
  486. */
  487. /**
  488. * @ref bx_acl-get_products_names "get_products_names"
  489. */
  490. public function serviceGetProductsNames($iVendorId = 0, $iLimit = 1000)
  491. {
  492. return $this->_oDb->getProductsNames($iVendorId, $iLimit);
  493. }
  494. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  495. {
  496. $CNF = &$this->_oConfig->CNF;
  497. $aItem = $this->serviceGetCartItem($iItemId);
  498. if(empty($aItem) || !is_array($aItem))
  499. return [];
  500. $oAcl = BxDolAcl::getInstance();
  501. $aItemInfo = $this->_oDb->getPrices(['type' => 'by_id', 'value' => $iItemId]);
  502. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iClientId);
  503. $aPeriod = ['period' => (int)$aItemInfo['period'], 'period_unit' => $aItemInfo['period_unit']];
  504. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0 && (int)$aItemInfo['level_id'] != (int)$aMembershipInfo['id'])
  505. $aPeriod = ['period' => (int)$aItemInfo['trial'], 'period_unit' => 'day', 'period_trial' => true];
  506. $iReserve = (int)getParam($CNF['PARAM_RECURRING_RESERVE']);
  507. if(!empty($iReserve))
  508. $aPeriod['period_reserve'] = $iReserve;
  509. $bImmediate = (int)$aItemInfo['immediate'] != 0;
  510. if(!$oAcl->setMembership($iClientId, $aItemInfo['level_id'], $aPeriod, $bImmediate, $sLicense))
  511. return [];
  512. $this->_oDb->insertLicense([
  513. 'profile_id' => $iClientId,
  514. 'price_id' => $iItemId,
  515. 'type' => $sType,
  516. 'order' => $sOrder,
  517. 'license' => $sLicense,
  518. 'added' => time()
  519. ]);
  520. if($bImmediate && !empty($aMembershipInfo['transaction_id'])) {
  521. $oPayments = BxDolPayments::getInstance();
  522. $sLicenseOld = $aMembershipInfo['transaction_id'];
  523. $aLicenseOld = $this->_oDb->getLicenses(['type' => 'by_license', 'license' => $sLicenseOld]);
  524. $sOrderOld = '';
  525. if(empty($aLicenseOld) || !is_array($aLicenseOld)) {
  526. $aOrders = $oPayments->getOrdersInfo(['license' => $sLicenseOld]);
  527. if(!empty($aOrders) && is_array($aOrders)) {
  528. $aOrder = reset($aOrders);
  529. $aPendings = $oPayments->getPendingOrdersInfo(['id' => $aOrder['pending_id']]);
  530. if(!empty($aPendings) && is_array($aPendings)) {
  531. $aPending = reset($aPendings);
  532. if($aPending['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
  533. $sOrderOld = $aPending['order'];
  534. }
  535. }
  536. }
  537. else if($aLicenseOld['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
  538. $sOrderOld = $aLicenseOld['order'];
  539. if(!empty($sOrderOld) && $sOrderOld != $sOrder && (($aResult = $oPayments->cancelSubscription($sOrderOld)) === false || (int)$aResult['code'] != 0)) {
  540. sendMailTemplate($CNF['ETEMPLATE_SBS_CANCEL_REQUIRED'], 0, $iClientId, [
  541. 'level' => _t($aMembershipInfo['name']),
  542. 'order' => $sOrderOld,
  543. 'date_starts' => $this->_oConfig->formatDate($aMembershipInfo['date_starts']),
  544. 'date_expires' => $this->_oConfig->formatDate($aMembershipInfo['date_expires']),
  545. ], BX_EMAIL_NOTIFY, true);
  546. }
  547. }
  548. return $aItem;
  549. }
  550. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, $sType)
  551. {
  552. $aItemNew = $this->serviceGetCartItem($iItemIdNew);
  553. if(empty($aItemNew) || !is_array($aItemNew))
  554. return array();
  555. /*
  556. * Note. Membership level cannot be reregistered immediately.
  557. * it will be automatically changed in the end of current period.
  558. */
  559. return $aItemNew;
  560. }
  561. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  562. {
  563. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  564. if(empty($aItemInfo) || !is_array($aItemInfo))
  565. return false;
  566. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  567. }
  568. /**
  569. * COMMON METHODS
  570. */
  571. public function getUserId()
  572. {
  573. return isLogged() ? bx_get_logged_profile_id() : 0;
  574. }
  575. public function getUserInfo($iUserId = 0)
  576. {
  577. $oProfile = BxDolProfile::getInstance($iUserId);
  578. if (!$oProfile)
  579. $oProfile = BxDolProfileUndefined::getInstance();
  580. return array(
  581. $oProfile->getDisplayName(),
  582. $oProfile->getUrl(),
  583. $oProfile->getThumb(),
  584. $oProfile->getUnit()
  585. );
  586. }
  587. }
  588. /** @} */