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

Ticket #5006 - Stripe Connect: Update integration. Changes in Paymens.

AntonLV 1 год назад
Родитель
Сommit
58e4f8762c

+ 20 - 2
modules/boonex/payment/classes/BxPaymentConfig.php

@@ -50,6 +50,8 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
 
             'KEY_ARRAY_PRICE_SINGLE' => 'price_single',
             'KEY_ARRAY_PRICE_RECURRING' => 'price_recurring',
+            'KEY_ARRAY_PERIOD_RECURRING' => 'period_recurring',
+            'KEY_ARRAY_PERIOD_UNIT_RECURRING' => 'period_unit_recurring',
             'KEY_ARRAY_TRIAL_RECURRING' => 'trial_recurring',
 
             // some params
@@ -195,6 +197,22 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
         return round((float)$fPrice, $iPrecision);
     }
 
+    public function getPeriod($sType, $aItem)
+    {
+        $mixedResult = false;
+
+        switch($sType) {
+            case BX_PAYMENT_TYPE_SINGLE:
+                break;
+
+            case BX_PAYMENT_TYPE_RECURRING:
+                $mixedResult = [$aItem[$this->getKey('KEY_ARRAY_PERIOD_RECURRING')], $aItem[$this->getKey('KEY_ARRAY_PERIOD_UNIT_RECURRING')]];
+                break;
+        }
+
+        return $mixedResult;
+    }
+
     public function getTrial($sType, $aItem)
     {
         $iTrial = 0;
@@ -227,12 +245,12 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
 
     public function a2s($a)
     {
-        return base64_encode(json_encode($a));
+        return base64_encode(serialize($a));
     }
 
     public function s2a($s)
     {
-        return json_decode(base64_decode($s), true);
+        return unserialize(base64_decode($s));
     }
 
     public function urlEncode($s)

+ 31 - 7
modules/boonex/payment/classes/BxPaymentProviderStripeBasic.php

@@ -575,12 +575,12 @@ class BxPaymentProviderStripeBasic extends BxBaseModPaymentProvider
         if($mixedResult === false)
             return false;
 
-        list($mixedPending, $oCharge) = $mixedResult;
-        if(empty($mixedPending) || empty($oCharge))
+        list($mixedPending, $aPaid) = $mixedResult;
+        if(empty($mixedPending) || empty($aPaid))
             return false;
 
-        $fChargeAmount = (float)$oCharge->amount / 100;
-        $sChargeCurrency = strtoupper($oCharge->currency);
+        $fChargeAmount = (float)$aPaid['amount'] / 100;
+        $sChargeCurrency = strtoupper($aPaid['currency']);
 
         if(is_string($mixedPending))
             return $this->_oModule->_oDb->insertStrpPaymentPending($mixedPending, $fChargeAmount, $sChargeCurrency);
@@ -660,12 +660,36 @@ class BxPaymentProviderStripeBasic extends BxBaseModPaymentProvider
         if(empty($oEvent))
             return false;
 
-        $sSubscriptionId = $oEvent->data->object->subscription;
+        /**
+         * Note. $oEvent->data->object->subscription and $oEvent->data->object->charge 
+         * are checked for backward compatibility.
+         */
+        $sSubscriptionId = '';
+        if(!empty($oEvent->data->object->subscription))
+            $sSubscriptionId = $oEvent->data->object->subscription;
+        else if(!empty($oEvent->data->object->parent) && $oEvent->data->object->parent->type == 'subscription_details')
+            $sSubscriptionId = $oEvent->data->object->parent->subscription_details->subscription;
+        if(!$sSubscriptionId)
+            return false;
 
         $aPending = $this->_oModule->_oDb->getOrderPending(['type' => 'order', 'order' => $sSubscriptionId]);
-        $oCharge = $this->_retrieveCharge($oEvent->data->object->charge);
 
-        return [!empty($aPending) && is_array($aPending) ? $aPending : $sSubscriptionId, $oCharge];
+        $aPaid = [];
+        if(!empty($oEvent->data->object->charge)) {
+            $oCharge = $this->_retrieveCharge($oEvent->data->object->charge);
+            if($oCharge)
+                $aPaid = [
+                    'amount' => $oCharge->amount,
+                    'currency' => $oCharge->currency
+                ];
+        }
+        else if(!empty($oEvent->data->object->amount_paid) && !empty($oEvent->data->object->currency))
+            $aPaid = [
+                'amount' => $oEvent->data->object->amount_paid,
+                'currency' => $oEvent->data->object->currency
+            ];            
+
+        return [!empty($aPending) && is_array($aPending) ? $aPending : $sSubscriptionId, $aPaid];
     }
 
     protected function _getDataChangeDetailsRecurring($iPendingId)

+ 79 - 54
modules/boonex/payment/classes/BxPaymentProviderStripeV3.php

@@ -631,42 +631,12 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                 $sMode = 'payment';
 
                 foreach($aCartInfo['items'] as $aItem) {
-                    $aProductData = [
-                        'name' => $aItem['title']
-                    ];
-                    if(!empty($aItem['description']))
-                        $aProductData['description'] = strmaxtextlen(strip_tags($aItem['description']), 60, '...');
-
-                    $aLineItems[] = [
-                        'price_data' => [
-                            'currency' => $aCartInfo['vendor_currency_code'],
-                            'product_data' => $aProductData,
-                            'unit_amount' => 100 * $this->_oModule->_oConfig->getPrice($sType, $aItem, $this->_iAmountPrecision),
-                        ],
-                        'quantity' => $aItem['quantity'],
-                    ];
+                    list($_aLineItems, $_aMetaItems) = $this->_getLineItems($sType, $aItem, [
+                        'vendor_currency_code' => $aCartInfo['vendor_currency_code']
+                    ]);
 
-                    $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aItem['module_id'], $aItem['id']]);
-
-                    if(!empty($aItem['addons']) && is_array($aItem['addons']))
-                        foreach($aItem['addons'] as $aAddon) {
-                            $aAddonData = [
-                                'name' => $aAddon['title']
-                            ];
-                            if(!empty($aAddon['description']))
-                                $aAddonData['description'] = strmaxtextlen(strip_tags($aAddon['description']), 60, '...');
-
-                            $aLineItems[] = [
-                                'price_data' => [
-                                    'currency' => $aCartInfo['vendor_currency_code'],
-                                    'product_data' => $aAddonData,
-                                    'unit_amount' => 100 * $this->_oModule->_oConfig->getPrice($sType, $aAddon, $this->_iAmountPrecision),
-                                ],
-                                'quantity' => $aAddon['quantity'],
-                            ];
-
-                            $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aAddon['module_id'], $aAddon['id']]);
-                        }
+                    $aLineItems = array_merge($aLineItems, $_aLineItems);
+                    $aMetaItems = array_merge($aMetaItems, $_aMetaItems);
                 }
 
                 $aParams['customer_creation'] = 'always';
@@ -677,25 +647,15 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                 $iTrial = 0;
 
                 foreach($aCartInfo['items'] as $aItem) {
-                    $aLineItems[] = [
-                        'price' => $aItem['name'],
-                        'quantity' => $aItem['quantity'],
-                    ];
+                    list($_aLineItems, $_aMetaItems) = $this->_getLineItems($sType, $aItem, [
+                        'vendor_currency_code' => $aCartInfo['vendor_currency_code']
+                    ]);
+
+                    $aLineItems = array_merge($aLineItems, $_aLineItems);
+                    $aMetaItems = array_merge($aMetaItems, $_aMetaItems);
 
                     if((int)$aItem['trial_recurring'] > 0)
                         $iTrial = (int)$aItem['trial_recurring'];
-
-                    $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aItem['module_id'], $aItem['id']]);
-                    
-                    if(!empty($aItem['addons']) && is_array($aItem['addons']))
-                        foreach($aItem['addons'] as $aAddon) {
-                            $aLineItems[] = [
-                                'price' => $aAddon['name'],
-                                'quantity' => $aAddon['quantity'],
-                            ];
-
-                            $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aAddon['module_id'], $aAddon['id']]);
-                        }
                 }
 
                 if($iTrial > 0) {
@@ -1139,11 +1099,76 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
         return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeSession($iVendorId, $iCustomerId, $aResult['amount_total'], $aResult['currency']);
     }
 
+    protected function _getLineItems($sType, $aItem, $aParams = [])
+    {
+        $aLineItems = $aMetaItems = [];
+
+        $bRecurring = $sType == BX_PAYMENT_TYPE_RECURRING;
+        $sCurrency = ($sKey = 'vendor_currency_code') && !empty($aParams[$sKey]) ? $aParams[$sKey] : $this->_oModule->_oConfig->getDefaultCurrencyCode();
+
+        $aProductData = [
+            'name' => $aItem['title']
+        ];
+        if(!empty($aItem['description']))
+            $aProductData['description'] = strmaxtextlen(strip_tags($aItem['description']), 60, '...');
+
+        $aLineItem = [
+            'price_data' => [
+                'currency' => $sCurrency,
+                'product_data' => $aProductData,
+                'unit_amount' => 100 * $this->_oModule->_oConfig->getPrice($sType, $aItem, $this->_iAmountPrecision),
+            ],
+            'quantity' => $aItem['quantity'],
+        ];
+        
+        if($bRecurring && ($mixedPeriod = $this->_oModule->_oConfig->getPeriod($sType, $aItem)) !== false) {
+            list($iPeriod, $sPeriodUnit) = $mixedPeriod;
+            $aLineItem['price_data']['recurring'] = [
+                'interval' => $sPeriodUnit,
+                'interval_count' => $iPeriod
+            ];
+        }
+
+        $aLineItems[] = $aLineItem;
+        $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aItem['module_id'], $aItem['id']]);
+
+        if(!empty($aItem['addons']) && is_array($aItem['addons']))
+            foreach($aItem['addons'] as $aAddon) {
+                $aAddonData = [
+                    'name' => $aAddon['title']
+                ];
+                if(!empty($aAddon['description']))
+                    $aAddonData['description'] = strmaxtextlen(strip_tags($aAddon['description']), 60, '...');
+
+                $aLineItem = [
+                    'price_data' => [
+                        'currency' => $sCurrency,
+                        'product_data' => $aAddonData,
+                        'unit_amount' => 100 * $this->_oModule->_oConfig->getPrice($sType, $aAddon, $this->_iAmountPrecision),
+                    ],
+                    'quantity' => $aAddon['quantity'],
+                ];
+                
+                if($bRecurring && ($mixedPeriod = $this->_oModule->_oConfig->getPeriod($sType, $aAddon)) !== false) {
+                    list($iPeriod, $sPeriodUnit) = $mixedPeriod;
+                    $aLineItem['price_data']['recurring'] = [
+                        'interval' => $sPeriodUnit,
+                        'interval_count' => $iPeriod
+                    ];
+                }
+
+                $aLineItems[] = $aLineItem;
+                $aMetaItems[] = $this->_oModule->_oConfig->descriptorA2S([$aAddon['module_id'], $aAddon['id']]);
+            }
+
+        return [$aLineItems, $aMetaItems];
+    }
+
     protected function _processException($sMessage, &$oException)
     {
-        if(method_exists($oException, 'getError')) {
-            $sError = $oException->getError()->message;
-            $aError = $oException->getError()->toArray();
+        if(method_exists($oException, 'getError') && ($oError = $oException->getError())) {
+            $sError = $oError->message;
+            $aError = $oError->toArray();
         }
         else { 
             $sError = $oException->getMessage();

+ 0 - 20
modules/boonex/stripe_connect/classes/BxStripeConnectPage.php

@@ -1,20 +0,0 @@
-<?php defined('BX_DOL') or die('hack attempt');
-/**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup    StripeConnect Stripe Connect
- * @ingroup     TridentModules
- *
- * @{
- */
-
-class BxStripeConnectPage extends BxTemplPage
-{
-    public function __construct($aObject, $oTemplate = false)
-    {
-        parent::__construct($aObject, $oTemplate);
-    }
-}
-
-/** @} */

+ 0 - 32
modules/boonex/stripe_connect/classes/BxStripeConnectStudioOptions.php

@@ -1,32 +0,0 @@
-<?php defined('BX_DOL') or die('hack attempt');
-/**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup    StripeConnect Stripe Connect
- * @ingroup     TridentModules
- *
- * @{
- */
-
-class BxStripeConnectStudioOptions extends BxTemplStudioOptions
-{
-    protected $_sModule;
-    protected $_oModule;
-
-    public function __construct($sType = '', $sCategory = '', $sMix = '')
-    {
-        parent::__construct($sType, $sCategory, $sMix);
-
-        $this->_sModule = 'bx_stripe_connect';
-        $this->_oModule = BxDolModule::getInstance($this->_sModule);
-    }
-
-    public function getCustomValueRedirectUrl($aItem, $mixedValue)
-    {
-        $CNF = &$this->_oModule->_oConfig->CNF;
-
-        return BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . $CNF['URI_REDIRECT'];
-    }
-}
-/** @} */