From bd78e59362dbf5934bb2f16ac006a9bfffff1562 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sun, 25 Jun 2023 17:21:47 +0100 Subject: [PATCH] Fix regression caused by 26620 --- CRM/Financial/BAO/Payment.php | 1 - .../CRM/Core/BAO/FinancialTrxnTest.php | 4 +- tests/phpunit/api/v3/PaymentTest.php | 69 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 97795f7cac..267a308264 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -188,7 +188,6 @@ class CRM_Financial_BAO_Payment { 'is_post_payment_create' => TRUE, 'is_email_receipt' => $params['is_send_contribution_notification'], 'trxn_date' => $params['trxn_date'], - 'trxn_id' => $params['trxn_id'] ?? NULL, 'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'], 'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ?? '', ]); diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index 4efdb22044..e661aed5e1 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -30,12 +30,12 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase { public function testCreate() { $contactId = $this->individualCreate(); $financialTypeId = 1; - $this->contributionCreate([ + $contributionID = $this->contributionCreate([ 'contact_id' => $contactId, 'financial_type_id' => $financialTypeId, ]); $params = [ - 'contribution_id' => $financialTypeId, + 'contribution_id' => $contributionID, 'to_financial_account_id' => 1, 'trxn_date' => 20091021184930, 'trxn_type' => 'Debit', diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 09b5a146e1..907db8fb50 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -882,6 +882,75 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $this->validateAllPayments(); } + /** + * Test create payment api for pay later contribution + * + * @throws \CRM_Core_Exception + */ + public function testCreatePaymentMultipleTrxnID(): void { + $this->createLoggedInUser(); + $processorID = $this->paymentProcessorCreate(); + $contributionParams = [ + 'total_amount' => 100, + 'currency' => 'USD', + 'contact_id' => $this->individualCreate(), + 'financial_type_id' => 1, + 'contribution_status_id' => 2, + 'is_pay_later' => 1, + 'trxn_id' => 'trxn_1', + ]; + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + //add payment for pay later transaction + $params = [ + 'contribution_id' => $contribution['id'], + 'total_amount' => 100, + 'card_type_id' => 'Visa', + 'pan_truncation' => '1234', + 'trxn_result_code' => 'Startling success', + 'payment_instrument_id' => $processorID, + 'trxn_id' => 'trxn_2', + ]; + $payment = $this->callAPISuccess('Payment', 'create', $params); + $expectedResult = [ + $payment['id'] => [ + 'from_financial_account_id' => 7, + 'to_financial_account_id' => 6, + 'total_amount' => 100, + 'status_id' => 1, + 'is_payment' => 1, + 'card_type_id' => 1, + 'pan_truncation' => '1234', + 'trxn_result_code' => 'Startling success', + 'trxn_id' => 'trxn_2', + 'payment_instrument_id' => 1, + ], + ]; + $this->checkPaymentResult($payment, $expectedResult); + // Check entity financial trxn created properly + $params = [ + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'financial_trxn_id' => $payment['id'], + ]; + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals(100, $eft['values'][$eft['id']]['amount']); + $params = [ + 'entity_table' => 'civicrm_financial_item', + 'financial_trxn_id' => $payment['id'], + ]; + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + $this->assertEquals(100, $eft['values'][$eft['id']]['amount']); + // Check contribution for completed status + $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]); + $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']); + $this->assertEquals('trxn_1,trxn_2', $contribution['values'][$contribution['id']]['trxn_id']); + $this->assertEquals(100.00, $contribution['values'][$contribution['id']]['total_amount']); + $this->callAPISuccess('Contribution', 'Delete', [ + 'id' => $contribution['id'], + ]); + $this->validateAllPayments(); + } + /** * Test net amount is set when fee amount is passed in. */ -- 2.25.1