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

Ticket #423 - Related changes in Payments module.

AntonLV 9 лет назад
Родитель
Сommit
b2f4abc02d

+ 18 - 0
inc/classes/BxDolPayments.php

@@ -145,6 +145,24 @@ class BxDolPayments extends BxDolFactory implements iBxDolSingleton
 		return BxDolService::call($this->_sActive, 'get_orders_count_new', $aSrvParams, 'Orders');
     }
 
+    public function getOrdersInfo($aConditions)
+    {
+    	if(!BxDolRequest::serviceExists($this->_sActive, 'get_orders_info', 'Orders'))
+    		return array();
+
+		$aSrvParams = array($aConditions);
+		return BxDolService::call($this->_sActive, 'get_orders_info', $aSrvParams, 'Orders');
+    }
+
+    public function getPendingOrdersInfo($aConditions)
+    {
+    	if(!BxDolRequest::serviceExists($this->_sActive, 'get_pending_orders_info', 'Orders'))
+    		return array();
+
+		$aSrvParams = array($aConditions);
+		return BxDolService::call($this->_sActive, 'get_pending_orders_info', $aSrvParams, 'Orders');
+    }
+
     public function getCartUrl($iVendor = 0)
     {
     	if(!BxDolRequest::serviceExists($this->_sActive, 'get_cart_url', 'Cart'))

+ 7 - 3
modules/boonex/payment/classes/BxPaymentDb.php

@@ -217,6 +217,12 @@ class BxPaymentDb extends BxBaseModPaymentDb
             	$sWhereClause = " AND `order`=:order";
                 $sLimitClause = " LIMIT 1";
                 break;
+
+            case 'mixed':
+                $aMethod['name'] = 'getAll';
+
+                $sWhereClause = ' AND ' . $this->arrayToSQL($aParams['conditions'], ' AND ');
+                break;
         }
 
         $aMethod['params'][0] = "SELECT * FROM `" . $this->_sPrefix . "transactions_pending` WHERE 1 " . $sWhereClause . $sLimitClause;
@@ -301,10 +307,8 @@ class BxPaymentDb extends BxBaseModPaymentDb
 
             case 'mixed':
                 $aMethod['name'] = 'getAll';
-                $aMethod['params'][1] = $aParams['conditions'];
 
-                foreach($aParams['conditions'] as $sKey => $sValue)
-                    $sWhereClause .= " AND `tt`.`" . $sKey . "`=:" . $sKey;
+                $sWhereClause = ' AND ' . $this->arrayToSQL($aParams['conditions'], ' AND ');
                 break;
 
         }

+ 2 - 21
modules/boonex/payment/classes/BxPaymentModule.php

@@ -58,8 +58,8 @@ define('BX_PAYMENT_RESULT_SUCCESS', 0);
  * BxDolService::call('payment', 'get_add_to_cart_link', array($iSellerId, $mixedModuleId, $iItemId, $iItemCount));
  *
  * Check transaction(s) in database which satisty all conditions.
- * @see BxPmtModule::serviceGetTransactionsInfo
- * BxDolService::call('payment', 'get_transactions_info', array($aConditions));
+ * @see BxPmtModule::serviceGetOrdersInfo
+ * BxDolService::call('payment', 'get_orders_info', array($aConditions), 'Orders');
  *
  * Get total count of items in Shopping Cart.
  * @see BxPmtModule::serviceGetCartItemsCount
@@ -207,25 +207,6 @@ class BxPaymentModule extends BxBaseModPaymentModule
         return $aResult;
     }
 
-    /**
-     * Check transaction(s) in database which satisty all conditions.
-     *
-     * @param array $aConditions an array of pears('key' => 'value'). Available keys are the following:
-     * a. license - internal license (string)
-     * b. client_id - client's ID (integer)
-     * c. seller_id - seller's ID (integer)
-     * d. module_id - modules's where the purchased product is located. (integer)
-     * e. item_id - item id in the database. (integer)
-     * f. date - the date when the payment was processed(UNIXTIME STAMP)
-     *
-     * @return array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
-     */
-    public function serviceGetTransactionsInfo($aConditions)
-    {
-        return $this->_oDb->getOrderProcessed(array('type' => 'mixed', 'conditions' => $aConditions));
-    }
-
-
     /**
      * Cart Processing Methods
      */

+ 44 - 0
modules/boonex/payment/classes/BxPaymentOrders.php

@@ -42,6 +42,50 @@ class BxPaymentOrders extends BxBaseModPaymentOrders
         );
     }
 
+    /**
+     * Check transaction(s) in database which satisty all conditions.
+     *
+     * @param array $aConditions an array of pears('key' => 'value'). Available keys are the following:
+     * a. license - internal license (string)
+     * b. client_id - client's ID (integer)
+     * c. seller_id - seller's ID (integer)
+     * d. module_id - modules's where the purchased product is located. (integer)
+     * e. item_id - item id in the database. (integer)
+     * f. date - the date when the payment was processed(UNIXTIME STAMP)
+     *
+     * @return array of transactions. Each transaction has full info(client ID, seller ID, external transaction ID, date and so on)
+     */
+    public function serviceGetOrdersInfo($aConditions)
+    {
+        if(empty($aConditions) || !is_array($aConditions))
+            return array();
+
+        return $this->_oModule->_oDb->getOrderProcessed(array('type' => 'mixed', 'conditions' => $aConditions));
+    }
+
+    /**
+     * Check pending transaction(s) in database which satisty all conditions.
+     *
+     * @param array $aConditions an array of pears('key' => 'value'). The most useful keys are the following:
+     * a. client_id - client's ID (integer)
+     * b. seller_id - seller's ID (integer)
+     * c. type - transaction type: single or recurring (string)
+     * d. amount - transaction amount (float)
+     * e. order - order ID received from payment provider (string)
+     * f. provider - payment provider name (string)
+     * g. date - the date when the payment was established(UNIXTIME STAMP)
+     * h. processed - whether the payment was processed or not (integer, 0 or 1)
+     *
+     * @return array of pending transactions. Each transaction has full info(client ID, seller ID, type, date and so on)
+     */
+    public function serviceGetPendingOrdersInfo($aConditions)
+    {
+        if(empty($aConditions) || !is_array($aConditions))
+            return array();
+
+        return $this->_oModule->_oDb->getOrderPending(array('type' => 'mixed', 'conditions' => $aConditions));
+    }
+
 	public function addOrder($aData)
     {
         $iSellerId = isset($aData['seller_id']) ? (int)$aData['seller_id'] : $this->_oModule->getProfileId();

+ 98 - 51
modules/boonex/payment/classes/BxPaymentProviderStripe.php

@@ -1,5 +1,4 @@
-<?php use Symfony\Component\Finder\Tests\FakeAdapter\DummyAdapter;
-defined('BX_DOL') or die('hack attempt');
+<?php defined('BX_DOL') or die('hack attempt');
 /**
  * Copyright (c) UNA, Inc - https://una.io
  * MIT License - https://opensource.org/licenses/MIT
@@ -150,7 +149,7 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 
 		switch($aPending['type']) {
 			case BX_PAYMENT_TYPE_SINGLE:
-			    $oCustomer = $this->_retrieveCustomer($sCustomerId);
+			    $oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_SINGLE, $sCustomerId);
 				$oCharge = $this->_retrieveCharge($sOrderId);
 				if($oCustomer === false || $oCharge === false)
 				    return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
@@ -168,7 +167,7 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 				break;
 
 			case BX_PAYMENT_TYPE_RECURRING:
-			    $oCustomer = $this->_retrieveCustomer($sCustomerId);
+			    $oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_RECURRING, $sCustomerId);
 				$oSubscription = $this->_retrieveSubscription($sCustomerId, $sOrderId);
 				if($oCustomer === false || $oSubscription === false)
 					return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
@@ -345,11 +344,11 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
             if(empty($aToken) || !is_array($aToken))
                 return $aResultError;
 
-            $aCard = $this->_createCard($sCustomerId, $aToken['id']);
+            $aCard = $this->_createCard(BX_PAYMENT_TYPE_RECURRING, $sCustomerId, $aToken['id']);
             if(empty($aCard) || !is_array($aCard))
                 return $aResultError;
 
-            $oCustomer = $this->_retrieveCustomer($sCustomerId);
+            $oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_RECURRING, $sCustomerId);
             if($oCustomer === false)
                 return $aResultError;
 
@@ -396,26 +395,42 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 		return $oToken->jsonSerialize();
 	}
 
-	protected function _createCustomer($sToken, $aClient)
+	protected function _createCustomer($sType, $sToken, $aClient)
 	{
-		try {
-			$aClientParams = array(
-				'card'  => $sToken,
-				'email' => !empty($aClient['email']) ? $aClient['email'] : ''
-			);
+	    $oCustomer = null;
+	    $aCustomer = array(
+			'card' => $sToken,
+			'email' => !empty($aClient['email']) ? $aClient['email'] : ''
+		);
+
+	    bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_customer', 0, $aClient['id'], array(
+	    	'type' => $sType,
+			'customer_object' => &$oCustomer, 
+			'customer_params' => &$aCustomer
+		));
 
-			$this->_oCustomer = \Stripe\Customer::create($aClientParams);
+		try {
+			$this->_oCustomer = !empty($oCustomer) ? $oCustomer : \Stripe\Customer::create($aCustomer);
 		}
-		catch (Stripe\Error\Base $oException) {
+		catch (Exception $oException) {
 			return $this->_processException('Create Customer Error: ', $oException);
 		}
 
 		return $this->_oCustomer->jsonSerialize();
 	}
 
-	protected function _retrieveCustomer($sId) {
+	protected function _retrieveCustomer($sType, $sId)
+	{
+	    $oCustomer = null;
+	    bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_retrieve_customer', 0, false, array(
+	    	'type' => $sType,
+	    	'customer_id' => &$sId,
+			'customer_object' => &$oCustomer
+		));
+
 		try {
-			$oCustomer = \Stripe\Customer::retrieve($sId);
+		    if(empty($oCustomer))
+			    $oCustomer = \Stripe\Customer::retrieve($sId);
 		}
 		catch (Exception $oException) {
 			return $this->_processException('Retrieve Customer Error: ', $oException);
@@ -426,26 +441,35 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 
 	protected function _createCharge($sToken, $iPendingId, &$aClient, &$aCartInfo) {
 		if(empty($this->_oCustomer))
-			$this->_createCustomer($sToken, $aClient);
+			$this->_createCustomer(BX_PAYMENT_TYPE_SINGLE, $sToken, $aClient);
 
 		if(empty($this->_oCustomer))
 			return false;
 
 		$fAmount = 100 * (float)$aCartInfo['items_price'];
 
+		$oCharge = null;
+		$aCharge = array(
+			'customer' => $this->_oCustomer->id,
+			'amount' => $fAmount,
+			'currency' => $aCartInfo['vendor_currency_code'],
+			'description' => $aCartInfo['items_title'],
+			'metadata' => array(
+				'vendor' => $aCartInfo['vendor_id'],
+				'client' => $aClient['id'],
+				'product' => $iPendingId,
+				'verification' => $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
+			)
+		);
+
+		bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_charge', $iPendingId, false, array(
+			'charge_object' => &$oCharge, 
+			'charge_params' => &$aCharge
+		));
+
 		try {
-			$oCharge = \Stripe\Charge::create(array(
-				'customer' => $this->_oCustomer->id,
-				'amount' => $fAmount,
-				'currency' => $aCartInfo['vendor_currency_code'],
-				'description' => $aCartInfo['items_title'],
-				'metadata' => array(
-					'vendor' => $aCartInfo['vendor_id'],
-					'client' => $aClient['id'],
-					'product' => $iPendingId,
-					'verification' => $this->_getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
-				)
-			));
+		    if(empty($oCharge))
+			    $oCharge = \Stripe\Charge::create($aCharge);
 		}
 		catch (Exception $oException) {
 			return $this->_processException('Create Charge Error: ', $oException);
@@ -468,8 +492,15 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 	}
 
 	protected function _retrieveCharge($sId) {
+	    $oCharge = null;
+	    bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_retrieve_charge', 0, false, array(
+	    	'charge_id' => &$sId,
+			'charge_object' => &$oCharge
+		));
+
 		try {
-			$oCharge = \Stripe\Charge::retrieve($sId);
+		    if(empty($oCharge))
+			    $oCharge = \Stripe\Charge::retrieve($sId);
 		}
 		catch (Exception $oException) {
 			return $this->_processException('Retrieve Charge Error: ', $oException);
@@ -481,28 +512,38 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 	protected function _createSubscription($sToken, $iPendingId, &$aClient, &$aCartInfo)
 	{
 		if(empty($this->_oCustomer))
-			$this->_createCustomer($sToken, $aClient);
+			$this->_createCustomer(BX_PAYMENT_TYPE_RECURRING, $sToken, $aClient);
 
 		if(empty($this->_oCustomer))
 			return false;
 
+		$aItem = array_shift($aCartInfo['items']);
+		if(empty($aItem) || !is_array($aItem))
+			return false;
+
+		$iTrial = $this->_oModule->_oConfig->getTrial(BX_PAYMENT_TYPE_RECURRING, $aItem);
+		$bTrial = !empty($iTrial);
+
+		$oSubscription = null;
+		$aSubscription = array(
+			'plan' => $aItem['name'],
+			'metadata' => array(
+				'vendor' => $aCartInfo['vendor_id'],
+				'client' => $aClient['id'],
+				'product' => $iPendingId,
+				'verification' => $this->_getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aItem['name'], $aCartInfo['vendor_currency_code'])
+			)
+		);
+
+		bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_subscription', $iPendingId, false, array(
+		    'customer' => &$this->_oCustomer,
+			'subscription_object' => &$oSubscription, 
+			'subscription_params' => &$aSubscription
+		));
+
 		try {
-			$aItem = array_shift($aCartInfo['items']);
-			if(empty($aItem) || !is_array($aItem))
-				return false;
-
-			$iTrial = $this->_oModule->_oConfig->getTrial(BX_PAYMENT_TYPE_RECURRING, $aItem);
-			$bTrial = !empty($iTrial);
-
-			$oSubscription = $this->_oCustomer->subscriptions->create(array(
-				'plan' => $aItem['name'],
-				'metadata' => array(
-					'vendor' => $aCartInfo['vendor_id'],
-					'client' => $aClient['id'],
-					'product' => $iPendingId,
-					'verification' => $this->_getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aItem['name'], $aCartInfo['vendor_currency_code'])
-				)
-			));
+             if(empty($oSubscription))
+    			$oSubscription = $this->_oCustomer->subscriptions->create($aSubscription);
 		}
 		catch (Exception $oException) {
 			return $this->_processException('Create Subscription Error: ', $oException);
@@ -528,7 +569,7 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 	protected function _retrieveSubscription($sCustomerId, $sSubscriptionId)
 	{
 		try {
-			$oCustomer = \Stripe\Customer::retrieve($sCustomerId);
+			$oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_RECURRING, $sCustomerId);
 			$oSubscription = $oCustomer->subscriptions->retrieve($sSubscriptionId);
 		}
 		catch (Exception $oException) {
@@ -550,10 +591,10 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
 		return $oPlans;
 	}
 
-    protected function _createCard($sCustomerId, $sToken)
+    protected function _createCard($sType, $sCustomerId, $sToken)
 	{
 		try {
-			$oCard = $this->_retrieveCustomer($sCustomerId)->sources->create(array(
+			$oCard = $this->_retrieveCustomer($sType, $sCustomerId)->sources->create(array(
             	'source' => $sToken
             ));
 		}
@@ -765,13 +806,19 @@ class BxPaymentProviderStripe extends BxBaseModPaymentProvider implements iBxBas
     			$sClientEmail = $oClient->getAccountObject()->getEmail();
     	}
 
+    	$sPublicKey = '';
+    	bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_button', 0, $iClientId, array(
+			'type' => &$sType, 
+			'public_key' => &$sPublicKey
+		));
+
     	return $this->_oModule->_oTemplate->parseHtmlByName('strp_button_' . $sType . '.html', array(
     		'type' => $sType,
     		'caption' => _t($this->_sLangsPrefix . 'strp_txt_checkout_with_' . $sType, $this->_sCaption),
     		'js_object' => $this->_oModule->_oConfig->getJsObject($this->_sName),
     		'js_code' => $this->_oModule->_oTemplate->getJsCode($this->_sName, array_merge(array(
 	    		'sProvider' => $this->_sName,
-	    		'sPublicKey' => $this->_getPublicKey(),
+	    		'sPublicKey' => !empty($sPublicKey) ? $sPublicKey : $this->_getPublicKey(),
 	    		'sVendorName' => '',
 	    		'sVendorCurrency' => '',
 	    		'sVendorIcon' => '',