| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?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('CBEE_MODE_LIVE', 1);
- define('CBEE_MODE_TEST', 2);
- use ChargeBee\ChargeBee\Environment;
- use ChargeBee\ChargeBee\Models\Addon;
- use ChargeBee\ChargeBee\Models\Customer;
- use ChargeBee\ChargeBee\Models\HostedPage;
- use ChargeBee\ChargeBee\Models\Subscription;
- class BxPaymentProviderChargebee extends BxBaseModPaymentProvider implements iBxBaseModPaymentProvider
- {
- protected $_iMode;
- protected $_bCheckAmount;
- function __construct($aConfig)
- {
- $this->MODULE = 'bx_payment';
- parent::__construct($aConfig);
- $this->_aSbsStatuses = array(
- 'future' => BX_PAYMENT_SBS_STATUS_SCHEDULED,
- 'in_trial' => BX_PAYMENT_SBS_STATUS_TRIAL,
- 'active' => BX_PAYMENT_SBS_STATUS_ACTIVE,
- 'non_renewing' => BX_PAYMENT_SBS_STATUS_ACTIVE,
- 'paused' => BX_PAYMENT_SBS_STATUS_PAUSED,
- 'canceled' => BX_PAYMENT_SBS_STATUS_CANCELED,
- );
- }
- public function initOptions($aOptions)
- {
- parent::initOptions($aOptions);
- $this->_iMode = (int)$this->getOption('mode');
- $this->_bCheckAmount = $this->getOption('check_amount') == 'on';
- $this->_bUseSsl = $this->getOption('ssl') == 'on';
- }
- public function initializeCheckout($iPendingId, $aCartInfo)
- {
- $aItem = array_shift($aCartInfo['items']);
- if(empty($aItem) || !is_array($aItem))
- return $this->_sLangsPrefix . 'err_empty_items';
- $aClient = $this->_oModule->getProfileInfo();
- $aVendor = $this->_oModule->getProfileInfo($aCartInfo['vendor_id']);
- $oPage = $this->createHostedPage($aItem, $aClient, $aVendor, $iPendingId);
- if($oPage === false)
- return $this->_sLangsPrefix . 'err_cannot_perform';
- return array(
- 'code' => 0,
- 'eval' => $this->_oModule->_oConfig->getJsObject('cart') . '.onSubscribeSubmit(oData);',
- 'redirect' => $oPage->url
- );
- }
- public function finalizeCheckout(&$aData)
- {
- $sPageId = bx_process_input($aData['id']);
- $iPendingId = bx_process_input($aData['pending_id'], BX_DATA_INT);
- if(empty($sPageId) || empty($iPendingId))
- return array('code' => 1, 'message' => $this->_sLangsPrefix . 'err_wrong_data');
- $oPage = $this->retreiveHostedPage($sPageId);
- if($oPage === false)
- return $this->_sLangsPrefix . 'err_cannot_perform';
- $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => $iPendingId));
- if(!empty($aPending['order']) || !empty($aPending['error_code']) || !empty($aPending['error_msg']) || (int)$aPending['processed'] != 0)
- return array('code' => 3, 'message' => $this->_sLangsPrefix . 'err_already_processed');
- $oCustomer = $oPage->content()->customer();
- $oSubscription = $oPage->content()->subscription();
- $aResult = array(
- 'code' => BX_PAYMENT_RESULT_SUCCESS,
- 'message' => $this->_sLangsPrefix . 'cbee_msg_subscribed',
- 'pending_id' => $iPendingId,
- 'customer_id' => $oCustomer->id,
- 'subscription_id' => $oSubscription->id,
- 'client_name' => _t($this->_sLangsPrefix . 'txt_buyer_name_mask', $oCustomer->firstName, $oCustomer->lastName),
- 'client_email' => $oCustomer->email,
- 'paid' => false,
- 'trial' => $oSubscription->status == 'in_trial',
- );
- //--- Update pending transaction ---//
- $this->_oModule->_oDb->updateOrderPending($iPendingId, array(
- 'order' => $oSubscription->id,
- 'error_code' => $aResult['code'],
- 'error_msg' => _t($aResult['message'])
- ));
- return $aResult;
- }
- public function notify()
- {
- $iResult = $this->_processEvent();
- http_response_code($iResult);
- }
- public function cancelRecurring($iPendingId, $sCustomerId, $sSubscriptionId)
- {
- return $this->deleteSubscription($sSubscriptionId);
- }
- public function createHostedPage($aItem, $aClient, $aVendor = array(), $iPendingId = 0)
- {
- $oPage = false;
- try {
- $aPage = array(
- 'embed' => false, //--- Note. 'embed' should be disabled to allow payments via PayPal
- 'subscription' => array(
- 'planId' => $aItem['name']
- ),
- 'customer' => array(
- 'email' => $aClient['email'],
- 'firstName' => $aClient['name']
- ),
- );
- if(!empty($aItem['addons']) && is_array($aItem['addons']))
- $aPage['addons'] = $aItem['addons'];
- if(!empty($aVendor) && is_array($aVendor) && !empty($aVendor))
- $aPage['redirectUrl'] = bx_append_url_params($this->getReturnDataUrl($aVendor['id']), array(
- 'pending_id' => $iPendingId
- ));
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = HostedPage::checkoutNew($aPage);
- $oPage = $oResult->hostedPage();
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Create Hosted Page Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $oPage;
- }
- public function retreiveHostedPage($sPageId)
- {
- $oPage = null;
- try {
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = HostedPage::retrieve($sPageId);
- $oPage = $oResult->hostedPage();
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Retrieve Hosted Page Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $oPage;
- }
- public function retrieveSubscription($sSubscriptionId)
- {
- $oSubscription = null;
- try {
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = Subscription::retrieve($sSubscriptionId);
- $oSubscription = $oResult->subscription();
- if($oSubscription->id != $sSubscriptionId)
- return false;
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Retrieve Subscription Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $oSubscription;
- }
- public function getSubscription($iPendingId, $sCustomerId, $sSubscriptionId)
- {
- $oSubscription = $this->retrieveSubscription($sSubscriptionId);
- if($oSubscription === false)
- return array();
- $sStatus = $oSubscription->status;
- $sStatus = isset($this->_aSbsStatuses[$sStatus]) ? $this->_aSbsStatuses[$sStatus] : BX_PAYMENT_SBS_STATUS_UNKNOWN;
- return array(
- 'status' => $sStatus,
- 'created' => $oSubscription->createdAt,
- 'started' => $oSubscription->startedAt,
- 'trial_start' => $oSubscription->trialStart,
- 'trial_end' => $oSubscription->trialEnd,
- 'cperiod_start' => $oSubscription->currentTermStart,
- 'cperiod_end' => $oSubscription->currentTermEnd,
- );
- }
-
- public function deleteSubscription($sSubscriptionId)
- {
- try {
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = Subscription::cancel($sSubscriptionId);
- $oSubscription = $oResult->subscription();
- if($oSubscription->status != 'cancelled')
- return false;
- }
- catch (Exception $oException) {
- $aError = $oException->getJsonBody();
- $this->log('Delete Subscription Error: ' . $aError['error']['message']);
- $this->log($aError);
- return false;
- }
- $this->onDeleteSubscription($sSubscriptionId, $oSubscription->getValues());
- return true;
- }
- public function retrieveAddons($sStatus = 'active', $iLimit = 0)
- {
- $aAddons = array();
- try {
- $aParams = array();
- if(!empty($sStatus))
- $aParams['status[is]'] = $sStatus;
- if(!empty($iLimit))
- $aParams['limit'] = $iLimit;
-
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResults = Addon::all($aParams);
- foreach($oResults as $oResult)
- $aAddons[] = $oResult->addon();
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Retrieve Addons Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $aAddons;
- }
- public function getAddons($sStatus = 'active', $iLimit = 0)
- {
- $aAddons = $this->retrieveAddons($sStatus, $iLimit);
- if(empty($aAddons) || !is_array($aAddons))
- return array();
- $aResult = array();
- foreach($aAddons as $oAddon)
- $aResult[] = $oAddon->getValues();
- return $aResult;
- }
- public function retrieveAddon($sId)
- {
- $oAddon = null;
- try {
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = Addon::retrieve($sId);
- $oAddon = $oResult->addon();
- if($oAddon->id != $sId)
- return false;
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Retrieve Addon Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $oAddon;
- }
- public function getAddon($sId)
- {
- $oAddon = $this->retrieveAddon($sId);
- if($oAddon === false)
- return array();
- return $oAddon->getValues();
- }
- public function retrieveCustomer($sCustomerId)
- {
- $oCustomer = null;
- try {
- Environment::configure($this->_getSite(), $this->_getApiKey());
- $oResult = Customer::retrieve($sCustomerId);
- $oCustomer = $oResult->customer();
- if($oCustomer->id != $sCustomerId)
- return false;
- }
- catch (Exception $oException) {
- $iError = $oException->getCode();
- $sError = $oException->getMessage();
- $this->log('Retrieve Customer Error: ' . $sError . '(' . $iError . ')');
- return false;
- }
- return $oCustomer;
- }
- public function onDeleteSubscription($sSubscriptionId, $aSubscription)
- {
- bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_cancel_subscription', 0, false, array(
- 'subscription_id' => $sSubscriptionId,
- 'subscription_info' => $aSubscription
- ));
- }
- protected function _getSite()
- {
- return $this->_iMode == CBEE_MODE_LIVE ? $this->getOption('live_site') : $this->getOption('test_site');
- }
- protected function _getApiKey()
- {
- return $this->_iMode == CBEE_MODE_LIVE ? $this->getOption('live_api_key') : $this->getOption('test_api_key');
- }
- protected function _processEvent()
- {
- $sInput = @file_get_contents("php://input");
- $aEvent = json_decode($sInput, true);
- if(empty($aEvent) || !is_array($aEvent))
- return 404;
- $sType = $aEvent['event_type'];
- if(!in_array($sType, array('payment_succeeded', ' payment_refunded', 'subscription_cancelled')))
- return 200;
- $this->log('Webhooks: ' . (!empty($sType) ? $sType : ''));
- $this->log($aEvent);
- $sMethod = '_processEvent' . bx_gen_method_name($sType, array('.', '_', '-'));
- if(!method_exists($this, $sMethod))
- return 200;
- return $this->$sMethod($aEvent) ? 200 : 403;
- }
- protected function _processEventPaymentSucceeded(&$aEvent)
- {
- $mixedResult = $this->_getDataTransaction($aEvent, 'success');
- if($mixedResult === false)
- return false;
- list($aPending, $aTransaction) = $mixedResult;
- $fTransactionAmount = (float)$aTransaction['amount'] / 100;
- $sTransactionCurrency = strtoupper($aTransaction['currency_code']);
- if($this->_bCheckAmount && ((float)$aPending['amount'] != $fTransactionAmount || strcasecmp($this->_oModule->_oConfig->getDefaultCurrencyCode(), $sTransactionCurrency) !== 0))
- return false;
- if($aPending['type'] == BX_PAYMENT_TYPE_RECURRING)
- $this->_oModule->getObjectSubscriptions()->prolong($aPending);
- return $this->_oModule->registerPayment($aPending);
- }
- protected function _processEventPaymentRefunded(&$aEvent)
- {
- $mixedResult = $this->_getDataTransaction($aEvent);
- if($mixedResult === false)
- return false;
- list($aPending) = $mixedResult;
- return $this->_oModule->refundPayment($aPending);
- }
- protected function _processEventSubscriptionCancelled(&$aEvent)
- {
- $mixedResult = $this->_getDataSubscription($aEvent);
- if($mixedResult === false)
- return false;
- list($aPending, $aSubscription) = $mixedResult;
- $this->onDeleteSubscription($aSubscription['id'], $aSubscription);
- return $this->_oModule->getObjectSubscriptions()->cancelLocal($aPending);
- }
- protected function _getDataTransaction(&$aEvent, $sWithStatusCheck = '')
- {
- $aTransaction = $aEvent['content']['transaction'];
- if(empty($aTransaction) || (!empty($sWithStatusCheck) && $aTransaction['status'] != $sWithStatusCheck))
- return false;
- $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'order', 'order' => $aTransaction['subscription_id']));
- if(empty($aPending) || !is_array($aPending))
- return false;
- return array($aPending, $aTransaction);
- }
- protected function _getDataSubscription(&$aEvent, $sWithStatusCheck = '')
- {
- $aSubscription = $aEvent['content']['subscription'];
- if(empty($aSubscription) || (!empty($sWithStatusCheck) && $aSubscription['status'] != $sWithStatusCheck))
- return false;
- $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'order', 'order' => $aSubscription['id']));
- if(empty($aPending) || !is_array($aPending))
- return false;
- return array($aPending, $aSubscription);
- }
- }
- /** @} */
|