BxPaymentModule.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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. * Invoice: statuses.
  37. */
  38. define('BX_PAYMENT_INV_STATUS_UNPAID', 'unpaid');
  39. define('BX_PAYMENT_INV_STATUS_PAID', 'paid');
  40. define('BX_PAYMENT_INV_STATUS_OVERDUE', 'overdue');
  41. /**
  42. * Payment module by BoonEx
  43. *
  44. * This module is needed to work with payment providers and organize the process
  45. * of some item purchasing. Shopping Cart and Orders Manager are included.
  46. *
  47. * Integration notes:
  48. * To integrate your module with this one, you need:
  49. * 1. Get 'Add To Cart' button using serviceGetAddToCartLink service.
  50. * 2. Add info about your module in the 'bx_pmt_modules' table.
  51. * 3. Realize the following service methods in your Module class.
  52. * a. serviceGetItems($iSellerId) - Is used in Orders Administration to get all products of the requested seller(vendor).
  53. * b. serviceGetCartItem($iClientId, $iItemId) - Is used in Shopping Cart to get one product by specified id.
  54. * c. serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Register purchased product.
  55. * d. serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Unregister the product purchased earlier.
  56. * @see You may see an example of integration in Membership module.
  57. *
  58. *
  59. * Profile's Wall:
  60. * no spy events
  61. *
  62. *
  63. *
  64. * Spy:
  65. * no spy events
  66. *
  67. *
  68. *
  69. * Memberships/ACL:
  70. * Doesn't depend on user's membership.
  71. *
  72. *
  73. *
  74. * Service methods:
  75. *
  76. * Is used to get "Add to cart" link for some item(s) in your module.
  77. * @see BxPmtModule::serviceGetAddToCartLink
  78. * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
  79. *
  80. * Check transaction(s) in database which satisty all conditions.
  81. * @see BxPmtModule::serviceGetOrdersInfo
  82. * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
  83. *
  84. * Get total count of items in Shopping Cart.
  85. * @see BxPmtModule::serviceGetCartItemsCount
  86. * BxDolService::call('payment', 'get_cart_items_count', array($iUserId, $iOldCount));
  87. * @note is needed for internal usage(integration with member tool bar).
  88. *
  89. * Get Shopping cart content.
  90. * @see BxPmtModule::serviceGetCartItems
  91. * BxDolService::call('payment', 'get_cart_items');
  92. * @note is needed for internal usage(integration with member tool bar).
  93. *
  94. *
  95. * Alerts:
  96. * no alerts available
  97. *
  98. */
  99. class BxPaymentModule extends BxBaseModPaymentModule
  100. {
  101. protected $_iUserId;
  102. protected $_aOrderTypes;
  103. function __construct($aModule)
  104. {
  105. parent::__construct($aModule);
  106. $this->_iUserId = $this->getProfileId();
  107. $this->_aOrderTypes = array(
  108. BX_PAYMENT_ORDERS_TYPE_PENDING,
  109. BX_PAYMENT_ORDERS_TYPE_PROCESSED,
  110. BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION,
  111. BX_PAYMENT_ORDERS_TYPE_HISTORY
  112. );
  113. }
  114. /**
  115. * Manage Orders Methods
  116. */
  117. public function actionGetClients()
  118. {
  119. $sTerm = bx_get('term');
  120. $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  121. echoJson($aResult);
  122. }
  123. public function actionGetItems($sType, $iModuleId)
  124. {
  125. $iSellerId = $this->getProfileId();
  126. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  127. echoJson(array(
  128. 'code' => 0,
  129. 'eval' => $this->_oConfig->getJsObject('processed') . '.onSelectModule(oData);',
  130. 'data' => $this->_oTemplate->displayItems($sType, $aItems)
  131. ));
  132. }
  133. public function actionGetFilterValuesItem($iSellerId, $iModuleId)
  134. {
  135. $sItems = '<option value="">' . _t('_bx_payment_txt_all_items') . '</option>';
  136. if(empty($iSellerId) || empty($iModuleId))
  137. return echoJson(array('code' => 1, 'content' => $sItems));
  138. $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
  139. $this->_oConfig->sortByColumn('title', $aItems);
  140. foreach($aItems as $aItem)
  141. $sItems .= '<option value="' . $aItem['id'] . '">' . $aItem['title'] . '</option>';
  142. echoJson(array('code' => 0, 'content' => $sItems));
  143. }
  144. public function serviceGetSafeServices()
  145. {
  146. $a = parent::serviceGetSafeServices();
  147. return array_merge($a, array (
  148. 'GetBlockJoin' => 'BxPaymentJoin',
  149. 'GetBlockCarts' => 'BxPaymentCart',
  150. 'GetBlockCart' => 'BxPaymentCart',
  151. 'GetBlockCartHistory' => 'BxPaymentCart',
  152. 'GetBlockListMy' => 'BxPaymentSubscriptions',
  153. 'GetBlockHistory' => 'BxPaymentSubscriptions',
  154. ));
  155. }
  156. /**
  157. * @page service Service Calls
  158. * @section bx_payment Payment
  159. * @subsection bx_payment-purchase_processing Purchase Processing
  160. * @subsubsection bx_payment-get_modules_with_payments get_modules_with_payments
  161. *
  162. * @code bx_srv('bx_payment', 'get_modules_with_payments', []); @endcode
  163. *
  164. * Get modules with payments functionality
  165. *
  166. * @return array with modules names
  167. *
  168. * @see BxPaymentModule::serviceGetModulesWithPayments
  169. */
  170. /**
  171. * @ref bx_payment-get_modules_with_payments "get_modules_with_payments"
  172. */
  173. public function serviceGetModulesWithPayments()
  174. {
  175. return $this->_oDb->getModulesWithPayments();
  176. }
  177. /**
  178. * @page service Service Calls
  179. * @section bx_payment Payment
  180. * @subsection bx_payment-purchase_processing Purchase Processing
  181. * @subsubsection bx_payment-is_credits_only is_credits_only
  182. *
  183. * @code bx_srv('bx_payment', 'is_credits_only'); @endcode
  184. *
  185. * Check whether the 'Credits Only' mode is enabled or not.
  186. *
  187. * @return boolean value determining the result of checking.
  188. *
  189. * @see BxPaymentModule::serviceIsCreditsOnly
  190. */
  191. /**
  192. * @ref bx_payment-is_credits_only "is_credits_only"
  193. */
  194. public function serviceIsCreditsOnly()
  195. {
  196. return $this->_oConfig->isCreditsOnly();
  197. }
  198. /**
  199. * @page service Service Calls
  200. * @section bx_payment Payment
  201. * @subsection bx_payment-purchase_processing Purchase Processing
  202. * @subsubsection bx_payment-is_accepting_payments is_accepting_payments
  203. *
  204. * @code bx_srv('bx_payment', 'is_accepting_payments', [...]); @endcode
  205. *
  206. * Check whether the specified vendor has a configured payment provider or not.
  207. *
  208. * @param $iVendorId integer value with vendor ID.
  209. * @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.
  210. * @return boolean value determining the result of checking.
  211. *
  212. * @see BxPaymentModule::serviceIsAcceptingPayments
  213. */
  214. /**
  215. * @ref bx_payment-is_accepting_payments "is_accepting_payments"
  216. */
  217. public function serviceIsAcceptingPayments($iVendorId, $sPaymentType = '')
  218. {
  219. $bResult = false;
  220. switch($sPaymentType) {
  221. case BX_PAYMENT_TYPE_SINGLE:
  222. $aProvidersCart = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  223. $bResult = !empty($aProvidersCart);
  224. break;
  225. case BX_PAYMENT_TYPE_RECURRING:
  226. $aProvidersSubscription = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  227. $bResult = !empty($aProvidersSubscription);
  228. break;
  229. default:
  230. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  231. $bResult = !empty($aProviders);
  232. }
  233. return $bResult;
  234. }
  235. /**
  236. * @deprecated since version 11.0.0
  237. *
  238. * @see BxPaymentModule::serviceIsProviderOptions
  239. */
  240. public function serviceIsPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  241. {
  242. return $this->serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  243. }
  244. /**
  245. * @deprecated since version 11.0.0
  246. *
  247. * @see BxPaymentModule::serviceGetProviderOptions
  248. */
  249. public function serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
  250. {
  251. return $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  252. }
  253. /**
  254. * @page service Service Calls
  255. * @section bx_payment Payment
  256. * @subsection bx_payment-purchase_processing Purchase Processing
  257. * @subsubsection bx_payment-is_provider_options is_provider_options
  258. *
  259. * @code bx_srv('bx_payment', 'is_provider_options', [...]); @endcode
  260. *
  261. * Check whether the specified vendor has configured the specified payment provider or not.
  262. *
  263. * @param $iVendorId integer value with vendor ID.
  264. * @param $sVendorProvider string value with payment provider name.
  265. * @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.
  266. * @return boolean value determining the result of checking.
  267. *
  268. * @see BxPaymentModule::serviceIsProviderOptions
  269. */
  270. /**
  271. * @ref bx_payment-is_provider_options "is_provider_options"
  272. */
  273. public function serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  274. {
  275. $aProvider = $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
  276. return $aProvider !== false;
  277. }
  278. /**
  279. * @page service Service Calls
  280. * @section bx_payment Payment
  281. * @subsection bx_payment-purchase_processing Purchase Processing
  282. * @subsubsection bx_payment-get_provider_options get_provider_options
  283. *
  284. * @code bx_srv('bx_payment', 'get_provider_options', [...]); @endcode
  285. *
  286. * Get configuration settings for the payment provider entered by the specified vendor.
  287. *
  288. * @param $iVendorId integer value with vendor ID.
  289. * @param $sVendorProvider string value with payment provider name.
  290. * @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.
  291. * @return an array with special format describing the payment provider or false value if something is wrong.
  292. *
  293. * @see BxPaymentModule::serviceGetProviderOptions
  294. */
  295. /**
  296. * @ref bx_payment-get_provider_options "get_provider_options"
  297. */
  298. public function serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
  299. {
  300. $aProviders = array();
  301. switch($sPaymentType) {
  302. case BX_PAYMENT_TYPE_SINGLE:
  303. $aProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
  304. break;
  305. case BX_PAYMENT_TYPE_RECURRING:
  306. $aProviders = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
  307. break;
  308. default:
  309. $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
  310. }
  311. return !empty($aProviders) && !empty($aProviders[$sVendorProvider]) && is_array(($aProviders[$sVendorProvider])) ? $aProviders[$sVendorProvider] : false;
  312. }
  313. /**
  314. * @page service Service Calls
  315. * @section bx_payment Payment
  316. * @subsection bx_payment-other Other
  317. * @subsubsection bx_payment-get_options_default_currency_code get_options_default_currency_code
  318. *
  319. * @code bx_srv('bx_payment', 'get_options_default_currency_code', [...]); @endcode
  320. *
  321. * Get an array with available currencies. Is used in forms.
  322. *
  323. * @return an array with available currencies represented as key => value pairs.
  324. *
  325. * @see BxPaymentModule::serviceGetOptionsDefaultCurrencyCode
  326. */
  327. /**
  328. * @ref bx_payment-get_options_default_currency_code "get_options_default_currency_code"
  329. */
  330. public function serviceGetOptionsDefaultCurrencyCode()
  331. {
  332. $CNF = &$this->_oConfig->CNF;
  333. $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES']);
  334. $aResult = array();
  335. foreach($aCurrencies as $sKey => $sValue)
  336. $aResult[] = array(
  337. 'key' => $sKey,
  338. 'value' => $sValue
  339. );
  340. return $aResult;
  341. }
  342. /**
  343. * @page service Service Calls
  344. * @section bx_payment Payment
  345. * @subsection bx_payment-other Other
  346. * @subsubsection bx_payment-get_options_site_admin get_options_site_admin
  347. *
  348. * @code bx_srv('bx_payment', 'get_options_site_admin', [...]); @endcode
  349. *
  350. * 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.
  351. *
  352. * @return an array with profiles represented as key => value pairs.
  353. *
  354. * @see BxPaymentModule::serviceGetOptionsSiteAdmin
  355. */
  356. /**
  357. * @ref bx_payment-get_options_site_admin "get_options_site_admin"
  358. */
  359. public function serviceGetOptionsSiteAdmin()
  360. {
  361. $aResult = array(
  362. array('key' => '', 'value' => _t('_Select_one'))
  363. );
  364. $aIds = $this->_oDb->getAdminsIds();
  365. foreach($aIds as $iId) {
  366. $aUser = $this->getProfileInfo($iId);
  367. $aResult[] = array(
  368. 'key' => $iId,
  369. 'value' => $aUser['name']
  370. );
  371. }
  372. return $aResult;
  373. }
  374. public function serviceGetMenuAddonManageTools()
  375. {
  376. return array(
  377. 'counter1_value' => (int)$this->_oDb->getInvoices(array('type' => 'status', 'status' => BX_PAYMENT_INV_STATUS_PAID, 'count' => true)),
  378. 'counter1_caption' => _t('_bx_payment_menu_item_title_admt_addon_counter1_caption'),
  379. 'counter2_value' => (int)$this->_oDb->getInvoices(array('type' => 'status', 'status' => BX_PAYMENT_INV_STATUS_OVERDUE, 'count' => true)),
  380. 'counter2_caption' => _t('_bx_payment_menu_item_title_admt_addon_counter2_caption'),
  381. 'counter3_value' => (int)$this->_oDb->getInvoices(array('type' => 'all_count'))
  382. );
  383. }
  384. public function serviceGetBlockCheckoutOffline()
  385. {
  386. $CNF = &$this->_oConfig->CNF;
  387. $oBuyer = BxDolProfile::getInstance();
  388. if(!$oBuyer)
  389. return MsgBox(_t('_bx_payment_err_required_login'));
  390. $iSeller = (int)bx_get('seller');
  391. $oSeller = BxDolProfile::getInstance($iSeller);
  392. if(!$oSeller)
  393. return MsgBox(_t('_bx_payment_err_unknown_vendor'));
  394. $aData = array(
  395. 'seller' => $iSeller,
  396. 'currency' => array(
  397. 'code' => bx_process_input(bx_get('currency_code')),
  398. 'sign' => bx_process_input(bx_get('currency_sign')),
  399. ),
  400. 'amount' => (float)bx_get('amount'),
  401. 'return_url' => bx_process_input(bx_get('return_url')),
  402. );
  403. if(empty($aData['currency']['code']))
  404. $aData['currency']['code'] = $this->_oConfig->getDefaultCurrencyCode();
  405. if(empty($aData['currency']['sign']))
  406. $aData['currency']['sign'] = $this->_oConfig->getDefaultCurrencySign();
  407. $sItems = '';
  408. $iItemsCount = (int)bx_get('items_count');
  409. for($i = 0; $i < $iItemsCount; $i++) {
  410. $aItem = array(
  411. 'title' => bx_process_input(bx_get('item_title_' . $i)),
  412. 'quantity' => (int)bx_get('item_quantity_' . $i)
  413. );
  414. $sItems .= _t('_bx_payment_txt_checkout_item', $aItem['title'], $aItem['quantity']);
  415. $aData['items'][] = $aItem;
  416. }
  417. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'checkout_offline', array(
  418. 'profile_name' => $oBuyer->getDisplayName(),
  419. 'profile_link' => $oBuyer->getUrl(),
  420. 'items' => $sItems,
  421. 'amount' => $aData['currency']['sign'] . sprintf("%.2f", (float)($aData['amount'])),
  422. 'date' => bx_time_js(time(), BX_FORMAT_DATE, true),
  423. 'pending_orders_link' => $this->getObjectOrders()->serviceGetPendingOrdersUrl()
  424. ), 0, $iSeller);
  425. $sEmail = '';
  426. $oProvider = $this->getObjectProvider($CNF['OBJECT_PP_OFFLINE'], $iSeller);
  427. if($oProvider !== false && $oProvider->isActive())
  428. $sEmail = $oProvider->getOption('checkout_email');
  429. if(empty($sEmail))
  430. $sEmail = $oBuyer->getAccountObject()->getEmail();
  431. if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  432. return MsgBox(_t('_bx_payment_err_cannot_perform'));
  433. return $this->_oTemplate->displayBlockCheckoutOffline($oBuyer, $oSeller, $aData);
  434. }
  435. /**
  436. * Cart Processing Methods
  437. */
  438. public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $sCustom = '')
  439. {
  440. if(empty($sCustom) && bx_get('custom') !== false)
  441. $sCustom = bx_process_input(bx_get('custom'));
  442. $aCustom = array();
  443. if(!empty($sCustom)) {
  444. $aCustom = unserialize(base64_decode($sCustom));
  445. if(empty($aCustom) || !is_array($aCustom))
  446. $aCustom = array();
  447. }
  448. $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom);
  449. echoJson($aResult);
  450. }
  451. /**
  452. * Isn't used yet.
  453. */
  454. public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
  455. {
  456. echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId));
  457. }
  458. /**
  459. * Isn't used yet.
  460. */
  461. public function actionEmptyCart($iSellerId)
  462. {
  463. echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId));
  464. }
  465. public function actionSubscribe()
  466. {
  467. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  468. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  469. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  470. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  471. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  472. if(empty($iItemCount))
  473. $iItemCount = 1;
  474. $sRedirect = bx_process_input(bx_get('redirect'));
  475. $sCustom = bx_process_input(bx_get('custom'));
  476. $aCustom = array();
  477. if(!empty($sCustom)) {
  478. $aCustom = unserialize(base64_decode($sCustom));
  479. if(empty($aCustom) || !is_array($aCustom))
  480. $aCustom = array();
  481. }
  482. $aResult = $this->getObjectSubscriptions()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  483. $bRedirect = !empty($aResult['redirect']);
  484. if(!empty($aResult['popup'])) {
  485. $sContent = '';
  486. $sContent .= $this->_oTemplate->displayCartJs(BX_PAYMENT_TYPE_RECURRING, $iSellerId);
  487. $sContent .= !empty($aResult['popup']['html']) ? $aResult['popup']['html'] : $aResult['popup'];
  488. return $this->_oTemplate->displayPageCodeResponse($sContent, false, true);
  489. }
  490. if(!empty($aResult['code']))
  491. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  492. if(!empty($aResult['message']) && !$bRedirect)
  493. return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  494. if($bRedirect) {
  495. header('Location: ' . $aResult['redirect']);
  496. exit;
  497. }
  498. }
  499. public function actionSubscribeJson()
  500. {
  501. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  502. $sSellerProvider = bx_process_input(bx_get('seller_provider'));
  503. $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
  504. $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
  505. $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
  506. if(empty($iItemCount))
  507. $iItemCount = 1;
  508. $sItemAddons = '';
  509. if(bx_get('item_addons') !== false) {
  510. $sItemAddons = bx_process_input(bx_get('item_addons'));
  511. if(is_array($sItemAddons))
  512. $sItemAddons = $this->_oConfig->a2s($sItemAddons);
  513. }
  514. $sRedirect = bx_process_input(bx_get('redirect'));
  515. $sCustom = bx_process_input(bx_get('custom'));
  516. $aCustom = array();
  517. if(!empty($sCustom)) {
  518. $aCustom = unserialize(base64_decode($sCustom));
  519. if(empty($aCustom) || !is_array($aCustom))
  520. $aCustom = array();
  521. }
  522. echoJson($this->getObjectSubscriptions()->serviceSubscribeWithAddons($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sItemAddons, $sRedirect, $aCustom));
  523. }
  524. public function actionSubscriptionGetDetails($iId)
  525. {
  526. echoJson($this->_oTemplate->displaySubscriptionGetDetails($iId));
  527. }
  528. public function actionSubscriptionChangeDetails($iId)
  529. {
  530. echoJson($this->_oTemplate->displaySubscriptionChangeDetails($iId));
  531. }
  532. public function actionSubscriptionGetBilling($iId)
  533. {
  534. echoJson($this->_oTemplate->displaySubscriptionGetBilling($iId));
  535. }
  536. public function actionSubscriptionChangeBilling($iId)
  537. {
  538. echoJson($this->_oTemplate->displaySubscriptionChangeBilling($iId));
  539. }
  540. public function actionSubscriptionCancelation($iId)
  541. {
  542. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  543. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  544. if(empty($aPending) || !is_array($aPending))
  545. return echoJson($aResult);
  546. $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
  547. if(empty($aSubscription) || !is_array($aSubscription))
  548. return echoJson($aResult);
  549. $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
  550. if(!$oRecipient)
  551. return echoJson($aResult);
  552. $mixedResult = $this->isAllowedManage($aPending);
  553. if($mixedResult !== true)
  554. return echoJson(array('code' => 2, 'message' => $mixedResult));
  555. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
  556. 'sibscription_id' => $aSubscription['subscription_id'],
  557. 'sibscription_customer' => $aSubscription['customer_id'],
  558. 'sibscription_date' => bx_time_js($aSubscription['date_add'], BX_FORMAT_DATE, true)
  559. ), 0, (int)$aPending['client_id']);
  560. $sEmail = '';
  561. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  562. if($oProvider !== false && $oProvider->isActive())
  563. $sEmail = $oProvider->getOption('cancellation_email');
  564. if(empty($sEmail))
  565. $sEmail = $oRecipient->getAccountObject()->getEmail();
  566. if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
  567. return echoJson($aResult);
  568. echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
  569. }
  570. public function actionSubscriptionCancel($iId, $mixedGridObject = false)
  571. {
  572. $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
  573. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
  574. if(empty($aPending) || !is_array($aPending))
  575. return echoJson($aResult);
  576. $mixedResult = $this->isAllowedManage($aPending);
  577. if($mixedResult !== true)
  578. return echoJson(array('code' => 2, 'message' => $mixedResult));
  579. if(!$this->getObjectSubscriptions()->cancel($iId))
  580. return echoJson($aResult);
  581. if(empty($mixedGridObject) && bx_get('grid') !== false)
  582. $mixedGridObject = bx_process_input(bx_get('grid'));
  583. if(!empty($mixedGridObject) && $aPending['client_id'] == $this->getProfileId()) {
  584. $oGrid = BxDolGrid::getObjectInstance($mixedGridObject, $this->_oTemplate);
  585. $oGrid->addQueryParam('client_id', $aPending['client_id']);
  586. return echoJson(array('object' => $mixedGridObject, 'grid' => $oGrid->getCode(false), 'blink' => array($iId)));
  587. }
  588. else
  589. return echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')));
  590. }
  591. /**
  592. * Payment Processing Methods
  593. */
  594. public function actionInitializeCheckout($sType)
  595. {
  596. if(!$this->isLogged())
  597. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
  598. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  599. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  600. $sProvider = bx_process_input(bx_get('provider'));
  601. $aItems = bx_process_input(bx_get('items'));
  602. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  603. if($mixedResult !== true)
  604. return $this->_oTemplate->displayPageCodeError($mixedResult);
  605. }
  606. header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
  607. exit;
  608. }
  609. /**
  610. * @page service Service Calls
  611. * @section bx_payment Payment
  612. * @subsection bx_payment-purchase_processing Purchase Processing
  613. * @subsubsection bx_payment-initialize_checkout initialize_checkout
  614. *
  615. * @code bx_srv('bx_payment', 'initialize_checkout', [...]); @endcode
  616. *
  617. * Initialize the checkout process.
  618. *
  619. * @param $sType string value with payment type (single or recurring).
  620. * @param $iSellerId integer value with seller ID.
  621. * @param $sProvider string value with payment provider name.
  622. * @param $aItems (optional) an array with items to be purchased.
  623. * @param $sRedirect (optional) string value with redirect URL if it's needed.
  624. * @param $aCustom (optional) array with custom data to attach to a payment.
  625. * @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.
  626. *
  627. * @see BxPaymentModule::serviceInitializeCheckout
  628. */
  629. /**
  630. * @ref bx_payment-initialize_checkout "initialize_checkout"
  631. */
  632. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '', $aCustoms = array())
  633. {
  634. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  635. $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
  636. if(!is_array($aItems))
  637. $aItems = array($aItems);
  638. $iSellerId = (int)$iSellerId;
  639. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  640. return $this->_sLangsPrefix . 'err_unknown_vendor';
  641. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  642. if($oProvider === false || !$oProvider->isActive())
  643. return $this->_sLangsPrefix . 'err_incorrect_provider';
  644. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  645. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  646. return $this->_sLangsPrefix . 'err_empty_order';
  647. /*
  648. * Process FREE (price = 0) items for LOGGED IN members
  649. * WITHOUT processing via payment provider.
  650. * Note. This section isn't used for now!
  651. */
  652. $bProcessedFree = false;
  653. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  654. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  655. foreach($aInfo['items'] as $iIndex => $aItem) {
  656. if((int)$aInfo['client_id'] == 0)
  657. continue;
  658. //--- For Single time payments.
  659. if($bTypeSingle && (float)$aItem[$sKeyPriceSingle] == 0) {
  660. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  661. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  662. $aCartItem = array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity']);
  663. $aCartItemCustom = $this->_oConfig->pullCustom($aCartItem, $aCartCustoms);
  664. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  665. if(!empty($aItemInfo) && is_array($aItemInfo)) {
  666. if(!empty($aItemInfo['addons']) && is_array($aItemInfo['addons']))
  667. foreach($aItemInfo['addons'] as $sAddon => $aAddon)
  668. $this->callRegisterCartItem((int)$aAddon['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aAddon['id'], $aAddon['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  669. $bProcessedFree = true;
  670. }
  671. $aInfo['items_count'] -= 1;
  672. unset($aInfo['items'][$iIndex]);
  673. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aCartItem) . ":?'", "", $aCart['items']), ":");
  674. $this->_oDb->setCartItems($aInfo['client_id'], $aCart['items'], $aCartCustoms);
  675. }
  676. //--- For Recurring payments.
  677. if($bTypeRecurring && (float)$aItem[$sKeyPriceRecurring] == 0) {
  678. //TODO: Process FREE subscription here, if such situation will be possible.
  679. }
  680. }
  681. if(empty($aInfo['items']))
  682. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  683. if($bTypeSingle && (empty($aCustoms) || !is_array($aCustoms))) {
  684. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  685. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  686. $aCustoms = array();
  687. foreach($aInfo['items'] as $aItem) {
  688. $sCartItem = $this->_oConfig->descriptorA2S(array($aItem['author_id'], $aItem['module_id'], $aItem['id']));
  689. $aCartItemCustom = $this->_oConfig->getCustom($sCartItem, $aCartCustoms);
  690. $this->_oConfig->putCustom($sCartItem, $aCartItemCustom, $aCustoms);
  691. }
  692. }
  693. $this->alert('before_pending', 0, bx_get_logged_profile_id(), array(
  694. 'client_id' => $this->_iUserId,
  695. 'type' => &$sType,
  696. 'provider' => &$sProvider,
  697. 'cart_info' => &$aInfo,
  698. 'custom' => &$aCustoms,
  699. ));
  700. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo, $aCustoms);
  701. if(empty($iPendingId))
  702. return $this->_sLangsPrefix . 'err_access_db';
  703. /*
  704. * Perform Join WITHOUT processing via payment provider
  705. * if a client ISN'T logged in and has only ONE FREE item in the card.
  706. * Note. This section isn't used for now because only a member can purchase!
  707. */
  708. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  709. reset($aInfo['items']);
  710. $aItem = current($aInfo['items']);
  711. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  712. $this->_oDb->updateOrderPending($iPendingId, array(
  713. 'order' => $this->_oConfig->getLicense(),
  714. 'error_code' => '1',
  715. 'error_msg' => ''
  716. ));
  717. $this->getObjectJoin()->performJoin($iPendingId);
  718. }
  719. }
  720. return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
  721. }
  722. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  723. {
  724. $aData = &$_REQUEST;
  725. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  726. if($oProvider === false || !$oProvider->isActive())
  727. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  728. $aResult = $oProvider->finalizeCheckout($aData);
  729. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  730. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  731. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  732. $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
  733. if($bTypeRecurring) {
  734. $sStatus = BX_PAYMENT_SBS_STATUS_UNPAID;
  735. if(isset($aResult['paid']) && $aResult['paid'])
  736. $sStatus = BX_PAYMENT_SBS_STATUS_ACTIVE;
  737. else if(isset($aResult['trial']) && $aResult['trial'])
  738. $sStatus = BX_PAYMENT_SBS_STATUS_TRIAL;
  739. $aSubscription = array(
  740. 'customer_id' => $aResult['customer_id'],
  741. 'subscription_id' => $aResult['subscription_id'],
  742. 'status' => $sStatus
  743. );
  744. if(!$this->getObjectSubscriptions()->register($aPending, $aSubscription))
  745. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_already_registered');
  746. }
  747. $this->onPaymentRegisterBefore($aPending);
  748. //--- Check "Pay Before Join" situation
  749. if((int)$aPending['client_id'] == 0)
  750. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  751. //--- Register payment for purchased items in associated modules
  752. if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
  753. $this->registerPayment($aPending);
  754. $this->alert('finalize_checkout', 0, bx_get_logged_profile_id(), array(
  755. 'pending' => $aPending,
  756. 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
  757. 'provider' => $oProvider,
  758. 'message' => &$aResult['message'],
  759. 'result' => &$aResult,
  760. ));
  761. if($oProvider->needRedirect()) {
  762. header('Location: ' . $oProvider->getReturnUrl());
  763. exit;
  764. }
  765. if(!empty($aResult['redirect'])) {
  766. header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
  767. exit;
  768. }
  769. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  770. }
  771. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  772. {
  773. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  774. if($oProvider === false || !$oProvider->isActive())
  775. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  776. $aResult = $oProvider->finalizedCheckout();
  777. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  778. }
  779. public function actionNotify($sProvider, $mixedVendorId = "")
  780. {
  781. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  782. if($oProvider === false || !$oProvider->isActive())
  783. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  784. $oProvider->notify();
  785. }
  786. /**
  787. * The method is needed to pass direct action calls to necessary Payment Provider.
  788. */
  789. public function actionCall($sProvider, $sAction, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  790. {
  791. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  792. if($oProvider === false)
  793. BxDolRequest::methodNotFound($sAction, $this->getName());
  794. $sMethod = 'action' . bx_gen_method_name($sAction);
  795. if(!method_exists($oProvider, $sMethod))
  796. BxDolRequest::methodNotFound($sAction, $this->getName());
  797. return call_user_func_array(array($oProvider, $sMethod), array_slice(func_get_args(), 2));
  798. }
  799. public function onProfileJoin($iProfileId)
  800. {
  801. $this->getObjectJoin()->onProfileJoin($iProfileId);
  802. }
  803. public function onProfileDelete($iProfileId)
  804. {
  805. $this->_oDb->onProfileDelete($iProfileId);
  806. }
  807. public function isAllowedPurchase($aItem, $bPerform = false)
  808. {
  809. $iUserId = (int)$this->getProfileId();
  810. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], isset($aItem['custom']) ? $aItem['custom'] : array()));
  811. if(empty($aItemInfo))
  812. return false;
  813. if(isAdmin())
  814. return true;
  815. $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
  816. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  817. return true;
  818. return $aCheckResult[CHECK_ACTION_MESSAGE];
  819. }
  820. public function isAllowedSell($aItem, $bPerform = false)
  821. {
  822. $iUserId = (int)$this->getProfileId();
  823. if(!$iUserId)
  824. return false;
  825. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id']));
  826. if(empty($aItemInfo))
  827. return false;
  828. if(isAdmin())
  829. return true;
  830. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  831. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  832. return true;
  833. return $aCheckResult[CHECK_ACTION_MESSAGE];
  834. }
  835. public function isAllowedManage($aPending, $bPerform = false)
  836. {
  837. $iUserId = (int)$this->getProfileId();
  838. if($iUserId == $aPending['client_id'] || $iUserId == $aPending['seller_id'] || isAdmin())
  839. return true;
  840. $aCheckResult = checkActionModule($iUserId, 'manage any purchase', $this->getName(), $bPerform);
  841. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  842. return true;
  843. return $aCheckResult[CHECK_ACTION_MESSAGE];
  844. }
  845. public function isAllowedManageInvoices($bPerform = false)
  846. {
  847. if(BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)) || isAdmin())
  848. return true;
  849. return _t('_sys_txt_access_denied');
  850. }
  851. public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom = array())
  852. {
  853. $CNF = &$this->_oConfig->CNF;
  854. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  855. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  856. $iClientId = $this->getProfileId();
  857. if(empty($iClientId)) {
  858. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  859. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  860. }
  861. $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId, 'custom' => $aCustom));
  862. if($mixedResult !== true) {
  863. if(is_string($mixedResult) && !empty($mixedResult))
  864. return array('code' => 2, 'message' => $mixedResult);
  865. else
  866. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  867. }
  868. if($iClientId == $iSellerId)
  869. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  870. $aSeller = $this->getVendorInfo($iSellerId);
  871. if(!$aSeller['active'])
  872. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  873. return true;
  874. }
  875. public function registerPayment($mixedPending)
  876. {
  877. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  878. if(empty($aPending) || !is_array($aPending))
  879. return false;
  880. $sType = $aPending['type'];
  881. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  882. if($bTypeSingle && (int)$aPending['processed'] == 1)
  883. return true;
  884. $iClientId = (int)$aPending['client_id'];
  885. $sLicense = $this->_oConfig->getLicense();
  886. $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
  887. $aCart = array();
  888. if($bTypeSingle) {
  889. $aCart = $this->_oDb->getCartContent($iClientId);
  890. $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  891. }
  892. $bResult = false;
  893. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  894. foreach($aItems as $aItem) {
  895. $sItem = $this->_oConfig->descriptorA2S(array($aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']));
  896. $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
  897. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  898. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
  899. if(empty($aItemInfo) || !is_array($aItemInfo))
  900. continue;
  901. $this->_oDb->insertOrderProcessed(array(
  902. 'pending_id' => $aPending['id'],
  903. 'client_id' => $aPending['client_id'],
  904. 'seller_id' => $aPending['seller_id'],
  905. 'author_id' => $aItem['vendor_id'],
  906. 'module_id' => (int)$aItem['module_id'],
  907. 'item_id' => (int)$aItem['item_id'],
  908. 'item_count' => (int)$aItem['item_count'],
  909. 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  910. 'license' => $sLicense,
  911. ));
  912. if($bTypeSingle) {
  913. $this->_oConfig->pullCustom($sItem, $aCart['customs']);
  914. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
  915. }
  916. $bResult = true;
  917. }
  918. if($bTypeSingle)
  919. $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
  920. if($bResult) {
  921. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  922. $this->onPaymentRegister($aPending);
  923. }
  924. return $bResult;
  925. }
  926. public function refundPayment($mixedPending)
  927. {
  928. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  929. if(empty($aPending) || !is_array($aPending))
  930. return false;
  931. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  932. $iCanceled = 0;
  933. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  934. foreach($aOrders as $aOrder) {
  935. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  936. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  937. if(!$bResult)
  938. continue;
  939. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  940. $iCanceled++;
  941. }
  942. if($iCanceled != count($aOrders))
  943. return false;
  944. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  945. if($bResult)
  946. $this->onPaymentRefund($aPending);
  947. return $bResult;
  948. }
  949. /**
  950. * @deprecated since version 12.0.0
  951. *
  952. * @use BxPaymentSubscriptions::register instead.
  953. */
  954. public function registerSubscription($aPending, $aParams = array())
  955. {
  956. return $this->getObjectSubscriptions()->register($aPending, $aParams);
  957. }
  958. /**
  959. * @deprecated since version 12.0.0
  960. *
  961. * @use BxPaymentSubscriptions::cancelLocal instead.
  962. */
  963. public function cancelSubscription($mixedPending)
  964. {
  965. return $this->getObjectSubscriptions()->cancelLocal($mixedPending);
  966. }
  967. public function processCommissions()
  968. {
  969. if(!defined('BX_DOL_CRON_EXECUTE'))
  970. return;
  971. $CNF = &$this->_oConfig->CNF;
  972. if((int)date('j') == $this->_oConfig->getInvoiceIssueDay())
  973. $this->_invoicesIssue();
  974. $this->_invoicesCheck();
  975. }
  976. protected function _invoicesIssue()
  977. {
  978. $CNF = &$this->_oConfig->CNF;
  979. $sInvoiceNameMask = 'INV-%d-%d-%d';
  980. $iMonth = date('m');
  981. $iYear = date('Y');
  982. $iPeriodStart = mktime(0, 0, 0, $iMonth-1, 1, $iYear);
  983. $iPeriodEnd = mktime(23, 59, 59, $iMonth, 0, $iYear);
  984. $iDateIssue = time();
  985. $iDateDue = $iDateIssue + 86400 * $this->_oConfig->getInvoiceLifetime();
  986. $aCommissionsInfo = array();
  987. $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, true, false, true);
  988. foreach($aAclLevels as $iAclLevelId => $sAclLevelTitle)
  989. $aCommissionsInfo[$iAclLevelId] = $this->_oDb->getCommissions(array('type' => 'acl_id', 'acl_id' => $iAclLevelId));
  990. $iMainSellerId = $this->_oConfig->getSiteAdmin();
  991. $aVendors = $this->_oDb->getOrderProcessed(array('type' => 'income', 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  992. foreach($aVendors as $aVendor) {
  993. $iVendorId = (int)$aVendor['id'];
  994. if($iVendorId == $iMainSellerId)
  995. continue;
  996. $aInvoice = $this->_oDb->getInvoices(array('type' => 'committent_id', 'committent_id' => $iVendorId, 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  997. if(!empty($aInvoice) && is_array($aInvoice))
  998. continue;
  999. $aVendorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($iVendorId);
  1000. $aCommissions = $aCommissionsInfo[$aVendorAcl['id']];
  1001. $fCommission = 0;
  1002. foreach($aCommissions as $aCommission) {
  1003. if((float)$aCommission['percentage'] > 0)
  1004. $fCommission += (float)$aVendor['amount'] * (float)$aCommission['percentage'] / 100;
  1005. if((float)$aCommission['installment'] > 0)
  1006. $fCommission += (float)$aCommission['installment'];
  1007. }
  1008. $this->alert('calculate_commission', 0, 0, array(
  1009. 'vendor' => $aVendor,
  1010. 'commissions' => $aCommissions,
  1011. 'override_result' => &$fCommission,
  1012. ));
  1013. $this->_oDb->insertInvoice(array(
  1014. 'name' => sprintf($sInvoiceNameMask, $iMainSellerId, $iVendorId, $this->_oDb->getInvoices(array(
  1015. 'type' => 'index',
  1016. 'commissionaire_id' => $iMainSellerId,
  1017. 'committent_id' => $iVendorId
  1018. ))),
  1019. 'commissionaire_id' => $iMainSellerId,
  1020. 'committent_id' => $iVendorId,
  1021. 'amount' => $fCommission,
  1022. 'period_start' => $iPeriodStart,
  1023. 'period_end' => $iPeriodEnd,
  1024. 'date_issue' => $iDateIssue,
  1025. 'date_due' => $iDateDue,
  1026. 'status' => BX_PAYMENT_INV_STATUS_UNPAID
  1027. ));
  1028. }
  1029. }
  1030. protected function _invoicesCheck()
  1031. {
  1032. $CNF = &$this->_oConfig->CNF;
  1033. $sPrefix = $this->_oConfig->getPrefix('general');
  1034. $oEmailTemplates = BxDolEmailTemplates::getInstance();
  1035. //--- Process expiring invoices.
  1036. $aInvoices = $this->_oDb->getInvoices(array('type' => 'expiring'));
  1037. if(!empty($aInvoices) && is_array($aInvoices))
  1038. foreach($aInvoices as $aInvoice) {
  1039. if((int)$aInvoice['ntf_exp'] > 0)
  1040. continue;
  1041. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1042. if(!$oCommittent)
  1043. continue;
  1044. $bResult = sendMailTemplate($sPrefix . 'expiring_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1045. 'invoice' => $aInvoice['name'],
  1046. 'days' => $this->_oConfig->getInvoiceExpirationNotify(),
  1047. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1048. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1049. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1050. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1051. ), BX_EMAIL_NOTIFY, true);
  1052. if($bResult)
  1053. $this->_oDb->updateInvoice($aInvoice['id'], array('ntf_exp' => 1));
  1054. }
  1055. //--- Process overdue invoices.
  1056. $aInvoices = $this->_oDb->getInvoices(array('type' => 'overdue'));
  1057. if(!empty($aInvoices) && is_array($aInvoices))
  1058. foreach($aInvoices as $aInvoice) {
  1059. if((int)$aInvoice['ntf_due'] > 0 && $aInvoice['status'] == BX_PAYMENT_INV_STATUS_OVERDUE)
  1060. continue;
  1061. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1062. if(!$oCommittent)
  1063. continue;
  1064. $bResult = sendMailTemplate($sPrefix . 'overdue_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1065. 'invoice' => $aInvoice['name'],
  1066. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1067. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1068. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1069. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1070. ), BX_EMAIL_NOTIFY, true);
  1071. $this->_oDb->updateInvoice($aInvoice['id'], array(
  1072. 'status' => BX_PAYMENT_INV_STATUS_OVERDUE,
  1073. 'ntf_due' => $bResult ? 1 : 0
  1074. ));
  1075. }
  1076. }
  1077. public function processTimeTracker()
  1078. {
  1079. if(!defined('BX_DOL_CRON_EXECUTE'))
  1080. return;
  1081. $aSubscriptions = $this->_oDb->getSubscription(array(
  1082. 'type' => 'time_tracker',
  1083. 'status_active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
  1084. 'status_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
  1085. 'status_unpaid' => BX_PAYMENT_SBS_STATUS_UNPAID,
  1086. 'pay_attempts_max' => $this->_oConfig->getPayAttemptsMax(),
  1087. 'pay_attempts_interval' => $this->_oConfig->getPayAttemptsInterval()
  1088. ));
  1089. if(empty($aSubscriptions) || !is_array($aSubscriptions))
  1090. return;
  1091. foreach($aSubscriptions as $aSubscription) {
  1092. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aSubscription['pending_id']));
  1093. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING) {
  1094. $this->log($aSubscription, 'Time Tracker', 'Cannot process subscription.');
  1095. continue;
  1096. }
  1097. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  1098. if($oProvider === false || !$oProvider->isActive() || !method_exists($oProvider, 'makePayment')) {
  1099. $this->log($aSubscription, 'Time Tracker', 'Payment provider unavailable or incorrect.');
  1100. continue;
  1101. }
  1102. if(!$oProvider->makePayment($aPending)) {
  1103. $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed.');
  1104. continue;
  1105. }
  1106. if(!$this->getObjectSubscriptions()->prolong($aPending)) {
  1107. $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.');
  1108. continue;
  1109. }
  1110. if(!$this->registerPayment($aPending)) {
  1111. $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.');
  1112. continue;
  1113. }
  1114. }
  1115. }
  1116. public function onPaymentRegisterBefore($aPending, $aResult = array())
  1117. {
  1118. $this->alert('before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1119. }
  1120. public function onPaymentRegister($aPending, $aResult = array())
  1121. {
  1122. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  1123. if($bTypeSingle) {
  1124. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1125. foreach($aItems as $aItem)
  1126. $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
  1127. }
  1128. $this->alert('register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1129. }
  1130. public function onPaymentRefund($aPending, $aResult = array())
  1131. {
  1132. $this->alert('refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1133. }
  1134. public function onSubscriptionCreate($aPending, $aSubscription, $aResult = array())
  1135. {
  1136. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1137. $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
  1138. $this->alert('create_subscription', 0, $aPending['client_id'], array(
  1139. 'pending' => $aPending,
  1140. 'subscription' => $aSubscription
  1141. ));
  1142. }
  1143. public function onSubscriptionProlong($aPending, $aSubscription, $aResult = array())
  1144. {
  1145. $this->alert('prolong_subscription', 0, $aPending['client_id'], array(
  1146. 'pending' => $aPending,
  1147. 'subscription' => $aSubscription
  1148. ));
  1149. }
  1150. public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = array())
  1151. {
  1152. $this->alert('overdue_subscription', 0, $aPending['client_id'], array(
  1153. 'pending' => $aPending,
  1154. 'subscription' => $aSubscription
  1155. ));
  1156. }
  1157. public function onSubscriptionCancel($aPending, $aSubscription, $aResult = array())
  1158. {
  1159. $this->alert('cancel_subscription', 0, $aPending['client_id'], array(
  1160. 'pending' => $aPending,
  1161. 'subscription' => $aSubscription
  1162. ));
  1163. }
  1164. public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
  1165. {
  1166. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  1167. if(!$oSiteSubmenu)
  1168. return;
  1169. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  1170. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  1171. if(!$oModuleSubmenu)
  1172. return;
  1173. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
  1174. $oModuleSubmenu->setSelected($sSelModule, $sSelName);
  1175. }
  1176. public function getProviderButtonJs($aCartItem, $aProvider, $sRedirect = '', $aCustom = array())
  1177. {
  1178. $aButtonJs = array();
  1179. $iClientId = $this->getProfileId();
  1180. $sItemAddons = '';
  1181. if(count($aCartItem) == 5)
  1182. $sItemAddons = array_pop($aCartItem);
  1183. list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $aCartItem;
  1184. $oProvider = $this->getObjectProvider($aProvider['name'], $iSellerId);
  1185. if($oProvider !== false && method_exists($oProvider, 'getButtonRecurring')) {
  1186. $aParams = array(
  1187. 'sObjNameCart' => $this->_oConfig->getJsObject('cart'),
  1188. 'iSellerId' => $iSellerId,
  1189. 'iModuleId' => $iModuleId,
  1190. 'iItemId' => $iItemId,
  1191. 'iItemCount' => $iItemCount,
  1192. 'sItemAddons' => $sItemAddons,
  1193. 'sRedirect' => $sRedirect,
  1194. 'sCustom' => base64_encode(serialize($aCustom))
  1195. );
  1196. $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));
  1197. if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
  1198. $aItem = array_shift($aCartInfo['items']);
  1199. $aParams = array_merge($aParams, array(
  1200. 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
  1201. 'sItemName' => $aItem['name'],
  1202. 'sItemTitle' => $aItem['title']
  1203. ));
  1204. }
  1205. $aButtonJs = $oProvider->getButtonRecurringJs($iClientId, $iSellerId, $aParams);
  1206. }
  1207. if(empty($aButtonJs) || !is_array($aButtonJs))
  1208. $aButtonJs = $this->getObjectSubscriptions()->serviceGetSubscribeJs($iSellerId, $aProvider['name'], $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  1209. return $aButtonJs;
  1210. }
  1211. /**
  1212. * Integration with itslef which is needed to pay an invoice.
  1213. */
  1214. /**
  1215. * @page service Service Calls
  1216. * @section bx_payment Payment
  1217. * @subsection bx_payment-payments Payments
  1218. * @subsubsection bx_payment-get_payment_data get_payment_data
  1219. *
  1220. * @code bx_srv('bx_payment', 'get_payment_data', [...]); @endcode
  1221. *
  1222. * Get an array with module's description. Is needed for payments processing module.
  1223. *
  1224. * @return an array with module's description.
  1225. *
  1226. * @see BxPaymentModule::serviceGetPaymentData
  1227. */
  1228. /**
  1229. * @ref bx_payment-get_payment_data "get_payment_data"
  1230. */
  1231. public function serviceGetPaymentData()
  1232. {
  1233. return $this->_aModule;
  1234. }
  1235. /**
  1236. * @page service Service Calls
  1237. * @section bx_payment Payment
  1238. * @subsection bx_payment-payments Payments
  1239. * @subsubsection bx_payment-get_cart_item get_cart_item
  1240. *
  1241. * @code bx_srv('bx_payment', 'get_cart_item', [...]); @endcode
  1242. *
  1243. * Get an array with prodict's description. Is used in Shopping Cart in payments processing module.
  1244. *
  1245. * @param $mixedItemId product's ID or Unique Name.
  1246. * @return an array with prodict's description. Empty array is returned if something is wrong.
  1247. *
  1248. * @see BxPaymentModule::serviceGetCartItem
  1249. */
  1250. /**
  1251. * @ref bx_payment-get_cart_item "get_cart_item"
  1252. */
  1253. public function serviceGetCartItem($mixedItemId)
  1254. {
  1255. $CNF = &$this->_oConfig->CNF;
  1256. if(!$mixedItemId)
  1257. return array();
  1258. if(is_numeric($mixedItemId))
  1259. $aItem = $this->_oDb->getInvoices(array('type' => 'id', 'id' => (int)$mixedItemId));
  1260. else
  1261. $aItem = $this->_oDb->getInvoices(array('type' => 'name', 'name' => $mixedItemId));
  1262. if(empty($aItem) || !is_array($aItem))
  1263. return array();
  1264. $oCommissionaire = BxDolProfile::getInstanceMagic($aItem['commissionaire_id']);
  1265. return array (
  1266. 'id' => $aItem['id'],
  1267. 'author_id' => $aItem['commissionaire_id'],
  1268. 'name' => $aItem['name'],
  1269. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $oCommissionaire->getDisplayName()),
  1270. 'description' => _t('_bx_payment_txt_invoice_description', bx_process_output($aItem['period_start'], BX_DATA_DATE_TS_UTC), bx_process_output($aItem['period_end'], BX_DATA_DATE_TS_UTC)),
  1271. 'url' => '',
  1272. 'price_single' => $aItem['amount'],
  1273. 'price_recurring' => '',
  1274. 'period_recurring' => 0,
  1275. 'period_unit_recurring' => '',
  1276. 'trial_recurring' => ''
  1277. );
  1278. }
  1279. /**
  1280. * @page service Service Calls
  1281. * @section bx_payment Payment
  1282. * @subsection bx_payment-payments Payments
  1283. * @subsubsection bx_payment-get_cart_items get_cart_items
  1284. *
  1285. * @code bx_srv('bx_payment', 'get_cart_items', [...]); @endcode
  1286. *
  1287. * Get an array with prodicts' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  1288. *
  1289. * @param $iCommissionaireId commissionaire ID.
  1290. * @return an array with prodicts' descriptions. Empty array is returned if something is wrong or seller doesn't have any products.
  1291. *
  1292. * @see BxPaymentModule::serviceGetCartItems
  1293. */
  1294. /**
  1295. * @ref bx_payment-get_cart_items "get_cart_items"
  1296. */
  1297. public function serviceGetCartItems($iCommissionaireId)
  1298. {
  1299. $CNF = &$this->_oConfig->CNF;
  1300. $iCommissionaireId = (int)$iCommissionaireId;
  1301. if(empty($iCommissionaireId))
  1302. return array();
  1303. $sCommissionaireName = BxDolProfile::getInstanceMagic($iCommissionaireId)->getDisplayName();
  1304. $aItems = $this->_oDb->getInvoices(array('type' => 'commissionaire_id', 'commissionaire_id' => $iCommissionaireId));
  1305. $aResult = array();
  1306. foreach($aItems as $aItem)
  1307. $aResult[] = array(
  1308. 'id' => $aItem['id'],
  1309. 'author_id' => $aItem['commissionaire_id'],
  1310. 'name' => $aItem['name'],
  1311. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $sCommissionaireName),
  1312. 'description' => _t('_bx_payment_txt_invoice_description', bx_process_output($aItem['period_start'], BX_DATA_DATE_TS_UTC), bx_process_output($aItem['period_end'], BX_DATA_DATE_TS_UTC)),
  1313. 'url' => '',
  1314. 'price_single' => $aItem['amount'],
  1315. 'price_recurring' => '',
  1316. 'period_recurring' => 0,
  1317. 'period_unit_recurring' => '',
  1318. 'trial_recurring' => ''
  1319. );
  1320. return $aResult;
  1321. }
  1322. /**
  1323. * @page service Service Calls
  1324. * @section bx_payment Payment
  1325. * @subsection bx_payment-payments Payments
  1326. * @subsubsection bx_payment-register_cart_item register_cart_item
  1327. *
  1328. * @code bx_srv('bx_payment', 'register_cart_item', [...]); @endcode
  1329. *
  1330. * Register a processed single time payment inside the Payment module. Is called with payment processing module after the payment was registered there.
  1331. *
  1332. * @param $iClientId client ID.
  1333. * @param $iSellerId seller ID
  1334. * @param $iItemId item ID.
  1335. * @param $iItemCount the number of purchased items.
  1336. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1337. * @param $sLicense license number genereted with payment processing module for internal usage
  1338. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  1339. *
  1340. * @see BxPaymentModule::serviceRegisterCartItem
  1341. */
  1342. /**
  1343. * @ref bx_payment-register_cart_item "register_cart_item"
  1344. */
  1345. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1346. {
  1347. $CNF = &$this->_oConfig->CNF;
  1348. $aItem = $this->serviceGetCartItem($iItemId);
  1349. if(empty($aItem) || !is_array($aItem))
  1350. return array();
  1351. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_PAID)))
  1352. return array();
  1353. $this->alert('invoice_marked_as_paid', 0, false, array(
  1354. 'product_id' => $iItemId,
  1355. 'profile_id' => $iClientId,
  1356. 'order' => $sOrder,
  1357. 'license' => $sLicense,
  1358. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1359. 'count' => $iItemCount,
  1360. 'duration' => '',
  1361. 'trial' => ''
  1362. ));
  1363. return $aItem;
  1364. }
  1365. /**
  1366. * @page service Service Calls
  1367. * @section bx_payment Payment
  1368. * @subsection bx_payment-payments Payments
  1369. * @subsubsection bx_payment-unregister_cart_item unregister_cart_item
  1370. *
  1371. * @code bx_srv('bx_payment', 'unregister_cart_item', [...]); @endcode
  1372. *
  1373. * Unregister an earlier processed single time payment inside the Payment module. Is called with payment processing module after the payment was unregistered there.
  1374. *
  1375. * @param $iClientId client ID.
  1376. * @param $iSellerId seller ID
  1377. * @param $iItemId item ID.
  1378. * @param $iItemCount the number of purchased items.
  1379. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1380. * @param $sLicense license number genereted with payment processing module for internal usage
  1381. * @return boolean value determining where the payment was unregistered or not.
  1382. *
  1383. * @see BxPaymentModule::serviceUnregisterCartItem
  1384. */
  1385. /**
  1386. * @ref bx_payment-unregister_cart_item "unregister_cart_item"
  1387. */
  1388. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1389. {
  1390. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_UNPAID)))
  1391. return false;
  1392. $this->alert('invoice_marked_as_unpaid', 0, false, array(
  1393. 'product_id' => $iItemId,
  1394. 'profile_id' => $iClientId,
  1395. 'order' => $sOrder,
  1396. 'license' => $sLicense,
  1397. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1398. 'count' => $iItemCount
  1399. ));
  1400. return true;
  1401. }
  1402. }
  1403. /** @} */