//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');
*
* @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'];
}
}
}
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;
+ }
+ }
}
}
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);
}
'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,
+ );
}
/**
$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.
*/