BxAclModule.php 24 KB

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