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

Ticket #4560 - Payments: Pending payment registration for Stripe.

AntonLV пре 2 година
родитељ
комит
61ff1e136e

+ 31 - 0
modules/boonex/payment/classes/BxPaymentDb.php

@@ -803,6 +803,37 @@ class BxPaymentDb extends BxBaseModPaymentDb
 
         return (int)$this->query("DELETE FROM `" . $CNF['TABLE_INVOICES'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
     }
+    
+    /**
+     * For Stripe integration only
+     */
+    public function getStrpPaymentPending($sSubscriptionId)
+    {
+        $aResult = $this->getRow("SELECT * FROM `" . $this->_sPrefix . "stripe_payments_pending` WHERE `subscription_id`=:subscription_id", [
+            'subscription_id' => $sSubscriptionId
+        ]);
+
+        if(!empty($aResult) && is_array($aResult))
+            $this->deleteStrpPaymentPending($sSubscriptionId);
+
+        return $aResult;
+    }
+
+    public function insertStrpPaymentPending($sSubscriptionId, $fAmount, $sCurrency)
+    {
+        return $this->query("INSERT IGNORE INTO `" . $this->_sPrefix . "stripe_payments_pending` SET " . $this->arrayToSQL([
+            'subscription_id' => $sSubscriptionId,
+            'amount' => $fAmount,
+            'currency' => $sCurrency
+        ])) !== false;
+    }
+
+    public function deleteStrpPaymentPending($sSubscriptionId)
+    {
+        return $this->query("DELETE FROM `" . $this->_sPrefix . "stripe_payments_pending` WHERE `subscription_id`=:subscription_id", [
+            'subscription_id' => $sSubscriptionId
+        ]) !== false;
+    }
 }
 
 /** @} */

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

@@ -500,23 +500,27 @@ class BxPaymentProviderStripeBasic extends BxBaseModPaymentProvider
 
     protected function _processEventInvoicePaymentSucceeded(&$aEvent)
     {
-        $mixedResult = $this->_getData($aEvent);
+        $mixedResult = $this->_getDataInvoicePaymentSucceeded($aEvent);
         if($mixedResult === false)
             return false;
 
-        list($aPending, $oCharge) = $mixedResult;
-        if(empty($aPending) || !is_array($aPending) || empty($oCharge))
+        list($mixedPending, $oCharge) = $mixedResult;
+        if(empty($mixedPending) || empty($oCharge))
             return false;
 
         $fChargeAmount = (float)$oCharge->amount / 100;
         $sChargeCurrency = strtoupper($oCharge->currency);
-        if($this->_bCheckAmount && ((float)$aPending['amount'] != $fChargeAmount || strcasecmp($this->_oModule->_oConfig->getDefaultCurrencyCode(), $sChargeCurrency) !== 0))
+
+        if(is_string($mixedPending))
+            return $this->_oModule->_oDb->insertStrpPaymentPending($mixedPending, $fChargeAmount, $sChargeCurrency);
+
+        if($this->_bCheckAmount && ((float)$mixedPending['amount'] != $fChargeAmount || strcasecmp($this->_oModule->_oConfig->getDefaultCurrencyCode(), $sChargeCurrency) !== 0))
             return false;
 
-        if($aPending['type'] == BX_PAYMENT_TYPE_RECURRING)
-            $this->_oModule->getObjectSubscriptions()->prolong($aPending);
+        if($mixedPending['type'] == BX_PAYMENT_TYPE_RECURRING)
+            $this->_oModule->getObjectSubscriptions()->prolong($mixedPending);
 
-        return $this->_oModule->registerPayment($aPending);
+        return $this->_oModule->registerPayment($mixedPending);
     }
 
     protected function _processEventChargeRefunded(&$aEvent)
@@ -575,6 +579,24 @@ class BxPaymentProviderStripeBasic extends BxBaseModPaymentProvider
         return array($aPending, $oCharge);
     }
 
+    protected function _getDataInvoicePaymentSucceeded(&$aEvent, $bRetrieve = true)
+    {
+        if($bRetrieve)
+            $oEvent = $this->_retrieveEvent($aEvent['id']);
+        else 
+            $oEvent = \Stripe\Util\Util::convertToStripeObject($aEvent, array());
+
+        if(empty($oEvent))
+            return false;
+
+        $sSubscriptionId = $oEvent->data->object->subscription;
+
+        $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];
+    }
+
     protected function _getDataChangeDetailsRecurring($iPendingId)
     {
         $aResult = array();

+ 11 - 2
modules/boonex/payment/classes/BxPaymentProviderStripeV3.php

@@ -301,13 +301,22 @@ class BxPaymentProviderStripeV3 extends BxPaymentProviderStripeBasic implements
                 if(empty($aCustomer) || !is_array($aCustomer) || empty($aSubscription) || !is_array($aSubscription))
                     return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
 
-                $aResult = array_merge($aResult, array(
+                $aResult = array_merge($aResult, [
                     'message' => $this->_sLangsPrefix . 'strp_msg_subscribed',
                     'customer_id' => $sCustomerId,
                     'subscription_id' => $sOrderId,
                     'client_email' => $aCustomer['email'],
                     'trial' => $this->isSubscriptionStatus(BX_PAYMENT_SBS_STATUS_TRIAL, $aSubscription)
-                ));
+                ]);
+
+                $aPaymentPending = $this->_oModule->_oDb->getStrpPaymentPending($sOrderId);
+                if(empty($aPaymentPending) || !is_array($aPaymentPending)) 
+                    break;
+
+                if($this->_bCheckAmount && ((float)$aPending['amount'] != (float)$aPaymentPending['amount'] || strcasecmp($this->_oModule->_oConfig->getDefaultCurrencyCode(), $aPaymentPending['currency']) !== 0))
+                    break;
+
+                $aResult['paid'] = true;
                 break;
         }
 

+ 9 - 0
modules/boonex/payment/install/sql/install.sql

@@ -174,6 +174,15 @@ CREATE TABLE IF NOT EXISTS `bx_payment_invoices` (
   UNIQUE KEY `name`(`name`)
 );
 
+CREATE TABLE IF NOT EXISTS `bx_payment_stripe_payments_pending` (
+  `id` int(11) NOT NULL auto_increment,
+  `subscription_id` varchar(32) NOT NULL default '',
+  `amount` float NOT NULL default '0',
+  `currency` varchar(4) NOT NULL default '',
+  PRIMARY KEY(`id`),
+  UNIQUE KEY `subscription_id` (`subscription_id`)
+);
+
 -- Generic payment provider
 INSERT INTO `bx_payment_providers`(`name`, `caption`, `description`, `option_prefix`, `for_visitor`, `for_single`, `for_recurring`, `single_seller`, `time_tracker`, `active`, `order`, `class_name`) VALUES
 ('generic', '_bx_payment_gc_cpt', '_bx_payment_gc_dsc', 'gc_', 0, 0, 0, 0, 0, 1, 0, 'BxPaymentProviderGeneric');

+ 1 - 0
modules/boonex/payment/install/sql/uninstall.sql

@@ -8,6 +8,7 @@ DROP TABLE IF EXISTS `bx_payment_transactions`, `bx_payment_transactions_pending
 DROP TABLE IF EXISTS `bx_payment_subscriptions`, `bx_payment_subscriptions_deleted`;
 DROP TABLE IF EXISTS `bx_payment_modules`;
 DROP TABLE IF EXISTS `bx_payment_commissions`, `bx_payment_invoices`;
+DROP TABLE IF EXISTS `bx_payment_stripe_payments_pending`;
 
 
 -- GRIDS