BxPaymentModule.php 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. //--- Cancel subscriptions
  806. $aSubscriptions = $this->_oDb->getSubscription(['type' => 'mixed_ext', 'conditions' => ['client_id' => $iProfileId]]);
  807. if(!empty($aSubscriptions) && is_array($aSubscriptions))
  808. foreach($aSubscriptions as $aSubscription)
  809. $this->getObjectSubscriptions()->cancel($aSubscription['pending_id']);
  810. //--- Clean DB
  811. $this->_oDb->onProfileDelete($iProfileId);
  812. }
  813. public function isAllowedPurchase($aItem, $bPerform = false)
  814. {
  815. $iUserId = (int)$this->getProfileId();
  816. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], isset($aItem['custom']) ? $aItem['custom'] : array()));
  817. if(empty($aItemInfo))
  818. return false;
  819. if(isAdmin())
  820. return true;
  821. $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
  822. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  823. return true;
  824. return $aCheckResult[CHECK_ACTION_MESSAGE];
  825. }
  826. public function isAllowedSell($aItem, $bPerform = false)
  827. {
  828. $iUserId = (int)$this->getProfileId();
  829. if(!$iUserId)
  830. return false;
  831. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id']));
  832. if(empty($aItemInfo))
  833. return false;
  834. if(isAdmin())
  835. return true;
  836. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  837. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  838. return true;
  839. return $aCheckResult[CHECK_ACTION_MESSAGE];
  840. }
  841. public function isAllowedManage($aPending, $bPerform = false)
  842. {
  843. $iUserId = (int)$this->getProfileId();
  844. if($iUserId == $aPending['client_id'] || $iUserId == $aPending['seller_id'] || isAdmin())
  845. return true;
  846. $aCheckResult = checkActionModule($iUserId, 'manage any purchase', $this->getName(), $bPerform);
  847. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  848. return true;
  849. return $aCheckResult[CHECK_ACTION_MESSAGE];
  850. }
  851. public function isAllowedManageInvoices($bPerform = false)
  852. {
  853. if(BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)) || isAdmin())
  854. return true;
  855. return _t('_sys_txt_access_denied');
  856. }
  857. public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom = array())
  858. {
  859. $CNF = &$this->_oConfig->CNF;
  860. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  861. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  862. $iClientId = $this->getProfileId();
  863. if(empty($iClientId)) {
  864. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  865. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  866. }
  867. $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId, 'custom' => $aCustom));
  868. if($mixedResult !== true) {
  869. if(is_string($mixedResult) && !empty($mixedResult))
  870. return array('code' => 2, 'message' => $mixedResult);
  871. else
  872. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  873. }
  874. if($iClientId == $iSellerId)
  875. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  876. $aSeller = $this->getVendorInfo($iSellerId);
  877. if(!$aSeller['active'])
  878. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  879. return true;
  880. }
  881. public function registerPayment($mixedPending)
  882. {
  883. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  884. if(empty($aPending) || !is_array($aPending))
  885. return false;
  886. $sType = $aPending['type'];
  887. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  888. if($bTypeSingle && (int)$aPending['processed'] == 1)
  889. return true;
  890. $iClientId = (int)$aPending['client_id'];
  891. $sLicense = $this->_oConfig->getLicense();
  892. $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
  893. $aCart = array();
  894. if($bTypeSingle) {
  895. $aCart = $this->_oDb->getCartContent($iClientId);
  896. $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  897. }
  898. $bResult = false;
  899. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  900. foreach($aItems as $aItem) {
  901. $sItem = $this->_oConfig->descriptorA2S(array($aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']));
  902. $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
  903. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  904. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
  905. if(empty($aItemInfo) || !is_array($aItemInfo))
  906. continue;
  907. $this->_oDb->insertOrderProcessed(array(
  908. 'pending_id' => $aPending['id'],
  909. 'client_id' => $aPending['client_id'],
  910. 'seller_id' => $aPending['seller_id'],
  911. 'author_id' => $aItem['vendor_id'],
  912. 'module_id' => (int)$aItem['module_id'],
  913. 'item_id' => (int)$aItem['item_id'],
  914. 'item_count' => (int)$aItem['item_count'],
  915. 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  916. 'license' => $sLicense,
  917. ));
  918. if($bTypeSingle) {
  919. $this->_oConfig->pullCustom($sItem, $aCart['customs']);
  920. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
  921. }
  922. $bResult = true;
  923. }
  924. if($bTypeSingle)
  925. $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
  926. if($bResult) {
  927. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  928. $this->onPaymentRegister($aPending);
  929. }
  930. return $bResult;
  931. }
  932. public function refundPayment($mixedPending)
  933. {
  934. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  935. if(empty($aPending) || !is_array($aPending))
  936. return false;
  937. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  938. $iCanceled = 0;
  939. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  940. foreach($aOrders as $aOrder) {
  941. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  942. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  943. if(!$bResult)
  944. continue;
  945. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  946. $iCanceled++;
  947. }
  948. if($iCanceled != count($aOrders))
  949. return false;
  950. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  951. if($bResult)
  952. $this->onPaymentRefund($aPending);
  953. return $bResult;
  954. }
  955. /**
  956. * @deprecated since version 12.0.0
  957. *
  958. * @use BxPaymentSubscriptions::register instead.
  959. */
  960. public function registerSubscription($aPending, $aParams = array())
  961. {
  962. return $this->getObjectSubscriptions()->register($aPending, $aParams);
  963. }
  964. /**
  965. * @deprecated since version 12.0.0
  966. *
  967. * @use BxPaymentSubscriptions::cancelLocal instead.
  968. */
  969. public function cancelSubscription($mixedPending)
  970. {
  971. return $this->getObjectSubscriptions()->cancelLocal($mixedPending);
  972. }
  973. public function processCommissions()
  974. {
  975. if(!defined('BX_DOL_CRON_EXECUTE'))
  976. return;
  977. $CNF = &$this->_oConfig->CNF;
  978. if((int)date('j') == $this->_oConfig->getInvoiceIssueDay())
  979. $this->_invoicesIssue();
  980. $this->_invoicesCheck();
  981. }
  982. protected function _invoicesIssue()
  983. {
  984. $CNF = &$this->_oConfig->CNF;
  985. $sInvoiceNameMask = 'INV-%d-%d-%d';
  986. $iMonth = date('m');
  987. $iYear = date('Y');
  988. $iPeriodStart = mktime(0, 0, 0, $iMonth-1, 1, $iYear);
  989. $iPeriodEnd = mktime(23, 59, 59, $iMonth, 0, $iYear);
  990. $iDateIssue = time();
  991. $iDateDue = $iDateIssue + 86400 * $this->_oConfig->getInvoiceLifetime();
  992. $aCommissionsInfo = array();
  993. $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, true, false, true);
  994. foreach($aAclLevels as $iAclLevelId => $sAclLevelTitle)
  995. $aCommissionsInfo[$iAclLevelId] = $this->_oDb->getCommissions(array('type' => 'acl_id', 'acl_id' => $iAclLevelId));
  996. $iMainSellerId = $this->_oConfig->getSiteAdmin();
  997. $aVendors = $this->_oDb->getOrderProcessed(array('type' => 'income', 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  998. foreach($aVendors as $aVendor) {
  999. $iVendorId = (int)$aVendor['id'];
  1000. if($iVendorId == $iMainSellerId)
  1001. continue;
  1002. $aInvoice = $this->_oDb->getInvoices(array('type' => 'committent_id', 'committent_id' => $iVendorId, 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  1003. if(!empty($aInvoice) && is_array($aInvoice))
  1004. continue;
  1005. $aVendorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($iVendorId);
  1006. $aCommissions = $aCommissionsInfo[$aVendorAcl['id']];
  1007. $fCommission = 0;
  1008. foreach($aCommissions as $aCommission) {
  1009. if((float)$aCommission['percentage'] > 0)
  1010. $fCommission += (float)$aVendor['amount'] * (float)$aCommission['percentage'] / 100;
  1011. if((float)$aCommission['installment'] > 0)
  1012. $fCommission += (float)$aCommission['installment'];
  1013. }
  1014. $this->alert('calculate_commission', 0, 0, array(
  1015. 'vendor' => $aVendor,
  1016. 'commissions' => $aCommissions,
  1017. 'override_result' => &$fCommission,
  1018. ));
  1019. $this->_oDb->insertInvoice(array(
  1020. 'name' => sprintf($sInvoiceNameMask, $iMainSellerId, $iVendorId, $this->_oDb->getInvoices(array(
  1021. 'type' => 'index',
  1022. 'commissionaire_id' => $iMainSellerId,
  1023. 'committent_id' => $iVendorId
  1024. ))),
  1025. 'commissionaire_id' => $iMainSellerId,
  1026. 'committent_id' => $iVendorId,
  1027. 'amount' => $fCommission,
  1028. 'period_start' => $iPeriodStart,
  1029. 'period_end' => $iPeriodEnd,
  1030. 'date_issue' => $iDateIssue,
  1031. 'date_due' => $iDateDue,
  1032. 'status' => BX_PAYMENT_INV_STATUS_UNPAID
  1033. ));
  1034. }
  1035. }
  1036. protected function _invoicesCheck()
  1037. {
  1038. $CNF = &$this->_oConfig->CNF;
  1039. $sPrefix = $this->_oConfig->getPrefix('general');
  1040. $oEmailTemplates = BxDolEmailTemplates::getInstance();
  1041. //--- Process expiring invoices.
  1042. $aInvoices = $this->_oDb->getInvoices(array('type' => 'expiring'));
  1043. if(!empty($aInvoices) && is_array($aInvoices))
  1044. foreach($aInvoices as $aInvoice) {
  1045. if((int)$aInvoice['ntf_exp'] > 0)
  1046. continue;
  1047. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1048. if(!$oCommittent)
  1049. continue;
  1050. $bResult = sendMailTemplate($sPrefix . 'expiring_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1051. 'invoice' => $aInvoice['name'],
  1052. 'days' => $this->_oConfig->getInvoiceExpirationNotify(),
  1053. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1054. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1055. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1056. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1057. ), BX_EMAIL_NOTIFY, true);
  1058. if($bResult)
  1059. $this->_oDb->updateInvoice($aInvoice['id'], array('ntf_exp' => 1));
  1060. }
  1061. //--- Process overdue invoices.
  1062. $aInvoices = $this->_oDb->getInvoices(array('type' => 'overdue'));
  1063. if(!empty($aInvoices) && is_array($aInvoices))
  1064. foreach($aInvoices as $aInvoice) {
  1065. if((int)$aInvoice['ntf_due'] > 0 && $aInvoice['status'] == BX_PAYMENT_INV_STATUS_OVERDUE)
  1066. continue;
  1067. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1068. if(!$oCommittent)
  1069. continue;
  1070. $bResult = sendMailTemplate($sPrefix . 'overdue_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1071. 'invoice' => $aInvoice['name'],
  1072. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1073. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1074. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1075. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1076. ), BX_EMAIL_NOTIFY, true);
  1077. $this->_oDb->updateInvoice($aInvoice['id'], array(
  1078. 'status' => BX_PAYMENT_INV_STATUS_OVERDUE,
  1079. 'ntf_due' => $bResult ? 1 : 0
  1080. ));
  1081. }
  1082. }
  1083. public function processTimeTracker()
  1084. {
  1085. if(!defined('BX_DOL_CRON_EXECUTE'))
  1086. return;
  1087. $aSubscriptions = $this->_oDb->getSubscription(array(
  1088. 'type' => 'time_tracker',
  1089. 'status_active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
  1090. 'status_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
  1091. 'status_unpaid' => BX_PAYMENT_SBS_STATUS_UNPAID,
  1092. 'pay_attempts_max' => $this->_oConfig->getPayAttemptsMax(),
  1093. 'pay_attempts_interval' => $this->_oConfig->getPayAttemptsInterval()
  1094. ));
  1095. if(empty($aSubscriptions) || !is_array($aSubscriptions))
  1096. return;
  1097. foreach($aSubscriptions as $aSubscription) {
  1098. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aSubscription['pending_id']));
  1099. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING) {
  1100. $this->log($aSubscription, 'Time Tracker', 'Cannot process subscription.');
  1101. continue;
  1102. }
  1103. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  1104. if($oProvider === false || !$oProvider->isActive() || !method_exists($oProvider, 'makePayment')) {
  1105. $this->log($aSubscription, 'Time Tracker', 'Payment provider unavailable or incorrect.');
  1106. continue;
  1107. }
  1108. if(!$oProvider->makePayment($aPending)) {
  1109. $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed.');
  1110. continue;
  1111. }
  1112. if(!$this->getObjectSubscriptions()->prolong($aPending)) {
  1113. $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.');
  1114. continue;
  1115. }
  1116. if(!$this->registerPayment($aPending)) {
  1117. $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.');
  1118. continue;
  1119. }
  1120. }
  1121. }
  1122. public function onPaymentRegisterBefore($aPending, $aResult = array())
  1123. {
  1124. $this->alert('before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1125. }
  1126. public function onPaymentRegister($aPending, $aResult = array())
  1127. {
  1128. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  1129. if($bTypeSingle) {
  1130. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1131. foreach($aItems as $aItem)
  1132. $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
  1133. }
  1134. $this->alert('register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1135. }
  1136. public function onPaymentRefund($aPending, $aResult = array())
  1137. {
  1138. $this->alert('refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1139. }
  1140. public function onSubscriptionCreate($aPending, $aSubscription, $aResult = array())
  1141. {
  1142. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1143. $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
  1144. $this->alert('create_subscription', 0, $aPending['client_id'], array(
  1145. 'pending' => $aPending,
  1146. 'subscription' => $aSubscription
  1147. ));
  1148. }
  1149. public function onSubscriptionProlong($aPending, $aSubscription, $aResult = array())
  1150. {
  1151. $this->alert('prolong_subscription', 0, $aPending['client_id'], array(
  1152. 'pending' => $aPending,
  1153. 'subscription' => $aSubscription
  1154. ));
  1155. }
  1156. public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = array())
  1157. {
  1158. $this->alert('overdue_subscription', 0, $aPending['client_id'], array(
  1159. 'pending' => $aPending,
  1160. 'subscription' => $aSubscription
  1161. ));
  1162. }
  1163. public function onSubscriptionCancel($aPending, $aSubscription, $aResult = array())
  1164. {
  1165. $this->alert('cancel_subscription', 0, $aPending['client_id'], array(
  1166. 'pending' => $aPending,
  1167. 'subscription' => $aSubscription
  1168. ));
  1169. }
  1170. public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
  1171. {
  1172. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  1173. if(!$oSiteSubmenu)
  1174. return;
  1175. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  1176. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  1177. if(!$oModuleSubmenu)
  1178. return;
  1179. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
  1180. $oModuleSubmenu->setSelected($sSelModule, $sSelName);
  1181. }
  1182. public function getProviderButtonJs($aCartItem, $aProvider, $sRedirect = '', $aCustom = array())
  1183. {
  1184. $aButtonJs = array();
  1185. $iClientId = $this->getProfileId();
  1186. $sItemAddons = '';
  1187. if(count($aCartItem) == 5)
  1188. $sItemAddons = array_pop($aCartItem);
  1189. list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $aCartItem;
  1190. $oProvider = $this->getObjectProvider($aProvider['name'], $iSellerId);
  1191. if($oProvider !== false && method_exists($oProvider, 'getButtonRecurring')) {
  1192. $aParams = array(
  1193. 'sObjNameCart' => $this->_oConfig->getJsObject('cart'),
  1194. 'iSellerId' => $iSellerId,
  1195. 'iModuleId' => $iModuleId,
  1196. 'iItemId' => $iItemId,
  1197. 'iItemCount' => $iItemCount,
  1198. 'sItemAddons' => $sItemAddons,
  1199. 'sRedirect' => $sRedirect,
  1200. 'sCustom' => base64_encode(serialize($aCustom))
  1201. );
  1202. $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));
  1203. if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
  1204. $aItem = array_shift($aCartInfo['items']);
  1205. $aParams = array_merge($aParams, array(
  1206. 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
  1207. 'sItemName' => $aItem['name'],
  1208. 'sItemTitle' => $aItem['title']
  1209. ));
  1210. }
  1211. $aButtonJs = $oProvider->getButtonRecurringJs($iClientId, $iSellerId, $aParams);
  1212. }
  1213. if(empty($aButtonJs) || !is_array($aButtonJs))
  1214. $aButtonJs = $this->getObjectSubscriptions()->serviceGetSubscribeJs($iSellerId, $aProvider['name'], $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  1215. return $aButtonJs;
  1216. }
  1217. /**
  1218. * Integration with itslef which is needed to pay an invoice.
  1219. */
  1220. /**
  1221. * @page service Service Calls
  1222. * @section bx_payment Payment
  1223. * @subsection bx_payment-payments Payments
  1224. * @subsubsection bx_payment-get_payment_data get_payment_data
  1225. *
  1226. * @code bx_srv('bx_payment', 'get_payment_data', [...]); @endcode
  1227. *
  1228. * Get an array with module's description. Is needed for payments processing module.
  1229. *
  1230. * @return an array with module's description.
  1231. *
  1232. * @see BxPaymentModule::serviceGetPaymentData
  1233. */
  1234. /**
  1235. * @ref bx_payment-get_payment_data "get_payment_data"
  1236. */
  1237. public function serviceGetPaymentData()
  1238. {
  1239. return $this->_aModule;
  1240. }
  1241. /**
  1242. * @page service Service Calls
  1243. * @section bx_payment Payment
  1244. * @subsection bx_payment-payments Payments
  1245. * @subsubsection bx_payment-get_cart_item get_cart_item
  1246. *
  1247. * @code bx_srv('bx_payment', 'get_cart_item', [...]); @endcode
  1248. *
  1249. * Get an array with prodict's description. Is used in Shopping Cart in payments processing module.
  1250. *
  1251. * @param $mixedItemId product's ID or Unique Name.
  1252. * @return an array with prodict's description. Empty array is returned if something is wrong.
  1253. *
  1254. * @see BxPaymentModule::serviceGetCartItem
  1255. */
  1256. /**
  1257. * @ref bx_payment-get_cart_item "get_cart_item"
  1258. */
  1259. public function serviceGetCartItem($mixedItemId)
  1260. {
  1261. $CNF = &$this->_oConfig->CNF;
  1262. if(!$mixedItemId)
  1263. return array();
  1264. if(is_numeric($mixedItemId))
  1265. $aItem = $this->_oDb->getInvoices(array('type' => 'id', 'id' => (int)$mixedItemId));
  1266. else
  1267. $aItem = $this->_oDb->getInvoices(array('type' => 'name', 'name' => $mixedItemId));
  1268. if(empty($aItem) || !is_array($aItem))
  1269. return array();
  1270. $oCommissionaire = BxDolProfile::getInstanceMagic($aItem['commissionaire_id']);
  1271. return array (
  1272. 'id' => $aItem['id'],
  1273. 'author_id' => $aItem['commissionaire_id'],
  1274. 'name' => $aItem['name'],
  1275. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $oCommissionaire->getDisplayName()),
  1276. '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)),
  1277. 'url' => '',
  1278. 'price_single' => $aItem['amount'],
  1279. 'price_recurring' => '',
  1280. 'period_recurring' => 0,
  1281. 'period_unit_recurring' => '',
  1282. 'trial_recurring' => ''
  1283. );
  1284. }
  1285. /**
  1286. * @page service Service Calls
  1287. * @section bx_payment Payment
  1288. * @subsection bx_payment-payments Payments
  1289. * @subsubsection bx_payment-get_cart_items get_cart_items
  1290. *
  1291. * @code bx_srv('bx_payment', 'get_cart_items', [...]); @endcode
  1292. *
  1293. * Get an array with prodicts' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  1294. *
  1295. * @param $iCommissionaireId commissionaire ID.
  1296. * @return an array with prodicts' descriptions. Empty array is returned if something is wrong or seller doesn't have any products.
  1297. *
  1298. * @see BxPaymentModule::serviceGetCartItems
  1299. */
  1300. /**
  1301. * @ref bx_payment-get_cart_items "get_cart_items"
  1302. */
  1303. public function serviceGetCartItems($iCommissionaireId)
  1304. {
  1305. $CNF = &$this->_oConfig->CNF;
  1306. $iCommissionaireId = (int)$iCommissionaireId;
  1307. if(empty($iCommissionaireId))
  1308. return array();
  1309. $sCommissionaireName = BxDolProfile::getInstanceMagic($iCommissionaireId)->getDisplayName();
  1310. $aItems = $this->_oDb->getInvoices(array('type' => 'commissionaire_id', 'commissionaire_id' => $iCommissionaireId));
  1311. $aResult = array();
  1312. foreach($aItems as $aItem)
  1313. $aResult[] = array(
  1314. 'id' => $aItem['id'],
  1315. 'author_id' => $aItem['commissionaire_id'],
  1316. 'name' => $aItem['name'],
  1317. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $sCommissionaireName),
  1318. '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)),
  1319. 'url' => '',
  1320. 'price_single' => $aItem['amount'],
  1321. 'price_recurring' => '',
  1322. 'period_recurring' => 0,
  1323. 'period_unit_recurring' => '',
  1324. 'trial_recurring' => ''
  1325. );
  1326. return $aResult;
  1327. }
  1328. /**
  1329. * @page service Service Calls
  1330. * @section bx_payment Payment
  1331. * @subsection bx_payment-payments Payments
  1332. * @subsubsection bx_payment-register_cart_item register_cart_item
  1333. *
  1334. * @code bx_srv('bx_payment', 'register_cart_item', [...]); @endcode
  1335. *
  1336. * Register a processed single time payment inside the Payment module. Is called with payment processing module after the payment was registered there.
  1337. *
  1338. * @param $iClientId client ID.
  1339. * @param $iSellerId seller ID
  1340. * @param $iItemId item ID.
  1341. * @param $iItemCount the number of purchased items.
  1342. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1343. * @param $sLicense license number genereted with payment processing module for internal usage
  1344. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  1345. *
  1346. * @see BxPaymentModule::serviceRegisterCartItem
  1347. */
  1348. /**
  1349. * @ref bx_payment-register_cart_item "register_cart_item"
  1350. */
  1351. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1352. {
  1353. $CNF = &$this->_oConfig->CNF;
  1354. $aItem = $this->serviceGetCartItem($iItemId);
  1355. if(empty($aItem) || !is_array($aItem))
  1356. return array();
  1357. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_PAID)))
  1358. return array();
  1359. $this->alert('invoice_marked_as_paid', 0, false, array(
  1360. 'product_id' => $iItemId,
  1361. 'profile_id' => $iClientId,
  1362. 'order' => $sOrder,
  1363. 'license' => $sLicense,
  1364. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1365. 'count' => $iItemCount,
  1366. 'duration' => '',
  1367. 'trial' => ''
  1368. ));
  1369. return $aItem;
  1370. }
  1371. /**
  1372. * @page service Service Calls
  1373. * @section bx_payment Payment
  1374. * @subsection bx_payment-payments Payments
  1375. * @subsubsection bx_payment-unregister_cart_item unregister_cart_item
  1376. *
  1377. * @code bx_srv('bx_payment', 'unregister_cart_item', [...]); @endcode
  1378. *
  1379. * Unregister an earlier processed single time payment inside the Payment module. Is called with payment processing module after the payment was unregistered there.
  1380. *
  1381. * @param $iClientId client ID.
  1382. * @param $iSellerId seller ID
  1383. * @param $iItemId item ID.
  1384. * @param $iItemCount the number of purchased items.
  1385. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1386. * @param $sLicense license number genereted with payment processing module for internal usage
  1387. * @return boolean value determining where the payment was unregistered or not.
  1388. *
  1389. * @see BxPaymentModule::serviceUnregisterCartItem
  1390. */
  1391. /**
  1392. * @ref bx_payment-unregister_cart_item "unregister_cart_item"
  1393. */
  1394. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1395. {
  1396. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_UNPAID)))
  1397. return false;
  1398. $this->alert('invoice_marked_as_unpaid', 0, false, array(
  1399. 'product_id' => $iItemId,
  1400. 'profile_id' => $iClientId,
  1401. 'order' => $sOrder,
  1402. 'license' => $sLicense,
  1403. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1404. 'count' => $iItemCount
  1405. ));
  1406. return true;
  1407. }
  1408. }
  1409. /** @} */