From: Jon Goldberg Date: Thu, 5 Nov 2020 19:24:38 +0000 (-0500) Subject: closes financial/#156: Set contribution status to refunded even if cancelled_payment_... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=26311ba367432f9c94379b8ed8163ba2c26b7ee8;p=civicrm-core.git closes financial/#156: Set contribution status to refunded even if cancelled_payment_id is set --- diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 2b93c545e1..44840e408c 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -98,44 +98,45 @@ class CRM_Financial_BAO_Payment { if ($params['total_amount'] < 0 && !empty($params['cancelled_payment_id'])) { self::reverseAllocationsFromPreviousPayment($params, $trxn->id); - return $trxn; } - list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($params['contribution_id']); + else { + list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($params['contribution_id']); - foreach ($lineItems as $key => $value) { - if ($value['allocation'] === (float) 0) { - continue; - } + foreach ($lineItems as $key => $value) { + if ($value['allocation'] === (float) 0) { + continue; + } - if (!empty($ftIds[$value['price_field_value_id']])) { - $financialItemID = $ftIds[$value['price_field_value_id']]; - } - else { - $financialItemID = self::getNewFinancialItemID($value, $params['trxn_date'], $contribution['contact_id'], $paymentTrxnParams['currency']); - } + if (!empty($ftIds[$value['price_field_value_id']])) { + $financialItemID = $ftIds[$value['price_field_value_id']]; + } + else { + $financialItemID = self::getNewFinancialItemID($value, $params['trxn_date'], $contribution['contact_id'], $paymentTrxnParams['currency']); + } - $eftParams = [ - 'entity_table' => 'civicrm_financial_item', - 'financial_trxn_id' => $trxn->id, - 'entity_id' => $financialItemID, - 'amount' => $value['allocation'], - ]; - - civicrm_api3('EntityFinancialTrxn', 'create', $eftParams); - - if (array_key_exists($value['price_field_value_id'], $taxItems)) { - // @todo - this is expected to be broken - it should be fixed to - // a) have the getPayableLineItems add the amount to allocate for tax - // b) call EntityFinancialTrxn directly - per above. - // - see https://github.com/civicrm/civicrm-core/pull/14763 - $entityParams = [ - 'contribution_total_amount' => $contribution['total_amount'], - 'trxn_total_amount' => $params['total_amount'], - 'trxn_id' => $trxn->id, - 'line_item_amount' => $taxItems[$value['price_field_value_id']]['amount'], + $eftParams = [ + 'entity_table' => 'civicrm_financial_item', + 'financial_trxn_id' => $trxn->id, + 'entity_id' => $financialItemID, + 'amount' => $value['allocation'], ]; - $eftParams['entity_id'] = $taxItems[$value['price_field_value_id']]['financial_item_id']; - CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); + + civicrm_api3('EntityFinancialTrxn', 'create', $eftParams); + + if (array_key_exists($value['price_field_value_id'], $taxItems)) { + // @todo - this is expected to be broken - it should be fixed to + // a) have the getPayableLineItems add the amount to allocate for tax + // b) call EntityFinancialTrxn directly - per above. + // - see https://github.com/civicrm/civicrm-core/pull/14763 + $entityParams = [ + 'contribution_total_amount' => $contribution['total_amount'], + 'trxn_total_amount' => $params['total_amount'], + 'trxn_id' => $trxn->id, + 'line_item_amount' => $taxItems[$value['price_field_value_id']]['amount'], + ]; + $eftParams['entity_id'] = $taxItems[$value['price_field_value_id']]['financial_item_id']; + CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); + } } } diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 5c36bde5d2..ad9f744763 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -621,6 +621,33 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $this->assertEquals($contribution['contribution_status'], 'Refunded Label**'); } + /** + * Test negative payment using create API when the "cancelled_payment_id" param is set. + * + * @throws \CRM_Core_Exception + */ + public function testRefundPaymentWithCancelledPaymentId() { + $result = $this->callAPISuccess('Contribution', 'create', [ + 'financial_type_id' => "Donation", + 'total_amount' => 100, + 'contact_id' => $this->_individualId, + ]); + $contributionID = $result['id']; + + //Refund the complete amount. + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $contributionID, + 'total_amount' => -100, + 'cancelled_payment_id' => 12345, + ]); + $contribution = $this->callAPISuccessGetSingle('Contribution', [ + 'return' => ['contribution_status_id'], + 'id' => $contributionID, + ]); + //Assert if main contribution status is updated to "Refunded". + $this->assertEquals($contribution['contribution_status'], 'Refunded Label**'); + } + /** * Test cancel payment api *