CRM-17070 add support for fee_amount to completetransaciton
authoreileenmcnaugton <eileen@fuzion.co.nz>
Sun, 23 Aug 2015 21:20:40 +0000 (09:20 +1200)
committereileenmcnaugton <eileen@fuzion.co.nz>
Sun, 23 Aug 2015 21:28:50 +0000 (09:28 +1200)
CRM/Contribute/BAO/Contribution.php
api/v3/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index ffb02e8458bd00c19d4fc64278e14754ca7775ad..7921301d8fbcf3b1d0b1576d713d6e7b39bc82fb 100644 (file)
@@ -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;
+        }
+      }
     }
   }
 
index 91e663629e8f3af35cafd13021135b510801ce2a..ee3d251e2b1d9896550212b7c36aa14ad6a6d59f 100644 (file)
@@ -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,
+  );
 }
 
 /**
index 34b8a8ae80ecb5b30c9b6f7d5bc7cea71eca9a11..1c63c2e08b60df9f124d0e9eb3fc77893dea9598 100644 (file)
@@ -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.
    */