BxAclModule.php 25 KB

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