From 080a561b00966114e59be738f199736d9cf7442c Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Mon, 24 Aug 2015 09:20:40 +1200 Subject: [PATCH] CRM-17070 add support for fee_amount to completetransaciton --- CRM/Contribute/BAO/Contribution.php | 27 +++++++++++++++++++---- api/v3/Contribution.php | 11 +++++++-- tests/phpunit/api/v3/ContributionTest.php | 22 ++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index ffb02e8458..7921301d8f 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -136,8 +136,8 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { //set defaults in create mode if (!$contributionID) { CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); - self::calculateMissingAmountParams($params); } + self::calculateMissingAmountParams($params, $contributionID); if (!empty($params['payment_instrument_id'])) { $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name'); @@ -259,9 +259,13 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { * * @param array $params * Params for a new contribution before they are saved. + * @param int|null $contributionID + * Contribution ID if we are dealing with an update. + * + * @throws \CiviCRM_API3_Exception */ - public static function calculateMissingAmountParams(&$params) { - if (!isset($params['fee_amount'])) { + public static function calculateMissingAmountParams(&$params, $contributionID) { + if (!$contributionID && !isset($params['fee_amount'])) { if (isset($params['total_amount']) && isset($params['net_amount'])) { $params['fee_amount'] = $params['total_amount'] - $params['net_amount']; } @@ -270,7 +274,22 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } if (!isset($params['net_amount'])) { - $params['net_amount'] = $params['total_amount'] - $params['fee_amount']; + if (!$contributionID) { + $params['net_amount'] = $params['total_amount'] - $params['fee_amount']; + } + else { + if (isset($params['fee_amount']) || isset($params['total_amount'])) { + // We have an existing contribution and fee_amount or total_amount has been passed in but not net_amount. + // net_amount may need adjusting. + $contribution = civicrm_api3('Contribution', 'getsingle', array( + 'id' => $contributionID, + 'return' => array('total_amount', 'net_amount'), + )); + $totalAmount = isset($params['total_amount']) ? $params['total_amount'] : CRM_Utils_Array::value('total_amount', $contribution); + $feeAmount = isset($params['fee_amount']) ? $params['fee_amount'] : CRM_Utils_Array::value('fee_amount', $contribution); + $params['net_amount'] = $totalAmount - $feeAmount; + } + } } } diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 91e663629e..ee3d251e2b 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -427,6 +427,9 @@ function civicrm_api3_contribution_completetransaction(&$params) { throw new API_Exception(ts('Contribution already completed'), 'contribution_completed'); } $input['trxn_id'] = !empty($params['trxn_id']) ? $params['trxn_id'] : $contribution->trxn_id; + if (!empty($params['fee_amount'])) { + $input['fee_amount'] = $params['fee_amount']; + } $params = _ipn_process_transaction($params, $contribution, $input, $ids); } @@ -460,12 +463,16 @@ function _civicrm_api3_contribution_completetransaction_spec(&$params) { 'description' => '. If not provided this will default to domain mail or contribution page', 'type' => CRM_Utils_Type::T_STRING, ); - $params['payment_processor_id'] = array( 'title' => 'Payment processor ID', - 'description' => '. Providing this is strongly recommended, as not possible to calculate it accurately always', + 'description' => 'Providing this is strongly recommended, as not possible to calculate it accurately always', 'type' => CRM_Utils_Type::T_INT, ); + $params['fee_amount'] = array( + 'title' => 'Fee charged on transaction', + 'description' => 'If a fee has been charged then the amount', + 'type' => CRM_Utils_Type::T_FLOAT, + ); } /** diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 34b8a8ae80..1c63c2e08b 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1367,6 +1367,28 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $mut->stop(); } + /** + * Test completing a transaction via the API. + * + * Note that we are creating a logged in user because email goes out from + * that person + */ + public function testCompleteTransactionFeeAmount() { + $this->createLoggedInUser(); + $params = array_merge($this->_params, array('contribution_status_id' => 2)); + $contribution = $this->callAPISuccess('contribution', 'create', $params); + $this->callAPISuccess('contribution', 'completetransaction', array( + 'id' => $contribution['id'], + 'fee_amount' => '.56', + 'trxn_id' => '7778888', + )); + $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1)); + $this->assertEquals('Completed', $contribution['contribution_status']); + $this->assertEquals('7778888', $contribution['trxn_id']); + $this->assertEquals('.56', $contribution['fee_amount']); + $this->assertEquals('99.44', $contribution['net_amount']); + } + /** * Test repeat contribution successfully creates line items. */ -- 2.25.1