From d8a1b674eb5365ca5931f7deaa0bd23f8f79bbcc Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 4 Dec 2020 21:09:27 +1300 Subject: [PATCH] Fix Payment edit form to use Payment.cancel & payment.create api --- CRM/Financial/Form/PaymentEdit.php | 35 +++++++------------ api/v3/Payment.php | 2 ++ .../CRM/Financial/Form/PaymentEditTest.php | 12 +++++-- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/CRM/Financial/Form/PaymentEdit.php b/CRM/Financial/Form/PaymentEdit.php index ff85efa735..0e5b126108 100644 --- a/CRM/Financial/Form/PaymentEdit.php +++ b/CRM/Financial/Form/PaymentEdit.php @@ -174,6 +174,7 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { * * @param array $submittedValues * + * @throws \CiviCRM_API3_Exception */ protected function submit($submittedValues) { // if payment instrument is changed then @@ -181,29 +182,17 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { // 2. Record a new financial transaction with new payment instrument // 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution if ($submittedValues['payment_instrument_id'] != $this->_values['payment_instrument_id']) { - $previousFinanciaTrxn = $this->_values; - $newFinancialTrxn = $submittedValues; - unset($previousFinanciaTrxn['id'], $newFinancialTrxn['id']); - $previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array::value('trxn_date', $submittedValues, date('YmdHis')); - $previousFinanciaTrxn['total_amount'] = -$previousFinanciaTrxn['total_amount']; - $previousFinanciaTrxn['net_amount'] = -$previousFinanciaTrxn['net_amount']; - $previousFinanciaTrxn['fee_amount'] = -$previousFinanciaTrxn['fee_amount']; - $previousFinanciaTrxn['contribution_id'] = $newFinancialTrxn['contribution_id'] = $this->_contributionID; + civicrm_api3('Payment', 'cancel', [ + 'id' => $this->_values['id'], + 'trxn_date' => $submittedValues['trxn_date'], + ]); + $newFinancialTrxn = $submittedValues; + unset($newFinancialTrxn['id']); $newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']); - foreach (['total_amount', 'currency', 'is_payment', 'status_id'] as $fieldName) { - $newFinancialTrxn[$fieldName] = $this->_values[$fieldName]; - } - - foreach ([$previousFinanciaTrxn, $newFinancialTrxn] as $financialTrxnParams) { - $financialTrxn = civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams); - $trxnParams = [ - 'total_amount' => $financialTrxnParams['total_amount'], - 'contribution_id' => $this->_contributionID, - ]; - $contributionTotalAmount = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionID, 'total_amount'); - CRM_Contribute_BAO_Contribution::assignProportionalLineItems($trxnParams, $financialTrxn['id'], $contributionTotalAmount); - } + $newFinancialTrxn['total_amount'] = $this->_values['total_amount']; + $newFinancialTrxn['currency'] = $this->_values['currency']; + civicrm_api3('Payment', 'create', $newFinancialTrxn); } else { // simply update the financial trxn @@ -217,8 +206,10 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { * Wrapper for unit testing the post process submit function. * * @param array $params + * + * @throws \CiviCRM_API3_Exception */ - public function testSubmit($params) { + public function testSubmit(array $params): void { $this->_id = $params['id']; $this->_contributionID = $params['contribution_id']; $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $params['id']]); diff --git a/api/v3/Payment.php b/api/v3/Payment.php index bf10d26f15..555c791c2d 100644 --- a/api/v3/Payment.php +++ b/api/v3/Payment.php @@ -94,6 +94,7 @@ function civicrm_api3_payment_cancel($params) { $eftParams = [ 'entity_table' => 'civicrm_contribution', 'financial_trxn_id' => $params['id'], + 'return' => ['entity', 'amount', 'entity_id', 'financial_trxn_id.check_number'], ]; $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams); @@ -102,6 +103,7 @@ function civicrm_api3_payment_cancel($params) { 'contribution_id' => $entity['entity_id'], 'trxn_date' => $params['trxn_date'] ?? 'now', 'cancelled_payment_id' => $params['id'], + 'check_number' => $entity['financial_trxn_id.check_number'] ?? NULL, ]; foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) { diff --git a/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php b/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php index c415642e8b..c300732d54 100644 --- a/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php +++ b/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php @@ -28,6 +28,8 @@ class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase { /** * Clean up after each test. + * + * @throws \CRM_Core_Exception */ public function tearDown() { $this->quickCleanUpFinancialEntities(); @@ -36,8 +38,12 @@ class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase { /** * Test the submit function of payment edit form. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException */ - public function testSubmitOnPaymentInstrumentChange() { + public function testSubmitOnPaymentInstrumentChange(): void { // First create a contribution using 'Check' as payment instrument $form = new CRM_Contribute_Form_Contribution(); $form->testSubmit([ @@ -61,7 +67,7 @@ class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase { 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card'), 'card_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', 'Visa'), 'pan_truncation' => 1111, - 'trnx_id' => 'txn_12AAAA', + 'trxn_id' => 'txn_12AAAA', 'trxn_date' => date('Y-m-d H:i:s'), 'contribution_id' => $contribution['id'], ]; @@ -81,7 +87,7 @@ class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase { 'total_amount' => -50.00, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', - 'status' => 'Completed', + 'status' => 'Refunded Label**', 'receive_date' => $params['trxn_date'], 'check_number' => '123XA', ], -- 2.25.1