Move existing recur checks for apiv3 to the apiv3 function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 20 Dec 2023 01:44:40 +0000 (14:44 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 20 Dec 2023 01:44:40 +0000 (14:44 +1300)
CRM/Contribute/BAO/ContributionRecur.php
api/v3/ContributionRecur.php

index e6beb6ca2e45fed16e7fc768a690204d8321a96e..d2308a9532528da11e6fdd8c508c486c1e21482e 100644 (file)
@@ -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;
   }
 
   /**
index 9121740712283e510128ca66fb51bf94f6eabf29..9abeccdb6ee707f2a4c31775a7c8ca04bf207cfe 100644 (file)
@@ -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();
 }
 
 /**