BxPaymentModule.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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_EMPTY_ID', 0);
  16. define('BX_PAYMENT_RESULT_SUCCESS', 0);
  17. /**
  18. * Payment module by BoonEx
  19. *
  20. * This module is needed to work with payment providers and organize the process
  21. * of some item purchasing. Shopping Cart and Orders Manager are included.
  22. *
  23. * Integration notes:
  24. * To integrate your module with this one, you need:
  25. * 1. Get 'Add To Cart' button using serviceGetAddToCartLink service.
  26. * 2. Add info about your module in the 'bx_pmt_modules' table.
  27. * 3. Realize the following service methods in your Module class.
  28. * a. serviceGetItems($iSellerId) - Is used in Orders Administration to get all products of the requested seller(vendor).
  29. * b. serviceGetCartItem($iClientId, $iItemId) - Is used in Shopping Cart to get one product by specified id.
  30. * c. serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Register purchased product.
  31. * d. serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Unregister the product purchased earlier.
  32. * @see You may see an example of integration in Membership module.
  33. *
  34. *
  35. * Profile's Wall:
  36. * no spy events
  37. *
  38. *
  39. *
  40. * Spy:
  41. * no spy events
  42. *
  43. *
  44. *
  45. * Memberships/ACL:
  46. * Doesn't depend on user's membership.
  47. *
  48. *
  49. *
  50. * Service methods:
  51. *
  52. * Is used to get "Add to cart" link for some item(s) in your module.
  53. * @see BxPmtModule::serviceGetAddToCartLink
  54. * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
  55. *
  56. * Check transaction(s) in database which satisty all conditions.
  57. * @see BxPmtModule::serviceGetOrdersInfo
  58. * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
  59. *
  60. * Get total count of items in Shopping Cart.
  61. * @see BxPmtModule::serviceGetCartItemsCount
  62. * BxDolService::call('payment', 'get_cart_items_count', array($iUserId, $iOldCount));
  63. * @note is needed for internal usage(integration with member tool bar).
  64. *
  65. * Get Shopping cart content.
  66. * @see BxPmtModule::serviceGetCartItems
  67. * BxDolService::call('payment', 'get_cart_items');
  68. * @note is needed for internal usage(integration with member tool bar).
  69. *
  70. *
  71. * Alerts:
  72. * no alerts available
  73. *
  74. */
  75. class BxPaymentModule extends BxBaseModPaymentModule
  76. {
  77. protected $_iUserId;
  78. protected $_aOrderTypes;
  79. function __construct($aModule)
  80. {
  81. parent::__construct($aModule);
  82. $this->_iUserId = $this->getProfileId();
  83. $this->_aOrderTypes = array(
  84. BX_PAYMENT_ORDERS_TYPE_PENDING,
  85. BX_PAYMENT_ORDERS_TYPE_PROCESSED,
  86. BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION,
  87. BX_PAYMENT_ORDERS_TYPE_HISTORY
  88. );
  89. }
  90. /**
  91. * Manage Orders Methods
  92. */
  93. public function actionGetClients()
  94. {
  95. $sTerm = bx_get('term');
  96. $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  97. echoJson($aResult);
  98. }
  99. public function actionGetItems($sType, $iModuleId)
  100. {
  101. $iSellerId = $this->getProfileId();
  102. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  103. echoJson(array(
  104. 'code' => 0,
  105. 'data' => $this->_oTemplate->displayItems($sType, $aItems)
  106. ));
  107. }
  108. /**
  109. * @page service Service Calls
  110. * @section bx_payment Payment
  111. * @subsection bx_payment-purchase_processing Purchase Processing
  112. * @subsubsection bx_payment-is_accepting_payments is_accepting_payments
  113. *
  114. * @code bx_srv('bx_payment', 'is_accepting_payments', [...]); @endcode
  115. *
  116. * Check whether the specified vendor has a configured payment provider or not.
  117. *
  118. * @param $iVendorId integer value with vendor ID.
  119. * @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.
  120. * @return boolean value determining the result of checking.
  121. *
  122. * @see BxPaymentModule::serviceIsAcceptingPayments
  123. */
  124. /**
  125. * @ref bx_payment-is_accepting_payments "is_accepting_payments"
  126. */
  127. public function serviceIsAcceptingPayments($iVendorId, $sPaymentType = '')
  128. {
  129. $bResult = false;
  130. switch($sPaymentType) {
  131. case BX_PAYMENT_TYPE_SINGLE:
  132. $aProvidersCart = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  133. $bResult = !empty($aProvidersCart);
  134. break;
  135. case BX_PAYMENT_TYPE_RECURRING:
  136. $aProvidersSubscription = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  137. $bResult = !empty($aProvidersSubscription);
  138. break;
  139. default:
  140. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  141. $bResult = !empty($aProviders);
  142. }
  143. return $bResult;
  144. }
  145. /**
  146. * @page service Service Calls
  147. * @section bx_payment Payment
  148. * @subsection bx_payment-purchase_processing Purchase Processing
  149. * @subsubsection bx_payment-is_payment_provider is_payment_provider
  150. *
  151. * @code bx_srv('bx_payment', 'is_payment_provider', [...]); @endcode
  152. *
  153. * Check whether the specified vendor has configured the specified payment provider or not.
  154. *
  155. * @param $iVendorId integer value with vendor ID.
  156. * @param $sVendorProvider string value with payment provider name.
  157. * @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.
  158. * @return boolean value determining the result of checking.
  159. *
  160. * @see BxPaymentModule::serviceIsPaymentProvider
  161. */
  162. /**
  163. * @ref bx_payment-is_payment_provider "is_payment_provider"
  164. */
  165. public function serviceIsPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  166. {
  167. $aProvider = $this->serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType);
  168. return $aProvider !== false;
  169. }
  170. /**
  171. * @page service Service Calls
  172. * @section bx_payment Payment
  173. * @subsection bx_payment-purchase_processing Purchase Processing
  174. * @subsubsection bx_payment-get_payment_provider get_payment_provider
  175. *
  176. * @code bx_srv('bx_payment', 'is_payment_provider', [...]); @endcode
  177. *
  178. * Get configuration settings for the payment provider entered by the specified vendor.
  179. *
  180. * @param $iVendorId integer value with vendor ID.
  181. * @param $sVendorProvider string value with payment provider name.
  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 an array with special format describing the payment provider or false value if something is wrong.
  184. *
  185. * @see BxPaymentModule::serviceGetPaymentProvider
  186. */
  187. /**
  188. * @ref bx_payment-get_payment_provider "get_payment_provider"
  189. */
  190. public function serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  191. {
  192. $aProviders = array();
  193. switch($sPaymentType) {
  194. case BX_PAYMENT_TYPE_SINGLE:
  195. $aProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  196. break;
  197. case BX_PAYMENT_TYPE_RECURRING:
  198. $aProviders = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  199. break;
  200. default:
  201. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  202. }
  203. return !empty($aProviders) && !empty($aProviders[$sVendorProvider]) && is_array(($aProviders[$sVendorProvider])) ? $aProviders[$sVendorProvider] : false;
  204. }
  205. /**
  206. * @page service Service Calls
  207. * @section bx_payment Payment
  208. * @subsection bx_payment-other Other
  209. * @subsubsection bx_payment-get_options_default_currency_code get_options_default_currency_code
  210. *
  211. * @code bx_srv('bx_payment', 'get_options_default_currency_code', [...]); @endcode
  212. *
  213. * Get an array with available currencies. Is used in forms.
  214. *
  215. * @return an array with available currencies represented as key => value pairs.
  216. *
  217. * @see BxPaymentModule::serviceGetOptionsDefaultCurrencyCode
  218. */
  219. /**
  220. * @ref bx_payment-get_options_default_currency_code "get_options_default_currency_code"
  221. */
  222. public function serviceGetOptionsDefaultCurrencyCode()
  223. {
  224. $CNF = &$this->_oConfig->CNF;
  225. $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES']);
  226. $aResult = array();
  227. foreach($aCurrencies as $sKey => $sValue)
  228. $aResult[] = array(
  229. 'key' => $sKey,
  230. 'value' => $sValue
  231. );
  232. return $aResult;
  233. }
  234. /**
  235. * @page service Service Calls
  236. * @section bx_payment Payment
  237. * @subsection bx_payment-other Other
  238. * @subsubsection bx_payment-get_options_site_admin get_options_site_admin
  239. *
  240. * @code bx_srv('bx_payment', 'get_options_site_admin', [...]); @endcode
  241. *
  242. * 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.
  243. *
  244. * @return an array with profiles represented as key => value pairs.
  245. *
  246. * @see BxPaymentModule::serviceGetOptionsSiteAdmin
  247. */
  248. /**
  249. * @ref bx_payment-get_options_site_admin "get_options_site_admin"
  250. */
  251. public function serviceGetOptionsSiteAdmin()
  252. {
  253. $aResult = array(
  254. array('key' => '', 'value' => _t('_Select_one'))
  255. );
  256. $aIds = $this->_oDb->getAdminsIds();
  257. foreach($aIds as $iId) {
  258. $aUser = $this->getProfileInfo($iId);
  259. $aResult[] = array(
  260. 'key' => $iId,
  261. 'value' => $aUser['name']
  262. );
  263. }
  264. return $aResult;
  265. }
  266. /**
  267. * Cart Processing Methods
  268. */
  269. public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $sCustom = '')
  270. {
  271. if(empty($sCustom) && bx_get('custom') !== false)
  272. $sCustom = bx_process_input(bx_get('custom'));
  273. $aCustom = array();
  274. if(!empty($sCustom)) {
  275. $aCustom = unserialize(base64_decode($sCustom));
  276. if(empty($aCustom) || !is_array($aCustom))
  277. $aCustom = array();
  278. }
  279. $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom);
  280. echoJson($aResult);
  281. }
  282. /**
  283. * Isn't used yet.
  284. */
  285. public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
  286. {
  287. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId);
  288. echoJson($aResult);
  289. }
  290. /**
  291. * Isn't used yet.
  292. */
  293. public function actionEmptyCart($iSellerId)
  294. {
  295. $aResult = $this->getObjectCart()->serviceDeleteFromCart($iSellerId);
  296. echoJson($aResult);
  297. }
  298. public function actionSubscribe()
  299. {
  300. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  301. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  302. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  303. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  304. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  305. if(empty($iItemCount))
  306. $iItemCount = 1;
  307. $sRedirect = bx_process_input(bx_get('redirect'));
  308. $sCustom = bx_process_input(bx_get('custom'));
  309. $aCustom = array();
  310. if(!empty($sCustom)) {
  311. $aCustom = unserialize(base64_decode($sCustom));
  312. if(empty($aCustom) || !is_array($aCustom))
  313. $aCustom = array();
  314. }
  315. $aResult = $this->getObjectSubscriptions()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  316. $bRedirect = !empty($aResult['redirect']);
  317. if(!empty($aResult['popup'])) {
  318. $sContent = '';
  319. $sContent .= $this->_oTemplate->displayCartJs(BX_PAYMENT_TYPE_RECURRING, $iSellerId);
  320. $sContent .= !empty($aResult['popup']['html']) ? $aResult['popup']['html'] : $aResult['popup'];
  321. return $this->_oTemplate->displayPageCodeResponse($sContent, false, true);
  322. }
  323. if(!empty($aResult['code']))
  324. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  325. if(!empty($aResult['message']) && !$bRedirect)
  326. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  327. if($bRedirect) {
  328. header('Location: ' . $aResult['redirect']);
  329. exit;
  330. }
  331. }
  332. public function actionSubscribeJson()
  333. {
  334. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  335. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  336. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  337. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  338. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  339. if(empty($iItemCount))
  340. $iItemCount = 1;
  341. $sRedirect = bx_process_input(bx_get('redirect'));
  342. $sCustom = bx_process_input(bx_get('custom'));
  343. $aCustom = array();
  344. if(!empty($sCustom)) {
  345. $aCustom = unserialize(base64_decode($sCustom));
  346. if(empty($aCustom) || !is_array($aCustom))
  347. $aCustom = array();
  348. }
  349. $aResult = $this->getObjectSubscriptions()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  350. echoJson($aResult);
  351. }
  352. public function actionSubscriptionGetDetails($iId)
  353. {
  354. $aResult = $this->_oTemplate->displaySubscriptionGetDetails($iId);
  355. echoJson($aResult);
  356. }
  357. public function actionSubscriptionChangeDetails($iId)
  358. {
  359. $aResult = $this->_oTemplate->displaySubscriptionChangeDetails($iId);
  360. echoJson($aResult);
  361. }
  362. public function actionSubscriptionGetBilling($iId)
  363. {
  364. $aResult = $this->_oTemplate->displaySubscriptionGetBilling($iId);
  365. echoJson($aResult);
  366. }
  367. public function actionSubscriptionChangeBilling($iId)
  368. {
  369. $aResult = $this->_oTemplate->displaySubscriptionChangeBilling($iId);
  370. echoJson($aResult);
  371. }
  372. public function actionSubscriptionCancelation($iId)
  373. {
  374. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  375. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  376. if(empty($aPending) && !is_array($aPending))
  377. return echoJson($aResult);
  378. $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
  379. if(empty($aSubscription) && !is_array($aSubscription))
  380. return echoJson($aResult);
  381. $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
  382. if(!$oRecipient)
  383. return echoJson($aResult);
  384. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
  385. 'sibscription_id' => $aSubscription['subscription_id'],
  386. 'sibscription_customer' => $aSubscription['customer_id'],
  387. 'sibscription_date' => bx_time_js($aSubscription['date'], BX_FORMAT_DATE, true)
  388. ), 0, (int)$aPending['client_id']);
  389. $sEmail = '';
  390. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  391. if($oProvider !== false && $oProvider->isActive())
  392. $sEmail = $oProvider->getOption('cancellation_email');
  393. if(empty($sEmail))
  394. $sEmail = $oRecipient->getAccountObject()->getEmail();
  395. if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  396. return echoJson($aResult);
  397. echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
  398. }
  399. /**
  400. * Payment Processing Methods
  401. */
  402. public function actionInitializeCheckout($sType)
  403. {
  404. if(!$this->isLogged())
  405. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  406. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  407. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  408. $sProvider = bx_process_input(bx_get('provider'));
  409. $aItems = bx_process_input(bx_get('items'));
  410. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  411. if($mixedResult !== true)
  412. return $this->_oTemplate->displayPageCodeError($mixedResult);
  413. }
  414. header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
  415. exit;
  416. }
  417. /**
  418. * @page service Service Calls
  419. * @section bx_payment Payment
  420. * @subsection bx_payment-purchase_processing Purchase Processing
  421. * @subsubsection bx_payment-initialize_checkout initialize_checkout
  422. *
  423. * @code bx_srv('bx_payment', 'initialize_checkout', [...]); @endcode
  424. *
  425. * Initialize the checkout process.
  426. *
  427. * @param $sType string value with payment type (single or recurring).
  428. * @param $iSellerId integer value with seller ID.
  429. * @param $sProvider string value with payment provider name.
  430. * @param $aItems (optional) an array with items to be purchased.
  431. * @param $sRedirect (optional) string value with redirect URL if it's needed.
  432. * @param $aCustom (optional) array with custom data to attach to a payment.
  433. * @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.
  434. *
  435. * @see BxPaymentModule::serviceInitializeCheckout
  436. */
  437. /**
  438. * @ref bx_payment-initialize_checkout "initialize_checkout"
  439. */
  440. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '', $aCustoms = array())
  441. {
  442. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  443. $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
  444. if(!is_array($aItems))
  445. $aItems = array($aItems);
  446. $iSellerId = (int)$iSellerId;
  447. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  448. return $this->_sLangsPrefix . 'err_unknown_vendor';
  449. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  450. if($oProvider === false || !$oProvider->isActive())
  451. return $this->_sLangsPrefix . 'err_incorrect_provider';
  452. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  453. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  454. return $this->_sLangsPrefix . 'err_empty_order';
  455. /*
  456. * Process FREE (price = 0) items for LOGGED IN members
  457. * WITHOUT processing via payment provider.
  458. * Note. This section isn't used for now!
  459. */
  460. $bProcessedFree = false;
  461. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  462. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  463. foreach($aInfo['items'] as $iIndex => $aItem) {
  464. if((int)$aInfo['client_id'] == 0)
  465. continue;
  466. //--- For Single time payments.
  467. if($bTypeSingle && (float)$aItem[$sKeyPriceSingle] == 0) {
  468. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  469. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  470. $aCartItem = array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity']);
  471. $aCartItemCustom = $this->_oConfig->pullCustom($aCartItem, $aCartCustoms);
  472. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  473. if(is_array($aItemInfo) && !empty($aItemInfo))
  474. $bProcessedFree = true;
  475. $aInfo['items_count'] -= 1;
  476. unset($aInfo['items'][$iIndex]);
  477. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aCartItem) . ":?'", "", $aCart['items']), ":");
  478. $this->_oDb->setCartItems($aInfo['client_id'], $aCart['items'], $aCartCustoms);
  479. }
  480. //--- For Recurring payments.
  481. if($bTypeRecurring && (float)$aItem[$sKeyPriceRecurring] == 0) {
  482. //TODO: Process FREE subscription here, if such situation will be possible.
  483. }
  484. }
  485. if(empty($aInfo['items']))
  486. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  487. if($bTypeSingle && (empty($aCustoms) || !is_array($aCustoms))) {
  488. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  489. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  490. $aCustoms = array();
  491. foreach($aInfo['items'] as $aItem) {
  492. $sCartItem = $this->_oConfig->descriptorA2S(array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id']));
  493. $aCartItemCustom = $this->_oConfig->getCustom($sCartItem, $aCartCustoms);
  494. $this->_oConfig->putCustom($sCartItem, $aCartItemCustom, $aCustoms);
  495. }
  496. }
  497. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo, $aCustoms);
  498. if(empty($iPendingId))
  499. return $this->_sLangsPrefix . 'err_access_db';
  500. /*
  501. * Perform Join WITHOUT processing via payment provider
  502. * if a client ISN'T logged in and has only ONE FREE item in the card.
  503. * Note. This section isn't used for now because only a member can purchase!
  504. */
  505. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  506. reset($aInfo['items']);
  507. $aItem = current($aInfo['items']);
  508. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  509. $this->_oDb->updateOrderPending($iPendingId, array(
  510. 'order' => $this->_oConfig->getLicense(),
  511. 'error_code' => '1',
  512. 'error_msg' => ''
  513. ));
  514. $this->getObjectJoin()->performJoin($iPendingId);
  515. }
  516. }
  517. return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
  518. }
  519. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  520. {
  521. $aData = &$_REQUEST;
  522. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  523. if($oProvider === false || !$oProvider->isActive())
  524. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  525. $aResult = $oProvider->finalizeCheckout($aData);
  526. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  527. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  528. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  529. $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
  530. if($bTypeRecurring)
  531. $this->registerSubscription($aPending, array(
  532. 'customer_id' => $aResult['customer_id'],
  533. 'subscription_id' => $aResult['subscription_id']
  534. ));
  535. $this->onPaymentRegisterBefore($aPending);
  536. //--- Check "Pay Before Join" situation
  537. if((int)$aPending['client_id'] == 0)
  538. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  539. //--- Register payment for purchased items in associated modules
  540. if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
  541. $this->registerPayment($aPending);
  542. bx_alert($this->getName(), 'finalize_checkout', 0, bx_get_logged_profile_id(), array(
  543. 'pending' => $aPending,
  544. 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
  545. 'provider' => $oProvider,
  546. 'message' => &$aResult['message'],
  547. ));
  548. if($oProvider->needRedirect()) {
  549. header('Location: ' . $oProvider->getReturnUrl());
  550. exit;
  551. }
  552. if(!empty($aResult['redirect'])) {
  553. header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
  554. exit;
  555. }
  556. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  557. }
  558. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  559. {
  560. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  561. if($oProvider === false || !$oProvider->isActive())
  562. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  563. $aResult = $oProvider->finalizedCheckout();
  564. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  565. }
  566. public function actionNotify($sProvider, $mixedVendorId = "")
  567. {
  568. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  569. if($oProvider === false || !$oProvider->isActive())
  570. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  571. $oProvider->notify();
  572. }
  573. public function onProfileJoin($iProfileId)
  574. {
  575. $this->getObjectJoin()->onProfileJoin($iProfileId);
  576. }
  577. public function onProfileDelete($iProfileId)
  578. {
  579. $this->_oDb->onProfileDelete($iProfileId);
  580. }
  581. public function isAllowedPurchase($aItem, $bPerform = false)
  582. {
  583. $iUserId = (int)$this->getProfileId();
  584. $aItemInfo = $this->callGetCartItem($aItem['module_id'], array($aItem['item_id']));
  585. if(empty($aItemInfo))
  586. return false;
  587. if(isAdmin())
  588. return true;
  589. $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
  590. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  591. return true;
  592. return $aCheckResult[CHECK_ACTION_MESSAGE];
  593. }
  594. public function isAllowedSell($aItem, $bPerform = false)
  595. {
  596. $iUserId = (int)$this->getProfileId();
  597. if(!$iUserId)
  598. return false;
  599. $aItemInfo = $this->callGetCartItem($aItem['module_id'], array($aItem['item_id']));
  600. if(empty($aItemInfo))
  601. return false;
  602. if(isAdmin())
  603. return true;
  604. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  605. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  606. return true;
  607. return $aCheckResult[CHECK_ACTION_MESSAGE];
  608. }
  609. public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount)
  610. {
  611. $CNF = &$this->_oConfig->CNF;
  612. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  613. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  614. $iClientId = $this->getProfileId();
  615. if(empty($iClientId)) {
  616. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  617. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  618. }
  619. $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId));
  620. if($mixedResult !== true) {
  621. if(is_string($mixedResult) && !empty($mixedResult))
  622. return array('code' => 2, 'message' => $mixedResult);
  623. else
  624. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  625. }
  626. if($iClientId == $iSellerId)
  627. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  628. $aSeller = $this->getVendorInfo($iSellerId);
  629. if(!$aSeller['active'])
  630. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  631. return true;
  632. }
  633. public function registerPayment($mixedPending)
  634. {
  635. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  636. if(empty($aPending) || !is_array($aPending))
  637. return false;
  638. $sType = $aPending['type'];
  639. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  640. if($bTypeSingle && (int)$aPending['processed'] == 1)
  641. return true;
  642. $iClientId = (int)$aPending['client_id'];
  643. $sLicense = $this->_oConfig->getLicense();
  644. $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
  645. $aCart = array();
  646. if($bTypeSingle) {
  647. $aCart = $this->_oDb->getCartContent($iClientId);
  648. $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  649. }
  650. $bResult = false;
  651. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  652. foreach($aItems as $aItem) {
  653. $sItem = $this->_oConfig->descriptorA2S(array($aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']));
  654. $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
  655. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  656. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
  657. if(empty($aItemInfo) || !is_array($aItemInfo))
  658. continue;
  659. $this->_oDb->insertOrderProcessed(array(
  660. 'pending_id' => $aPending['id'],
  661. 'client_id' => $aPending['client_id'],
  662. 'seller_id' => $aPending['seller_id'],
  663. 'module_id' => (int)$aItem['module_id'],
  664. 'item_id' => (int)$aItem['item_id'],
  665. 'item_count' => (int)$aItem['item_count'],
  666. 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  667. 'license' => $sLicense,
  668. ));
  669. if($bTypeSingle) {
  670. $this->_oConfig->pullCustom($sItem, $aCart['customs']);
  671. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
  672. }
  673. $bResult = true;
  674. }
  675. if($bTypeSingle)
  676. $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
  677. if($bResult) {
  678. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  679. $this->onPaymentRegister($aPending);
  680. }
  681. return $bResult;
  682. }
  683. public function refundPayment($mixedPending)
  684. {
  685. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  686. if(empty($aPending) || !is_array($aPending))
  687. return false;
  688. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  689. $iCanceled = 0;
  690. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  691. foreach($aOrders as $aOrder) {
  692. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  693. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  694. if(!$bResult)
  695. continue;
  696. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  697. $iCanceled++;
  698. }
  699. if($iCanceled != count($aOrders))
  700. return false;
  701. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  702. if($bResult)
  703. $this->onPaymentRefund($aPending);
  704. return $bResult;
  705. }
  706. public function registerSubscription($aPending, $aParams = array())
  707. {
  708. $this->_oDb->insertSubscription(array(
  709. 'pending_id' => $aPending['id'],
  710. 'customer_id' => $aParams['customer_id'],
  711. 'subscription_id' => $aParams['subscription_id']
  712. ));
  713. $this->onSubscriptionCreate($aPending);
  714. }
  715. public function updateSubscription($aPending, $aParams = array())
  716. {
  717. $this->_oDb->updateSubscription($aParams, array(
  718. 'pending_id' => $aPending['id']
  719. ));
  720. $this->onSubscriptionUpdate($aPending);
  721. }
  722. public function cancelSubscription($mixedPending)
  723. {
  724. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  725. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING)
  726. return false;
  727. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  728. foreach($aItems as $aItem)
  729. $this->callCancelSubscriptionItem((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order']));
  730. $this->onSubscriptionCancel($aPending);
  731. }
  732. public function onPaymentRegisterBefore($aPending, $aResult = array())
  733. {
  734. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  735. bx_alert('system', 'before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  736. //--- 'System' -> 'Before Register Payment' for Alerts Engine ---//
  737. }
  738. public function onPaymentRegister($aPending, $aResult = array())
  739. {
  740. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  741. if($bTypeSingle) {
  742. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  743. foreach($aItems as $aItem)
  744. $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
  745. }
  746. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  747. bx_alert('system', 'register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  748. //--- 'System' -> 'Register Payment' for Alerts Engine ---//
  749. }
  750. public function onPaymentRefund($aPending, $aResult = array())
  751. {
  752. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  753. bx_alert('system', 'refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  754. //--- 'System' -> 'Refund Payment' for Alerts Engine ---//
  755. }
  756. public function onSubscriptionCreate($aPending, $aResult = array())
  757. {
  758. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  759. $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
  760. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  761. bx_alert('system', 'create_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  762. //--- 'System' -> 'Create Subscription' for Alerts Engine ---//
  763. }
  764. public function onSubscriptionUpdate($aPending, $aResult = array())
  765. {
  766. //--- 'System' -> 'Update Subscription' for Alerts Engine ---//
  767. bx_alert('system', 'update_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  768. //--- 'System' -> 'Update Subscription' for Alerts Engine ---//
  769. }
  770. public function onSubscriptionCancel($aPending, $aResult = array())
  771. {
  772. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  773. bx_alert('system', 'cancel_subscription', 0, $aPending['client_id'], array('pending' => $aPending));
  774. //--- 'System' -> 'Cancel Subscription' for Alerts Engine ---//
  775. }
  776. public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
  777. {
  778. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  779. if(!$oSiteSubmenu)
  780. return;
  781. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  782. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  783. if(!$oModuleSubmenu)
  784. return;
  785. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
  786. $oModuleSubmenu->setSelected($sSelModule, $sSelName);
  787. }
  788. }
  789. /** @} */