فهرست منبع

Ticket #3597 - Payment: Add Tax support in Stripe V3

AntonLV 4 سال پیش
والد
کامیت
4ab5e2fb54

+ 96 - 5
modules/boonex/payment/classes/BxPaymentProviderStripe.php

@@ -159,6 +159,68 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
     	return $aParams;
     }
 
+    public function getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency)
+    {
+        $sCode = $this->_getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_code_charge', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'amount' => $fAmount,
+            'currency' => $sCurrency,
+            'override_result' => &$sCode
+        ));
+
+        return $sCode;
+    }
+
+    public function checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult)
+    {
+        $bCheckResult = $this->_checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_verify_charge', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'result' => $aResult,
+            'override_result' => &$bCheckResult
+        ));
+
+        return $bCheckResult;
+    }
+
+    public function getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency)
+    {
+        $sCode = $this->_getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_code_subscription', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'subscription' => $sSubscription,
+            'currency' => $sCurrency,
+            'override_result' => &$sCode
+        ));
+
+        return $sCode;
+    }
+    
+    public function checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult)
+    {
+        $bCheckResult = $this->_checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_verify_subscription', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'result' => $aResult,
+            'override_result' => &$bCheckResult
+        ));
+
+        return $bCheckResult;
+    }
+
     protected function _createToken($aCard)
     {
         try {
@@ -215,7 +277,7 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
                 'vendor' => $aCartInfo['vendor_id'],
                 'client' => $aClient['id'],
                 'product' => $iPendingId,
-                'verification' => $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
+                'verification' => $this->getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
             )
         );
 
@@ -237,7 +299,7 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
             return false;
 
         $aMetadata = $aResult['metadata'];
-        if(empty($aMetadata['verification']) || $aMetadata['verification'] != $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $aResult['amount'], $aResult['currency']))
+        if(!$this->checkVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $aResult))
             return false;
 
         return array(
@@ -270,7 +332,7 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
                 'vendor' => $aCartInfo['vendor_id'],
                 'client' => $aClient['id'],
                 'product' => $iPendingId,
-                'verification' => $this->_getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aItem['name'], $aCartInfo['vendor_currency_code'])
+                'verification' => $this->getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aItem['name'], $aCartInfo['vendor_currency_code'])
             )
         );
 
@@ -292,8 +354,7 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
         if(empty($aResult) || !is_array($aResult) || (!$bTrial && $aResult['status'] != 'active') || ($bTrial && !in_array($aResult['status'], array('active', 'trialing'))))
             return false;
 
-        $aMetadata = $aResult['metadata'];
-        if(empty($aMetadata['verification']) || $aMetadata['verification'] != $this->_getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aResult['plan']['id'], $aResult['plan']['currency']))
+        if(!$this->checkVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aResult))
             return false;
 
         return array(
@@ -376,6 +437,36 @@ class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iB
             'sClientEmail' => $sClientEmail,
         ), $aParams)), $sJsMethod);
     }
+    
+    protected function _getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency)
+    {
+        return md5(implode('#-#', array(
+            (int)$iVendorId,
+            (int)$iCustomerId,
+            (float)$fAmount,
+            strtoupper($sCurrency)
+        )));
+    }
+
+    protected function _checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult)
+    {
+        return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeCharge($iVendorId, $iCustomerId, $aResult['amount'], $aResult['currency']);
+    }
+
+    protected function _getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency)
+    {
+        return md5(implode('#-#', array(
+            (int)$iVendorId,
+            (int)$iCustomerId,
+            strtoupper($sSubscription),
+            strtoupper($sCurrency)
+        )));
+    }
+
+    protected function _checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult)
+    {
+        return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult['plan']['id'], $aResult['plan']['currency']);
+    }
 }
 
 /** @} */

+ 0 - 18
modules/boonex/payment/classes/BxPaymentProviderStripeBasic.php

@@ -603,24 +603,6 @@ class BxPaymentProviderStripeBasic extends BxBaseModPaymentProvider
         return $aResult;
     }
 
-    protected function _getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency) {
-        return md5(implode('#-#', array(
-            (int)$iVendorId,
-            (int)$iCustomerId,
-            (float)$fAmount,
-            strtoupper($sCurrency)
-        )));
-    }
-
-    protected function _getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency) {
-        return md5(implode('#-#', array(
-            (int)$iVendorId,
-            (int)$iCustomerId,
-            strtoupper($sSubscription),
-            strtoupper($sCurrency)
-        )));
-    }
-
     protected function _getButton($sType, $iClientId, $iVendorId, $aParams = array())
     {
         list($sJsCode, $sJsMethod) = $this->_getButtonJs($sType, $iClientId, $iVendorId, $aParams);

+ 98 - 5
modules/boonex/payment/classes/BxPaymentProviderStripeV3.php

@@ -234,6 +234,85 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
         );
     }
 
+    public function createTax($sName, $fPercentage, $bInclusive = false)
+    {
+        $oTax = null;
+        $aTax = [
+            'display_name' => $sName,
+            'percentage' => $fPercentage,
+            'inclusive' => $bInclusive,
+        ];
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_tax', 0, false, array(
+            'tax_object' => &$oTax, 
+            'tax_params' => &$aTax
+        ));
+
+        try {
+            if(empty($oTax))
+                $oTax = $this->_getStripe()->taxRates->create($aTax);
+        }
+        catch (Exception $oException) {
+            return $this->_processException('Create Tax Error: ', $oException);
+        }
+
+        $aResult = $oTax->jsonSerialize();
+        if(empty($aResult) || !is_array($aResult))
+            return false;
+
+        return $aResult['id'];
+    }
+
+    public function retrieveTax($sId)
+    {
+        $oTax = null;
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_retrieve_tax', 0, false, array(
+            'tax_id' => &$sId,
+            'tax_object' => &$oTax
+        ));
+
+        try {
+            if(empty($oTax))
+                $oTax = $this->_getStripe()->taxRates->retrieve($sId);
+        }
+        catch (Exception $oException) {
+            return $this->_processException('Retrieve Tax Error: ', $oException);
+        }
+
+        return $oTax;
+    }
+
+    public function getVerificationCodeSession($iVendorId, $iCustomerId, $fAmount, $sCurrency)
+    {
+        $sCode = $this->_getVerificationCodeSession($iVendorId, $iCustomerId, $fAmount, $sCurrency);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_code_session', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'amount' => $fAmount,
+            'currency' => $sCurrency,
+            'override_result' => &$sCode
+        ));
+
+        return $sCode;
+    }
+
+    public function checkVerificationCodeSession($iVendorId, $iCustomerId, $aResult)
+    {
+        $bCheckResult = $this->_checkVerificationCodeSession($iVendorId, $iCustomerId, $aResult);
+
+        bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_verify_session', 0, false, array(
+            'provider' => $this,
+            'vendor_id' => $iVendorId, 
+            'customer_id' => $iCustomerId,
+            'result' => $aResult,
+            'override_result' => &$bCheckResult
+        ));
+
+        return $bCheckResult;
+    }
+
     protected function _getStripe()
     {
         if(empty($this->_oStripe))
@@ -297,7 +376,7 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                         'quantity' => $aItem['quantity'],
                     );
 
-                    $aMetaItems[] = array('m' => $aItem['module_id'], 'i' => $aItem['id']);
+                    $aMetaItems[] = array('v' => $aCartInfo['vendor_id'], 'c' => $aClient['id'], 'm' => $aItem['module_id'], 'i' => $aItem['id']);
                 }
                 break;
 
@@ -310,7 +389,7 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                         'quantity' => $aItem['quantity'],
                     );
 
-                    $aMetaItems[] = array('m' => $aItem['module_id'], 'i' => $aItem['id']);
+                    $aMetaItems[] = array('v' => $aCartInfo['vendor_id'], 'c' => $aClient['id'], 'm' => $aItem['module_id'], 'i' => $aItem['id']);
                 }
                 break;
         }
@@ -328,7 +407,7 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                 'client' => $aClient['id'],
                 'type' => $sType, 
                 'items' => serialize($aMetaItems),
-                'verification' => $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
+                'verification' => $this->getVerificationCodeSession($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
             )
         );
 
@@ -349,8 +428,7 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
         if(empty($aResult) || !is_array($aResult))
             return false;
 
-        $aMetadata = $aResult['metadata'];
-        if(empty($aMetadata['verification']) || $aMetadata['verification'] != $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], (float)$aResult['amount_total'], $aResult['currency']))
+        if(!$this->checkVerificationCodeSession($aCartInfo['vendor_id'], $aClient['id'], $aResult))
             return false;
 
         return $aResult['id'];
@@ -495,6 +573,21 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
         ), $aParams)), $sJsMethod);
     }
 
+    protected function _getVerificationCodeSession($iVendorId, $iCustomerId, $fAmount, $sCurrency)
+    {
+        return md5(implode('#-#', array(
+            (int)$iVendorId,
+            (int)$iCustomerId,
+            (float)$fAmount,
+            strtoupper($sCurrency)
+        )));
+    }
+
+    protected function _checkVerificationCodeSession($iVendorId, $iCustomerId, $aResult)
+    {
+        return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeSession($iVendorId, $iCustomerId, $aResult['amount_total'], $aResult['currency']);
+    }
+
     protected function _processException($sMessage, &$oException)
     {
         if(method_exists($oException, 'getError')) {