BxPaymentModule.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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. public function actionInitializeCheckoutJson($sType)
  610. {
  611. if(!$this->isLogged())
  612. return echoJson(['code' => 1, 'message' => _t($this->_sLangsPrefix . 'err_required_login')]);
  613. if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
  614. $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
  615. $sProvider = bx_process_input(bx_get('provider'));
  616. $aItems = bx_process_input(bx_get('items'));
  617. $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
  618. if($mixedResult === false)
  619. return echoJson(['code' => 2, 'message' => _t($this->_sLangsPrefix . 'err_cannot_perform')]);
  620. if(is_string($mixedResult))
  621. return echoJson(['code' => 3, 'message' => $mixedResult]);
  622. else if(is_array($mixedResult))
  623. return echoJson($mixedResult);
  624. }
  625. return echoJson(['code' => 0, 'redirect' => $this->_oConfig->getUrl('URL_CART')]);
  626. }
  627. /**
  628. * @page service Service Calls
  629. * @section bx_payment Payment
  630. * @subsection bx_payment-purchase_processing Purchase Processing
  631. * @subsubsection bx_payment-initialize_checkout initialize_checkout
  632. *
  633. * @code bx_srv('bx_payment', 'initialize_checkout', [...]); @endcode
  634. *
  635. * Initialize the checkout process.
  636. *
  637. * @param $sType string value with payment type (single or recurring).
  638. * @param $iSellerId integer value with seller ID.
  639. * @param $sProvider string value with payment provider name.
  640. * @param $aItems (optional) an array with items to be purchased.
  641. * @param $sRedirect (optional) string value with redirect URL if it's needed.
  642. * @param $aCustom (optional) array with custom data to attach to a payment.
  643. * @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.
  644. *
  645. * @see BxPaymentModule::serviceInitializeCheckout
  646. */
  647. /**
  648. * @ref bx_payment-initialize_checkout "initialize_checkout"
  649. */
  650. public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '', $aCustoms = array())
  651. {
  652. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  653. $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
  654. if(!is_array($aItems))
  655. $aItems = array($aItems);
  656. $iSellerId = (int)$iSellerId;
  657. if($iSellerId == BX_PAYMENT_EMPTY_ID)
  658. return $this->_sLangsPrefix . 'err_unknown_vendor';
  659. $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
  660. if($oProvider === false || !$oProvider->isActive())
  661. return $this->_sLangsPrefix . 'err_incorrect_provider';
  662. $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
  663. if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
  664. return $this->_sLangsPrefix . 'err_empty_order';
  665. /*
  666. * Process FREE (price = 0) items for LOGGED IN members
  667. * WITHOUT processing via payment provider.
  668. * Note. This section isn't used for now!
  669. */
  670. $bProcessedFree = false;
  671. $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  672. $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
  673. foreach($aInfo['items'] as $iIndex => $aItem) {
  674. if((int)$aInfo['client_id'] == 0)
  675. continue;
  676. //--- For Single time payments.
  677. if($bTypeSingle && (float)$aItem[$sKeyPriceSingle] == 0) {
  678. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  679. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  680. $aCartItem = array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity']);
  681. $aCartItemCustom = $this->_oConfig->pullCustom($aCartItem, $aCartCustoms);
  682. $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  683. if(!empty($aItemInfo) && is_array($aItemInfo)) {
  684. if(!empty($aItemInfo['addons']) && is_array($aItemInfo['addons']))
  685. foreach($aItemInfo['addons'] as $sAddon => $aAddon)
  686. $this->callRegisterCartItem((int)$aAddon['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aAddon['id'], $aAddon['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
  687. $bProcessedFree = true;
  688. }
  689. $aInfo['items_count'] -= 1;
  690. unset($aInfo['items'][$iIndex]);
  691. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aCartItem) . ":?'", "", $aCart['items']), ":");
  692. $this->_oDb->setCartItems($aInfo['client_id'], $aCart['items'], $aCartCustoms);
  693. }
  694. //--- For Recurring payments.
  695. if($bTypeRecurring && (float)$aItem[$sKeyPriceRecurring] == 0) {
  696. //TODO: Process FREE subscription here, if such situation will be possible.
  697. }
  698. }
  699. if(empty($aInfo['items']))
  700. return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
  701. if($bTypeSingle && (empty($aCustoms) || !is_array($aCustoms))) {
  702. $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
  703. $aCartCustoms = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  704. $aCustoms = array();
  705. foreach($aInfo['items'] as $aItem) {
  706. $sCartItem = $this->_oConfig->descriptorA2S(array($aItem['author_id'], $aItem['module_id'], $aItem['id']));
  707. $aCartItemCustom = $this->_oConfig->getCustom($sCartItem, $aCartCustoms);
  708. $this->_oConfig->putCustom($sCartItem, $aCartItemCustom, $aCustoms);
  709. }
  710. }
  711. $this->alert('before_pending', 0, bx_get_logged_profile_id(), array(
  712. 'client_id' => $this->_iUserId,
  713. 'type' => &$sType,
  714. 'provider' => &$sProvider,
  715. 'cart_info' => &$aInfo,
  716. 'custom' => &$aCustoms,
  717. ));
  718. $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo, $aCustoms);
  719. if(empty($iPendingId))
  720. return $this->_sLangsPrefix . 'err_access_db';
  721. /*
  722. * Perform Join WITHOUT processing via payment provider
  723. * if a client ISN'T logged in and has only ONE FREE item in the card.
  724. * Note. This section isn't used for now because only a member can purchase!
  725. */
  726. if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
  727. reset($aInfo['items']);
  728. $aItem = current($aInfo['items']);
  729. if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
  730. $this->_oDb->updateOrderPending($iPendingId, array(
  731. 'order' => $this->_oConfig->getLicense(),
  732. 'error_code' => '1',
  733. 'error_msg' => ''
  734. ));
  735. $this->getObjectJoin()->performJoin($iPendingId);
  736. }
  737. }
  738. return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
  739. }
  740. public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
  741. {
  742. $aData = &$_REQUEST;
  743. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  744. if($oProvider === false || !$oProvider->isActive())
  745. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  746. $aResult = $oProvider->finalizeCheckout($aData);
  747. if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
  748. return $this->_oTemplate->displayPageCodeError($aResult['message']);
  749. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  750. $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
  751. if($bTypeRecurring) {
  752. $sStatus = BX_PAYMENT_SBS_STATUS_UNPAID;
  753. if(isset($aResult['paid']) && $aResult['paid'])
  754. $sStatus = BX_PAYMENT_SBS_STATUS_ACTIVE;
  755. else if(isset($aResult['trial']) && $aResult['trial'])
  756. $sStatus = BX_PAYMENT_SBS_STATUS_TRIAL;
  757. $aSubscription = array(
  758. 'customer_id' => $aResult['customer_id'],
  759. 'subscription_id' => $aResult['subscription_id'],
  760. 'status' => $sStatus
  761. );
  762. if(!$this->getObjectSubscriptions()->register($aPending, $aSubscription))
  763. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_already_registered');
  764. }
  765. $this->onPaymentRegisterBefore($aPending);
  766. //--- Check "Pay Before Join" situation
  767. if((int)$aPending['client_id'] == 0)
  768. $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
  769. //--- Register payment for purchased items in associated modules
  770. if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
  771. $this->registerPayment($aPending);
  772. $this->alert('finalize_checkout', 0, bx_get_logged_profile_id(), array(
  773. 'pending' => $aPending,
  774. 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
  775. 'provider' => $oProvider,
  776. 'message' => &$aResult['message'],
  777. 'result' => &$aResult,
  778. ));
  779. if($oProvider->needRedirect()) {
  780. header('Location: ' . $oProvider->getReturnUrl());
  781. exit;
  782. }
  783. if(!empty($aResult['redirect'])) {
  784. header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
  785. exit;
  786. }
  787. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  788. }
  789. public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
  790. {
  791. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  792. if($oProvider === false || !$oProvider->isActive())
  793. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  794. $aResult = $oProvider->finalizedCheckout();
  795. $this->_oTemplate->displayPageCodeResponse($aResult['message']);
  796. }
  797. public function actionNotify($sProvider, $mixedVendorId = "")
  798. {
  799. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  800. if($oProvider === false || !$oProvider->isActive())
  801. return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
  802. $oProvider->notify();
  803. }
  804. /**
  805. * The method is needed to pass direct action calls to necessary Payment Provider.
  806. */
  807. public function actionCall($sProvider, $sAction, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
  808. {
  809. $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
  810. if($oProvider === false)
  811. BxDolRequest::methodNotFound($sAction, $this->getName());
  812. $sMethod = 'action' . bx_gen_method_name($sAction);
  813. if(!method_exists($oProvider, $sMethod))
  814. BxDolRequest::methodNotFound($sAction, $this->getName());
  815. return call_user_func_array(array($oProvider, $sMethod), array_slice(func_get_args(), 2));
  816. }
  817. public function onProfileJoin($iProfileId)
  818. {
  819. $this->getObjectJoin()->onProfileJoin($iProfileId);
  820. }
  821. public function onProfileDelete($iProfileId)
  822. {
  823. //--- Cancel subscriptions
  824. $aSubscriptions = $this->_oDb->getSubscription(['type' => 'mixed_ext', 'conditions' => ['client_id' => $iProfileId]]);
  825. if(!empty($aSubscriptions) && is_array($aSubscriptions))
  826. foreach($aSubscriptions as $aSubscription)
  827. $this->getObjectSubscriptions()->cancel($aSubscription['pending_id']);
  828. //--- Clean DB
  829. $this->_oDb->onProfileDelete($iProfileId);
  830. }
  831. public function isAllowedPurchase($aItem, $bPerform = false)
  832. {
  833. $iUserId = (int)$this->getProfileId();
  834. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], isset($aItem['custom']) ? $aItem['custom'] : array()));
  835. if(empty($aItemInfo))
  836. return false;
  837. if(isAdmin())
  838. return true;
  839. $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
  840. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  841. return true;
  842. return $aCheckResult[CHECK_ACTION_MESSAGE];
  843. }
  844. public function isAllowedSell($aItem, $bPerform = false)
  845. {
  846. $iUserId = (int)$this->getProfileId();
  847. if(!$iUserId)
  848. return false;
  849. $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id']));
  850. if(empty($aItemInfo))
  851. return false;
  852. if(isAdmin())
  853. return true;
  854. $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
  855. if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  856. return true;
  857. return $aCheckResult[CHECK_ACTION_MESSAGE];
  858. }
  859. public function isAllowedManage($aPending, $bPerform = false)
  860. {
  861. $iUserId = (int)$this->getProfileId();
  862. if($iUserId == $aPending['client_id'] || $iUserId == $aPending['seller_id'] || isAdmin())
  863. return true;
  864. $aCheckResult = checkActionModule($iUserId, 'manage any purchase', $this->getName(), $bPerform);
  865. if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
  866. return true;
  867. return $aCheckResult[CHECK_ACTION_MESSAGE];
  868. }
  869. public function isAllowedManageInvoices($bPerform = false)
  870. {
  871. if(BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)) || isAdmin())
  872. return true;
  873. return _t('_sys_txt_access_denied');
  874. }
  875. public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom = array())
  876. {
  877. $CNF = &$this->_oConfig->CNF;
  878. if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
  879. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  880. $iClientId = $this->getProfileId();
  881. if(empty($iClientId)) {
  882. $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
  883. return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
  884. }
  885. $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId, 'custom' => $aCustom));
  886. if($mixedResult !== true) {
  887. if(is_string($mixedResult) && !empty($mixedResult))
  888. return array('code' => 2, 'message' => $mixedResult);
  889. else
  890. return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
  891. }
  892. if($iClientId == $iSellerId)
  893. return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
  894. $aSeller = $this->getVendorInfo($iSellerId);
  895. if(!$aSeller['active'])
  896. return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
  897. return true;
  898. }
  899. public function registerPayment($mixedPending)
  900. {
  901. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  902. if(empty($aPending) || !is_array($aPending))
  903. return false;
  904. $sType = $aPending['type'];
  905. $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
  906. if($bTypeSingle && (int)$aPending['processed'] == 1)
  907. return true;
  908. $iClientId = (int)$aPending['client_id'];
  909. $sLicense = $this->_oConfig->getLicense();
  910. $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
  911. $aCart = array();
  912. if($bTypeSingle) {
  913. $aCart = $this->_oDb->getCartContent($iClientId);
  914. $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
  915. }
  916. $bResult = false;
  917. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  918. foreach($aItems as $aItem) {
  919. $sItem = $this->_oConfig->descriptorA2S(array($aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']));
  920. $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
  921. $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
  922. $aItemInfo = $this->$sMethod((int)$aItem['module_id'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
  923. if(empty($aItemInfo) || !is_array($aItemInfo))
  924. continue;
  925. $this->_oDb->insertOrderProcessed(array(
  926. 'pending_id' => $aPending['id'],
  927. 'client_id' => $aPending['client_id'],
  928. 'seller_id' => $aPending['seller_id'],
  929. 'author_id' => $aItem['vendor_id'],
  930. 'module_id' => (int)$aItem['module_id'],
  931. 'item_id' => (int)$aItem['item_id'],
  932. 'item_count' => (int)$aItem['item_count'],
  933. 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
  934. 'license' => $sLicense,
  935. ));
  936. if($bTypeSingle) {
  937. $this->_oConfig->pullCustom($sItem, $aCart['customs']);
  938. $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
  939. }
  940. $bResult = true;
  941. }
  942. if($bTypeSingle)
  943. $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
  944. if($bResult) {
  945. $this->_oDb->updateOrderPending($aPending['id'], array('processed' => 1));
  946. $this->onPaymentRegister($aPending);
  947. }
  948. return $bResult;
  949. }
  950. public function refundPayment($mixedPending)
  951. {
  952. $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
  953. if(empty($aPending) || !is_array($aPending))
  954. return false;
  955. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  956. $iCanceled = 0;
  957. $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
  958. foreach($aOrders as $aOrder) {
  959. $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
  960. $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
  961. if(!$bResult)
  962. continue;
  963. if($this->_oDb->deleteOrderProcessed($aOrder['id']))
  964. $iCanceled++;
  965. }
  966. if($iCanceled != count($aOrders))
  967. return false;
  968. $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
  969. if($bResult)
  970. $this->onPaymentRefund($aPending);
  971. return $bResult;
  972. }
  973. /**
  974. * @deprecated since version 12.0.0
  975. *
  976. * @use BxPaymentSubscriptions::register instead.
  977. */
  978. public function registerSubscription($aPending, $aParams = array())
  979. {
  980. return $this->getObjectSubscriptions()->register($aPending, $aParams);
  981. }
  982. /**
  983. * @deprecated since version 12.0.0
  984. *
  985. * @use BxPaymentSubscriptions::cancelLocal instead.
  986. */
  987. public function cancelSubscription($mixedPending)
  988. {
  989. return $this->getObjectSubscriptions()->cancelLocal($mixedPending);
  990. }
  991. public function processCommissions()
  992. {
  993. if(!defined('BX_DOL_CRON_EXECUTE'))
  994. return;
  995. $CNF = &$this->_oConfig->CNF;
  996. if((int)date('j') == $this->_oConfig->getInvoiceIssueDay())
  997. $this->_invoicesIssue();
  998. $this->_invoicesCheck();
  999. }
  1000. protected function _invoicesIssue()
  1001. {
  1002. $CNF = &$this->_oConfig->CNF;
  1003. $sInvoiceNameMask = 'INV-%d-%d-%d';
  1004. $iMonth = date('m');
  1005. $iYear = date('Y');
  1006. $iPeriodStart = mktime(0, 0, 0, $iMonth-1, 1, $iYear);
  1007. $iPeriodEnd = mktime(23, 59, 59, $iMonth, 0, $iYear);
  1008. $iDateIssue = time();
  1009. $iDateDue = $iDateIssue + 86400 * $this->_oConfig->getInvoiceLifetime();
  1010. $aCommissionsInfo = array();
  1011. $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, true, false, true);
  1012. foreach($aAclLevels as $iAclLevelId => $sAclLevelTitle)
  1013. $aCommissionsInfo[$iAclLevelId] = $this->_oDb->getCommissions(array('type' => 'acl_id', 'acl_id' => $iAclLevelId));
  1014. $iMainSellerId = $this->_oConfig->getSiteAdmin();
  1015. $aVendors = $this->_oDb->getOrderProcessed(array('type' => 'income', 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  1016. foreach($aVendors as $aVendor) {
  1017. $iVendorId = (int)$aVendor['id'];
  1018. if($iVendorId == $iMainSellerId)
  1019. continue;
  1020. $aInvoice = $this->_oDb->getInvoices(array('type' => 'committent_id', 'committent_id' => $iVendorId, 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
  1021. if(!empty($aInvoice) && is_array($aInvoice))
  1022. continue;
  1023. $aVendorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($iVendorId);
  1024. $aCommissions = $aCommissionsInfo[$aVendorAcl['id']];
  1025. $fCommission = 0;
  1026. foreach($aCommissions as $aCommission) {
  1027. if((float)$aCommission['percentage'] > 0)
  1028. $fCommission += (float)$aVendor['amount'] * (float)$aCommission['percentage'] / 100;
  1029. if((float)$aCommission['installment'] > 0)
  1030. $fCommission += (float)$aCommission['installment'];
  1031. }
  1032. $this->alert('calculate_commission', 0, 0, array(
  1033. 'vendor' => $aVendor,
  1034. 'commissions' => $aCommissions,
  1035. 'override_result' => &$fCommission,
  1036. ));
  1037. $this->_oDb->insertInvoice(array(
  1038. 'name' => sprintf($sInvoiceNameMask, $iMainSellerId, $iVendorId, $this->_oDb->getInvoices(array(
  1039. 'type' => 'index',
  1040. 'commissionaire_id' => $iMainSellerId,
  1041. 'committent_id' => $iVendorId
  1042. ))),
  1043. 'commissionaire_id' => $iMainSellerId,
  1044. 'committent_id' => $iVendorId,
  1045. 'amount' => $fCommission,
  1046. 'period_start' => $iPeriodStart,
  1047. 'period_end' => $iPeriodEnd,
  1048. 'date_issue' => $iDateIssue,
  1049. 'date_due' => $iDateDue,
  1050. 'status' => BX_PAYMENT_INV_STATUS_UNPAID
  1051. ));
  1052. }
  1053. }
  1054. protected function _invoicesCheck()
  1055. {
  1056. $CNF = &$this->_oConfig->CNF;
  1057. $sPrefix = $this->_oConfig->getPrefix('general');
  1058. $oEmailTemplates = BxDolEmailTemplates::getInstance();
  1059. //--- Process expiring invoices.
  1060. $aInvoices = $this->_oDb->getInvoices(array('type' => 'expiring'));
  1061. if(!empty($aInvoices) && is_array($aInvoices))
  1062. foreach($aInvoices as $aInvoice) {
  1063. if((int)$aInvoice['ntf_exp'] > 0)
  1064. continue;
  1065. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1066. if(!$oCommittent)
  1067. continue;
  1068. $bResult = sendMailTemplate($sPrefix . 'expiring_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1069. 'invoice' => $aInvoice['name'],
  1070. 'days' => $this->_oConfig->getInvoiceExpirationNotify(),
  1071. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1072. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1073. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1074. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1075. ), BX_EMAIL_NOTIFY, true);
  1076. if($bResult)
  1077. $this->_oDb->updateInvoice($aInvoice['id'], array('ntf_exp' => 1));
  1078. }
  1079. //--- Process overdue invoices.
  1080. $aInvoices = $this->_oDb->getInvoices(array('type' => 'overdue'));
  1081. if(!empty($aInvoices) && is_array($aInvoices))
  1082. foreach($aInvoices as $aInvoice) {
  1083. if((int)$aInvoice['ntf_due'] > 0 && $aInvoice['status'] == BX_PAYMENT_INV_STATUS_OVERDUE)
  1084. continue;
  1085. $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
  1086. if(!$oCommittent)
  1087. continue;
  1088. $bResult = sendMailTemplate($sPrefix . 'overdue_notification_committent', 0, (int)$aInvoice['committent_id'], array(
  1089. 'invoice' => $aInvoice['name'],
  1090. 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
  1091. 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
  1092. 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
  1093. 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
  1094. ), BX_EMAIL_NOTIFY, true);
  1095. $this->_oDb->updateInvoice($aInvoice['id'], array(
  1096. 'status' => BX_PAYMENT_INV_STATUS_OVERDUE,
  1097. 'ntf_due' => $bResult ? 1 : 0
  1098. ));
  1099. }
  1100. }
  1101. public function processTimeTracker()
  1102. {
  1103. if(!defined('BX_DOL_CRON_EXECUTE'))
  1104. return;
  1105. $aSubscriptions = $this->_oDb->getSubscription(array(
  1106. 'type' => 'time_tracker',
  1107. 'status_active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
  1108. 'status_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
  1109. 'status_unpaid' => BX_PAYMENT_SBS_STATUS_UNPAID,
  1110. 'pay_attempts_max' => $this->_oConfig->getPayAttemptsMax(),
  1111. 'pay_attempts_interval' => $this->_oConfig->getPayAttemptsInterval()
  1112. ));
  1113. if(empty($aSubscriptions) || !is_array($aSubscriptions))
  1114. return;
  1115. foreach($aSubscriptions as $aSubscription) {
  1116. $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aSubscription['pending_id']));
  1117. if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING) {
  1118. $this->log($aSubscription, 'Time Tracker', 'Cannot process subscription.');
  1119. continue;
  1120. }
  1121. $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
  1122. if($oProvider === false || !$oProvider->isActive() || !method_exists($oProvider, 'makePayment')) {
  1123. $this->log($aSubscription, 'Time Tracker', 'Payment provider unavailable or incorrect.');
  1124. continue;
  1125. }
  1126. if(!$oProvider->makePayment($aPending)) {
  1127. $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed.');
  1128. continue;
  1129. }
  1130. if(!$this->getObjectSubscriptions()->prolong($aPending)) {
  1131. $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.');
  1132. continue;
  1133. }
  1134. if(!$this->registerPayment($aPending)) {
  1135. $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.');
  1136. continue;
  1137. }
  1138. }
  1139. }
  1140. public function onPaymentRegisterBefore($aPending, $aResult = array())
  1141. {
  1142. $this->alert('before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1143. }
  1144. public function onPaymentRegister($aPending, $aResult = array())
  1145. {
  1146. $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
  1147. if($bTypeSingle) {
  1148. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1149. foreach($aItems as $aItem)
  1150. $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
  1151. }
  1152. $this->alert('register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1153. }
  1154. public function onPaymentRefund($aPending, $aResult = array())
  1155. {
  1156. $this->alert('refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
  1157. }
  1158. public function onSubscriptionCreate($aPending, $aSubscription, $aResult = array())
  1159. {
  1160. $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
  1161. $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
  1162. $this->alert('create_subscription', 0, $aPending['client_id'], array(
  1163. 'pending' => $aPending,
  1164. 'subscription' => $aSubscription
  1165. ));
  1166. }
  1167. public function onSubscriptionProlong($aPending, $aSubscription, $aResult = array())
  1168. {
  1169. $this->alert('prolong_subscription', 0, $aPending['client_id'], array(
  1170. 'pending' => $aPending,
  1171. 'subscription' => $aSubscription
  1172. ));
  1173. }
  1174. public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = array())
  1175. {
  1176. $this->alert('overdue_subscription', 0, $aPending['client_id'], array(
  1177. 'pending' => $aPending,
  1178. 'subscription' => $aSubscription
  1179. ));
  1180. }
  1181. public function onSubscriptionCancel($aPending, $aSubscription, $aResult = array())
  1182. {
  1183. $this->alert('cancel_subscription', 0, $aPending['client_id'], array(
  1184. 'pending' => $aPending,
  1185. 'subscription' => $aSubscription
  1186. ));
  1187. }
  1188. public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
  1189. {
  1190. $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  1191. if(!$oSiteSubmenu)
  1192. return;
  1193. $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
  1194. $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
  1195. if(!$oModuleSubmenu)
  1196. return;
  1197. $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
  1198. $oModuleSubmenu->setSelected($sSelModule, $sSelName);
  1199. }
  1200. public function getProviderButtonJs($aCartItem, $aProvider, $sRedirect = '', $aCustom = array())
  1201. {
  1202. $aButtonJs = array();
  1203. $iClientId = $this->getProfileId();
  1204. $sItemAddons = '';
  1205. if(count($aCartItem) == 5)
  1206. $sItemAddons = array_pop($aCartItem);
  1207. list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $aCartItem;
  1208. $oProvider = $this->getObjectProvider($aProvider['name'], $iSellerId);
  1209. if($oProvider !== false && method_exists($oProvider, 'getButtonRecurring')) {
  1210. $aParams = array(
  1211. 'sObjNameCart' => $this->_oConfig->getJsObject('cart'),
  1212. 'iSellerId' => $iSellerId,
  1213. 'iModuleId' => $iModuleId,
  1214. 'iItemId' => $iItemId,
  1215. 'iItemCount' => $iItemCount,
  1216. 'sItemAddons' => $sItemAddons,
  1217. 'sRedirect' => $sRedirect,
  1218. 'sCustom' => base64_encode(serialize($aCustom))
  1219. );
  1220. $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));
  1221. if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
  1222. $aItem = array_shift($aCartInfo['items']);
  1223. $aParams = array_merge($aParams, array(
  1224. 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
  1225. 'sItemName' => $aItem['name'],
  1226. 'sItemTitle' => $aItem['title']
  1227. ));
  1228. }
  1229. $aButtonJs = $oProvider->getButtonRecurringJs($iClientId, $iSellerId, $aParams);
  1230. }
  1231. if(empty($aButtonJs) || !is_array($aButtonJs))
  1232. $aButtonJs = $this->getObjectSubscriptions()->serviceGetSubscribeJs($iSellerId, $aProvider['name'], $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
  1233. return $aButtonJs;
  1234. }
  1235. /**
  1236. * Integration with itslef which is needed to pay an invoice.
  1237. */
  1238. /**
  1239. * @page service Service Calls
  1240. * @section bx_payment Payment
  1241. * @subsection bx_payment-payments Payments
  1242. * @subsubsection bx_payment-get_payment_data get_payment_data
  1243. *
  1244. * @code bx_srv('bx_payment', 'get_payment_data', [...]); @endcode
  1245. *
  1246. * Get an array with module's description. Is needed for payments processing module.
  1247. *
  1248. * @return an array with module's description.
  1249. *
  1250. * @see BxPaymentModule::serviceGetPaymentData
  1251. */
  1252. /**
  1253. * @ref bx_payment-get_payment_data "get_payment_data"
  1254. */
  1255. public function serviceGetPaymentData()
  1256. {
  1257. return $this->_aModule;
  1258. }
  1259. /**
  1260. * @page service Service Calls
  1261. * @section bx_payment Payment
  1262. * @subsection bx_payment-payments Payments
  1263. * @subsubsection bx_payment-get_cart_item get_cart_item
  1264. *
  1265. * @code bx_srv('bx_payment', 'get_cart_item', [...]); @endcode
  1266. *
  1267. * Get an array with prodict's description. Is used in Shopping Cart in payments processing module.
  1268. *
  1269. * @param $mixedItemId product's ID or Unique Name.
  1270. * @return an array with prodict's description. Empty array is returned if something is wrong.
  1271. *
  1272. * @see BxPaymentModule::serviceGetCartItem
  1273. */
  1274. /**
  1275. * @ref bx_payment-get_cart_item "get_cart_item"
  1276. */
  1277. public function serviceGetCartItem($mixedItemId)
  1278. {
  1279. $CNF = &$this->_oConfig->CNF;
  1280. if(!$mixedItemId)
  1281. return array();
  1282. if(is_numeric($mixedItemId))
  1283. $aItem = $this->_oDb->getInvoices(array('type' => 'id', 'id' => (int)$mixedItemId));
  1284. else
  1285. $aItem = $this->_oDb->getInvoices(array('type' => 'name', 'name' => $mixedItemId));
  1286. if(empty($aItem) || !is_array($aItem))
  1287. return array();
  1288. $oCommissionaire = BxDolProfile::getInstanceMagic($aItem['commissionaire_id']);
  1289. return array (
  1290. 'id' => $aItem['id'],
  1291. 'author_id' => $aItem['commissionaire_id'],
  1292. 'name' => $aItem['name'],
  1293. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $oCommissionaire->getDisplayName()),
  1294. '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)),
  1295. 'url' => '',
  1296. 'price_single' => $aItem['amount'],
  1297. 'price_recurring' => '',
  1298. 'period_recurring' => 0,
  1299. 'period_unit_recurring' => '',
  1300. 'trial_recurring' => ''
  1301. );
  1302. }
  1303. /**
  1304. * @page service Service Calls
  1305. * @section bx_payment Payment
  1306. * @subsection bx_payment-payments Payments
  1307. * @subsubsection bx_payment-get_cart_items get_cart_items
  1308. *
  1309. * @code bx_srv('bx_payment', 'get_cart_items', [...]); @endcode
  1310. *
  1311. * Get an array with prodicts' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  1312. *
  1313. * @param $iCommissionaireId commissionaire ID.
  1314. * @return an array with prodicts' descriptions. Empty array is returned if something is wrong or seller doesn't have any products.
  1315. *
  1316. * @see BxPaymentModule::serviceGetCartItems
  1317. */
  1318. /**
  1319. * @ref bx_payment-get_cart_items "get_cart_items"
  1320. */
  1321. public function serviceGetCartItems($iCommissionaireId)
  1322. {
  1323. $CNF = &$this->_oConfig->CNF;
  1324. $iCommissionaireId = (int)$iCommissionaireId;
  1325. if(empty($iCommissionaireId))
  1326. return array();
  1327. $sCommissionaireName = BxDolProfile::getInstanceMagic($iCommissionaireId)->getDisplayName();
  1328. $aItems = $this->_oDb->getInvoices(array('type' => 'commissionaire_id', 'commissionaire_id' => $iCommissionaireId));
  1329. $aResult = array();
  1330. foreach($aItems as $aItem)
  1331. $aResult[] = array(
  1332. 'id' => $aItem['id'],
  1333. 'author_id' => $aItem['commissionaire_id'],
  1334. 'name' => $aItem['name'],
  1335. 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $sCommissionaireName),
  1336. '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)),
  1337. 'url' => '',
  1338. 'price_single' => $aItem['amount'],
  1339. 'price_recurring' => '',
  1340. 'period_recurring' => 0,
  1341. 'period_unit_recurring' => '',
  1342. 'trial_recurring' => ''
  1343. );
  1344. return $aResult;
  1345. }
  1346. /**
  1347. * @page service Service Calls
  1348. * @section bx_payment Payment
  1349. * @subsection bx_payment-payments Payments
  1350. * @subsubsection bx_payment-register_cart_item register_cart_item
  1351. *
  1352. * @code bx_srv('bx_payment', 'register_cart_item', [...]); @endcode
  1353. *
  1354. * Register a processed single time payment inside the Payment module. Is called with payment processing module after the payment was registered there.
  1355. *
  1356. * @param $iClientId client ID.
  1357. * @param $iSellerId seller ID
  1358. * @param $iItemId item ID.
  1359. * @param $iItemCount the number of purchased items.
  1360. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1361. * @param $sLicense license number genereted with payment processing module for internal usage
  1362. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  1363. *
  1364. * @see BxPaymentModule::serviceRegisterCartItem
  1365. */
  1366. /**
  1367. * @ref bx_payment-register_cart_item "register_cart_item"
  1368. */
  1369. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1370. {
  1371. $CNF = &$this->_oConfig->CNF;
  1372. $aItem = $this->serviceGetCartItem($iItemId);
  1373. if(empty($aItem) || !is_array($aItem))
  1374. return array();
  1375. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_PAID)))
  1376. return array();
  1377. $this->alert('invoice_marked_as_paid', 0, false, array(
  1378. 'product_id' => $iItemId,
  1379. 'profile_id' => $iClientId,
  1380. 'order' => $sOrder,
  1381. 'license' => $sLicense,
  1382. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1383. 'count' => $iItemCount,
  1384. 'duration' => '',
  1385. 'trial' => ''
  1386. ));
  1387. return $aItem;
  1388. }
  1389. /**
  1390. * @page service Service Calls
  1391. * @section bx_payment Payment
  1392. * @subsection bx_payment-payments Payments
  1393. * @subsubsection bx_payment-unregister_cart_item unregister_cart_item
  1394. *
  1395. * @code bx_srv('bx_payment', 'unregister_cart_item', [...]); @endcode
  1396. *
  1397. * Unregister an earlier processed single time payment inside the Payment module. Is called with payment processing module after the payment was unregistered there.
  1398. *
  1399. * @param $iClientId client ID.
  1400. * @param $iSellerId seller ID
  1401. * @param $iItemId item ID.
  1402. * @param $iItemCount the number of purchased items.
  1403. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1404. * @param $sLicense license number genereted with payment processing module for internal usage
  1405. * @return boolean value determining where the payment was unregistered or not.
  1406. *
  1407. * @see BxPaymentModule::serviceUnregisterCartItem
  1408. */
  1409. /**
  1410. * @ref bx_payment-unregister_cart_item "unregister_cart_item"
  1411. */
  1412. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1413. {
  1414. if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_UNPAID)))
  1415. return false;
  1416. $this->alert('invoice_marked_as_unpaid', 0, false, array(
  1417. 'product_id' => $iItemId,
  1418. 'profile_id' => $iClientId,
  1419. 'order' => $sOrder,
  1420. 'license' => $sLicense,
  1421. 'type' => BX_PAYMENT_TYPE_SINGLE,
  1422. 'count' => $iItemCount
  1423. ));
  1424. return true;
  1425. }
  1426. }
  1427. /** @} */