From 1eade77d05ee344d827568d98c28f804c3878165 Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Wed, 16 Dec 2015 16:25:37 +0000 Subject: [PATCH] CRM-17718 update repeattransaction to accept contribution_recur_id instead of original_contribution_id wq I feel like passing in the contribution_recur_id actually makes more sense - the internals of which contribution is repeated should be 'core business' & consistent across processors. Importantly it has been set here to repeat the latest rather than the earliest which is not as good as saving such things as line items against the recurring transaction but better than using the earliest - ie. pick up changes over time --- api/v3/Contribution.php | 14 +++++++- tests/phpunit/api/v3/ContributionTest.php | 40 +++++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 4ee8872a01..27d6bf3aa1 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -535,6 +535,14 @@ function _civicrm_api3_contribution_completetransaction_spec(&$params) { */ function civicrm_api3_contribution_repeattransaction(&$params) { $input = $ids = array(); + civicrm_api3_verify_one_mandatory($params, NULL, array('contribution_recur_id', 'original_contribution_id')); + if (empty($params['original_contribution_id'])) { + $params['original_contribution_id'] = civicrm_api3('contribution', 'getvalue', array( + 'return' => 'id', + 'contribution_recur_id' => $params['contribution_recur_id'], + 'options' => array('limit' => 1, 'sort' => 'id DESC'), + )); + } $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $params['original_contribution_id']; if (!$contribution->find(TRUE)) { @@ -617,8 +625,12 @@ function _ipn_process_transaction(&$params, $contribution, $input, $ids, $firstC function _civicrm_api3_contribution_repeattransaction_spec(&$params) { $params['original_contribution_id'] = array( 'title' => 'Original Contribution ID', + 'description' => 'Contribution ID to copy (will be calculated from recurring contribution if not provided)', + 'type' => CRM_Utils_Type::T_INT, + ); + $params['contribution_recur_id'] = array( + 'title' => 'Recurring contribution ID', 'type' => CRM_Utils_Type::T_INT, - 'api.required' => TRUE, ); $params['trxn_id'] = array( 'title' => 'Transaction ID', diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 2c82baaeda..c8e5ea3b91 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -49,6 +49,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase { protected $_params; protected $_ids = array(); protected $_pageParams = array(); + /** + * Payment processor ID (dummy processor). + * + * @var int + */ + protected $paymentProcessorID; /** * Parameters to create payment processor. @@ -94,12 +100,13 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'url_recur' => 'http://dummy.com', 'billing_mode' => 1, ); + $this->paymentProcessorID = $this->processorCreate(); $this->_pageParams = array( 'title' => 'Test Contribution Page', 'financial_type_id' => 1, 'currency' => 'USD', 'financial_account_id' => 1, - 'payment_processor' => $this->processorCreate(), + 'payment_processor' => $this->paymentProcessorID, 'is_active' => 1, 'is_allow_other_amount' => 1, 'min_amount' => 10, @@ -1446,6 +1453,35 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->quickCleanUpFinancialEntities(); } + /** + * Test repeat contribution accepts recur_id instead of original_contribution_id. + */ + public function testRepeatTransactionAcceptRecurID() { + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( + '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, + array('contribution_recur_id' => $contributionRecur['id'])) + ); + + $this->callAPISuccess('contribution', 'repeattransaction', array( + 'contribution_recur_id' => $contributionRecur['id'], + 'contribution_status_id' => 'Completed', + 'trxn_id' => uniqid(), + )); + + $this->quickCleanUpFinancialEntities(); + } + /** * CRM-16397 test appropriate action if total amount has changed for single line items. */ @@ -1515,7 +1551,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { } /** - * CRM-16397 test appropriate action if total amount has changed for single line items. + * CRM-16397 test appropriate action if campaign has been passed in. */ public function testRepeatTransactionPassedInCampaign() { $paymentProcessorID = $this->paymentProcessorCreate(); -- 2.25.1