diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index fdb7c478f3a9..10219870fc62 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -342,20 +342,6 @@ public function submit($submittedValues) { if ($this->_component == 'event') { $participantId = $this->_id; } - $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', - 'contribution_status_id', - ['labelColumn' => 'name'] - ); - $contributionStatusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id'); - if ($contributionStatuses[$contributionStatusID] == 'Pending') { - civicrm_api3('Contribution', 'create', - [ - 'id' => $this->_contributionId, - 'contribution_status_id' => array_search('Partially paid', $contributionStatuses), - 'is_pay_later' => 0, - ] - ); - } if ($this->_mode) { // process credit card diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 0854915d035a..ccc65c2dd832 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -171,12 +171,7 @@ public static function create($params) { } } elseif ($contributionStatus === 'Pending') { - civicrm_api3('Contribution', 'create', - [ - 'id' => $contribution['id'], - 'contribution_status_id' => 'Partially paid', - ] - ); + self::updateContributionStatus($contribution['id'], 'Partially Paid'); } CRM_Contribute_BAO_Contribution::recordPaymentActivity($params['contribution_id'], CRM_Utils_Array::value('participant_id', $params), $params['total_amount'], $trxn->currency, $trxn->trxn_date); return $trxn; @@ -534,4 +529,24 @@ protected static function isPaymentCompletesContribution($contributionID, $payme return ($cmp == 0 || $cmp == 1); } + /** + * Update the status of the contribution. + * + * We pass the is_post_payment_create as we have already created the line items + * + * @param int $contributionID + * @param string $status + * + * @throws \CiviCRM_API3_Exception + */ + private static function updateContributionStatus(int $contributionID, string $status) { + civicrm_api3('Contribution', 'create', + [ + 'id' => $contributionID, + 'is_post_payment_create' => TRUE, + 'contribution_status_id' => $status, + ] + ); + } + } diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 09895d9acb7b..e00dcab64f20 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -670,7 +670,28 @@ public function testCreatePaymentPayLater() { /** * Test create payment api for pay later contribution with partial payment. * - * @throws \Exception + * https://lab.civicrm.org/dev/financial/issues/69 + */ + public function testCreatePaymentIncompletePaymentPartialPayment() { + $contributionParams = [ + 'total_amount' => 100, + 'currency' => 'USD', + 'contact_id' => $this->_individualId, + 'financial_type_id' => 1, + 'contribution_status_id' => 2, + ]; + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $contribution['id'], + 'total_amount' => 50, + 'payment_instrument_id' => 'Cash', + ]); + $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values']; + $this->assertCount(1, $payments); + } + + /** + * Test create payment api for pay later contribution with partial payment. */ public function testCreatePaymentPayLaterPartialPayment() { $this->createLoggedInUser();