Преглед изворни кода

Ticket #4971 - Build-in API support: Sell membership.

AntonLV пре 1 година
родитељ
комит
3888d18fa1

+ 64 - 11
modules/boonex/acl/classes/BxAclGridView.php

@@ -42,20 +42,53 @@ class BxAclGridView extends BxAclGridLevels
 
         $aIds = $this->_getIds();
         if($aIds === false)
-            return echoJson(array());
+            return $this->_bIsApi ? [] : echoJson([]);
 
-        $aItem = $this->_oModule->_oDb->getPrices(array('type' => 'by_id', 'value' => $aIds[0]));
+        $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => $aIds[0]]);
         if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
-        	return echoJson(array());
+            return $this->_bIsApi ? [] : echoJson(array());
 
-        $aResult = array();
+        $aResult = [];
         $iUserId = bx_get_logged_profile_id();
-        if(BxDolAcl::getInstance()->setMembership($iUserId, $aItem['level_id'], array('period' => $aItem['period'], 'period_unit' => $aItem['period_unit']), true))
-            $aResult = array('grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t('_bx_acl_msg_performed'));
+        if(BxDolAcl::getInstance()->setMembership($iUserId, $aItem['level_id'], ['period' => $aItem['period'], 'period_unit' => $aItem['period_unit']], true))
+            $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t('_bx_acl_msg_performed')];
         else
-            $aResult = array('msg' => _t('_bx_acl_err_cannot_perform'));
+            $aResult = ['msg' => _t('_bx_acl_err_cannot_perform')];
 
-        return echoJson($aResult);
+        return $this->_bIsApi ? [] : echoJson($aResult);
+    }
+
+    public function performActionBuy()
+    {
+        if(!$this->_bIsApi)
+            return echoJson([]);
+        
+        $aIds = $this->_getIds();
+        if($aIds === false)
+            return [];
+
+        $aResult = $this->_oPayment->addToCart($this->_iOwner, $this->MODULE, array_shift($aIds), 1, true);
+        if(isset($aResult['code']) && (int)$aResult['code'] != 0)
+            return [bx_api_get_msg($aResult['message'])];
+
+        return [];
+    }
+
+    public function performActionSubscribe()
+    {
+        if(!$this->_bIsApi)
+            return echoJson([]);
+        
+        $aIds = $this->_getIds();
+        if($aIds === false)
+            return [];
+
+        //TODO: Payment Provider selector should be realized.
+        $aResult = $this->_oPayment->subscribeWithAddons($this->_iOwner, 'stripe_v3', $this->MODULE, array_shift($aIds), 1, true);
+        if(isset($aResult['code']) && (int)$aResult['code'] != 0)
+            return [bx_api_get_msg($aResult['message'])];
+
+        return [];
     }
 
     public function getCode($isDisplayHeader = true)
@@ -80,7 +113,10 @@ class BxAclGridView extends BxAclGridLevels
     protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
     {
         if((float)$aRow['price'] != 0 || BxDolAcl::getInstance()->isMemberLevelInSet(array($aRow['level_id'])))
-            return '';
+            return $this->_bIsApi ? [] : '';
+
+        if($this->_bIsApi)
+            return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
 
         return  parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
     }
@@ -90,7 +126,15 @@ class BxAclGridView extends BxAclGridLevels
         $CNF = &$this->_oModule->_oConfig->CNF;
 
         if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || ($this->_bTypeRecurring && getParam($CNF['PARAM_RECURRING_PRIORITIZE']) == 'on' && !$this->_isLifetime($aRow)))
-            return '';
+            return $this->_bIsApi ? [] : '';
+
+        if($this->_bIsApi)
+            return array_merge($a, [
+                'name' => $sKey, 
+                'type' => 'callback', 
+                'on_callback' => 'redirect',
+                'redirect_url' => bx_api_get_relative_url($this->_oPayment->getCartUrl($this->_iOwner))
+            ]);
 
     	$aJs = $this->_oPayment->getAddToCartJs($this->_iOwner, $this->MODULE, $aRow['id'], 1, true);
     	if(!empty($aJs) && is_array($aJs)) {
@@ -112,7 +156,16 @@ class BxAclGridView extends BxAclGridLevels
     protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
     {
         if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring || ($this->_bTypeSingle && $this->_isLifetime($aRow)))
-            return '';
+            return $this->_bIsApi ? [] : '';
+
+        if($this->_bIsApi)
+            return array_merge($a, [
+                'name' => $sKey, 
+                'type' => 'object', 
+                'object_name' => 'stripe_v3',
+                'seller_id' => $this->_iOwner,
+                'items' => [$this->_oPayment->getCartItemDescriptor($this->_iOwner, $this->_oModule->_oConfig->getId(), $aRow['id'], 1)],
+            ]);
 
     	$aJs = $this->_oPayment->getSubscribeJs($this->_iOwner, '', $this->MODULE, $aRow['id'], 1);
         if(!empty($aJs) && is_array($aJs)) {

+ 17 - 12
modules/boonex/acl/classes/BxAclModule.php

@@ -14,7 +14,7 @@ bx_import('BxDolAcl');
 define('BX_ACL_LICENSE_TYPE_SINGLE', 'single'); //--- one-time payment license
 define('BX_ACL_LICENSE_TYPE_RECURRING', 'recurring'); //--- recurring payment license
 
-class BxAclModule extends BxDolModule
+class BxAclModule extends BxBaseModGeneralModule
 {
     /**
      * Constructor
@@ -107,17 +107,22 @@ class BxAclModule extends BxDolModule
      */
 	public function serviceGetBlockView()
 	{
-	    bx_require_authentication(false, false, $this->serviceGetViewUrl());
-
-		$sGrid = $this->_oConfig->getGridObject('view');
-		$oGrid = BxDolGrid::getObjectInstance($sGrid);
-        if(!$oGrid)
-            return '';
-
-        $this->_oTemplate->addCss(array('view.css'));
-		return array(
-            'content' => $oGrid->getCode()
-        );
+            bx_require_authentication(false, false, $this->serviceGetViewUrl());
+
+            $sGrid = $this->_oConfig->getGridObject('view');
+            $oGrid = BxDolGrid::getObjectInstance($sGrid);
+            if(!$oGrid)
+                return $this->_bIsApi ? [] : '';
+
+            if($this->_bIsApi)
+                return [
+                    bx_api_get_block('grid', $oGrid->getCodeAPI())
+                ];
+
+            $this->_oTemplate->addCss(['view.css']);
+            return [
+                'content' => $oGrid->getCode()
+            ];
 	}
 
 	/**

+ 11 - 0
modules/boonex/stripe_connect/classes/BxStripeConnectConfig.php

@@ -13,6 +13,7 @@ class BxStripeConnectConfig extends BxBaseModGeneralConfig
 {
     protected $_oDb;
 
+    protected $_iMainSeller;
     protected $_sMode;
 
     protected $_sApiPublicKey;
@@ -102,6 +103,8 @@ class BxStripeConnectConfig extends BxBaseModGeneralConfig
             'commissions_popup' => $sPrefix . '-commissions-popup-',
         ];
 
+        $this->_iMainSeller = 0;
+
         $this->_sAccountType = 'standard'; //'express'
     }
 
@@ -119,6 +122,14 @@ class BxStripeConnectConfig extends BxBaseModGeneralConfig
         $this->_sReturnLink = $this->_sRefreshLink;
     }
 
+    public function getMainSeller()
+    {
+        if(empty($this->_iMainSeller))
+            $this->_iMainSeller = (int)BxDolPayments::getInstance()->getOption('site_admin');
+
+    	return $this->_iMainSeller;
+    }
+
     public function getMode()
     {
         return $this->_sMode;

+ 8 - 4
modules/boonex/stripe_connect/classes/BxStripeConnectModule.php

@@ -339,13 +339,14 @@ class BxStripeConnectModule extends BxBaseModGeneralModule
     {
         $CNF = &$this->_oConfig->CNF;
 
-        if(empty($aParams['vendor_id']))
+        $iVendorId = 0;
+        if(empty($aParams['vendor_id']) || ($iVendorId = (int)$aParams['vendor_id']) == $this->_oConfig->getMainSeller())
             return false;
 
         $sModeUc = strtoupper($this->_oConfig->getMode());
         $sAccIdField = $CNF['FIELD_' . $sModeUc . '_ACCOUNT_ID'];
-            
-        $aAccount = $this->_oDb->getAccount(['sample' => 'profile_id', 'profile_id' => (int)$aParams['vendor_id']]);
+
+        $aAccount = $this->_oDb->getAccount(['sample' => 'profile_id', 'profile_id' => $iVendorId]);
         if(empty($aAccount) || !is_array($aAccount) || empty($aAccount[$sAccIdField]))
             return false;
 
@@ -365,8 +366,11 @@ class BxStripeConnectModule extends BxBaseModGeneralModule
         try {
             $sModeUc = strtoupper($this->_oConfig->getMode());
             $sAccIdField = $CNF['FIELD_' . $sModeUc . '_ACCOUNT_ID'];
-
+            
             $iVendorId = (int)$aParams['session_params']['metadata']['vendor'];
+            if($iVendorId == $this->_oConfig->getMainSeller())
+                return false;
+
             $aAccount = $this->_oDb->getAccount(['sample' => 'profile_id', 'profile_id' => $iVendorId]);
             if(empty($aAccount) || !is_array($aAccount) || empty($aAccount[$sAccIdField]))
                 return false;