Skip to content

Commit

Permalink
Merge pull request #22487 from mattwire/repeattransactiontemplatecont…
Browse files Browse the repository at this point in the history
…ribution

dev/financial#6 Use template contribution for Contribution.repeattransaction
  • Loading branch information
eileenmcnaughton authored May 23, 2022
2 parents 98b5722 + 9c15bfd commit f20abb0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,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
*
Expand Down
13 changes: 5 additions & 8 deletions api/v3/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,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'];
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit f20abb0

Please sign in to comment.