| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608 |
- <?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');
- /**
- * 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 actionGetItems($sType, $iModuleId)
- {
- $iSellerId = $this->getProfileId();
- $aItems = $this->callGetCartItems((int)$iModuleId, array($iSellerId));
- echoJson(array(
- 'code' => 0,
- 'eval' => $this->_oConfig->getJsObject('processed') . '.onSelectModule(oData);',
- 'data' => $this->_oTemplate->displayItems($sType, $aItems)
- ));
- }
- 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()
- {
- $a = parent::serviceGetSafeServices();
- return array_merge($a, array (
- 'GetBlockJoin' => 'BxPaymentJoin',
- 'GetBlockCarts' => 'BxPaymentCart',
- 'GetBlockCart' => 'BxPaymentCart',
- 'GetBlockCartHistory' => 'BxPaymentCart',
- 'GetBlockListMy' => 'BxPaymentSubscriptions',
- 'GetBlockHistory' => 'BxPaymentSubscriptions',
- ));
- }
- /**
- * @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;
- }
- /**
- * @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'))
- );
- }
- /**
- * Cart Processing Methods
- */
- public function actionAddToCart($iSellerId, $iModuleId, $iItemId, $iItemCount, $sCustom = '')
- {
- if(empty($sCustom) && bx_get('custom') !== false)
- $sCustom = bx_process_input(bx_get('custom'));
- $aCustom = array();
- if(!empty($sCustom)) {
- $aCustom = unserialize(base64_decode($sCustom));
- if(empty($aCustom) || !is_array($aCustom))
- $aCustom = array();
- }
- $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 = unserialize(base64_decode($sCustom));
- 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 = unserialize(base64_decode($sCustom));
- if(empty($aCustom) || !is_array($aCustom))
- $aCustom = array();
- }
- 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(
- 'sibscription_id' => $aSubscription['subscription_id'],
- 'sibscription_customer' => $aSubscription['customer_id'],
- 'sibscription_date' => bx_time_js($aSubscription['date'], 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)
- {
- $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);
- $mixedResult = $this->isAllowedManage($aPending);
- if($mixedResult !== true)
- return echoJson(array('code' => 2, 'message' => $mixedResult));
- if(!$this->getObjectSubscriptions()->cancel($iId))
- 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(array('object' => $mixedGridObject, 'grid' => $oGrid->getCode(false), 'blink' => array($iId)));
- }
- else
- return echoJson(array('code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')));
- }
- /**
- * Payment Processing Methods
- */
- 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;
- }
- /**
- * @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 = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
- $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 = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
- $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);
- }
- }
- bx_alert($this->getName(), 'before_pending', 0, bx_get_logged_profile_id(), array(
- 'client_id' => $this->_iUserId,
- 'type' => &$sType,
- 'provider' => &$sProvider,
- 'cart_info' => &$aInfo,
- 'custom' => &$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 actionFinalizeCheckout($sProvider, $mixedVendorId = "")
- {
- $aData = &$_REQUEST;
- $oProvider = $this->getObjectProvider($sProvider, $mixedVendorId);
- if($oProvider === false || !$oProvider->isActive())
- return $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_incorrect_provider');
- $aResult = $oProvider->finalizeCheckout($aData);
- if((int)$aResult['code'] != BX_PAYMENT_RESULT_SUCCESS)
- return $this->_oTemplate->displayPageCodeError($aResult['message']);
- $aPending = $this->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
- $bTypeRecurring = $aPending['type'] == BX_PAYMENT_TYPE_RECURRING;
- if($bTypeRecurring) {
- $sStatus = BX_PAYMENT_SBS_STATUS_UNPAID;
- if(isset($aResult['paid']) && $aResult['paid'])
- $sStatus = BX_PAYMENT_SBS_STATUS_ACTIVE;
- else if(isset($aResult['trial']) && $aResult['trial'])
- $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 $this->_oTemplate->displayPageCodeError($this->_sLangsPrefix . 'err_already_registered');
- }
- $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'] : '');
- //--- Register payment for purchased items in associated modules
- if(!empty($aResult['paid']) || ($bTypeRecurring && !empty($aResult['trial'])))
- $this->registerPayment($aPending);
- bx_alert($this->getName(), '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,
- ));
- if($oProvider->needRedirect()) {
- header('Location: ' . $oProvider->getReturnUrl());
- exit;
- }
- if(!empty($aResult['redirect'])) {
- header('Location: ' . base64_decode(urldecode($aResult['redirect'])));
- exit;
- }
- $this->_oTemplate->displayPageCodeResponse($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)
- {
- $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'], 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']));
- 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(BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)) || isAdmin())
- 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 registerPayment($mixedPending)
- {
- $aPending = is_array($mixedPending) ? $mixedPending : $this->_oDb->getOrderPending(array('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'];
- $sLicense = $this->_oConfig->getLicense();
- $aCustoms = !empty($aPending['customs']) ? unserialize($aPending['customs']) : array();
- $aCart = array();
- if($bTypeSingle) {
- $aCart = $this->_oDb->getCartContent($iClientId);
- $aCart['customs'] = !empty($aCart['customs']) ? unserialize($aCart['customs']) : array();
- }
- $bResult = false;
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- foreach($aItems as $aItem) {
- $sItem = $this->_oConfig->descriptorA2S(array($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'], array($aPending['client_id'], $aPending['seller_id'], $aItem['item_id'], $aItem['item_count'], $aPending['order'], $sLicense, $aItemCustom));
- if(empty($aItemInfo) || !is_array($aItemInfo))
- continue;
- $this->_oDb->insertOrderProcessed(array(
- 'pending_id' => $aPending['id'],
- 'client_id' => $aPending['client_id'],
- 'seller_id' => $aPending['seller_id'],
- 'author_id' => $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),
- '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'], array('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;
- $CNF = &$this->_oConfig->CNF;
- if((int)date('j') == $CNF['PARAM_CMSN_INVOICE_ISSUE_DAY'])
- $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 * $CNF['PARAM_CMSN_INVOICE_LIFETIME'];
- $aCommissionsInfo = array();
- $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, true, false, true);
- foreach($aAclLevels as $iAclLevelId => $sAclLevelTitle)
- $aCommissionsInfo[$iAclLevelId] = $this->_oDb->getCommissions(array('type' => 'acl_id', 'acl_id' => $iAclLevelId));
- $iMainSellerId = $this->_oConfig->getSiteAdmin();
- $aVendors = $this->_oDb->getOrderProcessed(array('type' => 'income', 'period_start' => $iPeriodStart, 'period_end' => $iPeriodEnd));
- foreach($aVendors as $aVendor) {
- $iVendorId = (int)$aVendor['id'];
- if($iVendorId == $iMainSellerId)
- continue;
- $aInvoice = $this->_oDb->getInvoices(array('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->_oDb->insertInvoice(array(
- 'name' => sprintf($sInvoiceNameMask, $iMainSellerId, $iVendorId, $this->_oDb->getInvoices(array(
- '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' => $CNF['PARAM_CMSN_INVOICE_EXPIRATION_NOTIFY'],
- '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.');
- 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.');
- continue;
- }
- if(!$oProvider->makePayment($aPending)) {
- $this->log($aSubscription, 'Time Tracker', 'Payment cannot be processed.');
- continue;
- }
- if(!$this->getObjectSubscriptions()->prolong($aPending)) {
- $this->log($aSubscription, 'Time Tracker', 'Cannot prolong subscription.');
- continue;
- }
- if(!$this->registerPayment($aPending)) {
- $this->log($aSubscription, 'Time Tracker', 'Cannot register payment.');
- continue;
- }
- }
- }
- public function onPaymentRegisterBefore($aPending, $aResult = array())
- {
- bx_alert('system', 'before_register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
- }
- public function onPaymentRegister($aPending, $aResult = array())
- {
- $bTypeSingle = $aPending['type'] == BX_PAYMENT_TYPE_SINGLE;
- if($bTypeSingle) {
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- foreach($aItems as $aItem)
- $this->isAllowedPurchase(array('module_id' => $aItem['module_id'], 'item_id' => $aItem['item_id']), true);
- }
- bx_alert('system', 'register_payment', 0, $aPending['client_id'], array('pending' => $aPending));
- }
- public function onPaymentRefund($aPending, $aResult = array())
- {
- bx_alert('system', 'refund_payment', 0, $aPending['client_id'], array('pending' => $aPending));
- }
- public function onSubscriptionCreate($aPending, $aSubscription, $aResult = array())
- {
- $aItems = $this->_oConfig->descriptorsM2A($aPending['items']);
- $this->isAllowedPurchase(array('module_id' => $aItems[0]['module_id'], 'item_id' => $aItems[0]['item_id']), true);
- bx_alert('system', 'create_subscription', 0, $aPending['client_id'], array(
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ));
- }
- public function onSubscriptionProlong($aPending, $aSubscription, $aResult = array())
- {
- bx_alert('system', 'prolong_subscription', 0, $aPending['client_id'], array(
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ));
- }
- public function onSubscriptionOverdue($aPending, $aSubscription, $aResult = array())
- {
- bx_alert('system', 'overdue_subscription', 0, $aPending['client_id'], array(
- 'pending' => $aPending,
- 'subscription' => $aSubscription
- ));
- }
- public function onSubscriptionCancel($aPending, $aSubscription, $aResult = array())
- {
- bx_alert('system', 'cancel_subscription', 0, $aPending['client_id'], array(
- '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(serialize($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();
- bx_alert($this->getName(), '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;
- bx_alert($this->getName(), '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;
- }
-
- }
- /** @} */
|