diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index 980782b6a071..c51e3fca2483 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -493,7 +493,7 @@ public static function ensureTemplateContributionExists(int $id) { * * @param int $id * @param array $overrides - * Parameters that should be overriden. Add unit tests if using parameters other than total_amount & financial_type_id. + * Parameters that should be overridden. Add unit tests if using parameters other than total_amount & financial_type_id. * * @return array * diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index e3d49c086c09..cfa4f77ca773 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -585,14 +585,11 @@ function _civicrm_api3_contribution_completetransaction_spec(&$params) { function civicrm_api3_contribution_repeattransaction($params) { civicrm_api3_verify_one_mandatory($params, NULL, ['contribution_recur_id', 'original_contribution_id']); if (empty($params['original_contribution_id'])) { - // CRM-19873 call with test mode. - $params['original_contribution_id'] = civicrm_api3('contribution', 'getvalue', [ - 'return' => 'id', - 'contribution_status_id' => ['IN' => ['Completed']], - 'contribution_recur_id' => $params['contribution_recur_id'], - 'contribution_test' => CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $params['contribution_recur_id'], 'is_test'), - 'options' => ['limit' => 1, 'sort' => 'id DESC'], - ]); + $templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($params['contribution_recur_id']); + if (empty($templateContribution)) { + throw new CiviCRM_API3_Exception('Contribution.repeattransaction failed to get original_contribution_id for recur with ID: ' . $params['contribution_recur_id']); + } + $params['original_contribution_id'] = $templateContribution['id']; } $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $params['original_contribution_id']; diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 87981fa8e9e0..d658334e3fd5 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -2523,6 +2523,47 @@ public function testRepeatTransactionTestRecurId() { $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']); } + /** + * Test repeat contribution accepts recur_id instead of + * original_contribution_id. + * + * @throws \CRM_Core_Exception + */ + public function testRepeatTransactionPreviousContributionRefunded(): void { + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [ + 'contact_id' => $this->_individualId, + 'installments' => '12', + 'frequency_interval' => '1', + 'amount' => '100', + 'contribution_status_id' => 1, + 'start_date' => '2012-01-01 00:00:00', + 'currency' => 'USD', + 'frequency_unit' => 'month', + 'payment_processor_id' => $this->paymentProcessorID, + ]); + $this->callAPISuccess('contribution', 'create', array_merge( + $this->_params, + [ + 'contribution_recur_id' => $contributionRecur['id'], + 'contribution_status_id' => 'Refunded', + ] + ) + ); + + $this->callAPISuccess('contribution', 'repeattransaction', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'trxn_id' => 1234, + ]); + $contributions = $this->callAPISuccess('contribution', 'get', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'sequential' => 1, + ]); + // We should have contribution 0 in "Refunded" status and contribution 1 in "Pending" status + $this->assertEquals(2, $contributions['count']); + $this->assertEquals(7, $contributions['values'][0]['contribution_status_id']); + $this->assertEquals(2, $contributions['values'][1]['contribution_status_id']); + } + /** * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed *