Просмотр исходного кода

Ticket #4092 - Paid Levels: Add an option to immediately start newly purchased level: Changes in Payments.

AntonLV 4 лет назад
Родитель
Сommit
a7e9ea12f8

+ 5 - 8
inc/classes/BxDolPayments.php

@@ -504,16 +504,13 @@ class BxDolPayments extends BxDolFactory implements iBxDolSingleton
         return bx_srv($this->_sActive, 'initialize_checkout', $aSrvParams);
     }
 
-    /**
-     * Isn't used anywhere for now. Is needed when license 'prolong' is requested from license keeping module like Market and ACL.  
-     */
-    public function prolongSubscription($sOrderId)
+    public function cancelSubscription($sOrderId)
     {
-        if(!BxDolRequest::serviceExists($this->_sActive, 'prolong_subscription'))
-            return '';
+        if(!BxDolRequest::serviceExists($this->_sActive, 'cancel', 'Subscriptions'))
+            return false;
 
-        $aSrvParams = array($sOrderId);
-        return bx_srv($this->_sActive, 'prolong_subscription', $aSrvParams);
+        $aSrvParams = [$sOrderId];
+        return bx_srv($this->_sActive, 'cancel', $aSrvParams, 'Subscriptions');
     }
 
     public function processOrder($iSellerId, $iClientId, $iModuleId, $aItems, $sType, $sOrder)

+ 45 - 17
modules/boonex/payment/classes/BxPaymentProviderPayPalApi.php

@@ -206,7 +206,12 @@ class BxPaymentProviderPayPalApi extends BxBaseModPaymentProvider implements iBx
 
     public function getMenuItemsActionsRecurring($iClientId, $iVendorId, $aParams = array())
     {
-        return array();
+        return [];
+    }
+
+    public function cancelRecurring($iPendingId, $sCustomerId, $sSubscriptionId)
+    {
+        return $this->_cancelSubscription($sSubscriptionId);
     }
 
     protected function _finalizeCheckoutSingle(&$aData)
@@ -384,6 +389,21 @@ class BxPaymentProviderPayPalApi extends BxBaseModPaymentProvider implements iBx
         return $mixedResult;
     }
 
+    protected function _cancelSubscription($sSubscriptionId)
+    {
+        list($sCode, $sResponse) = $this->_apiCallAuthorized($this->_sEndpoint . 'v1/billing/subscriptions/' . $sSubscriptionId . '/cancel', [
+            'reason' => 'Not needed anymore'
+        ], [], 'post-json', true);
+
+        if($sCode != 204) {
+            $this->log($mixedResult, 'Cancel Subscription:');
+
+            return false;
+        }
+
+        return true;
+    }
+
     protected function _createProduct($aItem)
     {
         if(empty($aItem['description']))
@@ -521,12 +541,12 @@ class BxPaymentProviderPayPalApi extends BxBaseModPaymentProvider implements iBx
 
     protected function _getToken()
     {
-        $mixedResult = $this->_apiCall($this->_sEndpoint . 'v1/oauth2/token', array(
+        $mixedResult = $this->_apiCallAuthorize($this->_sEndpoint . 'v1/oauth2/token', [
             'grant_type' => 'client_credentials'
-        ), array(), 'post', array(
+        ], [
             'user' => $this->getClientId(),
             'password' => $this->getSecret()
-        ));
+        ]);
 
         if($mixedResult === false || empty($mixedResult['access_token'])) {
             $this->log($mixedResult, 'Get Token:');
@@ -537,32 +557,40 @@ class BxPaymentProviderPayPalApi extends BxBaseModPaymentProvider implements iBx
         return $mixedResult['access_token'];
     }
 
-    protected function _apiCallAuthorized($sEndpoint, $aParams = array(), $aHeaders = array(), $sMethod = 'post-json')
+    protected function _apiCallAuthorize($sEndpoint, $aParams = [], $aBasicAuth = [], $sMethod = 'post')
+    {
+        $sResponse = $this->_apiCall($sEndpoint, $aParams, [], $sMethod, $aBasicAuth);
+
+        return !empty($sResponse) ? json_decode($sResponse, true) : false;
+    }
+
+    protected function _apiCallAuthorized($sEndpoint, $aParams = [], $aHeaders = [], $sMethod = 'post-json', $bRawResponse = false)
     {
         if(!$this->_sToken)
             $this->_sToken = $this->_getToken();
 
-        $aHeaders = array_merge(array(
+        $aHeaders = array_merge([
             'Authorization: Bearer ' . $this->_sToken,
             'Prefer: return=representation'
-        ), $aHeaders);
+        ], $aHeaders);
+
+        $sHttpCode = $bRawResponse ? true : null;
+
+        $sResponse = $this->_apiCall($sEndpoint, $aParams, $aHeaders, $sMethod, [], $sHttpCode);
+        if($bRawResponse)
+            return [$sHttpCode, $sResponse];
 
-        return $this->_apiCall($sEndpoint, $aParams, $aHeaders, $sMethod);
+        return !empty($sResponse) ? json_decode($sResponse, true) : false;
     }
 
-    protected function _apiCall($sEndpoint, $aParams = array(), $aHeaders = array(), $sMethod = 'post-json', $aBasicAuth = array())
+    protected function _apiCall($sEndpoint, $aParams = [], $aHeaders = [], $sMethod = 'post-json', $aBasicAuth = [], &$sHttpCode = null)
     {
-        $sHttpCode = null;
-        $aHeaders = array_merge(array(
+        $aHeaders = array_merge([
             'Accept: application/json',
             'Content-Type: application/json'
-        ), $aHeaders);
-
-        $sResult = bx_file_get_contents($sEndpoint, $aParams, $sMethod, $aHeaders, $sHttpCode, $aBasicAuth);
-        if(empty($sResult))
-            return false;
+        ], $aHeaders);
 
-        return json_decode($sResult, true);
+        return bx_file_get_contents($sEndpoint, $aParams, $sMethod, $aHeaders, $sHttpCode, $aBasicAuth);
     }
 
     protected function _getIntervalUnit($sPeriodUnit)

+ 12 - 0
modules/boonex/payment/classes/BxPaymentSubscriptions.php

@@ -257,6 +257,18 @@ class BxPaymentSubscriptions extends BxBaseModPaymentSubscriptions
 
         return $mixedResult;
     }
+    
+    public function serviceCancel($sOrder)
+    {
+        $aPending = $this->_oModule->_oDb->getOrderPending(['type' => 'order', 'order' => $sOrder]);
+        if(empty($aPending) || !is_array($aPending))
+            return ['code' => 1, 'message' => _t('_bx_payment_err_not_found_pending')];
+        
+        if(!$this->cancel((int)$aPending['id']))
+            return ['code' => 2, 'message' => _t('_bx_payment_err_cannot_perform')];
+
+        return ['code' => 0, 'message' => _t('_bx_payment_msg_successfully_performed')];
+    }
 
     /**
      * @page service Service Calls