From b24ac775d4b8dcc7df9f97fd59bae654664ad6cc Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 20 Dec 2023 14:44:40 +1300 Subject: [PATCH] Move existing recur checks for apiv3 to the apiv3 function --- CRM/Contribute/BAO/ContributionRecur.php | 10 ---------- api/v3/ContributionRecur.php | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index e6beb6ca2e..d2308a9532 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -339,16 +339,6 @@ class CRM_Contribute_BAO_ContributionRecur extends CRM_Contribute_DAO_Contributi $transaction->commit(); return TRUE; } - else { - // if already cancelled, return true - $recur->whereAdd(); - $recur->whereAdd("contribution_status_id = $cancelledId"); - if ($recur->find(TRUE)) { - return TRUE; - } - } - - return FALSE; } /** diff --git a/api/v3/ContributionRecur.php b/api/v3/ContributionRecur.php index 9121740712..9abeccdb6e 100644 --- a/api/v3/ContributionRecur.php +++ b/api/v3/ContributionRecur.php @@ -15,6 +15,8 @@ * @package CiviCRM_APIv3 */ +use Civi\Api4\ContributionRecur; + /** * Create or update a ContributionRecur. * @@ -69,7 +71,19 @@ function civicrm_api3_contribution_recur_get($params) { * @throws \CRM_Core_Exception */ function civicrm_api3_contribution_recur_cancel(array $params): array { - return CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($params) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while cancelling recurring contribution')); + $existing = ContributionRecur::get(TRUE) + ->addWhere('id', '=', $params['id']) + ->addSelect('contribution_status_id:name') + ->execute()->first(); + if (!$existing) { + throw new CRM_Core_Exception('record not found'); + } + if ($existing['contribution_status_id:name'] === 'Cancelled') { + return civicrm_api3_create_success([$existing['id'] => $existing]); + } + + CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($params); + return civicrm_api3_create_success(); } /** -- 2.25.1