BxPaymentSubscriptions.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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-other Other
  88. * @subsubsection bx_payment-get_subscription_orders_info get_subscription_orders_info
  89. *
  90. * @code bx_srv('bx_payment', 'get_subscription_orders_info', [...], 'Subscriptions'); @endcode
  91. *
  92. * Get subscription transaction(s) which meets all requirements.
  93. *
  94. * @param $aConditions an array of pears('key' => 'value'). The most useful keys are the following:
  95. * a. client_id - client's ID (integer)
  96. * b. seller_id - seller's ID (integer)
  97. * c. type - transaction type: single or recurring (string)
  98. * d. amount - transaction amount (float)
  99. * e. order - order ID received from payment provider (string)
  100. * f. provider - payment provider name (string)
  101. * g. date - the date when the payment was established(UNIXTIME STAMP)
  102. * h. processed - whether the payment was processed or not (integer, 0 or 1)
  103. * @return an array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
  104. *
  105. * @see BxPaymentSubscriptions::serviceGetSubscriptionOrdersInfo
  106. */
  107. /**
  108. * @ref bx_payment-get_subscription_orders_info "get_subscription_orders_info"
  109. */
  110. public function serviceGetSubscriptionOrdersInfo($aConditions)
  111. {
  112. if(empty($aConditions) || !is_array($aConditions))
  113. return array();
  114. return $this->_oModule->_oDb->getOrderSubscription(array('type' => 'mixed', 'conditions' => $aConditions));
  115. }
  116. /**
  117. * @page service Service Calls
  118. * @section bx_payment Payment
  119. * @subsection bx_payment-other Other
  120. * @subsubsection bx_payment-get_subscriptions_info get_subscriptions_info
  121. *
  122. * @code bx_srv('bx_payment', 'get_subscriptions_info', [...], 'Subscriptions'); @endcode
  123. *
  124. * Get subscription(s) which meets all requirements.
  125. *
  126. * @param $aConditions an array of pears('key' => 'value'). The most useful keys are the following:
  127. * a. pending_id - pending transaction ID (integer)
  128. * b. customer_id - customer ID from payment provider (string)
  129. * c. subscription_id - subscription ID from payment provider (string)
  130. * d. paid - flag determining whether the subscription paid or not (integer)
  131. * e. date - the date when the subscription was established(UNIXTIME STAMP)
  132. * @param $bCheckInProvider boolean value determining whether the subscription should be checked in associated payment provider.
  133. * @return an array of subscriptions. Each subscription has full info(pending ID, customer ID, subscription ID and so on)
  134. *
  135. * @see BxPaymentSubscriptions::serviceGetSubscriptionsInfo
  136. */
  137. /**
  138. * @ref bx_payment-get_subscriptions_info "get_subscriptions_info"
  139. */
  140. public function serviceGetSubscriptionsInfo($aConditions, $bCheckInProvider = false)
  141. {
  142. if(empty($aConditions) || !is_array($aConditions))
  143. return array();
  144. $aSubscriptions = $this->_oModule->_oDb->getSubscription(array('type' => 'mixed_ext', 'conditions' => $aConditions));
  145. if(empty($aSubscriptions) || !is_array($aSubscriptions) || !$bCheckInProvider)
  146. return $aSubscriptions;
  147. foreach($aSubscriptions as $iKey => $aSubscription) {
  148. if(empty($aSubscription['provider']) || $aSubscription['provider'] == 'manual')
  149. continue;
  150. $oProvider = $this->_oModule->getObjectProvider($aSubscription['provider'], $aSubscription['seller_id']);
  151. if(!$oProvider)
  152. continue;
  153. $aSubscriptions[$iKey]['data'] = $oProvider->getSubscription($aSubscription['pending_id'], $aSubscription['customer_id'], $aSubscription['subscription_id']);
  154. }
  155. return $aSubscriptions;
  156. }
  157. /**
  158. * @page service Service Calls
  159. * @section bx_payment Payment
  160. * @subsection bx_payment-purchase_processing Purchase Processing
  161. * @subsubsection bx_payment-subscribe subscribe
  162. *
  163. * @code bx_srv('bx_payment', 'subscribe', [...], 'Subscriptions'); @endcode
  164. *
  165. * Initialize subscription for specified item.
  166. *
  167. * @param $iSellerId integer value with seller ID.
  168. * @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.
  169. * @param $iModuleId integer value with module ID.
  170. * @param $iItemId integer value with item ID.
  171. * @param $iItemCount integer value with a number of items for purchasing. It's equal to 1 in case of subscription.
  172. * @param $sRedirect (optional) string value with redirect URL, if it's needed.
  173. * @return an array with special format which describes the result of operation.
  174. *
  175. * @see BxPaymentSubscriptions::serviceSubscribe
  176. */
  177. /**
  178. * @ref bx_payment-subscribe "subscribe"
  179. */
  180. public function serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect = '', $aCustom = array())
  181. {
  182. return $this->serviceSubscribeWithAddons($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, '', $sRedirect, $aCustom);
  183. }
  184. /**
  185. * @page service Service Calls
  186. * @section bx_payment Payment
  187. * @subsection bx_payment-purchase_processing Purchase Processing
  188. * @subsubsection bx_payment-subscribe_with_addons subscribe_with_addons
  189. *
  190. * @code bx_srv('bx_payment', 'subscribe_with_addons', [...], 'Subscriptions'); @endcode
  191. *
  192. * Initialize subscription for specified item.
  193. *
  194. * @param $iSellerId integer value with seller ID.
  195. * @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.
  196. * @param $iModuleId integer value with module ID.
  197. * @param $iItemId integer value with item ID.
  198. * @param $iItemCount integer value with a number of items for purchasing. It's equal to 1 in case of subscription.
  199. * @param $sItemAddons (optional) string with attached addons.
  200. * @param $sRedirect (optional) string value with redirect URL, if it's needed.
  201. * @return an array with special format which describes the result of operation.
  202. *
  203. * @see BxPaymentSubscriptions::serviceSubscribeWithAddons
  204. */
  205. /**
  206. * @ref bx_payment-subscribe_with_addons "subscribe_with_addons"
  207. */
  208. public function serviceSubscribeWithAddons($iSellerId, $sSellerProvider, $mixedModuleId, $iItemId, $iItemCount, $sItemAddons = '', $sRedirect = '', $aCustom = array())
  209. {
  210. $CNF = &$this->_oModule->_oConfig->CNF;
  211. $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
  212. $iClientId = $this->_oModule->getProfileId();
  213. $mixedResult = $this->_oModule->checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount);
  214. if($mixedResult !== true)
  215. return $mixedResult;
  216. $aSellerProviders = $this->_oModule->_oDb->getVendorInfoProvidersRecurring($iSellerId, true);
  217. if(empty($aSellerProviders))
  218. return array('code' => 5, 'message' => _t($CNF['T']['ERR_NOT_ACCEPT_PAYMENTS']));
  219. $aCartItem = array($iSellerId, $iModuleId, $iItemId, $iItemCount, $sItemAddons);
  220. if(empty($sSellerProvider)) {
  221. $sId = $this->_oModule->_oConfig->getHtmlIds('cart', 'providers_select') . BX_PAYMENT_TYPE_RECURRING;
  222. $sTitle = _t($CNF['T']['POPUP_PROVIDERS_SELECT']);
  223. return array('popup' => array(
  224. 'html' => BxTemplFunctions::getInstance()->popupBox($sId, $sTitle, $this->_oModule->_oTemplate->displayProvidersSelector($aCartItem, $aSellerProviders, $sRedirect, $aCustom)),
  225. 'options' => array('closeOnOuterClick' => true)
  226. ));
  227. }
  228. $aCustoms = array();
  229. $this->_oModule->_oConfig->putCustom($aCartItem, $aCustom, $aCustoms);
  230. $sCartItem = $this->_oModule->_oConfig->descriptorA2S($aCartItem);
  231. $mixedResult = $this->_oModule->serviceInitializeCheckout(BX_PAYMENT_TYPE_RECURRING, $iSellerId, $aSellerProviders[$sSellerProvider]['name'], array($sCartItem), $sRedirect, $aCustoms);
  232. if(is_string($mixedResult))
  233. return array('code' => 6, 'message' => _t($mixedResult));
  234. return $mixedResult;
  235. }
  236. public function serviceCancel($sOrder, $bForce = true)
  237. {
  238. $aPending = $this->_oModule->_oDb->getOrderPending(['type' => 'order', 'order' => $sOrder]);
  239. if(empty($aPending) || !is_array($aPending))
  240. return ['code' => 1, 'message' => _t('_bx_payment_err_not_found_pending')];
  241. if(!(bool)$bForce && ($mixedResult = $this->_oModule->isAllowedManage($aPending)) !== true)
  242. return ['code' => 2, 'message' => $mixedResult];
  243. if(!$this->cancel((int)$aPending['id']))
  244. return ['code' => 3, 'message' => _t('_bx_payment_err_cannot_perform')];
  245. return ['code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')];
  246. }
  247. public function serviceCancelByPendingId($iId, $bForce = true)
  248. {
  249. $aPending = $this->_oModule->_oDb->getOrderPending(['type' => 'id', 'id' => $iId]);
  250. if(empty($aPending) || !is_array($aPending))
  251. return ['code' => 1, 'message' => _t('_bx_payment_err_not_found_pending')];
  252. if(!(bool)$bForce && ($mixedResult = $this->_oModule->isAllowedManage($aPending)) !== true)
  253. return ['code' => 2, 'message' => $mixedResult];
  254. if(!$this->cancel((int)$aPending['id']))
  255. return ['code' => 3, 'message' => _t('_bx_payment_err_cannot_perform')];
  256. return ['code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')];
  257. }
  258. /**
  259. * @page service Service Calls
  260. * @section bx_payment Payment
  261. * @subsection bx_payment-purchase_processing Purchase Processing
  262. * @subsubsection bx_payment-send_subscription_expiration_letters send_subscription_expiration_letters
  263. *
  264. * @code bx_srv('bx_payment', 'send_subscription_expiration_letters', [...], 'Subscriptions'); @endcode
  265. *
  266. * Send subscription expiration letters.
  267. *
  268. * @param $iPendingId integer value with pending transaction ID.
  269. * @param $sOrderId string value with order ID.
  270. *
  271. * @see BxPaymentSubscriptions::serviceSendSubscriptionExpirationLetters
  272. */
  273. /**
  274. * @ref bx_payment-send_subscription_expiration_letters "send_subscription_expiration_letters"
  275. */
  276. public function serviceSendSubscriptionExpirationLetters($iPendingId, $sOrderId)
  277. {
  278. $CNF = &$this->_oModule->_oConfig->CNF;
  279. $sPrefix = $this->_oModule->_oConfig->getPrefix('general');
  280. $aSubscription = $this->serviceGetSubscriptionsInfo(array('subscription_id' => $sOrderId));
  281. if(empty($aSubscription) || !is_array($aSubscription))
  282. return;
  283. $aSubscription = array_shift($aSubscription);
  284. $oSeller = BxDolProfile::getInstance((int)$aSubscription['seller_id']);
  285. $oClient = BxDolProfile::getInstance((int)$aSubscription['client_id']);
  286. $aEtParams = array(
  287. 'subscription_id' => $aSubscription['subscription_id'],
  288. 'subscription_customer' => $aSubscription['customer_id'],
  289. 'subscription_date' => bx_time_js($aSubscription['date_add'], BX_FORMAT_DATE, true)
  290. );
  291. /**
  292. * Notify seller.
  293. */
  294. if($oSeller !== false) {
  295. $sEmail = '';
  296. $oProvider = $this->_oModule->getObjectProvider($aSubscription['provider'], $aSubscription['seller_id']);
  297. if($oProvider !== false && $oProvider->isActive())
  298. $sEmail = $oProvider->getOption('expiration_email');
  299. if(empty($sEmail))
  300. $sEmail = $oSeller->getAccountObject()->getEmail();
  301. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sPrefix . 'expiration_notification_seller', $aEtParams, 0, (int)$aSubscription['client_id']);
  302. sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
  303. }
  304. /**
  305. * Notify client.
  306. */
  307. if($oClient !== false) {
  308. $sEmail = $oClient->getAccountObject()->getEmail();
  309. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sPrefix . 'expiration_notification_client', $aEtParams, 0, (int)$aSubscription['seller_id']);
  310. sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
  311. }
  312. }
  313. public function register($aPending, $aParams = array())
  314. {
  315. if($this->_oModule->_oDb->isSubscriptionByPending($aPending['id']) || empty($aPending['items']))
  316. return false;
  317. $aInfo = $this->_oModule->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $aPending['client_id'], $aPending['seller_id'], $aPending['items']);
  318. if(empty($aInfo['items']) || !is_array($aInfo['items']))
  319. return false;
  320. $aItem = array_shift($aInfo['items']);
  321. $iPeriod = (int)$aItem['period_recurring'];
  322. $iTrial = (int)$aItem['trial_recurring'];
  323. $oDate = date_create();
  324. $iNow = $iNext = date_format($oDate, 'U');
  325. if((!empty($aParams['status']) && $aParams['status'] == BX_PAYMENT_SBS_STATUS_ACTIVE) || $iTrial > 0) {
  326. $sInterval = $this->_getInterval($iPeriod, $aItem['period_unit_recurring'], $iTrial);
  327. date_add($oDate, new DateInterval($sInterval));
  328. $iNext = date_format($oDate, 'U');
  329. }
  330. $sUnique = genRndPwd(9, false);
  331. $aSubscription = array(
  332. 'pending_id' => $aPending['id'],
  333. 'customer_id' => !empty($aParams['customer_id']) ? $aParams['customer_id'] : 'bx_cus_' . $sUnique,
  334. 'subscription_id' => !empty($aParams['subscription_id']) ? $aParams['subscription_id'] : 'bx_sub_' . $sUnique,
  335. 'period' => $iPeriod,
  336. 'period_unit' => $aItem['period_unit_recurring'],
  337. 'trial' => $iTrial,
  338. 'date_add' => $iNow,
  339. 'date_next' => $iNext,
  340. 'status' => !empty($aParams['status']) ? $aParams['status'] : ($iTrial > 0 ? BX_PAYMENT_SBS_STATUS_TRIAL : BX_PAYMENT_SBS_STATUS_UNPAID)
  341. );
  342. if(!$this->_oModule->_oDb->insertSubscription($aSubscription))
  343. return false;
  344. $this->_oModule->onSubscriptionCreate($aPending, $aSubscription);
  345. return true;
  346. }
  347. public function prolong($aPending, $aParams = array())
  348. {
  349. if(empty($aPending['items']))
  350. return false;
  351. $aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $aPending['id']));
  352. if(empty($aSubscription) || !is_array($aSubscription))
  353. return false;
  354. $aInfo = $this->_oModule->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $aPending['client_id'], $aPending['seller_id'], $aPending['items']);
  355. if(empty($aInfo['items']) || !is_array($aInfo['items']))
  356. return false;
  357. $aItem = array_shift($aInfo['items']);
  358. $iItemPeriod = (int)$aItem['period_recurring'];
  359. $sItemPeriodUnit = $aItem['period_unit_recurring'];
  360. $iDateNext = 0;
  361. if(!empty($iItemPeriod) && !empty($sItemPeriodUnit)) {
  362. $sInterval = $this->_getInterval($iItemPeriod, $sItemPeriodUnit);
  363. if(empty($sInterval))
  364. return false;
  365. $oDate = date_create('@' . $aSubscription['date_next']);
  366. date_add($oDate, new DateInterval($sInterval));
  367. $iDateNext = date_format($oDate, 'U');
  368. }
  369. $this->_oModule->_oDb->updateSubscription(array_merge(array(
  370. 'date_next' => $iDateNext,
  371. 'pay_attempts' => 0,
  372. 'status' => BX_PAYMENT_SBS_STATUS_ACTIVE
  373. ), $aParams), array(
  374. 'id' => $aSubscription['id']
  375. ));
  376. $this->_oModule->onSubscriptionProlong($aPending, $aSubscription);
  377. return true;
  378. }
  379. public function overdue($aPending, $aParams = array())
  380. {
  381. $aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $aPending['id']));
  382. if(empty($aSubscription) || !is_array($aSubscription))
  383. return false;
  384. $this->_oModule->_oDb->updateSubscription(array_merge(array(
  385. 'status' => BX_PAYMENT_SBS_STATUS_UNPAID
  386. ), $aParams), array(
  387. 'id' => $aSubscription['id']
  388. ));
  389. $this->_oModule->onSubscriptionOverdue($aPending, $aSubscription);
  390. return true;
  391. }
  392. public function cancel($iPendingId, $sReason = '')
  393. {
  394. /*
  395. if(!$this->cancelRemote($iPendingId))
  396. return false;
  397. */
  398. if(!$this->cancelLocal($iPendingId, $sReason))
  399. return false;
  400. return true;
  401. }
  402. public function cancelRemote($mixedPending)
  403. {
  404. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  405. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING)
  406. return false;
  407. $aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $aPending['id']));
  408. if(empty($aSubscription) || !is_array($aSubscription))
  409. return false;
  410. $iSellerId = (int)$aPending['seller_id'];
  411. $oProvider = $this->_oModule->getObjectProvider($aPending['provider'], $iSellerId);
  412. if($oProvider === false || !$oProvider->isActive())
  413. return false;
  414. if(!$oProvider->cancelRecurring($aPending['id'], $aSubscription['customer_id'], $aSubscription['subscription_id']))
  415. return false;
  416. return true;
  417. }
  418. public function cancelLocal($mixedPending, $sReason = 'cancel')
  419. {
  420. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  421. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING)
  422. return false;
  423. $aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $aPending['id']));
  424. if(empty($aSubscription) || !is_array($aSubscription))
  425. return false;
  426. $aItems = $this->_oModule->_oConfig->descriptorsM2A($aPending['items']);
  427. foreach($aItems as $aItem)
  428. $this->_oModule->callCancelSubscriptionItem((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order']));
  429. if(!$this->_oModule->_oDb->deleteSubscription($aSubscription['id'], $sReason))
  430. return false;
  431. $this->_oModule->onSubscriptionCancel($aPending, $aSubscription);
  432. return true;
  433. }
  434. public function getPopupCancelSurvey($iId)
  435. {
  436. $CNF = &$this->_oModule->_oConfig->CNF;
  437. $oForm = $this->_getFormCancelSurvey($iId);
  438. $oForm->initChecker();
  439. if($oForm->isSubmittedAndValid()) {
  440. $aReasons = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_REASONS_CANCEL']);
  441. return [
  442. 'code' => 0,
  443. 'msg' => ($sOther = $oForm->getCleanValue('other')) ? $sOther : $aReasons[$oForm->getCleanValue('items')]
  444. ];
  445. }
  446. $sPopupId = $this->_oModule->_oConfig->getHtmlIds('subscription', 'popup_subscription_cancel_survay') . $iId;
  447. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oModule->_oTemplate->parseHtmlByName('subscription_form.html', [
  448. 'form_id' => $oForm->aFormAttrs['id'],
  449. 'form' => $oForm->getCode(true)
  450. ]));
  451. return ['code' => 1, 'popup' => ['html' => $sPopupContent, 'options' => ['closeOnOuterClick' => false]]];
  452. }
  453. protected function _getBlock($sType)
  454. {
  455. $CNF = &$this->_oModule->_oConfig->CNF;
  456. $sMethod = 'displayBlockSbs' . bx_gen_method_name($sType);
  457. if(!$this->_oModule->_oTemplate->isMethodExists($sMethod))
  458. return $this->_bIsApi ? [] : [
  459. 'content' => MsgBox(_t('_Empty'))
  460. ];
  461. $iUserId = $this->_oModule->getProfileId();
  462. if(empty($iUserId))
  463. return $this->_bIsApi ? [] : [
  464. 'content' => MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']))
  465. ];
  466. $mixedBlockContent = $this->_oModule->_oTemplate->$sMethod($iUserId);
  467. if($this->_bIsApi)
  468. return !$mixedBlockContent ? [] : [
  469. bx_api_get_block('grid', $mixedBlockContent)
  470. ];
  471. $this->_oModule->setSiteSubmenu('menu_dashboard', 'system', 'dashboard-subscriptions');
  472. $sBlockSubmenu = $this->_oModule->_oConfig->getObject('menu_sbs_submenu');
  473. $oBlockSubmenu = BxDolMenu::getObjectInstance($sBlockSubmenu);
  474. if($oBlockSubmenu)
  475. $oBlockSubmenu->setSelected($this->MODULE, 'sbs-' . str_replace('_', '-', $sType));
  476. return [
  477. 'content' => $mixedBlockContent,
  478. 'menu' => $this->_oModule->_oConfig->getObject('menu_sbs_submenu')
  479. ];
  480. }
  481. private function _getInterval($iPeriod, $sPeriodUnit, $iTrial = 0)
  482. {
  483. if((int)$iTrial > 0)
  484. return 'P' . $iTrial . 'D';
  485. $sInterval = '';
  486. switch($sPeriodUnit) {
  487. case BX_PAYMENT_SBS_PU_YEAR:
  488. $sInterval = 'P' . $iPeriod . 'Y';
  489. break;
  490. case BX_PAYMENT_SBS_PU_MONTH:
  491. $sInterval = 'P' . $iPeriod . 'M';
  492. break;
  493. case BX_PAYMENT_SBS_PU_WEEK:
  494. $sInterval = 'P' . (7 * $iPeriod) . 'D';
  495. break;
  496. case BX_PAYMENT_SBS_PU_DAY:
  497. $sInterval = 'P' . $iPeriod . 'D';
  498. break;
  499. case BX_PAYMENT_SBS_PU_HOUR:
  500. $sInterval = 'PT' . $iPeriod . 'H';
  501. break;
  502. case BX_PAYMENT_SBS_PU_MINUTE:
  503. $sInterval = 'PT' . $iPeriod . 'I';
  504. break;
  505. }
  506. return $sInterval;
  507. }
  508. protected function _getFormCancelSurvey($iId)
  509. {
  510. $CNF = &$this->_oModule->_oConfig->CNF;
  511. $sJsObject = $this->_oModule->_oConfig->getJsObject('subscription');
  512. $aReasons = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_REASONS_CANCEL']);
  513. $aForm = [
  514. 'form_attrs' => [
  515. 'id' => $this->_oModule->_oConfig->getHtmlIds('subscription', 'form_subscription_cancel_survay') . $iId,
  516. 'action' => BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'subscription_cancel/' . $iId,
  517. 'method' => BX_DOL_FORM_METHOD_DEFAULT,
  518. 'onsubmit' => 'return ' . $sJsObject . '.onSubmitCancelationReason(this)'
  519. ],
  520. 'params' => [
  521. 'db' => [
  522. 'table' => '',
  523. 'key' => '',
  524. 'uri' => '',
  525. 'uri_title' => '',
  526. 'submit_name' => 'do_submit'
  527. ],
  528. ],
  529. 'inputs' => [
  530. 'items' => [
  531. 'type' => 'radio_set',
  532. 'name' => 'items',
  533. 'caption' => _t('_bx_payment_form_cs_input_items'),
  534. 'values' => $aReasons,
  535. 'value' => key($aReasons),
  536. 'required' => '1',
  537. 'attrs' => [
  538. 'onchange' => $sJsObject . '.onChangeCancelationReason(this)'
  539. ],
  540. 'db' => [
  541. 'pass' => 'Xss',
  542. ],
  543. 'checker' => [
  544. 'func' => 'Avail',
  545. 'params' => [],
  546. 'error' => _t('_bx_payment_form_input_err'),
  547. ],
  548. ],
  549. 'other' => [
  550. 'type' => 'text',
  551. 'name' => 'other',
  552. 'caption' => _t('_bx_payment_form_cs_input_other'),
  553. 'value' => '',
  554. 'required' => '1',
  555. 'tr_attrs' => [
  556. 'class' => 'bx-payment-cs-other bx-payment-cs-hidden'
  557. ],
  558. 'db' => [
  559. 'pass' => 'Xss',
  560. ],
  561. ],
  562. 'controls' => [
  563. 'name' => 'controls',
  564. 'type' => 'input_set',
  565. [
  566. 'type' => 'submit',
  567. 'name' => 'do_submit',
  568. 'value' => _t('_bx_payment_form_cs_input_submit'),
  569. ], [
  570. 'type' => 'reset',
  571. 'name' => 'do_cancel',
  572. 'value' => _t('_bx_payment_form_cs_input_cancel'),
  573. 'attrs' => [
  574. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  575. 'class' => 'bx-def-margin-sec-left',
  576. ],
  577. ]
  578. ]
  579. ]
  580. ];
  581. return new BxTemplFormView($aForm);
  582. }
  583. }
  584. /** @} */