From d0b559aa6aee52306c44645605f5b7424822beeb Mon Sep 17 00:00:00 2001 From: Sunil Pawar Date: Thu, 27 Aug 2020 11:56:25 +0530 Subject: [PATCH] dev/core#912 update payment instrument of main contribution record --- CRM/Financial/BAO/Payment.php | 1 + api/v3/Contribution.php | 3 ++ tests/phpunit/api/v3/ContributionTest.php | 50 +++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index f841363f14..e13837e996 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -157,6 +157,7 @@ class CRM_Financial_BAO_Payment { 'is_post_payment_create' => TRUE, 'is_email_receipt' => $params['is_send_contribution_notification'], 'trxn_date' => $params['trxn_date'], + 'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'], ]); // Get the trxn $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index a1b06305e9..7a5e94dcb5 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -678,6 +678,9 @@ function _ipn_process_transaction(&$params, $contribution, $input, $ids, $firstC } $input['card_type_id'] = $params['card_type_id'] ?? NULL; $input['pan_truncation'] = $params['pan_truncation'] ?? NULL; + if (!empty($params['payment_instrument_id'])) { + $input['payment_instrument_id'] = $params['payment_instrument_id']; + } return CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects, $params['is_post_payment_create'] ?? NULL); } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 8de1a75b89..8b673eb9e3 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -4899,4 +4899,54 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals('2020-02-02 00:00:00', $contribution['receive_date']); } + /** + * Make sure that recording a payment with Different Payment Instrument update main contribution record payment + * instrument too. If multiple Payment Recorded, last payment record payment (when No more due) instrument set to main + * payment + */ + public function testPaymentVerifyPaymentInstrumentChange() { + // Create Pending contribution with pay later mode, with payment instrument Check + $params = [ + 'contact_id' => $this->_individualId, + 'total_amount' => 100, + 'receive_date' => '2020-02-02', + 'contribution_status_id' => 'Pending', + 'is_pay_later' => 1, + 'payment_instrument_id' => 'Check', + ]; + $contributionID = $this->contributionCreate($params); + + + // Record the the Payment with instrument other than Check, e.g EFT + $paymentParams = [ + 'contribution_id' => $contributionID, + 'total_amount' => 50, + 'trxn_date' => '2020-03-04', + 'payment_instrument_id' => 'EFT' + ]; + $this->callAPISuccess('payment', 'create', $paymentParams); + + $contribution = $this->callAPISuccess('Contribution', 'getSingle', [ + 'id' => $contributionID, + ]); + // payment status should be 'Partially paid' + $this->assertEquals('Partially paid', $contribution['contribution_status']); + + // Record the the Payment with instrument other than Check, e.g Cash (pay all remaining amount) + $paymentParams = [ + 'contribution_id' => $contributionID, + 'total_amount' => 50, + 'trxn_date' => '2020-03-04', + 'payment_instrument_id' => 'Cash' + ]; + $this->callAPISuccess('payment', 'create', $paymentParams); + + //check if contribution Payment Instrument (Payment Method) is is set to "Cash". + $contribution = $this->callAPISuccess('Contribution', 'getSingle', [ + 'id' => $contributionID, + ]); + $this->assertEquals('Cash', $contribution['payment_instrument']); + $this->assertEquals('Completed', $contribution['contribution_status']); + } + } -- 2.25.1