Fix regression caused by 26620
authorMatthew Wire <mjw@mjwconsult.co.uk>
Sun, 25 Jun 2023 16:21:47 +0000 (17:21 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Mon, 3 Jul 2023 10:37:11 +0000 (11:37 +0100)
CRM/Financial/BAO/Payment.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php
tests/phpunit/api/v3/PaymentTest.php

index 97795f7cac191bea154a63f8d6670002f46fb089..267a3082646feeabd40991f0633cb4680d24aa55 100644 (file)
@@ -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'] ?? '',
         ]);
index 4efdb2204429d363871ab465e2a6c43954db7167..e661aed5e1628ff758a927152cc27fc430e76bd7 100644 (file)
@@ -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',
index 09b5a146e1382af43b4dc5d6c22b7f251988b35b..907db8fb507fa2a9f7dc99681fcfdad684722e66 100644 (file)
@@ -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.
    */