BxPaymentModule.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. define('BX_PAYMENT_ORDERS_TYPE_PENDING', 'pending');
  12. define('BX_PAYMENT_ORDERS_TYPE_PROCESSED', 'processed');
  13. define('BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION', 'subscription');
  14. define('BX_PAYMENT_ORDERS_TYPE_HISTORY', 'history');
  15. define('BX_PAYMENT_RESULT_SUCCESS', 0);
  16. /*
  17. * Subscriptions: statuses.
  18. */
  19. define('BX_PAYMENT_SBS_STATUS_SCHEDULED', 'scheduled');
  20. define('BX_PAYMENT_SBS_STATUS_TRIAL', 'trial');
  21. define('BX_PAYMENT_SBS_STATUS_ACTIVE', 'active');
  22. define('BX_PAYMENT_SBS_STATUS_UNPAID', 'unpaid');
  23. define('BX_PAYMENT_SBS_STATUS_PAUSED', 'paused');
  24. define('BX_PAYMENT_SBS_STATUS_CANCELED', 'canceled');
  25. define('BX_PAYMENT_SBS_STATUS_UNKNOWN', 'unknown');
  26. /*
  27. * Subscriptions: period units.
  28. */
  29. define('BX_PAYMENT_SBS_PU_YEAR', 'year');
  30. define('BX_PAYMENT_SBS_PU_MONTH', 'month');
  31. define('BX_PAYMENT_SBS_PU_WEEK', 'week');
  32. define('BX_PAYMENT_SBS_PU_DAY', 'day');
  33. define('BX_PAYMENT_SBS_PU_HOUR', 'hour');
  34. define('BX_PAYMENT_SBS_PU_MINUTE', 'minute');
  35. /**
  36. * Payment module by BoonEx
  37. *
  38. * This module is needed to work with payment providers and organize the process
  39. * of some item purchasing. Shopping Cart and Orders Manager are included.
  40. *
  41. * Integration notes:
  42. * To integrate your module with this one, you need:
  43. * 1. Get 'Add To Cart' button using serviceGetAddToCartLink service.
  44. * 2. Add info about your module in the 'bx_pmt_modules' table.
  45. * 3. Realize the following service methods in your Module class.
  46. * a. serviceGetItems($iSellerId) - Is used in Orders Administration to get all products of the requested seller(vendor).
  47. * b. serviceGetCartItem($iClientId, $iItemId) - Is used in Shopping Cart to get one product by specified id.
  48. * c. serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Register purchased product.
  49. * d. serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Unregister the product purchased earlier.
  50. * @see You may see an example of integration in Membership module.
  51. *
  52. *
  53. * Profile's Wall:
  54. * no spy events
  55. *
  56. *
  57. *
  58. * Spy:
  59. * no spy events
  60. *
  61. *
  62. *
  63. * Memberships/ACL:
  64. * Doesn't depend on user's membership.
  65. *
  66. *
  67. *
  68. * Service methods:
  69. *
  70. * Is used to get "Add to cart" link for some item(s) in your module.
  71. * @see BxPmtModule::serviceGetAddToCartLink
  72. * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
  73. *
  74. * Check transaction(s) in database which satisty all conditions.
  75. * @see BxPmtModule::serviceGetOrdersInfo
  76. * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
  77. *
  78. * Get total count of items in Shopping Cart.
  79. * @see BxPmtModule::serviceGetCartItemsCount
  80. * BxDolService::call('payment', 'get_cart_items_count', array($iUserId, $iOldCount));
  81. * @note is needed for internal usage(integration with member tool bar).
  82. *
  83. * Get Shopping cart content.
  84. * @see BxPmtModule::serviceGetCartItems
  85. * BxDolService::call('payment', 'get_cart_items');
  86. * @note is needed for internal usage(integration with member tool bar).
  87. *
  88. *
  89. * Alerts:
  90. * no alerts available
  91. *
  92. */
  93. class BxPaymentModule extends BxBaseModPaymentModule
  94. {
  95. protected $_iUserId;
  96. protected $_aOrderTypes;
  97. function __construct($aModule)
  98. {
  99. parent::__construct($aModule);
  100. $this->_iUserId = $this->getProfileId();
  101. $this->_aOrderTypes = array(
  102. BX_PAYMENT_ORDERS_TYPE_PENDING,
  103. BX_PAYMENT_ORDERS_TYPE_PROCESSED,
  104. BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION,
  105. BX_PAYMENT_ORDERS_TYPE_HISTORY
  106. );
  107. }
  108. /**
  109. * Manage Orders Methods
  110. */
  111. public function actionGetClients()
  112. {
  113. $sTerm = bx_get('term');
  114. $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  115. echoJson($aResult);
  116. }
  117. public function actionGetItems($sType, $iModuleId)
  118. {
  119. $iSellerId = $this->getProfileId();
  120. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  121. echoJson(array(
  122. 'code' => 0,
  123. 'eval' => $this->_oConfig->getJsObject('processed') . '.onSelectModule(oData);',
  124. 'data' => $this->_oTemplate->displayItems($sType, $aItems)
  125. ));
  126. }
  127. public function actionGetFilterValuesItem($iSellerId, $iModuleId)
  128. {
  129. $sItems = '<option value="">' . _t('_bx_payment_txt_all_items') . '</option>';
  130. if(empty($iSellerId) || empty($iModuleId))
  131. return echoJson(array('code' => 1, 'content' => $sItems));
  132. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  133. $this->_oConfig->sortByColumn('title', $aItems);
  134. foreach($aItems as $aItem)
  135. $sItems .= '<option value="' . $aItem['id'] . '">' . $aItem['title'] . '</option>';
  136. echoJson(array('code' => 0, 'content' => $sItems));
  137. }
  138. public function serviceGetSafeServices()
  139. {
  140. $a = parent::serviceGetSafeServices();
  141. return array_merge($a, array (
  142. 'GetBlockJoin' => 'BxPaymentJoin',
  143. 'GetBlockCarts' => 'BxPaymentCart',
  144. 'GetBlockCart' => 'BxPaymentCart',
  145. 'GetBlockCartHistory' => 'BxPaymentCart',
  146. 'GetBlockListMy' => 'BxPaymentSubscriptions',
  147. 'GetBlockHistory' => 'BxPaymentSubscriptions',
  148. ));
  149. }
  150. /**
  151. * @page service Service Calls
  152. * @section bx_payment Payment
  153. * @subsection bx_payment-purchase_processing Purchase Processing
  154. * @subsubsection bx_payment-get_modules_with_payments get_modules_with_payments
  155. *
  156. * @code bx_srv('bx_payment', 'get_modules_with_payments', []); @endcode
  157. *
  158. * Get modules with payments functionality
  159. *
  160. * @return array with modules names
  161. *
  162. * @see BxPaymentModule::serviceGetModulesWithPayments
  163. */
  164. /**
  165. * @ref bx_payment-get_modules_with_payments "get_modules_with_payments"
  166. */
  167. public function serviceGetModulesWithPayments()
  168. {
  169. return $this->_oDb->getModulesWithPayments();
  170. }
  171. /**
  172. * @page service Service Calls
  173. * @section bx_payment Payment
  174. * @subsection bx_payment-purchase_processing Purchase Processing
  175. * @subsubsection bx_payment-is_credits_only is_credits_only
  176. *
  177. * @code bx_srv('bx_payment', 'is_credits_only'); @endcode
  178. *
  179. * Check whether the 'Credits Only' mode is enabled or not.
  180. *
  181. * @return boolean value determining the result of checking.
  182. *
  183. * @see BxPaymentModule::serviceIsCreditsOnly
  184. */
  185. /**
  186. * @ref bx_payment-is_credits_only "is_credits_only"
  187. */
  188. public function serviceIsCreditsOnly()
  189. {
  190. return $this->_oConfig->isCreditsOnly();
  191. }
  192. /**
  193. * @page service Service Calls
  194. * @section bx_payment Payment
  195. * @subsection bx_payment-purchase_processing Purchase Processing
  196. * @subsubsection bx_payment-is_accepting_payments is_accepting_payments
  197. *
  198. * @code bx_srv('bx_payment', 'is_accepting_payments', [...]); @endcode
  199. *
  200. * Check whether the specified vendor has a configured payment provider or not.
  201. *
  202. * @param $iVendorId integer value with vendor ID.
  203. * @param $sPaymentType (optional) string value with payment type. If specified then the vendor will be checked for having the payment provider of the requested type.
  204. * @return boolean value determining the result of checking.
  205. *
  206. * @see BxPaymentModule::serviceIsAcceptingPayments
  207. */
  208. /**
  209. * @ref bx_payment-is_accepting_payments "is_accepting_payments"
  210. */
  211. public function serviceIsAcceptingPayments($iVendorId, $sPaymentType = '')
  212. {
  213. $bResult = false;
  214. switch($sPaymentType) {
  215. case BX_PAYMENT_TYPE_SINGLE:
  216. $aProvidersCart = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  217. $bResult = !empty($aProvidersCart);
  218. break;
  219. case BX_PAYMENT_TYPE_RECURRING:
  220. $aProvidersSubscription = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  221. $bResult = !empty($aProvidersSubscription);
  222. break;
  223. default:
  224. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  225. $bResult = !empty($aProviders);
  226. }
  227. return $bResult;
  228. }
  229. /**
  230. * @deprecated since version 11.0.0
  231. *
  232. * @see BxPaymentModule::serviceIsProviderOptions
  233. */
  234. public function serviceIsPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  235. {
  236. return $this->serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  237. }
  238. /**
  239. * @deprecated since version 11.0.0
  240. *
  241. * @see BxPaymentModule::serviceGetProviderOptions
  242. */
  243. public function serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  244. {
  245. return $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  246. }
  247. /**
  248. * @page service Service Calls
  249. * @section bx_payment Payment
  250. * @subsection bx_payment-purchase_processing Purchase Processing
  251. * @subsubsection bx_payment-is_provider_options is_provider_options
  252. *
  253. * @code bx_srv('bx_payment', 'is_provider_options', [...]); @endcode
  254. *
  255. * Check whether the specified vendor has configured the specified payment provider or not.
  256. *
  257. * @param $iVendorId integer value with vendor ID.
  258. * @param $sVendorProvider string value with payment provider name.
  259. * @param $sPaymentType (optional) string value with payment type. If specified then the vendor will be checked for having the payment provider of the requested type.
  260. * @return boolean value determining the result of checking.
  261. *
  262. * @see BxPaymentModule::serviceIsProviderOptions
  263. */
  264. /**
  265. * @ref bx_payment-is_provider_options "is_provider_options"
  266. */
  267. public function serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  268. {
  269. $aProvider = $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  270. return $aProvider !== false;
  271. }
  272. /**
  273. * @page service Service Calls
  274. * @section bx_payment Payment
  275. * @subsection bx_payment-purchase_processing Purchase Processing
  276. * @subsubsection bx_payment-get_provider_options get_provider_options
  277. *
  278. * @code bx_srv('bx_payment', 'get_provider_options', [...]); @endcode
  279. *
  280. * Get configuration settings for the payment provider entered by the specified vendor.
  281. *
  282. * @param $iVendorId integer value with vendor ID.
  283. * @param $sVendorProvider string value with payment provider name.
  284. * @param $sPaymentType (optional) string value with payment type. If specified then the vendor will be checked for having the payment provider of the requested type.
  285. * @return an array with special format describing the payment provider or false value if something is wrong.
  286. *
  287. * @see BxPaymentModule::serviceGetProviderOptions
  288. */
  289. /**
  290. * @ref bx_payment-get_provider_options "get_provider_options"
  291. */
  292. public function serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  293. {
  294. $aProviders = array();
  295. switch($sPaymentType) {
  296. case BX_PAYMENT_TYPE_SINGLE:
  297. $aProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  298. break;
  299. case BX_PAYMENT_TYPE_RECURRING:
  300. $aProviders = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  301. break;
  302. default:
  303. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  304. }
  305. return !empty($aProviders) && !empty($aProviders[$sVendorProvider]) && is_array(($aProviders[$sVendorProvider])) ? $aProviders[$sVendorProvider] : false;
  306. }
  307. /**
  308. * @page service Service Calls
  309. * @section bx_payment Payment
  310. * @subsection bx_payment-other Other
  311. * @subsubsection bx_payment-get_options_default_currency_code get_options_default_currency_code
  312. *
  313. * @code bx_srv('bx_payment', 'get_options_default_currency_code', [...]); @endcode
  314. *
  315. * Get an array with available currencies. Is used in forms.
  316. *
  317. * @return an array with available currencies represented as key => value pairs.
  318. *
  319. * @see BxPaymentModule::serviceGetOptionsDefaultCurrencyCode
  320. */
  321. /**
  322. * @ref bx_payment-get_options_default_currency_code "get_options_default_currency_code"
  323. */
  324. public function serviceGetOptionsDefaultCurrencyCode()
  325. {
  326. $CNF = &$this->_oConfig->CNF;
  327. $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES']);
  328. $aResult = array();
  329. foreach($aCurrencies as $sKey => $sValue)
  330. $aResult[] = array(
  331. 'key' => $sKey,
  332. 'value' => $sValue
  333. );
  334. return $aResult;
  335. }
  336. /**
  337. * @page service Service Calls
  338. * @section bx_payment Payment
  339. * @subsection bx_payment-other Other
  340. * @subsubsection bx_payment-get_options_site_admin get_options_site_admin
  341. *
  342. * @code bx_srv('bx_payment', 'get_options_site_admin', [...]); @endcode
  343. *
  344. * Get an array with profiles which can be used as 'Site Admin (Owner)'. 'Site Admin' is a person who will be displayed to buyers when something is selling on behalf of the site.
  345. *
  346. * @return an array with profiles represented as key => value pairs.
  347. *
  348. * @see BxPaymentModule::serviceGetOptionsSiteAdmin
  349. */
  350. /**
  351. * @ref bx_payment-get_options_site_admin "get_options_site_admin"
  352. */
  353. public function serviceGetOptionsSiteAdmin()
  354. {
  355. $aResult = array(
  356. array('key' => '', 'value' => _t('_Select_one'))
  357. );
  358. $aIds = $this->_oDb->getAdminsIds();
  359. foreach($aIds as $iId) {
  360. $aUser = $this->getProfileInfo($iId);
  361. $aResult[] = array(
  362. 'key' => $iId,
  363. 'value' => $aUser['name']
  364. );
  365. }
  366. return $aResult;
  367. }
  368. /**
  369. * Cart Processing Methods
  370. */
  371. public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $sCustom = '')
  372. {
  373. if(empty($sCustom) && bx_get('custom') !== false)
  374. $sCustom = bx_process_input(bx_get('custom'));
  375. $aCustom = array();
  376. if(!empty($sCustom)) {
  377. $aCustom = unserialize(base64_decode($sCustom));
  378. if(empty($aCustom) || !is_array($aCustom))
  379. $aCustom = array();
  380. }
  381. $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom);
  382. echoJson($aResult);
  383. }
  384. /**
  385. * Isn't used yet.
  386. */
  387. public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
  388. {
  389. echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId));
  390. }
  391. /**
  392. * Isn't used yet.
  393. */
  394. public function actionEmptyCart($iSellerId)
  395. {
  396. echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId));
  397. }
  398. public function actionSubscribe()
  399. {
  400. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  401. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  402. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  403. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  404. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  405. if(empty($iItemCount))
  406. $iItemCount = 1;
  407. $sRedirect = bx_process_input(bx_get('redirect'));
  408. $sCustom = bx_process_input(bx_get('custom'));
  409. $aCustom = array();
  410. if(!empty($sCustom)) {
  411. $aCustom = unserialize(base64_decode($sCustom));
  412. if(empty($aCustom) || !is_array($aCustom))
  413. $aCustom = array();
  414. }
  415. $aResult = $this->getObjectSubscriptions()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  416. $bRedirect = !empty($aResult['redirect']);
  417. if(!empty($aResult['popup'])) {
  418. $sContent = '';
  419. $sContent .= $this->_oTemplate->displayCartJs(BX_PAYMENT_TYPE_RECURRING, $iSellerId);
  420. $sContent .= !empty($aResult['popup']['html']) ? $aResult['popup']['html'] : $aResult['popup'];
  421. return $this->_oTemplate->displayPageCodeResponse($sContent, false, true);
  422. }
  423. if(!empty($aResult['code']))
  424. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  425. if(!empty($aResult['message']) && !$bRedirect)
  426. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  427. if($bRedirect) {
  428. header('Location: ' . $aResult['redirect']);
  429. exit;
  430. }
  431. }
  432. public function actionSubscribeJson()
  433. {
  434. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  435. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  436. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  437. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  438. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  439. if(empty($iItemCount))
  440. $iItemCount = 1;
  441. $sItemAddons = '';
  442. if(bx_get('item_addons') !== false) {
  443. $sItemAddons = bx_process_input(bx_get('item_addons'));
  444. if(is_array($sItemAddons))
  445. $sItemAddons = $this->_oConfig->a2s($sItemAddons);
  446. }
  447. $sRedirect = bx_process_input(bx_get('redirect'));
  448. $sCustom = bx_process_input(bx_get('custom'));
  449. $aCustom = array();
  450. if(!empty($sCustom)) {
  451. $aCustom = unserialize(base64_decode($sCustom));
  452. if(empty($aCustom) || !is_array($aCustom))
  453. $aCustom = array();
  454. }
  455. echoJson($this->getObjectSubscriptions()->serviceSubscribeWithAddons($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sItemAddons, $sRedirect, $aCustom));
  456. }
  457. public function actionSubscriptionGetDetails($iId)
  458. {
  459. echoJson($this->_oTemplate->displaySubscriptionGetDetails($iId));
  460. }
  461. public function actionSubscriptionChangeDetails($iId)
  462. {
  463. echoJson($this->_oTemplate->displaySubscriptionChangeDetails($iId));
  464. }
  465. public function actionSubscriptionGetBilling($iId)
  466. {
  467. echoJson($this->_oTemplate->displaySubscriptionGetBilling($iId));
  468. }
  469. public function actionSubscriptionChangeBilling($iId)
  470. {
  471. echoJson($this->_oTemplate->displaySubscriptionChangeBilling($iId));
  472. }
  473. public function actionSubscriptionCancelation($iId)
  474. {
  475. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  476. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  477. if(empty($aPending) || !is_array($aPending))
  478. return echoJson($aResult);
  479. $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
  480. if(empty($aSubscription) || !is_array($aSubscription))
  481. return echoJson($aResult);
  482. $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
  483. if(!$oRecipient)
  484. return echoJson($aResult);
  485. $mixedResult = $this->isAllowedManage($aPending);
  486. if($mixedResult !== true)
  487. return echoJson(array('code' => 2, 'message' => $mixedResult));
  488. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
  489. 'sibscription_id' => $aSubscription['subscription_id'],
  490. 'sibscription_customer' => $aSubscription['customer_id'],
  491. 'sibscription_date' => bx_time_js($aSubscription['date'], BX_FORMAT_DATE, true)
  492. ), 0, (int)$aPending['client_id']);
  493. $sEmail = '';
  494. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  495. if($oProvider !== false && $oProvider->isActive())
  496. $sEmail = $oProvider->getOption('cancellation_email');
  497. if(empty($sEmail))
  498. $sEmail = $oRecipient->getAccountObject()->getEmail();
  499. if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  500. return echoJson($aResult);
  501. echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
  502. }
  503. public function actionSubscriptionCancel($iId, $mixedGridObject = false)
  504. {
  505. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  506. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  507. if(empty($aPending) || !is_array($aPending))
  508. return echoJson($aResult);
  509. $mixedResult = $this->isAllowedManage($aPending);
  510. if($mixedResult !== true)
  511. return echoJson(array('code' => 2, 'message' => $mixedResult));
  512. if(!$this->getObjectSubscriptions()->cancel($iId))
  513. return echoJson($aResult);
  514. if(empty($mixedGridObject) && bx_get('grid') !== false)
  515. $mixedGridObject = bx_process_input(bx_get('grid'));
  516. if(!empty($mixedGridObject) && $aPending['client_id'] == $this->getProfileId()) {
  517. $oGrid = BxDolGrid::getObjectInstance($mixedGridObject, $this->_oTemplate);
  518. $oGrid->addQueryParam('client_id', $aPending['client_id']);
  519. return echoJson(array('object' => $mixedGridObject, 'grid' => $oGrid->getCode(false), 'blink' => array($iId)));
  520. }
  521. else
  522. return echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')));
  523. }
  524. /**
  525. * Payment Processing Methods
  526. */
  527. public function actionInitializeCheckout($sType)
  528. {
  529. if(!$this->isLogged())
  530. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  531. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  532. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  533. $sProvider = bx_process_input(bx_get('provider'));
  534. $aItems = bx_process_input(bx_get('items'));
  535. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  536. if($mixedResult !== true)
  537. return $this->_oTemplate->displayPageCodeError($mixedResult);
  538. }
  539. header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
  540. exit;
  541. }
  542. /**
  543. * @page service Service Calls
  544. * @section bx_payment Payment
  545. * @subsection bx_payment-purchase_processing Purchase Processing
  546. * @subsubsection bx_payment-initialize_checkout initialize_checkout
  547. *
  548. * @code bx_srv('bx_payment', 'initialize_checkout', [...]); @endcode
  549. *
  550. * Initialize the checkout process.
  551. *
  552. * @param $sType string value with payment type (single or recurring).
  553. * @param $iSellerId integer value with seller ID.
  554. * @param $sProvider string value with payment provider name.
  555. * @param $aItems (optional) an array with items to be purchased.
  556. * @param $sRedirect (optional) string value with redirect URL if it's needed.
  557. * @param $aCustom (optional) array with custom data to attach to a payment.
  558. * @return the result depends on the payment provider which is used for processing or represented as string value with error message if something is wrong.
  559. *
  560. * @see BxPaymentModule::serviceInitializeCheckout
  561. */
  562. /**
  563. * @ref bx_payment-initialize_checkout "initialize_checkout"
  564. */
  565. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '', $aCustoms = array())
  566. {
  567. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  568. $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
  569. if(!is_array($aItems))
  570. $aItems = array($aItems);
  571. $iSellerId = (int)$iSellerId;
  572. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  573. return $this->_sLangsPrefix . 'err_unknown_vendor';
  574. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  575. if($oProvider === false || !$oProvider->isActive())
  576. return $this->_sLangsPrefix . 'err_incorrect_provider';
  577. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  578. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  579. return $this->_sLangsPrefix . 'err_empty_order';
  580. /*
  581. * Process FREE (price = 0) items for LOGGED IN members
  582. * WITHOUT processing via payment provider.
  583. * Note. This section isn't used for now!
  584. */
  585. $bProcessedFree = false;
  586. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  587. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  588. foreach($aInfo['items'] as $iIndex => $aItem) {
  589. if((int)$aInfo['client_id'] == 0)
  590. continue;
  591. //--- For Single time payments.
  592. if($bTypeSingle && (float)$aItem[$sKeyPriceSingle] == 0) {
  593. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  594. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  595. $aCartItem = array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity']);
  596. $aCartItemCustom = $this->_oConfig->pullCustom($aCartItem, $aCartCustoms);
  597. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  598. if(!empty($aItemInfo) && is_array($aItemInfo)) {
  599. if(!empty($aItemInfo['addons']) && is_array($aItemInfo['addons']))
  600. foreach($aItemInfo['addons'] as $sAddon => $aAddon)
  601. $this->callRegisterCartItem((int)$aAddon['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aAddon['id'], $aAddon['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  602. $bProcessedFree = true;
  603. }
  604. $aInfo['items_count'] -= 1;
  605. unset($aInfo['items'][$iIndex]);
  606. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aCartItem) . ":?'", "", $aCart['items']), ":");
  607. $this->_oDb->setCartItems($aInfo['client_id'], $aCart['items'], $aCartCustoms);
  608. }
  609. //--- For Recurring payments.
  610. if($bTypeRecurring && (float)$aItem[$sKeyPriceRecurring] == 0) {
  611. //TODO: Process FREE subscription here, if such situation will be possible.
  612. }
  613. }
  614. if(empty($aInfo['items']))
  615. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  616. if($bTypeSingle && (empty($aCustoms) || !is_array($aCustoms))) {
  617. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  618. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  619. $aCustoms = array();
  620. foreach($aInfo['items'] as $aItem) {
  621. $sCartItem = $this->_oConfig->descriptorA2S(array($aItem['author_id'], $aItem['module_id'], $aItem['id']));
  622. $aCartItemCustom = $this->_oConfig->getCustom($sCartItem, $aCartCustoms);
  623. $this->_oConfig->putCustom($sCartItem, $aCartItemCustom, $aCustoms);
  624. }
  625. }
  626. bx_alert($this->getName(), 'before_pending', 0, bx_get_logged_profile_id(), array(
  627. 'client_id' => $this->_iUserId,
  628. 'type' => &$sType,
  629. 'provider' => &$sProvider,
  630. 'cart_info' => &$aInfo,
  631. 'custom' => &$aCustoms,
  632. ));
  633. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo, $aCustoms);
  634. if(empty($iPendingId))
  635. return $this->_sLangsPrefix . 'err_access_db';
  636. /*
  637. * Perform Join WITHOUT processing via payment provider
  638. * if a client ISN'T logged in and has only ONE FREE item in the card.
  639. * Note. This section isn't used for now because only a member can purchase!
  640. */
  641. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  642. reset($aInfo['items']);
  643. $aItem = current($aInfo['items']);
  644. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  645. $this->_oDb->updateOrderPending($iPendingId, array(
  646. 'order' => $this->_oConfig->getLicense(),
  647. 'error_code' => '1',
  648. 'error_msg' => ''
  649. ));
  650. $this->getObjectJoin()->performJoin($iPendingId);
  651. }
  652. }
  653. return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
  654. }
  655. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  656. {
  657. $aData = &$_REQUEST;
  658. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  659. if($oProvider === false || !$oProvider->isActive())
  660. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  661. $aResult = $oProvider->finalizeCheckout($aData);
  662. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  663. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  664. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  665. $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
  666. if($bTypeRecurring) {
  667. $sStatus = BX_PAYMENT_SBS_STATUS_UNPAID;
  668. if(isset($aResult['paid']) && $aResult['paid'])
  669. $sStatus = BX_PAYMENT_SBS_STATUS_ACTIVE;
  670. else if(isset($aResult['trial']) && $aResult['trial'])
  671. $sStatus = BX_PAYMENT_SBS_STATUS_TRIAL;
  672. $aSubscription = array(
  673. 'customer_id' => $aResult['customer_id'],
  674. 'subscription_id' => $aResult['subscription_id'],
  675. 'status' => $sStatus
  676. );
  677. if(!$this->getObjectSubscriptions()->register($aPending, $aSubscription))
  678. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_already_registered');
  679. }
  680. $this->onPaymentRegisterBefore($aPending);
  681. //--- Check "Pay Before Join" situation
  682. if((int)$aPending['client_id'] == 0)
  683. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  684. //--- Register payment for purchased items in associated modules
  685. if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
  686. $this->registerPayment($aPending);
  687. bx_alert($this->getName(), 'finalize_checkout', 0, bx_get_logged_profile_id(), array(
  688. 'pending' => $aPending,
  689. 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
  690. 'provider' => $oProvider,
  691. 'message' => &$aResult['message'],
  692. 'result' => &$aResult,
  693. ));
  694. if($oProvider->needRedirect()) {
  695. header('Location: ' . $oProvider->getReturnUrl());
  696. exit;
  697. }
  698. if(!empty($aResult['redirect'])) {
  699. header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
  700. exit;
  701. }
  702. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  703. }
  704. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  705. {
  706. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  707. if($oProvider === false || !$oProvider->isActive())
  708. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  709. $aResult = $oProvider->finalizedCheckout();
  710. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  711. }
  712. public function actionNotify($sProvider, $mixedVendorId = "")
  713. {
  714. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  715. if($oProvider === false || !$oProvider->isActive())
  716. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  717. $oProvider->notify();
  718. }
  719. /**
  720. * The method is needed to pass direct action calls to necessary Payment Provider.
  721. */
  722. public function actionCall($sProvider, $sAction, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  723. {
  724. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  725. if($oProvider === false)
  726. BxDolRequest::methodNotFound($sAction, $this->getName());
  727. $sMethod = 'action' . bx_gen_method_name($sAction);
  728. if(!method_exists($oProvider, $sMethod))
  729. BxDolRequest::methodNotFound($sAction, $this->getName());
  730. return call_user_func_array(array($oProvider, $sMethod), array_slice(func_get_args(), 2));
  731. }
  732. public function onProfileJoin($iProfileId)
  733. {
  734. $this->getObjectJoin()->onProfileJoin($iProfileId);
  735. }
  736. public function onProfileDelete($iProfileId)
  737. {
  738. $this->_oDb->onProfileDelete($iProfileId);
  739. }
  740. public function isAllowedPurchase($aItem, $bPerform = false)
  741. {
  742. $iUserId = (int)$this->getProfileId();
  743. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], isset($aItem['custom']) ? $aItem['custom'] : array()));
  744. if(empty($aItemInfo))
  745. return false;
  746. if(isAdmin())
  747. return true;
  748. $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
  749. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  750. return true;
  751. return $aCheckResult[CHECK_ACTION_MESSAGE];
  752. }
  753. public function isAllowedSell($aItem, $bPerform = false)
  754. {
  755. $iUserId = (int)$this->getProfileId();
  756. if(!$iUserId)
  757. return false;
  758. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id']));
  759. if(empty($aItemInfo))
  760. return false;
  761. if(isAdmin())
  762. return true;
  763. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  764. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  765. return true;
  766. return $aCheckResult[CHECK_ACTION_MESSAGE];
  767. }
  768. public function isAllowedManage($aPending, $bPerform = false)
  769. {
  770. $iUserId = (int)$this->getProfileId();
  771. if($iUserId == $aPending['client_id'] || $iUserId == $aPending['seller_id'] || isAdmin())
  772. return true;
  773. $aCheckResult = checkActionModule($iUserId, 'manage any purchase', $this->getName(), $bPerform);
  774. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  775. return true;
  776. return $aCheckResult[CHECK_ACTION_MESSAGE];
  777. }
  778. public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom = array())
  779. {
  780. $CNF = &$this->_oConfig->CNF;
  781. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  782. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  783. $iClientId = $this->getProfileId();
  784. if(empty($iClientId)) {
  785. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  786. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  787. }
  788. $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId, 'custom' => $aCustom));
  789. if($mixedResult !== true) {
  790. if(is_string($mixedResult) && !empty($mixedResult))
  791. return array('code' => 2, 'message' => $mixedResult);
  792. else
  793. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  794. }
  795. if($iClientId == $iSellerId)
  796. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  797. $aSeller = $this->getVendorInfo($iSellerId);
  798. if(!$aSeller['active'])
  799. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  800. return true;
  801. }
  802. public function registerPayment($mixedPending)
  803. {
  804. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  805. if(empty($aPending) || !is_array($aPending))
  806. return false;
  807. $sType = $aPending['type'];
  808. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  809. if($bTypeSingle && (int)$aPending['processed'] == 1)
  810. return true;
  811. $iClientId = (int)$aPending['client_id'];
  812. $sLicense = $this->_oConfig->getLicense();
  813. $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
  814. $aCart = array();
  815. if($bTypeSingle) {
  816. $aCart = $this->_oDb->getCartContent($iClientId);
  817. $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  818. }
  819. $bResult = false;
  820. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  821. foreach($aItems as $aItem) {
  822. $sItem = $this->_oConfig->descriptorA2S(array($aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']));
  823. $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
  824. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  825. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
  826. if(empty($aItemInfo) || !is_array($aItemInfo))
  827. continue;
  828. $this->_oDb->insertOrderProcessed(array(
  829. 'pending_id' => $aPending['id'],
  830. 'client_id' => $aPending['client_id'],
  831. 'seller_id' => $aPending['seller_id'],
  832. 'author_id' => $aItem['vendor_id'],
  833. 'module_id' => (int)$aItem['module_id'],
  834. 'item_id' => (int)$aItem['item_id'],
  835. 'item_count' => (int)$aItem['item_count'],
  836. 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  837. 'license' => $sLicense,
  838. ));
  839. if($bTypeSingle) {
  840. $this->_oConfig->pullCustom($sItem, $aCart['customs']);
  841. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
  842. }
  843. $bResult = true;
  844. }
  845. if($bTypeSingle)
  846. $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
  847. if($bResult) {
  848. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  849. $this->onPaymentRegister($aPending);
  850. }
  851. return $bResult;
  852. }
  853. public function refundPayment($mixedPending)
  854. {
  855. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  856. if(empty($aPending) || !is_array($aPending))
  857. return false;
  858. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  859. $iCanceled = 0;
  860. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  861. foreach($aOrders as $aOrder) {
  862. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  863. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  864. if(!$bResult)
  865. continue;
  866. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  867. $iCanceled++;
  868. }
  869. if($iCanceled != count($aOrders))
  870. return false;
  871. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  872. if($bResult)
  873. $this->onPaymentRefund($aPending);
  874. return $bResult;
  875. }
  876. /**
  877. * @deprecated since version 12.0.0
  878. *
  879. * @use BxPaymentSubscriptions::register instead.
  880. */
  881. public function registerSubscription($aPending, $aParams = array())
  882. {
  883. return $this->getObjectSubscriptions()->register($aPending, $aParams);
  884. }
  885. /**
  886. * @deprecated since version 12.0.0
  887. *
  888. * @use BxPaymentSubscriptions::cancelLocal instead.
  889. */
  890. public function cancelSubscription($mixedPending)
  891. {
  892. return $this->getObjectSubscriptions()->cancelLocal($mixedPending);
  893. }
  894. public function processTimeTracker()
  895. {
  896. if(!defined('BX_DOL_CRON_EXECUTE'))
  897. return;
  898. $aSubscriptions = $this->_oDb->getSubscription(array(
  899. 'type' => 'time_tracker',
  900. 'status_active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
  901. 'status_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
  902. 'status_unpaid' => BX_PAYMENT_SBS_STATUS_UNPAID,
  903. 'pay_attempts_max' => $this->_oConfig->getPayAttemptsMax(),
  904. 'pay_attempts_interval' => $this->_oConfig->getPayAttemptsInterval()
  905. ));
  906. if(empty($aSubscriptions) || !is_array($aSubscriptions))
  907. return;
  908. foreach($aSubscriptions as $aSubscription) {
  909. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aSubscription['pending_id']));
  910. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING) {
  911. $this->log($aSubscription, 'Time Tracker', 'Cannot process subscription.');
  912. continue;
  913. }
  914. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  915. if($oProvider === false || !$oProvider->isActive() || !method_exists($oProvider, 'makePayment')) {
  916. $this->log($aSubscription, 'Time Tracker', 'Payment provider unavailable or incorrect.');
  917. continue;
  918. }
  919. if(!$oProvider->makePayment($aPending)) {
  920. $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed.');
  921. continue;
  922. }
  923. if(!$this->getObjectSubscriptions()->prolong($aPending)) {
  924. $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.');
  925. continue;
  926. }
  927. if(!$this->registerPayment($aPending)) {
  928. $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.');
  929. continue;
  930. }
  931. }
  932. }
  933. public function onPaymentRegisterBefore($aPending, $aResult = array())
  934. {
  935. bx_alert('system', 'before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  936. }
  937. public function onPaymentRegister($aPending, $aResult = array())
  938. {
  939. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  940. if($bTypeSingle) {
  941. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  942. foreach($aItems as $aItem)
  943. $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
  944. }
  945. bx_alert('system', 'register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  946. }
  947. public function onPaymentRefund($aPending, $aResult = array())
  948. {
  949. bx_alert('system', 'refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  950. }
  951. public function onSubscriptionCreate($aPending, $aSubscription, $aResult = array())
  952. {
  953. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  954. $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
  955. bx_alert('system', 'create_subscription', 0, $aPending['client_id'], array(
  956. 'pending' => $aPending,
  957. 'subscription' => $aSubscription
  958. ));
  959. }
  960. public function onSubscriptionProlong($aPending, $aSubscription, $aResult = array())
  961. {
  962. bx_alert('system', 'prolong_subscription', 0, $aPending['client_id'], array(
  963. 'pending' => $aPending,
  964. 'subscription' => $aSubscription
  965. ));
  966. }
  967. public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = array())
  968. {
  969. bx_alert('system', 'overdue_subscription', 0, $aPending['client_id'], array(
  970. 'pending' => $aPending,
  971. 'subscription' => $aSubscription
  972. ));
  973. }
  974. public function onSubscriptionCancel($aPending, $aSubscription, $aResult = array())
  975. {
  976. bx_alert('system', 'cancel_subscription', 0, $aPending['client_id'], array(
  977. 'pending' => $aPending,
  978. 'subscription' => $aSubscription
  979. ));
  980. }
  981. public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
  982. {
  983. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  984. if(!$oSiteSubmenu)
  985. return;
  986. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  987. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  988. if(!$oModuleSubmenu)
  989. return;
  990. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
  991. $oModuleSubmenu->setSelected($sSelModule, $sSelName);
  992. }
  993. public function getProviderButtonJs($aCartItem, $aProvider, $sRedirect = '', $aCustom = array())
  994. {
  995. $aButtonJs = array();
  996. $iClientId = $this->getProfileId();
  997. $sItemAddons = '';
  998. if(count($aCartItem) == 5)
  999. $sItemAddons = array_pop($aCartItem);
  1000. list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $aCartItem;
  1001. $oProvider = $this->getObjectProvider($aProvider['name'], $iSellerId);
  1002. if($oProvider !== false && method_exists($oProvider, 'getButtonRecurring')) {
  1003. $aParams = array(
  1004. 'sObjNameCart' => $this->_oConfig->getJsObject('cart'),
  1005. 'iSellerId' => $iSellerId,
  1006. 'iModuleId' => $iModuleId,
  1007. 'iItemId' => $iItemId,
  1008. 'iItemCount' => $iItemCount,
  1009. 'sItemAddons' => $sItemAddons,
  1010. 'sRedirect' => $sRedirect,
  1011. 'sCustom' => base64_encode(serialize($aCustom))
  1012. );
  1013. $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));
  1014. if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
  1015. $aItem = array_shift($aCartInfo['items']);
  1016. $aParams = array_merge($aParams, array(
  1017. 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
  1018. 'sItemName' => $aItem['name'],
  1019. 'sItemTitle' => $aItem['title']
  1020. ));
  1021. }
  1022. $aButtonJs = $oProvider->getButtonRecurringJs($iClientId, $iSellerId, $aParams);
  1023. }
  1024. if(empty($aButtonJs) || !is_array($aButtonJs))
  1025. $aButtonJs = $this->getObjectSubscriptions()->serviceGetSubscribeJs($iSellerId, $aProvider['name'], $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  1026. return $aButtonJs;
  1027. }
  1028. }
  1029. /** @} */