BxPaymentModule.php 65 KB

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