CRM-18661 further fix to ensure payment processor account is used
authoreileen <emcnaughton@wikimedia.org>
Mon, 6 Jun 2016 00:53:03 +0000 (18:53 -0600)
committereileen <emcnaughton@wikimedia.org>
Mon, 6 Jun 2016 01:09:51 +0000 (19:09 -0600)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContributionPageTest.php
tests/phpunit/api/v3/ContributionTest.php

index 3468a8370ef53f0c88ce211ea57b4e769ecae472..b98c50405a0c8dd4cb35f52abfee5347ae381185 100644 (file)
@@ -2084,7 +2084,6 @@ LEFT JOIN  civicrm_contribution contribution ON ( componentPayment.contribution_
       if (!empty($input['amount'])) {
         $contribution->total_amount = $contributionParams['total_amount'] = $input['amount'];
       }
-      $contributionParams['payment_processor'] = $input['payment_processor'] = $paymentProcessorID;
 
       if (!empty($contributionParams['contribution_recur_id'])) {
         $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', array(
@@ -3120,6 +3119,10 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       }
       elseif (!empty($params['payment_processor'])) {
         $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($params['payment_processor'], 'civicrm_payment_processor', 'financial_account_id');
+        $params['payment_instrument_id'] = civicrm_api3('PaymentProcessor', 'getvalue', array(
+          'id' => $params['payment_processor'],
+          'return' => 'payment_instrument_id',
+        ));
       }
       elseif (!empty($params['payment_instrument_id'])) {
         $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($params['payment_instrument_id']);
@@ -3146,7 +3149,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
         'currency' => $params['contribution']->currency,
         'trxn_id' => $params['contribution']->trxn_id,
         'status_id' => $statusId,
-        'payment_instrument_id' => $params['contribution']->payment_instrument_id,
+        'payment_instrument_id' => CRM_Utils_Array::value('payment_instrument_id', $params, $params['contribution']->payment_instrument_id),
         'check_number' => CRM_Utils_Array::value('check_number', $params),
       );
       if ($contributionStatus == 'Refunded' || $contributionStatus == 'Chargeback') {
@@ -4397,6 +4400,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
       'source' => self::getRecurringContributionDescription($contribution, $event),
     ), array_intersect_key($input, array_fill_keys($inputContributionWhiteList, 1)
     ));
+    $contributionParams['payment_processor'] = $input['payment_processor'] = $paymentProcessorId;
 
     if ($recurringContributionID) {
       $contributionParams['contribution_recur_id'] = $recurringContributionID;
index d6e83bb63cc715968df19f6e22464fe9613f32fa..08f91f733a28b7acfd0661086783a11f80e3243b 100644 (file)
@@ -3469,4 +3469,106 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     CRM_Contact_BAO_Group::getPermissionClause(TRUE);
   }
 
+  /**
+   * @param array $params
+   * @param $context
+   */
+  public function _checkFinancialRecords($params, $context) {
+    $entityParams = array(
+      'entity_id' => $params['id'],
+      'entity_table' => 'civicrm_contribution',
+    );
+    $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
+    $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
+    if ($context == 'pending') {
+      $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
+      $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
+      return;
+    }
+    $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
+    $trxnParams = array(
+      'id' => $trxn['financial_trxn_id'],
+    );
+    if ($context != 'online' && $context != 'payLater') {
+      $compareParams = array(
+        'to_financial_account_id' => 6,
+        'total_amount' => CRM_Utils_Array::value('total_amount', $params, 100),
+        'status_id' => 1,
+      );
+    }
+    if ($context == 'feeAmount') {
+      $compareParams['fee_amount'] = 50;
+    }
+    elseif ($context == 'online') {
+      $compareParams = array(
+        'to_financial_account_id' => 12,
+        'total_amount' => CRM_Utils_Array::value('total_amount', $params, 100),
+        'status_id' => 1,
+        'payment_instrument_id' => 1,
+      );
+    }
+    elseif ($context == 'payLater') {
+      $compareParams = array(
+        'to_financial_account_id' => 7,
+        'total_amount' => CRM_Utils_Array::value('total_amount', $params, 100),
+        'status_id' => 2,
+      );
+    }
+    $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
+    $entityParams = array(
+      'financial_trxn_id' => $trxn['financial_trxn_id'],
+      'entity_table' => 'civicrm_financial_item',
+    );
+    $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
+    $fitemParams = array(
+      'id' => $entityTrxn['entity_id'],
+    );
+    $compareParams = array(
+      'amount' => CRM_Utils_Array::value('total_amount', $params, 100),
+      'status_id' => 1,
+      'financial_account_id' => 1,
+    );
+    if ($context == 'payLater') {
+      $compareParams = array(
+        'amount' => CRM_Utils_Array::value('total_amount', $params, 100),
+        'status_id' => 3,
+        'financial_account_id' => 1,
+      );
+    }
+    $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
+    if ($context == 'feeAmount') {
+      $maxParams = array(
+        'entity_id' => $params['id'],
+        'entity_table' => 'civicrm_contribution',
+      );
+      $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
+      $trxnParams = array(
+        'id' => $maxTrxn['financial_trxn_id'],
+      );
+      $compareParams = array(
+        'to_financial_account_id' => 5,
+        'from_financial_account_id' => 6,
+        'total_amount' => 50,
+        'status_id' => 1,
+      );
+      $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
+      $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
+      $fitemParams = array(
+        'entity_id' => $trxnId['financialTrxnId'],
+        'entity_table' => 'civicrm_financial_trxn',
+      );
+      $compareParams = array(
+        'amount' => 50,
+        'status_id' => 1,
+        'financial_account_id' => 5,
+      );
+      $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
+    }
+    // This checks that empty Sales tax rows are not being created. If for any reason it needs to be removed the
+    // line should be copied into all the functions that call this function & evaluated there
+    // Be really careful not to remove or bypass this without ensuring stray rows do not re-appear
+    // when calling completeTransaction or repeatTransaction.
+    $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
+  }
+
 }
index a0bb32d67cb38919c405e72b6174fa4b981057da..27837185353094f02140e9bba8de3dbc46b832b1 100644 (file)
@@ -281,6 +281,10 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals(10, $contribution['total_amount']);
     $this->assertEquals(.85, $contribution['fee_amount']);
     $this->assertEquals(9.15, $contribution['net_amount']);
+    $this->_checkFinancialRecords(array(
+      'id' => $contribution['id'],
+      'total_amount' => $contribution['total_amount'],
+    ), 'online');
   }
 
   /**
index 1c439bab9e397224fc0b182b5fb128840bdd8b6a..07a06a2c8bae8f30099402a2278df63338f4ed59 100644 (file)
@@ -2791,107 +2791,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     return $optionValue['values'][$optionValue['id']]['value'];
   }
 
-  /**
-   * @param array $params
-   * @param $context
-   */
-  public function _checkFinancialRecords($params, $context) {
-    $entityParams = array(
-      'entity_id' => $params['id'],
-      'entity_table' => 'civicrm_contribution',
-    );
-    $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
-    $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
-    if ($context == 'pending') {
-      $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
-      $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
-      return;
-    }
-    $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
-    $trxnParams = array(
-      'id' => $trxn['financial_trxn_id'],
-    );
-    if ($context != 'online' && $context != 'payLater') {
-      $compareParams = array(
-        'to_financial_account_id' => 6,
-        'total_amount' => 100,
-        'status_id' => 1,
-      );
-    }
-    if ($context == 'feeAmount') {
-      $compareParams['fee_amount'] = 50;
-    }
-    elseif ($context == 'online') {
-      $compareParams = array(
-        'to_financial_account_id' => 12,
-        'total_amount' => 100,
-        'status_id' => 1,
-      );
-    }
-    elseif ($context == 'payLater') {
-      $compareParams = array(
-        'to_financial_account_id' => 7,
-        'total_amount' => 100,
-        'status_id' => 2,
-      );
-    }
-    $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
-    $entityParams = array(
-      'financial_trxn_id' => $trxn['financial_trxn_id'],
-      'entity_table' => 'civicrm_financial_item',
-    );
-    $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
-    $fitemParams = array(
-      'id' => $entityTrxn['entity_id'],
-    );
-    $compareParams = array(
-      'amount' => 100,
-      'status_id' => 1,
-      'financial_account_id' => 1,
-    );
-    if ($context == 'payLater') {
-      $compareParams = array(
-        'amount' => 100,
-        'status_id' => 3,
-        'financial_account_id' => 1,
-      );
-    }
-    $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
-    if ($context == 'feeAmount') {
-      $maxParams = array(
-        'entity_id' => $params['id'],
-        'entity_table' => 'civicrm_contribution',
-      );
-      $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
-      $trxnParams = array(
-        'id' => $maxTrxn['financial_trxn_id'],
-      );
-      $compareParams = array(
-        'to_financial_account_id' => 5,
-        'from_financial_account_id' => 6,
-        'total_amount' => 50,
-        'status_id' => 1,
-      );
-      $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
-      $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
-      $fitemParams = array(
-        'entity_id' => $trxnId['financialTrxnId'],
-        'entity_table' => 'civicrm_financial_trxn',
-      );
-      $compareParams = array(
-        'amount' => 50,
-        'status_id' => 1,
-        'financial_account_id' => 5,
-      );
-      $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
-    }
-    // This checks that empty Sales tax rows are not being created. If for any reason it needs to be removed the
-    // line should be copied into all the functions that call this function & evaluated there
-    // Be really careful not to remove or bypass this without ensuring stray rows do not re-appear
-    // when calling completeTransaction or repeatTransaction.
-    $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
-  }
-
   /**
    * Set up the basic recurring contribution for tests.
    *