| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup Payment Payment
- * @ingroup UnaModules
- *
- * @{
- */
- define('BX_PAYMENT_ORDERS_TYPE_PENDING', 'pending');
- define('BX_PAYMENT_ORDERS_TYPE_PROCESSED', 'processed');
- define('BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION', 'subscription');
- define('BX_PAYMENT_ORDERS_TYPE_HISTORY', 'history');
- define('BX_PAYMENT_RESULT_SUCCESS', 0);
- /*
- * Subscriptions: statuses.
- */
- define('BX_PAYMENT_SBS_STATUS_SCHEDULED', 'scheduled');
- define('BX_PAYMENT_SBS_STATUS_TRIAL', 'trial');
- define('BX_PAYMENT_SBS_STATUS_ACTIVE', 'active');
- define('BX_PAYMENT_SBS_STATUS_UNPAID', 'unpaid');
- define('BX_PAYMENT_SBS_STATUS_PAUSED', 'paused');
- define('BX_PAYMENT_SBS_STATUS_CANCELED', 'canceled');
- define('BX_PAYMENT_SBS_STATUS_UNKNOWN', 'unknown');
- /*
- * Subscriptions: period units.
- */
- define('BX_PAYMENT_SBS_PU_YEAR', 'year');
- define('BX_PAYMENT_SBS_PU_MONTH', 'month');
- define('BX_PAYMENT_SBS_PU_WEEK', 'week');
- define('BX_PAYMENT_SBS_PU_DAY', 'day');
- define('BX_PAYMENT_SBS_PU_HOUR', 'hour');
- define('BX_PAYMENT_SBS_PU_MINUTE', 'minute');
- /*
- * Invoice: statuses.
- */
- define('BX_PAYMENT_INV_STATUS_UNPAID', 'unpaid');
- define('BX_PAYMENT_INV_STATUS_PAID', 'paid');
- define('BX_PAYMENT_INV_STATUS_OVERDUE', 'overdue');
- /*
- * Provider options: extended mode.
- */
- define('BX_PAYMENT_POPT_EXT_NONE', 0);
- define('BX_PAYMENT_POPT_EXT_SAVE', 1);
- define('BX_PAYMENT_POPT_EXT_RESET', 2);
- /**
- * Payment module by BoonEx
- *
- * This module is needed to work with payment providers and organize the process
- * of some item purchasing. Shopping Cart and Orders Manager are included.
- *
- * Integration notes:
- * To integrate your module with this one, you need:
- * 1. Get 'Add To Cart' button using serviceGetAddToCartLink service.
- * 2. Add info about your module in the 'bx_pmt_modules' table.
- * 3. Realize the following service methods in your Module class.
- * a. serviceGetItems($iSellerId) - Is used in Orders Administration to get all products of the requested seller(vendor).
- * b. serviceGetCartItem($iClientId, $iItemId) - Is used in Shopping Cart to get one product by specified id.
- * c. serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Register purchased product.
- * d. serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrderId) - Unregister the product purchased earlier.
- * @see You may see an example of integration in Membership module.
- *
- *
- * Profile's Wall:
- * no spy events
- *
- *
- *
- * Spy:
- * no spy events
- *
- *
- *
- * Memberships/ACL:
- * Doesn't depend on user's membership.
- *
- *
- *
- * Service methods:
- *
- * Is used to get "Add to cart" link for some item(s) in your module.
- * @see BxPmtModule::serviceGetAddToCartLink
- * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
- *
- * Check transaction(s) in database which satisty all conditions.
- * @see BxPmtModule::serviceGetOrdersInfo
- * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
- *
- * Get total count of items in Shopping Cart.
- * @see BxPmtModule::serviceGetCartItemsCount
- * BxDolService::call('payment', 'get_cart_items_count', array($iUserId, $iOldCount));
- * @note is needed for internal usage(integration with member tool bar).
- *
- * Get Shopping cart content.
- * @see BxPmtModule::serviceGetCartItems
- * BxDolService::call('payment', 'get_cart_items');
- * @note is needed for internal usage(integration with member tool bar).
- *
- *
- * Alerts:
- * no alerts available
- *
- */
- class BxPaymentModule extends BxBaseModPaymentModule
- {
- protected $_iUserId;
- protected $_aOrderTypes;
- function __construct($aModule)
- {
- parent::__construct($aModule);
- $this->_iUserId = $this->getProfileId();
- $this->_aOrderTypes = array(
- BX_PAYMENT_ORDERS_TYPE_PENDING,
- BX_PAYMENT_ORDERS_TYPE_PROCESSED,
- BX_PAYMENT_ORDERS_TYPE_SUBSCRIPTION,
- BX_PAYMENT_ORDERS_TYPE_HISTORY
- );
- }
- /**
- * Manage Orders Methods
- */
- public function actionGetClients()
- {
- $sTerm = bx_get('term');
- $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
- echoJson($aResult);
- }
- public function serviceGetClients($sParams)
- {
- $sTerm = '';
- if(!($aParams = bx_api_get_browse_params($sParams)) || !($sTerm = $aParams['term'] ?? false))
- return [];
- return BxDolService::call('system', 'profiles_search', [$sTerm], 'TemplServiceProfiles');
- }
- public function actionGetItems($sType, $iModuleId)
- {
- echoJson([
- 'code' => 0,
- 'eval' => $this->_oConfig->getJsObject('processed') . '.onSelectModule(oData);',
- 'data' => $this->serviceGetItems($sType, $iModuleId, bx_get('filter'))
- ]);
- }
- public function serviceGetItems($sType, $iModuleId, $sFilter = '')
- {
- $iSellerId = $this->getProfileId();
- $aItems = $this->callGetCartItems((int)$iModuleId, [$iSellerId]);
- if($this->_bIsApi)
- return $aItems;
- return $this->_oTemplate->displayItems($iSellerId, $sType, $aItems, [
- 'filter' => $sFilter
- ]);
- }
- public function actionGetFilterValuesItem($iSellerId, $iModuleId)
- {
- $sItems = '<option value="">' . _t('_bx_payment_txt_all_items') . '</option>';
- if(empty($iSellerId) || empty($iModuleId))
- return echoJson(array('code' => 1, 'content' => $sItems));
- $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
- $this->_oConfig->sortByColumn('title', $aItems);
- foreach($aItems as $aItem)
- $sItems .= '<option value="' . $aItem['id'] . '">' . $aItem['title'] . '</option>';
- echoJson(array('code' => 0, 'content' => $sItems));
- }
- public function serviceGetSafeServices()
- {
- return array_merge(parent::serviceGetSafeServices(), [
- 'GetBlockJoin' => 'BxPaymentJoin',
- 'GetBlockCarts' => 'BxPaymentCart',
- 'GetBlockCart' => 'BxPaymentCart',
- 'GetBlockCartHistory' => 'BxPaymentCart',
-
- 'GetBlockListMy' => 'BxPaymentSubscriptions',
- 'GetBlockHistory' => 'BxPaymentSubscriptions',
- 'Cancel' => 'BxPaymentSubscriptions',
- 'CancelByPendingId' => 'BxPaymentSubscriptions',
- 'GetClients' => '',
- 'GetItems' => '',
- 'GetProviderOptions' => '',
- 'InitializeCheckoutApi' => '',
- 'StripeV3CreateSessionApi' => '',
- 'FinalizeCheckout' => ''
- ]);
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-get_modules_with_payments get_modules_with_payments
- *
- * @code bx_srv('bx_payment', 'get_modules_with_payments', []); @endcode
- *
- * Get modules with payments functionality
- *
- * @return array with modules names
- *
- * @see BxPaymentModule::serviceGetModulesWithPayments
- */
- /**
- * @ref bx_payment-get_modules_with_payments "get_modules_with_payments"
- */
- public function serviceGetModulesWithPayments()
- {
- return $this->_oDb->getModulesWithPayments();
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-is_credits_only is_credits_only
- *
- * @code bx_srv('bx_payment', 'is_credits_only'); @endcode
- *
- * Check whether the 'Credits Only' mode is enabled or not.
- *
- * @return boolean value determining the result of checking.
- *
- * @see BxPaymentModule::serviceIsCreditsOnly
- */
- /**
- * @ref bx_payment-is_credits_only "is_credits_only"
- */
- public function serviceIsCreditsOnly()
- {
- return $this->_oConfig->isCreditsOnly();
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-is_accepting_payments is_accepting_payments
- *
- * @code bx_srv('bx_payment', 'is_accepting_payments', [...]); @endcode
- *
- * Check whether the specified vendor has a configured payment provider or not.
- *
- * @param $iVendorId integer value with vendor ID.
- * @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.
- * @return boolean value determining the result of checking.
- *
- * @see BxPaymentModule::serviceIsAcceptingPayments
- */
- /**
- * @ref bx_payment-is_accepting_payments "is_accepting_payments"
- */
- public function serviceIsAcceptingPayments($iVendorId, $sPaymentType = '')
- {
- $bResult = false;
- switch($sPaymentType) {
- case BX_PAYMENT_TYPE_SINGLE:
- $aProvidersCart = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
- $bResult = !empty($aProvidersCart);
- break;
- case BX_PAYMENT_TYPE_RECURRING:
- $aProvidersSubscription = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
- $bResult = !empty($aProvidersSubscription);
- break;
- default:
- $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
- $bResult = !empty($aProviders);
- }
- return $bResult;
- }
- /**
- * @deprecated since version 11.0.0
- *
- * @see BxPaymentModule::serviceIsProviderOptions
- */
- public function serviceIsPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
- {
- return $this->serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
- }
- /**
- * @deprecated since version 11.0.0
- *
- * @see BxPaymentModule::serviceGetProviderOptions
- */
- public function serviceGetPaymentProvider($iVendorId, $sVendorProvider, $sPaymentType = '')
- {
- return $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-is_provider_options is_provider_options
- *
- * @code bx_srv('bx_payment', 'is_provider_options', [...]); @endcode
- *
- * Check whether the specified vendor has configured the specified payment provider or not.
- *
- * @param $iVendorId integer value with vendor ID.
- * @param $sVendorProvider string value with payment provider name.
- * @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.
- * @return boolean value determining the result of checking.
- *
- * @see BxPaymentModule::serviceIsProviderOptions
- */
- /**
- * @ref bx_payment-is_provider_options "is_provider_options"
- */
- public function serviceIsProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
- {
- $aProvider = $this->serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType);
- return $aProvider !== false;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-get_provider_options get_provider_options
- *
- * @code bx_srv('bx_payment', 'get_provider_options', [...]); @endcode
- *
- * Get configuration settings for the payment provider entered by the specified vendor.
- *
- * @param $iVendorId integer value with vendor ID.
- * @param $sVendorProvider string value with payment provider name.
- * @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.
- * @return an array with special format describing the payment provider or false value if something is wrong.
- *
- * @see BxPaymentModule::serviceGetProviderOptions
- */
- /**
- * @ref bx_payment-get_provider_options "get_provider_options"
- */
- public function serviceGetProviderOptions($iVendorId, $sVendorProvider, $sPaymentType = '')
- {
- $aProviders = array();
- switch($sPaymentType) {
- case BX_PAYMENT_TYPE_SINGLE:
- $aProviders = $this->_oDb->getVendorInfoProvidersSingle($iVendorId);
- break;
- case BX_PAYMENT_TYPE_RECURRING:
- $aProviders = $this->_oDb->getVendorInfoProvidersRecurring($iVendorId);
- break;
- default:
- $aProviders = $this->_oDb->getVendorInfoProviders($iVendorId);
- }
- return !empty($aProviders) && !empty($aProviders[$sVendorProvider]) && is_array(($aProviders[$sVendorProvider])) ? $aProviders[$sVendorProvider] : false;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-other Other
- * @subsubsection bx_payment-get_options_default_currency_code get_options_default_currency_code
- *
- * @code bx_srv('bx_payment', 'get_options_default_currency_code', [...]); @endcode
- *
- * Get an array with available currencies. Is used in forms.
- *
- * @return an array with available currencies represented as key => value pairs.
- *
- * @see BxPaymentModule::serviceGetOptionsDefaultCurrencyCode
- */
- /**
- * @ref bx_payment-get_options_default_currency_code "get_options_default_currency_code"
- */
- public function serviceGetOptionsDefaultCurrencyCode()
- {
- $CNF = &$this->_oConfig->CNF;
- $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES']);
- $aResult = array();
- foreach($aCurrencies as $sKey => $sValue)
- $aResult[] = array(
- 'key' => $sKey,
- 'value' => $sValue
- );
- return $aResult;
- }
- public function serviceGetOptionsPersonalCurrencyCode()
- {
- return array_merge([[
- 'key' => '',
- 'value' => _t('_bx_payment_gc_currency_code_default')
- ]], $this->serviceGetOptionsDefaultCurrencyCode());
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-other Other
- * @subsubsection bx_payment-get_options_site_admin get_options_site_admin
- *
- * @code bx_srv('bx_payment', 'get_options_site_admin', [...]); @endcode
- *
- * 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.
- *
- * @return an array with profiles represented as key => value pairs.
- *
- * @see BxPaymentModule::serviceGetOptionsSiteAdmin
- */
- /**
- * @ref bx_payment-get_options_site_admin "get_options_site_admin"
- */
- public function serviceGetOptionsSiteAdmin()
- {
- $aResult = array(
- array('key' => '', 'value' => _t('_Select_one'))
- );
- $aIds = $this->_oDb->getAdminsIds();
- foreach($aIds as $iId) {
- $aUser = $this->getProfileInfo($iId);
- $aResult[] = array(
- 'key' => $iId,
- 'value' => $aUser['name']
- );
- }
- return $aResult;
- }
- public function serviceGetMenuAddonManageTools()
- {
- return array(
- 'counter1_value' => (int)$this->_oDb->getInvoices(array('type' => 'status', 'status' => BX_PAYMENT_INV_STATUS_PAID, 'count' => true)),
- 'counter1_caption' => _t('_bx_payment_menu_item_title_admt_addon_counter1_caption'),
- 'counter2_value' => (int)$this->_oDb->getInvoices(array('type' => 'status', 'status' => BX_PAYMENT_INV_STATUS_OVERDUE, 'count' => true)),
- 'counter2_caption' => _t('_bx_payment_menu_item_title_admt_addon_counter2_caption'),
- 'counter3_value' => (int)$this->_oDb->getInvoices(array('type' => 'all_count'))
- );
- }
- public function serviceGetBlockCheckoutOffline()
- {
- $CNF = &$this->_oConfig->CNF;
- $oBuyer = BxDolProfile::getInstance();
- if(!$oBuyer)
- return MsgBox(_t('_bx_payment_err_required_login'));
-
- $iSeller = (int)bx_get('seller');
- $oSeller = BxDolProfile::getInstance($iSeller);
- if(!$oSeller)
- return MsgBox(_t('_bx_payment_err_unknown_vendor'));
- $aData = array(
- 'seller' => $iSeller,
- 'currency' => array(
- 'code' => bx_process_input(bx_get('currency_code')),
- 'sign' => bx_process_input(bx_get('currency_sign')),
- ),
- 'amount' => (float)bx_get('amount'),
- 'return_url' => bx_process_input(bx_get('return_url')),
- );
- if(empty($aData['currency']['code']))
- $aData['currency']['code'] = $this->_oConfig->getDefaultCurrencyCode();
- if(empty($aData['currency']['sign']))
- $aData['currency']['sign'] = $this->_oConfig->getDefaultCurrencySign();
- $sItems = '';
- $iItemsCount = (int)bx_get('items_count');
- for($i = 0; $i < $iItemsCount; $i++) {
- $aItem = array(
- 'title' => bx_process_input(bx_get('item_title_' . $i)),
- 'quantity' => (int)bx_get('item_quantity_' . $i)
- );
- $sItems .= _t('_bx_payment_txt_checkout_item', $aItem['title'], $aItem['quantity']);
- $aData['items'][] = $aItem;
- }
- $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'checkout_offline', array(
- 'profile_name' => $oBuyer->getDisplayName(),
- 'profile_link' => $oBuyer->getUrl(),
- 'items' => $sItems,
- 'amount' => $aData['currency']['sign'] . sprintf("%.2f", (float)($aData['amount'])),
- 'date' => bx_time_js(time(), BX_FORMAT_DATE, true),
- 'pending_orders_link' => $this->getObjectOrders()->serviceGetPendingOrdersUrl()
- ), 0, $iSeller);
- $sEmail = '';
- $oProvider = $this->getObjectProvider($CNF['OBJECT_PP_OFFLINE'], $iSeller);
- if($oProvider !== false && $oProvider->isActive())
- $sEmail = $oProvider->getOption('checkout_email');
- if(empty($sEmail))
- $sEmail = $oBuyer->getAccountObject()->getEmail();
- if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
- return MsgBox(_t('_bx_payment_err_cannot_perform'));
- return $this->_oTemplate->displayBlockCheckoutOffline($oBuyer, $oSeller, $aData);
- }
- /**
- * Cart Processing Methods
- */
- public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $sCustom = '')
- {
- if(empty($sCustom) && ($sCustom = bx_get('custom')) !== false)
- $sCustom = bx_process_input($sCustom);
- $aCustom = [];
- if(!empty($sCustom)) {
- $aCustom = $this->_oConfig->s2a($sCustom);
- if(empty($aCustom) || !is_array($aCustom))
- $aCustom = [];
- }
- $aResult = $this->getObjectCart()->serviceAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom);
- echoJson($aResult);
- }
- /**
- * Isn't used yet.
- */
- public function actionDeleteFromCart($iSellerId, $iModuleId, $iItemId)
- {
- echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId, $iModuleId, $iItemId));
- }
- /**
- * Isn't used yet.
- */
- public function actionEmptyCart($iSellerId)
- {
- echoJson($this->getObjectCart()->serviceDeleteFromCart($iSellerId));
- }
- public function actionSubscribe()
- {
- $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
- $sSellerProvider = bx_process_input(bx_get('seller_provider'));
- $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
- $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
- $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
- if(empty($iItemCount))
- $iItemCount = 1;
- $sRedirect = bx_process_input(bx_get('redirect'));
- $sCustom = bx_process_input(bx_get('custom'));
- $aCustom = array();
- if(!empty($sCustom)) {
- $aCustom = json_decode(base64_decode($sCustom), true);
- if(empty($aCustom) || !is_array($aCustom))
- $aCustom = array();
- }
- $aResult = $this->getObjectSubscriptions()->serviceSubscribe($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
- $bRedirect = !empty($aResult['redirect']);
- if(!empty($aResult['popup'])) {
- $sContent = '';
- $sContent .= $this->_oTemplate->displayCartJs(BX_PAYMENT_TYPE_RECURRING, $iSellerId);
- $sContent .= !empty($aResult['popup']['html']) ? $aResult['popup']['html'] : $aResult['popup'];
- return $this->_oTemplate->displayPageCodeResponse($sContent, false, true);
- }
- if(!empty($aResult['code']))
- return $this->_oTemplate->displayPageCodeError($aResult['message']);
- if(!empty($aResult['message']) && !$bRedirect)
- return $this->_oTemplate->displayPageCodeResponse($aResult['message']);
- if($bRedirect) {
- header('Location: ' . $aResult['redirect']);
- exit;
- }
- }
- public function actionSubscribeJson()
- {
- $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
- $sSellerProvider = bx_process_input(bx_get('seller_provider'));
- $iModuleId = bx_process_input(bx_get('module_id'), BX_DATA_INT);
- $iItemId = bx_process_input(bx_get('item_id'), BX_DATA_INT);
- $iItemCount = bx_process_input(bx_get('item_count'), BX_DATA_INT);
- if(empty($iItemCount))
- $iItemCount = 1;
- $sItemAddons = '';
- if(bx_get('item_addons') !== false) {
- $sItemAddons = bx_process_input(bx_get('item_addons'));
- if(is_array($sItemAddons))
- $sItemAddons = $this->_oConfig->a2s($sItemAddons);
- }
- $sRedirect = bx_process_input(bx_get('redirect'));
- $sCustom = bx_process_input(bx_get('custom'));
- $aCustom = array();
- if(!empty($sCustom)) {
- $aCustom = $this->_oConfig->s2a($sCustom);
- if(empty($aCustom) || !is_array($aCustom))
- $aCustom = [];
- }
- echoJson($this->getObjectSubscriptions()->serviceSubscribeWithAddons($iSellerId, $sSellerProvider, $iModuleId, $iItemId, $iItemCount, $sItemAddons, $sRedirect, $aCustom));
- }
- public function actionSubscriptionGetDetails($iId)
- {
- echoJson($this->_oTemplate->displaySubscriptionGetDetails($iId));
- }
- public function actionSubscriptionChangeDetails($iId)
- {
- echoJson($this->_oTemplate->displaySubscriptionChangeDetails($iId));
- }
- public function actionSubscriptionGetBilling($iId)
- {
- echoJson($this->_oTemplate->displaySubscriptionGetBilling($iId));
- }
- public function actionSubscriptionChangeBilling($iId)
- {
- echoJson($this->_oTemplate->displaySubscriptionChangeBilling($iId));
- }
- public function actionSubscriptionCancelation($iId)
- {
- $aResult = array('code' => 1, 'message' => _t('_bx_payment_err_cannot_perform'));
- $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => $iId));
- if(empty($aPending) || !is_array($aPending))
- return echoJson($aResult);
- $aSubscription = $this->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iId));
- if(empty($aSubscription) || !is_array($aSubscription))
- return echoJson($aResult);
- $oRecipient = BxDolProfile::getInstance((int)$aPending['seller_id']);
- if(!$oRecipient)
- return echoJson($aResult);
- $mixedResult = $this->isAllowedManage($aPending);
- if($mixedResult !== true)
- return echoJson(array('code' => 2, 'message' => $mixedResult));
- $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($this->_oConfig->getPrefix('general') . 'cancelation_request', array(
- 'subscription_id' => $aSubscription['subscription_id'],
- 'subscription_customer' => $aSubscription['customer_id'],
- 'subscription_date' => bx_time_js($aSubscription['date_add'], BX_FORMAT_DATE, true)
- ), 0, (int)$aPending['client_id']);
- $sEmail = '';
- $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
- if($oProvider !== false && $oProvider->isActive())
- $sEmail = $oProvider->getOption('cancellation_email');
- if(empty($sEmail))
- $sEmail = $oRecipient->getAccountObject()->getEmail();
- if(!sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM))
- return echoJson($aResult);
- echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_cancelation_request_sent')));
- }
- public function actionSubscriptionCancel($iId, $mixedGridObject = false)
- {
- $CNF = &$this->_oConfig->CNF;
- $aResult = ['code' => 1, 'message' => _t('_bx_payment_err_cannot_perform')];
- $aPending = $this->_oDb->getOrderPending(['type' => 'id', 'id' => $iId]);
- if(empty($aPending) || !is_array($aPending))
- return echoJson($aResult);
- $mixedResult = $this->isAllowedManage($aPending);
- if($mixedResult !== true)
- return echoJson(['code' => 2, 'message' => $mixedResult]);
- $oSubscriptions = $this->getObjectSubscriptions();
-
- $sReason = '';
- if(getParam($CNF['PARAM_SBS_CANCEL_SURVEY']) == 'on') {
- $aCsResult = $oSubscriptions->getPopupCancelSurvey($iId);
- if($aCsResult['code'] != 0)
- return echoJson($aCsResult);
- $sReason = $aCsResult['msg'];
- }
- if(!$oSubscriptions->cancel($iId, $sReason))
- return echoJson($aResult);
- if(empty($mixedGridObject) && bx_get('grid') !== false)
- $mixedGridObject = bx_process_input(bx_get('grid'));
- if(!empty($mixedGridObject) && $aPending['client_id'] == $this->getProfileId()) {
- $oGrid = BxDolGrid::getObjectInstance($mixedGridObject, $this->_oTemplate);
- $oGrid->addQueryParam('client_id', $aPending['client_id']);
- return echoJson(['object' => $mixedGridObject, 'grid' => $oGrid->getCode(false), 'blink' => array($iId)]);
- }
- else
- return echoJson(['code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')]);
- }
- /**
- * Payment Processing Methods
- */
- public function actionAuthorizeCheckout($sType)
- {
- if(!$this->isLogged())
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
- if(bx_get('seller_id') === false || bx_get('provider') === false || bx_get('items') === false)
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_wrong_data');
- $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
- $sProvider = bx_process_input(bx_get('provider'));
- $aItems = bx_process_input(bx_get('items'));
- $mixedResult = $this->serviceAuthorizeCheckout($sType, $iSellerId, $sProvider, $aItems);
- if(is_string($mixedResult))
- return $this->_oTemplate->displayPageCodeError($mixedResult);
- if(is_array($mixedResult) && !empty($mixedResult['redirect'])) {
- header('Location: ' . $mixedResult['redirect']);
- exit;
- }
- return $this->_oTemplate->displayPageCodeResponse($this->_sLangsPrefix . 'msg_successfully_performed');
- }
- public function actionCaptureAuthorizedCheckout($sType)
- {
- $iProfileId = bx_get_logged_profile_id();
- if(!$iProfileId)
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
- $sOrderAuth = '';
- if(($sOrderAuth = bx_get('order')) !== false)
- $sOrderAuth = bx_process_input($sOrderAuth);
- if(empty($sOrderAuth))
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_empty_order');
- $aPending = $this->_oDb->getOrderPending(['type' => 'order', 'order' => $sOrderAuth]);
- if(empty($aPending) || !is_array($aPending))
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_empty_order');
- if((int)$aPending['seller_id'] != $iProfileId)
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_wrong_vendor');
- $mixedResult = $this->serviceCaptureAuthorizedCheckout($sType, $sOrderAuth);
- if(is_string($mixedResult))
- return $this->_oTemplate->displayPageCodeError($mixedResult);
- if(!empty($mixedResult['redirect'])) {
- header('Location: ' . base64_decode(urldecode($mixedResult['redirect'])));
- exit;
- }
- return $this->_oTemplate->displayPageCodeResponse($mixedResult['message']);
- }
- public function actionInitializeCheckout($sType)
- {
- if(!$this->isLogged())
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_required_login');
- if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
- $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
- $sProvider = bx_process_input(bx_get('provider'));
- $aItems = bx_process_input(bx_get('items'));
- $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
- if($mixedResult !== true)
- return $this->_oTemplate->displayPageCodeError($mixedResult);
- }
- header('Location: ' . $this->_oConfig->getUrl('URL_CART'));
- exit;
- }
- public function actionInitializeCheckoutJson($sType)
- {
- if(!$this->isLogged())
- return echoJson(['code' => 1, 'message' => _t($this->_sLangsPrefix . 'err_required_login')]);
- if(bx_get('seller_id') !== false && bx_get('provider') !== false && bx_get('items') !== false) {
- $iSellerId = bx_process_input(bx_get('seller_id'), BX_DATA_INT);
- $sProvider = bx_process_input(bx_get('provider'));
- $aItems = bx_process_input(bx_get('items'));
- $mixedResult = $this->serviceInitializeCheckout(BX_PAYMENT_TYPE_SINGLE, $iSellerId, $sProvider, $aItems);
- if($mixedResult === false)
- return echoJson(['code' => 2, 'message' => _t($this->_sLangsPrefix . 'err_cannot_perform')]);
- if(is_string($mixedResult))
- return echoJson(['code' => 3, 'message' => $mixedResult]);
- else if(is_array($mixedResult))
- return echoJson($mixedResult);
- }
- return echoJson(['code' => 0, 'redirect' => $this->_oConfig->getUrl('URL_CART')]);
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-authorize_checkout authorize_checkout
- *
- * @code bx_srv('bx_payment', 'authorize_checkout', [...]); @endcode
- *
- * Authorize order for future checkout.
- *
- * @param $sType string value with payment type (single or recurring).
- * @param $iSellerId integer value with seller ID.
- * @param $sProvider string value with payment provider name.
- * @param $aItems (optional) an array with items to be purchased.
- * @param $sRedirect (optional) string value with redirect URL if it's needed.
- * @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.
- *
- * @see BxPaymentModule::serviceAuthorizeCheckout
- */
- /**
- * @ref bx_payment-authorize_checkout "authorize_checkout"
- */
- public function serviceAuthorizeCheckout($sType, $iSellerId, $sProvider, $aItems = [], $sRedirect = '')
- {
- $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
- $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
- if(!is_array($aItems))
- $aItems = [$aItems];
- $iSellerId = (int)$iSellerId;
- if($iSellerId == BX_PAYMENT_EMPTY_ID)
- return $this->_sLangsPrefix . 'err_unknown_vendor';
- $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_sLangsPrefix . 'err_incorrect_provider';
- $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
- if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
- return $this->_sLangsPrefix . 'err_empty_order';
- $this->alert('before_authorize', 0, $this->_iUserId, array(
- 'client_id' => $this->_iUserId,
- 'seller_id' => $iSellerId,
- 'type' => &$sType,
- 'provider' => &$sProvider,
- 'cart_info' => &$aInfo,
- 'type_ref' => &$sType,
- 'provider_ref' => &$sProvider,
- 'cart_info_ref' => &$aInfo,
- ));
- $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo);
- if(empty($iPendingId))
- return $this->_sLangsPrefix . 'err_access_db';
- return $oProvider->authorizeCheckout($iPendingId, $aInfo, $sRedirect);
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-capture_authorized_checkout capture_authorized_checkout
- *
- * @code bx_srv('bx_payment', 'capture_authorized_checkout', [...]); @endcode
- *
- * Capture funds for previously authorized order.
- *
- * @param $sType string value with payment type (single or recurring).
- * @param $sOrderAuth string value with order's authorization ID.
- * @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.
- *
- * @see BxPaymentModule::serviceCaptureAuthorizedCheckout
- */
- /**
- * @ref bx_payment-capture_authorized_checkout "capture_authorized_checkout"
- */
- public function serviceCaptureAuthorizedCheckout($sType, $sOrderAuth, $sRedirect = '')
- {
- $aPending = $this->_oDb->getOrderPending(['type' => 'order', 'order' => $sOrderAuth]);
- if(empty($aPending) || !is_array($aPending))
- return $this->_sLangsPrefix . 'err_empty_order';
- if((int)$aPending['processed'] != 0)
- return $this->_sLangsPrefix . 'err_already_processed';
- $iClientId = (int)$aPending['client_id'];
- $iSellerId = (int)$aPending['seller_id'];
- $sProvider = $aPending['provider'];
- $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_sLangsPrefix . 'err_incorrect_provider';
- $aInfo = $this->getObjectCart()->getInfo($sType, $iClientId, $iSellerId, $aPending['items']);
- if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
- return $this->_sLangsPrefix . 'err_empty_order';
- $this->alert('before_capture_authorized', 0, $iClientId, array(
- 'order_auth' => $sOrderAuth,
- 'client_id' => $iClientId,
- 'seller_id' => $iSellerId,
- 'type' => &$sType,
- 'provider' => &$sProvider,
- 'cart_info' => &$aInfo,
- 'type_ref' => &$sType,
- 'provider_ref' => &$sProvider,
- 'cart_info_ref' => &$aInfo,
- ));
- $aResult = $oProvider->captureAuthorizedCheckout($sOrderAuth, $aPending, $aInfo, $sRedirect);
- if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
- return $aResult['message'];
- //--- Get updated pending order
- $aPending = $this->_oDb->getOrderPending(['type' => 'id', 'id' => $aResult['pending_id']]);
- //--- Register payment for purchased items in associated modules
- if(isset($aResult['paid']) && $aResult['paid'])
- $this->registerPayment($aPending);
- $this->alert('finalize_checkout', 0, bx_get_logged_profile_id(), array(
- 'pending' => $aPending,
- 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
- 'provider' => $oProvider,
- 'message' => &$aResult['message'],
- 'result' => &$aResult,
- 'message_ref' => &$aResult['message'],
- 'result_ref' => &$aResult,
- ));
- if(empty($aResult['redirect']) && $oProvider->needRedirect())
- $aResult['redirect'] = $oProvider->getReturnUrl();
- return $aResult;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-purchase_processing Purchase Processing
- * @subsubsection bx_payment-initialize_checkout initialize_checkout
- *
- * @code bx_srv('bx_payment', 'initialize_checkout', [...]); @endcode
- *
- * Initialize the checkout process.
- *
- * @param $sType string value with payment type (single or recurring).
- * @param $iSellerId integer value with seller ID.
- * @param $sProvider string value with payment provider name.
- * @param $aItems (optional) an array with items to be purchased.
- * @param $sRedirect (optional) string value with redirect URL if it's needed.
- * @param $aCustom (optional) array with custom data to attach to a payment.
- * @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.
- *
- * @see BxPaymentModule::serviceInitializeCheckout
- */
- /**
- * @ref bx_payment-initialize_checkout "initialize_checkout"
- */
- public function serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems = array(), $sRedirect = '', $aCustoms = array())
- {
- $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
- $bTypeRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
- if(!is_array($aItems))
- $aItems = array($aItems);
- $iSellerId = (int)$iSellerId;
- if($iSellerId == BX_PAYMENT_EMPTY_ID)
- return $this->_sLangsPrefix . 'err_unknown_vendor';
- $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_sLangsPrefix . 'err_incorrect_provider';
- $aInfo = $this->getObjectCart()->getInfo($sType, $this->_iUserId, $iSellerId, $aItems);
- if(empty($aInfo) || $aInfo['vendor_id'] == BX_PAYMENT_EMPTY_ID || empty($aInfo['items']))
- return $this->_sLangsPrefix . 'err_empty_order';
- /*
- * Process FREE (price = 0) items for LOGGED IN members
- * WITHOUT processing via payment provider.
- * Note. This section isn't used for now!
- */
- $bProcessedFree = false;
- $sKeyPriceSingle = $this->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
- $sKeyPriceRecurring = $this->_oConfig->getKey('KEY_ARRAY_PRICE_RECURRING');
- foreach($aInfo['items'] as $iIndex => $aItem) {
- if((int)$aInfo['client_id'] == 0)
- continue;
- //--- For Single time payments.
- if($bTypeSingle && (float)$aItem[$sKeyPriceSingle] == 0) {
- $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
- $aCartCustoms = $aCart['customs'];
- $aCartItem = array($aInfo['vendor_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity']);
- $aCartItemCustom = $this->_oConfig->pullCustom($aCartItem, $aCartCustoms);
- $aItemInfo = $this->callRegisterCartItem((int)$aItem['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aItem['id'], $aItem['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
- if(!empty($aItemInfo) && is_array($aItemInfo)) {
- if(!empty($aItemInfo['addons']) && is_array($aItemInfo['addons']))
- foreach($aItemInfo['addons'] as $sAddon => $aAddon)
- $this->callRegisterCartItem((int)$aAddon['module_id'], array($aInfo['client_id'], $aInfo['vendor_id'], $aAddon['id'], $aAddon['quantity'], $this->_oConfig->getLicense(), $aCartItemCustom));
- $bProcessedFree = true;
- }
- $aInfo['items_count'] -= 1;
- unset($aInfo['items'][$iIndex]);
- $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aCartItem) . ":?'", "", $aCart['items']), ":");
- $this->_oDb->setCartItems($aInfo['client_id'], $aCart['items'], $aCartCustoms);
- }
- //--- For Recurring payments.
- if($bTypeRecurring && (float)$aItem[$sKeyPriceRecurring] == 0) {
- //TODO: Process FREE subscription here, if such situation will be possible.
- }
- }
- if(empty($aInfo['items']))
- return $this->_sLangsPrefix . ($bProcessedFree ? 'msg_successfully_processed_free' : 'err_empty_order');
- if($bTypeSingle && (empty($aCustoms) || !is_array($aCustoms))) {
- $aCart = $this->_oDb->getCartContent($aInfo['client_id']);
- $aCartCustoms = $aCart['customs'];
- $aCustoms = array();
- foreach($aInfo['items'] as $aItem) {
- $sCartItem = $this->_oConfig->descriptorA2S(array($aItem['author_id'], $aItem['module_id'], $aItem['id']));
- $aCartItemCustom = $this->_oConfig->getCustom($sCartItem, $aCartCustoms);
- $this->_oConfig->putCustom($sCartItem, $aCartItemCustom, $aCustoms);
- }
- }
- $this->alert('before_pending', 0, bx_get_logged_profile_id(), array(
- 'client_id' => $this->_iUserId,
- 'seller_id' => $iSellerId,
- 'type' => &$sType,
- 'provider' => &$sProvider,
- 'cart_info' => &$aInfo,
- 'custom' => &$aCustoms,
- 'type_ref' => &$sType,
- 'provider_ref' => &$sProvider,
- 'cart_info_ref' => &$aInfo,
- 'custom_ref' => &$aCustoms,
- ));
- $iPendingId = $this->_oDb->insertOrderPending($this->_iUserId, $sType, $sProvider, $aInfo, $aCustoms);
- if(empty($iPendingId))
- return $this->_sLangsPrefix . 'err_access_db';
- /*
- * Perform Join WITHOUT processing via payment provider
- * if a client ISN'T logged in and has only ONE FREE item in the card.
- * Note. This section isn't used for now because only a member can purchase!
- */
- if((int)$aInfo['client_id'] == 0 && (int)$aInfo['items_count'] == 1) {
- reset($aInfo['items']);
- $aItem = current($aInfo['items']);
- if(!empty($aItem) && $this->_oConfig->getPrice($sType, $aItem)) {
- $this->_oDb->updateOrderPending($iPendingId, array(
- 'order' => $this->_oConfig->getLicense(),
- 'error_code' => '1',
- 'error_msg' => ''
- ));
- $this->getObjectJoin()->performJoin($iPendingId);
- }
- }
- return $oProvider->initializeCheckout($iPendingId, $aInfo, $sRedirect);
- }
- public function serviceInitializeCheckoutApi($sType, $iSellerId, $sProvider, $aItems = [], $sRedirect = '', $aCustoms = [])
- {
- if(is_string($aItems))
- $aItems = $this->_oConfig->descriptorsM2A($aItems, true);
- $mixedResult = $this->serviceInitializeCheckout($sType, $iSellerId, $sProvider, $aItems, $sRedirect, $aCustoms);
- if(is_string($mixedResult))
- return [bx_api_get_msg($mixedResult)];
-
- if(is_array($mixedResult)) {
- if(($sRedirect = $mixedResult['redirect'] ?? false))
- return ['url' => $sRedirect];
- if(($sMsg = $mixedResult['message'] ?? false))
- return [bx_api_get_msg($sMsg)];
- }
- return [];
- }
- public function actionFinalizeCheckout($sProvider, $mixedVendorId = "")
- {
- $aData = &$_REQUEST;
- $aResult = $this->serviceFinalizeCheckout($sProvider, $mixedVendorId, $aData);
- if(isset($aResult['error']))
- return $this->_oTemplate->displayPageCodeError($aResult['error']);
- else if(isset($aResult['redirect'])) {
- header('Location: ' . $aResult['redirect']);
- exit;
- }
- else
- $this->_oTemplate->displayPageCodeResponse($aResult['message']);
- }
-
- public function serviceFinalizeCheckout($sProvider, $mixedVendorId = "", $aData = [])
- {
- if($this->_bIsApi && is_string($aData))
- $aData = bx_api_get_browse_params($aData);
- $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
- if($oProvider === false || !$oProvider->isActive())
- return ($sMsg = $this->_sLangsPrefix . 'err_incorrect_provider') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['error' => $sMsg];
- $aResult = $oProvider->finalizeCheckout($aData);
- if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
- return $this->_bIsApi ? [bx_api_get_msg($aResult['message'])] : ['error' => $aResult['message']];
- $aPending = $this->_oDb->getOrderPending(['type' => 'id', 'id' => (int)$aResult['pending_id']]);
- $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
- $bAuthorized = isset($aResult['authorized']) && $aResult['authorized'];
- $bPaid = isset($aResult['paid']) && $aResult['paid'];
- $bTrial = isset($aResult['trial']) && $aResult['trial'];
- //--- Register subscription
- if($bTypeRecurring) {
- $sStatus = BX_PAYMENT_SBS_STATUS_UNPAID;
- if($bPaid)
- $sStatus = BX_PAYMENT_SBS_STATUS_ACTIVE;
- else if($bTrial)
- $sStatus = BX_PAYMENT_SBS_STATUS_TRIAL;
- $aSubscription = array(
- 'customer_id' => $aResult['customer_id'],
- 'subscription_id' => $aResult['subscription_id'],
- 'status' => $sStatus
- );
- if(!$this->getObjectSubscriptions()->register($aPending, $aSubscription))
- return ($sMsg = $this->_sLangsPrefix . 'err_already_registered') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['error' => $sMsg];
- }
- $this->onPaymentRegisterBefore($aPending);
- //--- Check "Pay Before Join" situation
- if((int)$aPending['client_id'] == 0)
- $this->getObjectJoin()->performJoin((int)$aPending['id'], isset($aResult['client_name']) ? $aResult['client_name'] : '', isset($aResult['client_email']) ? $aResult['client_email'] : '');
- //--- Authorize order for selected items in associated modules
- if($bAuthorized)
- $this->authorizePayment($aPending);
- //--- Register payment for purchased items in associated modules
- if($bPaid || ($bTypeRecurring && $bTrial))
- $this->registerPayment($aPending);
- //--- Get updated pending order
- $aPending = $this->_oDb->getOrderPending(['type' => 'id', 'id' => $aPending['id']]);
- $this->alert('finalize_checkout', 0, bx_get_logged_profile_id(), array(
- 'pending' => $aPending,
- 'transactions' => $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id'])),
- 'provider' => $oProvider,
- 'message' => &$aResult['message'],
- 'result' => &$aResult,
- 'message_ref' => &$aResult['message'],
- 'result_ref' => &$aResult,
- ));
- if($oProvider->needRedirect())
- return ['redirect' => $oProvider->getReturnUrl()];
- if(!empty($aResult['redirect']))
- return ['redirect' => $this->_oConfig->urlDecode($aResult['redirect'])];
- return $this->_bIsApi ? [bx_api_get_msg($aResult['message'])] : ['message' => $aResult['message']];
- }
- public function actionFinalizedCheckout($sProvider, $mixedVendorId = "")
- {
- $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
- $aResult = $oProvider->finalizedCheckout();
- $this->_oTemplate->displayPageCodeResponse($aResult['message']);
- }
- public function actionNotify($sProvider, $mixedVendorId = "")
- {
- $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
- $oProvider->notify();
- }
-
- /**
- * The method is needed to pass direct action calls to necessary Payment Provider.
- */
- public function actionCall($sProvider, $sAction, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
- {
- $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
- if($oProvider === false)
- BxDolRequest::methodNotFound($sAction, $this->getName());
- $sMethod = 'action' . bx_gen_method_name($sAction);
- if(!method_exists($oProvider, $sMethod))
- BxDolRequest::methodNotFound($sAction, $this->getName());
- return call_user_func_array(array($oProvider, $sMethod), array_slice(func_get_args(), 2));
- }
- public function onProfileJoin($iProfileId)
- {
- $this->getObjectJoin()->onProfileJoin($iProfileId);
- }
- public function onProfileDelete($iProfileId)
- {
- //--- Cancel subscriptions
- $aSubscriptions = $this->_oDb->getSubscription(['type' => 'mixed_ext', 'conditions' => ['client_id' => $iProfileId]]);
- if(!empty($aSubscriptions) && is_array($aSubscriptions))
- foreach($aSubscriptions as $aSubscription)
- $this->getObjectSubscriptions()->cancel($aSubscription['pending_id']);
- //--- Clean DB
- $this->_oDb->onProfileDelete($iProfileId);
- }
- public function isAllowedPurchase($aItem, $bPerform = false)
- {
- $iUserId = (int)$this->getProfileId();
- $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], $iUserId, isset($aItem['custom']) ? $aItem['custom'] : array()));
- if(empty($aItemInfo))
- return false;
- if(isAdmin())
- return true;
- $aCheckResult = checkActionModule($iUserId, 'purchase', $this->getName(), $bPerform);
- if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
- return true;
- return $aCheckResult[CHECK_ACTION_MESSAGE];
- }
- public function isAllowedSell($aItem, $bPerform = false)
- {
- $iUserId = (int)$this->getProfileId();
- if(!$iUserId)
- return false;
- $aItemInfo = $this->callGetCartItem((int)$aItem['module_id'], array($aItem['item_id'], $iUserId));
- if(empty($aItemInfo))
- return false;
- if(isAdmin())
- return true;
- $aCheckResult = checkActionModule($iUserId, 'sell', $this->getName(), $bPerform);
- if((int)$aItemInfo['author_id'] == $iUserId && $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
- return true;
- return $aCheckResult[CHECK_ACTION_MESSAGE];
- }
- public function isAllowedManage($aPending, $bPerform = false)
- {
- $iUserId = (int)$this->getProfileId();
- if($iUserId == $aPending['client_id'] || $iUserId == $aPending['seller_id'] || isAdmin())
- return true;
- $aCheckResult = checkActionModule($iUserId, 'manage any purchase', $this->getName(), $bPerform);
- if($aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)
- return true;
- return $aCheckResult[CHECK_ACTION_MESSAGE];
- }
- public function isAllowedManageInvoices($bPerform = false)
- {
- if($this->_isModerator())
- return true;
- return _t('_sys_txt_access_denied');
- }
- public function checkData($iClientId, $iSellerId, $iModuleId, $iItemId, $iItemCount, $aCustom = array())
- {
- $CNF = &$this->_oConfig->CNF;
- if($iSellerId == BX_PAYMENT_EMPTY_ID || empty($iModuleId) || empty($iItemId) || empty($iItemCount))
- return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
- $iClientId = $this->getProfileId();
- if(empty($iClientId)) {
- $sLoginUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=login');
- return array('code' => 2, 'eval' => 'window.open("' . $sLoginUrl . '", "_self");');
- }
- $mixedResult = $this->isAllowedPurchase(array('module_id' => $iModuleId, 'item_id' => $iItemId, 'custom' => $aCustom));
- if($mixedResult !== true) {
- if(is_string($mixedResult) && !empty($mixedResult))
- return array('code' => 2, 'message' => $mixedResult);
- else
- return array('code' => 1, 'message' => _t($CNF['T']['ERR_WRONG_DATA']));
- }
- if($iClientId == $iSellerId)
- return array('code' => 3, 'message' => _t($CNF['T']['ERR_SELF_PURCHASE']));
- $aSeller = $this->getVendorInfo($iSellerId);
- if(!$aSeller['active'])
- return array('code' => 4, 'message' => _t($CNF['T']['ERR_INACTIVE_VENDOR']));
- return true;
- }
- public function authorizePayment($mixedPending)
- {
- $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(['type' => 'id', 'id' => (int)$mixedPending]);
- if(empty($aPending) || !is_array($aPending))
- return false;
- $sType = $aPending['type'];
- $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
- if((int)$aPending['authorized'] == 1)
- return true;
- $iClientId = (int)$aPending['client_id'];
- $aCart = array();
- if($bTypeSingle)
- $aCart = $this->_oDb->getCartContent($iClientId);
-
- $bResult = false;
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- foreach($aItems as $aItem) {
- $sItem = $this->_oConfig->descriptorA2S([$aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']]);
-
- $sMethod = $bTypeSingle ? 'callAuthorizeCartItem' : 'callAuthorizeSubscriptionItem';
- $aItemInfo = $this->$sMethod((int)$aItem['module_id'], [$aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order']]);
- if(empty($aItemInfo) || !is_array($aItemInfo))
- continue;
- if($bTypeSingle)
- $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
- $bResult = true;
- }
- if($bResult) {
- $this->_oDb->updateOrderPending($aPending['id'], ['authorized' => 1]);
- $this->onPaymentAuthorize($aPending);
- }
- return $bResult;
- }
-
- public function registerPayment($mixedPending)
- {
- $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(['type' => 'id', 'id' => (int)$mixedPending]);
- if(empty($aPending) || !is_array($aPending))
- return false;
- $sType = $aPending['type'];
- $bTypeSingle = $sType == BX_PAYMENT_TYPE_SINGLE;
- if($bTypeSingle && (int)$aPending['processed'] == 1)
- return true;
- $iClientId = (int)$aPending['client_id'];
- $iSellerId = (int)$aPending['seller_id'];
- $sLicense = $this->_oConfig->getLicense();
- $aCustoms = !empty($aPending['customs']) ? $this->_oConfig->s2a($aPending['customs'], false) : [];
- $aCart = $bTypeSingle ? $this->_oDb->getCartContent($iClientId) : [];
- $bResult = false;
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- foreach($aItems as $aItem) {
- $sItem = $this->_oConfig->descriptorA2S([$aItem['vendor_id'], $aItem['module_id'], $aItem['item_id']]);
- $aItemCustom = $this->_oConfig->getCustom($sItem, $aCustoms);
- $sMethod = $bTypeSingle ? 'callRegisterCartItem' : 'callRegisterSubscriptionItem';
- $aItemInfo = $this->$sMethod((int)$aItem['module_id'], [$iClientId, $iSellerId, $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom]);
- if(empty($aItemInfo) || !is_array($aItemInfo))
- continue;
- $this->_oDb->insertOrderProcessed([
- 'pending_id' => $aPending['id'],
- 'client_id' => $iClientId,
- 'seller_id' => $iSellerId,
- 'author_id' => (int)$aItem['vendor_id'],
- 'module_id' => (int)$aItem['module_id'],
- 'item_id' => (int)$aItem['item_id'],
- 'item_count' => (int)$aItem['item_count'],
- 'amount' => (int)$aItem['item_count'] * $this->_oConfig->getPrice($sType, $aItemInfo),
- 'currency' => !empty($aPending['currency']) ? $aPending['currency'] : $this->getVendorCurrencyCode($iSellerId),
- 'license' => $sLicense,
- ]);
- if($bTypeSingle) {
- $this->_oConfig->pullCustom($sItem, $aCart['customs']);
- $aCart['items'] = trim(preg_replace("'" . $this->_oConfig->descriptorA2S($aItem) . ":?'", "", $aCart['items']), ":");
- }
- $bResult = true;
- }
- if($bTypeSingle)
- $this->_oDb->setCartItems($iClientId, $aCart['items'], $aCart['customs']);
- if($bResult) {
- $this->_oDb->updateOrderPending($aPending['id'], ['processed' => 1]);
- $this->onPaymentRegister($aPending);
- }
- return $bResult;
- }
- public function refundPayment($mixedPending)
- {
- $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$mixedPending));
- if(empty($aPending) || !is_array($aPending))
- return false;
- $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
- $iCanceled = 0;
- $aOrders = $this->_oDb->getOrderProcessed(array('type' => 'pending_id', 'pending_id' => (int)$aPending['id']));
- foreach($aOrders as $aOrder) {
- $sMethod = $bTypeSingle ? 'callUnregisterCartItem' : 'callUnregisterSubscriptionItem';
- $bResult = $this->$sMethod((int)$aOrder['module_id'], array($aOrder['client_id'], $aOrder['seller_id'], $aOrder['item_id'], $aOrder['item_count'], $aPending['order'], $aOrder['license']));
- if(!$bResult)
- continue;
- if($this->_oDb->deleteOrderProcessed($aOrder['id']))
- $iCanceled++;
- }
- if($iCanceled != count($aOrders))
- return false;
- $bResult = $this->_oDb->deleteOrderPending($aPending['id']);
- if($bResult)
- $this->onPaymentRefund($aPending);
- return $bResult;
- }
- /**
- * @deprecated since version 12.0.0
- *
- * @use BxPaymentSubscriptions::register instead.
- */
- public function registerSubscription($aPending, $aParams = array())
- {
- return $this->getObjectSubscriptions()->register($aPending, $aParams);
- }
- /**
- * @deprecated since version 12.0.0
- *
- * @use BxPaymentSubscriptions::cancelLocal instead.
- */
- public function cancelSubscription($mixedPending)
- {
- return $this->getObjectSubscriptions()->cancelLocal($mixedPending);
- }
- public function processCommissions()
- {
- if(!defined('BX_DOL_CRON_EXECUTE'))
- return;
- if(!$this->_oConfig->isCommissions())
- return;
- if((int)date('j') == $this->_oConfig->getInvoiceIssueDay())
- $this->_invoicesIssue();
- $this->_invoicesCheck();
- }
- protected function _invoicesIssue()
- {
- $CNF = &$this->_oConfig->CNF;
- $sInvoiceNameMask = 'INV-%d-%d-%d';
- $iMonth = date('m');
- $iYear = date('Y');
- $iPeriodStart = mktime(0, 0, 0, $iMonth-1, 1, $iYear);
- $iPeriodEnd = mktime(23, 59, 59, $iMonth, 0, $iYear);
- $iDateIssue = time();
- $iDateDue = $iDateIssue + 86400 * $this->_oConfig->getInvoiceLifetime();
- $aCommissionsInfo = array();
- $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, true, false, true);
- foreach($aAclLevels as $iAclLevelId => $sAclLevelTitle)
- $aCommissionsInfo[$iAclLevelId] = $this->_oDb->getCommissions(['type' => 'acl_id', 'acl_id' => $iAclLevelId]);
- $iMainSellerId = $this->_oConfig->getSiteAdmin();
- $aVendors = $this->_oDb->getOrderProcessed(['type' => 'income', 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd]);
- foreach($aVendors as $aVendor) {
- $iVendorId = (int)$aVendor['id'];
- if($iVendorId == $iMainSellerId)
- continue;
- $aInvoice = $this->_oDb->getInvoices(['type' => 'committent_id', 'committent_id' => $iVendorId, 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd]);
- if(!empty($aInvoice) && is_array($aInvoice))
- continue;
- $aVendorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($iVendorId);
- $aCommissions = $aCommissionsInfo[$aVendorAcl['id']];
- $fCommission = 0;
- foreach($aCommissions as $aCommission) {
- if((float)$aCommission['percentage'] > 0)
- $fCommission += (float)$aVendor['amount'] * (float)$aCommission['percentage'] / 100;
- if((float)$aCommission['installment'] > 0)
- $fCommission += (float)$aCommission['installment'];
- }
- $this->alert('calculate_commission', 0, 0, [
- 'vendor' => $aVendor,
- 'commissions' => $aCommissions,
- 'override_result' => &$fCommission,
- ]);
- if($fCommission > 0)
- $this->_oDb->insertInvoice([
- 'name' => sprintf($sInvoiceNameMask, $iMainSellerId, $iVendorId, $this->_oDb->getInvoices([
- 'type' => 'index',
- 'commissionaire_id' => $iMainSellerId,
- 'committent_id' => $iVendorId
- ])),
- 'commissionaire_id' => $iMainSellerId,
- 'committent_id' => $iVendorId,
- 'amount' => $fCommission,
- 'period_start' => $iPeriodStart,
- 'period_end' => $iPeriodEnd,
- 'date_issue' => $iDateIssue,
- 'date_due' => $iDateDue,
- 'status' => BX_PAYMENT_INV_STATUS_UNPAID
- ]);
- }
- }
- protected function _invoicesCheck()
- {
- $CNF = &$this->_oConfig->CNF;
- $sPrefix = $this->_oConfig->getPrefix('general');
- $oEmailTemplates = BxDolEmailTemplates::getInstance();
- //--- Process expiring invoices.
- $aInvoices = $this->_oDb->getInvoices(array('type' => 'expiring'));
- if(!empty($aInvoices) && is_array($aInvoices))
- foreach($aInvoices as $aInvoice) {
- if((int)$aInvoice['ntf_exp'] > 0)
- continue;
- $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
- if(!$oCommittent)
- continue;
- $bResult = sendMailTemplate($sPrefix . 'expiring_notification_committent', 0, (int)$aInvoice['committent_id'], array(
- 'invoice' => $aInvoice['name'],
- 'days' => $this->_oConfig->getInvoiceExpirationNotify(),
- 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
- 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
- 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
- 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
- ), BX_EMAIL_NOTIFY, true);
- if($bResult)
- $this->_oDb->updateInvoice($aInvoice['id'], array('ntf_exp' => 1));
- }
- //--- Process overdue invoices.
- $aInvoices = $this->_oDb->getInvoices(array('type' => 'overdue'));
- if(!empty($aInvoices) && is_array($aInvoices))
- foreach($aInvoices as $aInvoice) {
- if((int)$aInvoice['ntf_due'] > 0 && $aInvoice['status'] == BX_PAYMENT_INV_STATUS_OVERDUE)
- continue;
- $oCommittent = BxDolProfile::getInstance($aInvoice['committent_id']);
- if(!$oCommittent)
- continue;
- $bResult = sendMailTemplate($sPrefix . 'overdue_notification_committent', 0, (int)$aInvoice['committent_id'], array(
- 'invoice' => $aInvoice['name'],
- 'period_start' => $this->_oConfig->formatDate($aInvoice['period_start']),
- 'period_end' => $this->_oConfig->formatDate($aInvoice['period_end']),
- 'date_issue' => $this->_oConfig->formatDateTime($aInvoice['date_issue']),
- 'date_due' => $this->_oConfig->formatDateTime($aInvoice['date_due']),
- ), BX_EMAIL_NOTIFY, true);
- $this->_oDb->updateInvoice($aInvoice['id'], array(
- 'status' => BX_PAYMENT_INV_STATUS_OVERDUE,
- 'ntf_due' => $bResult ? 1 : 0
- ));
- }
- }
- public function processTimeTracker()
- {
- if(!defined('BX_DOL_CRON_EXECUTE'))
- return;
- $aSubscriptions = $this->_oDb->getSubscription(array(
- 'type' => 'time_tracker',
- 'status_active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
- 'status_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
- 'status_unpaid' => BX_PAYMENT_SBS_STATUS_UNPAID,
- 'pay_attempts_max' => $this->_oConfig->getPayAttemptsMax(),
- 'pay_attempts_interval' => $this->_oConfig->getPayAttemptsInterval()
- ));
- if(empty($aSubscriptions) || !is_array($aSubscriptions))
- return;
- foreach($aSubscriptions as $aSubscription) {
- $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aSubscription['pending_id']));
- if(empty($aPending) || !is_array($aPending) || $aPending['type'] != BX_PAYMENT_TYPE_RECURRING) {
- $this->log($aSubscription, 'Time Tracker', 'Cannot process subscription.', BX_LOG_ERR);
- continue;
- }
- $oProvider = $this->getObjectProvider($aPending['provider'], $aPending['seller_id']);
- if($oProvider === false || !$oProvider->isActive() || !method_exists($oProvider, 'makePayment')) {
- $this->log($aSubscription, 'Time Tracker', 'Payment provider unavailable or incorrect.', BX_LOG_ERR);
- continue;
- }
- $this->_oDb->updateSubscription(['pay_attempts' => (int)$aSubscription['pay_attempts'] + 1], ['id' => $aSubscription['id']]);
- if(($mixedResult = $oProvider->makePayment($aPending)) !== true) {
- $this->_oDb->updateSubscription(['status' => BX_PAYMENT_SBS_STATUS_UNPAID], ['id' => $aSubscription['id']]);
- $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed (code: ' . (!empty($mixedResult['code']) ? (int)$mixedResult['code'] : 0) . ').', BX_LOG_ERR);
- continue;
- }
- if(!$this->getObjectSubscriptions()->prolong($aPending)) {
- $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.', BX_LOG_ERR);
- continue;
- }
- if(!$this->registerPayment($aPending)) {
- $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.', BX_LOG_ERR);
- continue;
- }
- }
- }
- public function onPaymentRegisterBefore($aPending, $aResult = [])
- {
- $this->alert('before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
- }
- public function onPaymentAuthorize($aPending, $aResult = [])
- {
- $this->alert('authorize_payment', 0, $aPending['client_id'], ['pending' => $aPending]);
- }
- public function onPaymentRegister($aPending, $aResult = [])
- {
- $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
- if($bTypeSingle) {
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- foreach($aItems as $aItem)
- $this->isAllowedPurchase(['module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']], true);
- }
- $this->alert('register_payment', 0, $aPending['client_id'], ['pending' => $aPending]);
- }
- public function onPaymentRefund($aPending, $aResult = [])
- {
- $this->alert('refund_payment', 0, $aPending['client_id'], ['pending' => $aPending]);
- }
- public function onSubscriptionCreate($aPending, $aSubscription, $aResult = [])
- {
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- $this->isAllowedPurchase(['module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']], true);
- $this->alert('create_subscription', 0, $aPending['client_id'], [
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ]);
- }
- public function onSubscriptionProlong($aPending, $aSubscription, $aResult = [])
- {
- $this->alert('prolong_subscription', 0, $aPending['client_id'], [
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ]);
- }
- public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = [])
- {
- $this->alert('overdue_subscription', 0, $aPending['client_id'], [
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ]);
- }
- public function onSubscriptionCancel($aPending, $aSubscription, $aResult = [])
- {
- $this->alert('cancel_subscription', 0, $aPending['client_id'], [
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ]);
- }
- public function setSiteSubmenu($sSubmenu, $sSelModule, $sSelName)
- {
- $oSiteSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
- if(!$oSiteSubmenu)
- return;
- $sModuleSubmenu = $this->_oConfig->getObject($sSubmenu);
- $oModuleSubmenu = BxDolMenu::getObjectInstance($sModuleSubmenu);
- if(!$oModuleSubmenu)
- return;
- $oSiteSubmenu->setObjectSubmenu($sModuleSubmenu);
- $oModuleSubmenu->setSelected($sSelModule, $sSelName);
- }
- public function getProviderButtonJs($aCartItem, $aProvider, $sRedirect = '', $aCustom = array())
- {
- $aButtonJs = array();
- $iClientId = $this->getProfileId();
- $sItemAddons = '';
- if(count($aCartItem) == 5)
- $sItemAddons = array_pop($aCartItem);
- list($iSellerId, $iModuleId, $iItemId, $iItemCount) = $aCartItem;
- $oProvider = $this->getObjectProvider($aProvider['name'], $iSellerId);
- if($oProvider !== false && method_exists($oProvider, 'getButtonRecurring')) {
- $aParams = array(
- 'sObjNameCart' => $this->_oConfig->getJsObject('cart'),
- 'iSellerId' => $iSellerId,
- 'iModuleId' => $iModuleId,
- 'iItemId' => $iItemId,
- 'iItemCount' => $iItemCount,
- 'sItemAddons' => $sItemAddons,
- 'sRedirect' => $sRedirect,
- 'sCustom' => base64_encode(json_encode($aCustom))
- );
- $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));
- if(!empty($aCartInfo['items_price']) && !empty($aCartInfo['items']) && is_array($aCartInfo['items'])) {
- $aItem = array_shift($aCartInfo['items']);
- $aParams = array_merge($aParams, array(
- 'iAmount' => (int)round(100 * (float)$aCartInfo['items_price']),
- 'sItemName' => $aItem['name'],
- 'sItemTitle' => $aItem['title']
- ));
- }
- $aButtonJs = $oProvider->getButtonRecurringJs($iClientId, $iSellerId, $aParams);
- }
- if(empty($aButtonJs) || !is_array($aButtonJs))
- $aButtonJs = $this->getObjectSubscriptions()->serviceGetSubscribeJs($iSellerId, $aProvider['name'], $iModuleId, $iItemId, $iItemCount, $sRedirect, $aCustom);
- return $aButtonJs;
- }
- /**
- * Integration with itslef which is needed to pay an invoice.
- */
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-payments Payments
- * @subsubsection bx_payment-get_payment_data get_payment_data
- *
- * @code bx_srv('bx_payment', 'get_payment_data', [...]); @endcode
- *
- * Get an array with module's description. Is needed for payments processing module.
- *
- * @return an array with module's description.
- *
- * @see BxPaymentModule::serviceGetPaymentData
- */
- /**
- * @ref bx_payment-get_payment_data "get_payment_data"
- */
- public function serviceGetPaymentData()
- {
- return $this->_aModule;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-payments Payments
- * @subsubsection bx_payment-get_cart_item get_cart_item
- *
- * @code bx_srv('bx_payment', 'get_cart_item', [...]); @endcode
- *
- * Get an array with prodict's description. Is used in Shopping Cart in payments processing module.
- *
- * @param $mixedItemId product's ID or Unique Name.
- * @return an array with prodict's description. Empty array is returned if something is wrong.
- *
- * @see BxPaymentModule::serviceGetCartItem
- */
- /**
- * @ref bx_payment-get_cart_item "get_cart_item"
- */
- public function serviceGetCartItem($mixedItemId)
- {
- $CNF = &$this->_oConfig->CNF;
- if(!$mixedItemId)
- return array();
- if(is_numeric($mixedItemId))
- $aItem = $this->_oDb->getInvoices(array('type' => 'id', 'id' => (int)$mixedItemId));
- else
- $aItem = $this->_oDb->getInvoices(array('type' => 'name', 'name' => $mixedItemId));
- if(empty($aItem) || !is_array($aItem))
- return array();
- $oCommissionaire = BxDolProfile::getInstanceMagic($aItem['commissionaire_id']);
- return array (
- 'id' => $aItem['id'],
- 'author_id' => $aItem['commissionaire_id'],
- 'name' => $aItem['name'],
- 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $oCommissionaire->getDisplayName()),
- '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)),
- 'url' => '',
- 'price_single' => $aItem['amount'],
- 'price_recurring' => '',
- 'period_recurring' => 0,
- 'period_unit_recurring' => '',
- 'trial_recurring' => ''
- );
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-payments Payments
- * @subsubsection bx_payment-get_cart_items get_cart_items
- *
- * @code bx_srv('bx_payment', 'get_cart_items', [...]); @endcode
- *
- * Get an array with prodicts' descriptions by seller. Is used in Manual Order Processing in payments processing module.
- *
- * @param $iCommissionaireId commissionaire ID.
- * @return an array with prodicts' descriptions. Empty array is returned if something is wrong or seller doesn't have any products.
- *
- * @see BxPaymentModule::serviceGetCartItems
- */
- /**
- * @ref bx_payment-get_cart_items "get_cart_items"
- */
- public function serviceGetCartItems($iCommissionaireId)
- {
- $CNF = &$this->_oConfig->CNF;
- $iCommissionaireId = (int)$iCommissionaireId;
- if(empty($iCommissionaireId))
- return array();
- $sCommissionaireName = BxDolProfile::getInstanceMagic($iCommissionaireId)->getDisplayName();
- $aItems = $this->_oDb->getInvoices(array('type' => 'commissionaire_id', 'commissionaire_id' => $iCommissionaireId));
- $aResult = array();
- foreach($aItems as $aItem)
- $aResult[] = array(
- 'id' => $aItem['id'],
- 'author_id' => $aItem['commissionaire_id'],
- 'name' => $aItem['name'],
- 'title' => _t('_bx_payment_txt_invoice_title', $aItem['name'], $sCommissionaireName),
- '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)),
- 'url' => '',
- 'price_single' => $aItem['amount'],
- 'price_recurring' => '',
- 'period_recurring' => 0,
- 'period_unit_recurring' => '',
- 'trial_recurring' => ''
- );
- return $aResult;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-payments Payments
- * @subsubsection bx_payment-register_cart_item register_cart_item
- *
- * @code bx_srv('bx_payment', 'register_cart_item', [...]); @endcode
- *
- * Register a processed single time payment inside the Payment module. Is called with payment processing module after the payment was registered there.
- *
- * @param $iClientId client ID.
- * @param $iSellerId seller ID
- * @param $iItemId item ID.
- * @param $iItemCount the number of purchased items.
- * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
- * @param $sLicense license number genereted with payment processing module for internal usage
- * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
- *
- * @see BxPaymentModule::serviceRegisterCartItem
- */
- /**
- * @ref bx_payment-register_cart_item "register_cart_item"
- */
- public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
- {
- $CNF = &$this->_oConfig->CNF;
- $aItem = $this->serviceGetCartItem($iItemId);
- if(empty($aItem) || !is_array($aItem))
- return array();
- if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_PAID)))
- return array();
- $this->alert('invoice_marked_as_paid', 0, false, array(
- 'product_id' => $iItemId,
- 'profile_id' => $iClientId,
- 'order' => $sOrder,
- 'license' => $sLicense,
- 'type' => BX_PAYMENT_TYPE_SINGLE,
- 'count' => $iItemCount,
- 'duration' => '',
- 'trial' => ''
- ));
- return $aItem;
- }
- /**
- * @page service Service Calls
- * @section bx_payment Payment
- * @subsection bx_payment-payments Payments
- * @subsubsection bx_payment-unregister_cart_item unregister_cart_item
- *
- * @code bx_srv('bx_payment', 'unregister_cart_item', [...]); @endcode
- *
- * Unregister an earlier processed single time payment inside the Payment module. Is called with payment processing module after the payment was unregistered there.
- *
- * @param $iClientId client ID.
- * @param $iSellerId seller ID
- * @param $iItemId item ID.
- * @param $iItemCount the number of purchased items.
- * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
- * @param $sLicense license number genereted with payment processing module for internal usage
- * @return boolean value determining where the payment was unregistered or not.
- *
- * @see BxPaymentModule::serviceUnregisterCartItem
- */
- /**
- * @ref bx_payment-unregister_cart_item "unregister_cart_item"
- */
- public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
- {
- if(!$this->_oDb->updateInvoice($iItemId, array('status' => BX_PAYMENT_INV_STATUS_UNPAID)))
- return false;
- $this->alert('invoice_marked_as_unpaid', 0, false, array(
- 'product_id' => $iItemId,
- 'profile_id' => $iClientId,
- 'order' => $sOrder,
- 'license' => $sLicense,
- 'type' => BX_PAYMENT_TYPE_SINGLE,
- 'count' => $iItemCount
- ));
- return true;
- }
- public function serviceStripeV3CreateSessionApi($mixedParams)
- {
- $aParams = is_array($mixedParams) ? $mixedParams : json_decode($mixedParams, true);
- if(!isset($aParams['type'], $aParams['seller_id'], $aParams['items']))
- return [bx_api_get_msg(_t($this->_sLangsPrefix . 'err_wrong_data'))];
- $sType = $aParams['type'];
- $sProvider = 'stripe_v3';
- $iClientId = $this->getProfileId();
- $iSellerId = (int)$aParams['seller_id'];
- $sItems = $aParams['items'];
- $oProvider = $this->getObjectProvider($sProvider, $iSellerId);
- if($oProvider === false || !$oProvider->isActive())
- return [bx_api_get_msg(_t($this->_sLangsPrefix . 'err_incorrect_provider'))];
- $aSessionParams = [];
- if(!empty($aParams['return_url'])) {
- $sRedirect = $this->_oConfig->urlEncode($this->_oConfig->getUrl('URL_' . ($sType == BX_PAYMENT_TYPE_RECURRING ? 'SUBSCRIPTIONS' : 'HISTORY')));
- $aSessionParams['return_url'] = $aParams['return_url'] . '/api.php?r=' . $this->getName() . '/initialize_checkout_api¶ms[]=' . implode('¶ms[]=', [$sType, $iSellerId, $sProvider, $sItems, $sRedirect]);
- }
- $minxedSession = $oProvider->createSessionPaymentEmbedded($sType, $iClientId, $iSellerId, $sItems, $aSessionParams);
- if($minxedSession === false)
- return [bx_api_get_msg(_t($this->_sLangsPrefix . 'err_cannot_perform'))];
- return [
- 'clientSecret' => $minxedSession
- ];
- }
- public function serviceConvert($fAmount, $sCurrencyFrom, $sCurrencyTo, $fRate = false)
- {
- $sCurrencyDefault = $this->_oConfig->getDefaultCurrencyCode();
- $bFromDefault = strcmp($sCurrencyFrom, $sCurrencyDefault) == 0;
- $bToDefault = strcmp($sCurrencyTo, $sCurrencyDefault) == 0;
- if(!$bFromDefault && !$bToDefault)
- return false;
- if($fRate === false)
- $fRate = $this->_oDb->getCurrencyExchangeRate($bFromDefault ? $sCurrencyTo : $sCurrencyFrom);
- if($fRate === false)
- return false;
- $fRate = (float)$fRate;
- if($bToDefault)
- $fRate = 1/$fRate;
- return $fAmount * $fRate;
- }
- public function updateCurrencyExchangeRates()
- {
- $CNF = &$this->_oConfig->CNF;
- $sApiKey = getParam($CNF['PARAM_CURRENCY_EXCHANGE_API']);
- if(empty($sApiKey))
- return;
- $sCurrency = $this->_oDb->getParam($CNF['PARAM_CURRENCY_CODE'], false);
- $aCurrencies = BxDolForm::getDataItems($CNF['OBJECT_FORM_PRELISTS_CURRENCIES'], false, BX_DATA_VALUES_ADDITIONAL);
- unset($aCurrencies[$sCurrency]);
- $sApiEndpoint = 'https://api.apilayer.com/exchangerates_data/latest';
- $aApiParams = [
- 'symbols' => implode(',', array_keys($aCurrencies)),
- 'base' => $sCurrency
- ];
- $sResponse = bx_file_get_contents($sApiEndpoint, $aApiParams, 'get', [
- 'apikey: ' . $sApiKey
- ]);
- $aResponse = json_decode($sResponse, true);
- if(empty($aResponse['rates']) || !is_array($aResponse['rates']))
- return;
- $this->_oDb->deleteCurrencyExchangeRate($sCurrency);
- foreach($aResponse['rates'] as $sCode => $fRate)
- $this->_oDb->updateCurrencyExchangeRate($sCode, $fRate);
- }
- }
- /** @} */
|