BxPaymentModule.php 46 KB

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