Sfoglia il codice sorgente

Ticket #1182 - Chargebee V3 integration fixes and improvements including 'Self-serve Portal' integration.

AntonLV 8 anni fa
parent
commit
d0eb7fea72

+ 19 - 1
modules/boonex/payment/classes/BxPaymentGridSbsList.php

@@ -13,19 +13,37 @@ require_once('BxPaymentGridSbsAdministration.php');
 
 class BxPaymentGridSbsList extends BxPaymentGridSbsAdministration
 {
+	protected $_aProvidersAttached;
+
     public function __construct ($aOptions, $oTemplate = false)
     {
         parent::__construct ($aOptions, $oTemplate);
+
+        $this->_aProvidersAttached = array();
     }
 
     protected function _getActionActions ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
     {
+    	$sJsCode = '';
+    	if(!empty($aRow['seller_id']) && !empty($aRow['provider']) && !in_array($aRow['provider'], $this->_aProvidersAttached)) {
+    		$oProvider = $this->_oModule->getObjectProvider($aRow['provider'], $aRow['seller_id']);
+    		if($oProvider) {
+    			$oProvider->addJsCss();
+    			 
+    			$sMethodName = 'getJsCode';
+    			if(method_exists($oProvider, $sMethodName))
+    				$sJsCode = $oProvider->$sMethodName();
+
+    			$this->_aProvidersAttached[] = $aRow['provider'];
+    		}
+    	}
+
     	unset($a['attr']['bx_grid_action_single']);
     	$a['attr'] = array_merge($a['attr'], array(
     		"onclick" => "bx_menu_popup('" . $this->_oModule->_oConfig->getObject('menu_sbs_actions') . "', this, {id: 'bx-payment-subscription-" . $aRow['id'] . "'}, {id: " . $aRow['id'] . ", grid: '" . $this->_sObject . "'});"
     	));
 
-        return  parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
+        return $sJsCode . parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
     }
 
 	protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)

+ 76 - 1
modules/boonex/payment/classes/BxPaymentProviderChargebeeV3.php

@@ -43,6 +43,29 @@ class BxPaymentProviderChargebeeV3 extends BxPaymentProviderChargebee
         echo $oPage->toJson();
     }
 
+    public function actionGetPortal($iPendingId)
+    {
+    	if(!isLogged())
+    		return echoJson(array());
+
+    	$aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => $iPendingId));
+    	if(empty($aPending) || !is_array($aPending))
+    		return echoJson(array());
+
+    	$this->setOptionsByVendor((int)$aPending['seller_id']);
+
+    	$aSubscription = $this->_oModule->_oDb->getSubscription(array('type' => 'pending_id', 'pending_id' => $iPendingId));
+    	if(empty($aSubscription) || !is_array($aSubscription))
+    		return echoJson(array());
+
+    	$oPortal = $this->getPortal($aSubscription['customer_id'], $aSubscription['subscription_id']);
+    	if($oPortal === false)
+    		return echoJson(array());
+
+    	header('Content-type: text/html; charset=utf-8');
+    	echo $oPortal->toJson();
+    }
+
     public function addJsCss()
     {
     	if(!$this->isActive())
@@ -136,8 +159,47 @@ class BxPaymentProviderChargebeeV3 extends BxPaymentProviderChargebee
         return $aResult;
     }
 
+    public function getPortal($sCustomerId, $sSubscriptionId)
+    {
+    	$oPortal = false;
+
+    	try {
+    		ChargeBee_Environment::configure($this->_getSite(), $this->_getApiKey());
+    		$oResult = ChargeBee_PortalSession::create(array(
+				'customer' => array(
+					'id' => $sCustomerId
+				)));
+
+    		$oPortal = $oResult->portalSession();
+    	}
+    	catch (Exception $oException) {
+    		$iError = $oException->getCode();
+    		$sError = $oException->getMessage();
+
+    		$this->log('Get Portal Error: ' . $sError . '(' . $iError . ')');
+
+    		return false;
+    	}
+
+    	return $oPortal;
+    }
+
+    public function getJsCode($aParams = array())
+    {
+    	$sSite = '';
+    	bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_js_code', 0, 0, array(
+	    	'site' => &$sSite,
+	    	'params' => &$aParams
+    	));
+
+    	return $this->_oModule->_oTemplate->getJsCode($this->_sName, array_merge(array(
+			'sProvider' => $this->_sName,
+			'sSite' => !empty($sSite) ? $sSite : $this->_getSite()
+    	), $aParams));
+    }
+
     /**
-     * Single tome payments aren't available with Chargebee
+     * Single time payments aren't available with Chargebee
      */
     public function getButtonSingle($iClientId, $iVendorId, $aParams = array())
     {
@@ -170,6 +232,19 @@ class BxPaymentProviderChargebeeV3 extends BxPaymentProviderChargebee
 	    	), $aParams))
     	));
     }
+
+    public function getMenuItemsActionsRecurring($iClientId, $iVendorId, $aParams = array())
+    {
+    	if(empty($aParams['id']))
+    		return array();
+
+    	$sPrefix = 'bx-payment-strp-';
+    	$sJsObject = $this->_oModule->_oConfig->getJsObject($this->_sName);
+
+    	return array(
+   			array('id' => $sPrefix . 'manager', 'name' => $sPrefix . 'manager', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => "javascript:return " . $sJsObject . ".manage(this, '" . $aParams['id'] . "')", 'target' => '_self', 'title' => _t('_bx_payment_cbee_menu_item_title_manager'))
+    	);
+    }
 }
 
 /** @} */

+ 1 - 0
modules/boonex/payment/install/langs/en.xml

@@ -359,6 +359,7 @@
 	<string name="_bx_payment_cbee_msg_subscribed"><![CDATA[Thank you! Subscription created. Please, check your email for further instructions.]]></string>
 	<string name="_bx_payment_cbee_txt_checkout_with_single"><![CDATA[Checkout with {0}]]></string>
     <string name="_bx_payment_cbee_txt_checkout_with_recurring"><![CDATA[Checkout with {0}]]></string>
+    <string name="_bx_payment_cbee_menu_item_title_manager"><![CDATA[Self-serve Portal]]></string>
 	<string name="_bx_payment_txt_name_chargebee"><![CDATA[Chargebee]]></string>
 	<string name="_bx_payment_txt_name_chargebee_v3"><![CDATA[Chargebee]]></string>
 	<string name="_bx_payment_txt_cart_chargebee"><![CDATA[Chargebee]]></string>

+ 28 - 9
modules/boonex/payment/js/chargebee_v3.js

@@ -21,15 +21,15 @@ BxPaymentProviderChargebeeV3.prototype.init = function(oOptions) {
     this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
     this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
 
-    this._sObjNameCart = oOptions.sObjNameCart;
-    this._iClientId = oOptions.iClientId;
-    this._iSellerId  = oOptions.iSellerId;
-    this._iModuleId  = oOptions.iModuleId;
-    this._iItemId  = oOptions.iItemId;
-    this._sItemName = oOptions.sItemName;
-    this._iItemCount  = oOptions.iItemCount;
-    this._sRedirect  = oOptions.sRedirect;
-    this._sCustom  = oOptions.sCustom;
+    this._sObjNameCart = oOptions.sObjNameCart == undefined ? '' : oOptions.sObjNameCart;
+    this._iClientId = oOptions.iClientId == undefined ? 0 : oOptions.iClientId;
+    this._iSellerId = oOptions.iSellerId == undefined ? 0 : oOptions.iSellerId;
+    this._iModuleId  = oOptions.iModuleId == undefined ? 0 : oOptions.iModuleId;
+    this._iItemId  = oOptions.iItemId == undefined ? 0 : oOptions.iItemId;
+    this._sItemName = oOptions.sItemName == undefined ? '' : oOptions.sItemName;
+    this._iItemCount  = oOptions.iItemCount == undefined ? 0 : oOptions.iItemCount;
+    this._sRedirect  = oOptions.sRedirect == undefined ? '' : oOptions.sRedirect;
+    this._sCustom  = oOptions.sCustom == undefined ? '' : oOptions.sCustom;
 
 	this._rHandler = Chargebee.init({
         site: oOptions.sSite
@@ -88,4 +88,23 @@ BxPaymentProviderChargebeeV3.prototype.subscribe = function(oLink) {
 	return false;
 };
 
+BxPaymentProviderChargebeeV3.prototype.manage = function(oLink, iPendingId) {
+	var $this = this;
+    var oDate = new Date();
+
+    $(".bx-popup-applied:visible").dolPopupHide();
+
+	this._rHandler.setPortalSession(function() {
+    	return $.post({
+    		url: $this._sActionsUrl + 'call/' + $this._sProvider + '/get_portal/' + iPendingId + '/',
+    		dataType: 'json'
+    	});
+    });
+
+	var cbPortal = this._rHandler.createChargebeePortal();
+	cbPortal.open({});
+
+	return false;
+};
+
 /** @} */

+ 1 - 0
modules/boonex/russian/data/langs/bx_payment/ru.xml

@@ -359,6 +359,7 @@
 	<string name="_bx_payment_cbee_msg_subscribed"><![CDATA[Спасибо! Подписка была создана. Пожалуйста, проверьте ваш емэйл для получения дальнейших инструкций.]]></string>
 	<string name="_bx_payment_cbee_txt_checkout_with_single"><![CDATA[Оплатить через {0}]]></string>
     <string name="_bx_payment_cbee_txt_checkout_with_recurring"><![CDATA[Оплатить через {0}]]></string>
+    <string name="_bx_payment_cbee_menu_item_title_manager"><![CDATA[Портал самообслуживания]]></string>
 	<string name="_bx_payment_txt_name_chargebee"><![CDATA[Chargebee]]></string>
 	<string name="_bx_payment_txt_name_chargebee_v3"><![CDATA[Chargebee]]></string>
 	<string name="_bx_payment_txt_cart_chargebee"><![CDATA[Chargebee]]></string>