Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Payment.create to update (recalculate) contribution fee_amount #20008

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CRM_Financial_BAO_Payment {
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function create($params) {
public static function create(array $params): CRM_Financial_DAO_FinancialTrxn {
$contribution = civicrm_api3('Contribution', 'getsingle', ['id' => $params['contribution_id']]);
$contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution['contribution_status_id']);
$isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount'], $contributionStatus);
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function create($params) {
self::reverseAllocationsFromPreviousPayment($params, $trxn->id);
}
else {
list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($params['contribution_id']);
[$ftIds, $taxItems] = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($params['contribution_id']);

foreach ($lineItems as $key => $value) {
if ($value['allocation'] === (float) 0) {
Expand Down Expand Up @@ -185,15 +185,32 @@ public static function create($params) {

/**
* Function to update contribution's check_number and trxn_id by
* concatenating values from financial trxn's check_number and trxn_id respectively
* concatenating values from financial trxn's check_number and trxn_id
* respectively
*
* @param array $params
* @param int $contributionID
*
* @throws \CiviCRM_API3_Exception
*/
public static function updateRelatedContribution($params, $contributionID) {
public static function updateRelatedContribution(array $params, int $contributionID): void {
$contributionDAO = new CRM_Contribute_DAO_Contribution();
$contributionDAO->id = $contributionID;
$contributionDAO->find(TRUE);
if (isset($params['fee_amount'])) {
// Update contribution.fee_amount to be be the total of all fees
// since the payment is already saved the total here will be right.
$payments = civicrm_api3('Payment', 'get', [
'contribution_id' => $contributionID,
'return' => 'fee_amount',
])['values'];
$totalFees = 0;
foreach ($payments as $payment) {
$totalFees += $payment['fee_amount'] ?? 0;
}
$contributionDAO->fee_amount = $totalFees;
$contributionDAO->net_amount = $contributionDAO->total_amount - $contributionDAO->fee_amount;
}

foreach (['trxn_id', 'check_number'] as $fieldName) {
if (!empty($params[$fieldName])) {
Expand Down Expand Up @@ -292,7 +309,7 @@ protected static function loadRelatedEntities($id) {
);

$contactID = self::getPaymentContactID($contributionID);
list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
[$displayName, $email] = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
$entities['contact'] = ['id' => $contactID, 'display_name' => $displayName, 'email' => $email];
$contact = civicrm_api3('Contact', 'getsingle', ['id' => $contactID, 'return' => 'email_greeting']);
$entities['contact']['email_greeting'] = $contact['email_greeting_display'];
Expand Down
6 changes: 5 additions & 1 deletion tests/phpunit/api/v3/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1290,25 +1290,29 @@ protected function checkPaymentIsValid(int $paymentID, int $contributionID, $amo
public function testPaymentCreateTrxnIdAndDates(): void {

$trxnDate = '2010-01-01 09:00:00';
$trxnID = 'aabbccddeeffggh';
$trxnID = 'abcd';
$originalReceiveDate = '2010-02-02 22:22:22';

$contributionID = $this->contributionCreate([
'contact_id' => $this->individualCreate(),
'total_amount' => 100,
'contribution_status_id' => 'Pending',
'receive_date' => $originalReceiveDate,
'fee_amount' => 0,
]);

$this->callAPISuccess('Payment', 'create', [
'total_amount' => 100,
'order_id' => $contributionID,
'trxn_date' => $trxnDate,
'trxn_id' => $trxnID,
'fee_amount' => .2,
]);

$contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
$this->assertEquals('Completed', $contribution['contribution_status']);
$this->assertEquals(.2, $contribution['fee_amount']);
$this->assertEquals(99.8, $contribution['net_amount']);

$this->assertEquals($trxnID, $contribution['trxn_id'],
"Contribution trxn_id should have been set to that of the payment.");
Expand Down