From bf72204950093ebf07c6f7f003ea52595d64d662 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 5 Jun 2016 18:53:03 -0600 Subject: [PATCH] CRM-18661 further fix to ensure payment processor account is used --- CRM/Contribute/BAO/Contribution.php | 8 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 102 ++++++++++++++++++ tests/phpunit/api/v3/ContributionPageTest.php | 4 + tests/phpunit/api/v3/ContributionTest.php | 101 ----------------- 4 files changed, 112 insertions(+), 103 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 3468a8370e..b98c50405a 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -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; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index d6e83bb63c..08f91f733a 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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); + } + } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index a0bb32d67c..2783718535 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -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'); } /** diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 1c439bab9e..07a06a2c8b 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -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. * -- 2.25.1