Stop bypassing hooks on ContributionRecur.cancel
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 20 Dec 2023 02:38:03 +0000 (15:38 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 20 Dec 2023 08:31:08 +0000 (21:31 +1300)
CRM/Contribute/BAO/ContributionRecur.php
api/v3/ContributionRecur.php

index d2308a9532528da11e6fdd8c508c486c1e21482e..df1dea9185b30a105a66be752e511bb035caf826 100644 (file)
@@ -283,62 +283,53 @@ class CRM_Contribute_BAO_ContributionRecur extends CRM_Contribute_DAO_Contributi
     if (!$params['id']) {
       return FALSE;
     }
-    $activityParams = [
-      'subject' => !empty($params['membership_id']) ? ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
-      'details' => $params['processor_message'] ?? NULL,
-    ];
-
-    $cancelledId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', 'Cancelled');
-    $recur = new CRM_Contribute_DAO_ContributionRecur();
-    $recur->id = $params['id'];
-    $recur->whereAdd("contribution_status_id != $cancelledId");
-
-    if ($recur->find(TRUE)) {
-      $transaction = new CRM_Core_Transaction();
-      $recur->contribution_status_id = $cancelledId;
-      $recur->cancel_reason = $params['cancel_reason'] ?? NULL;
-      $recur->cancel_date = date('YmdHis');
-      $recur->save();
-
-      // @fixme https://lab.civicrm.org/dev/core/issues/927 Cancelling membership etc is not desirable for all use-cases and we should be able to disable it
-      $dao = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params['id']);
-      if ($dao && $dao->recur_id) {
-        $details = $activityParams['details'] ?? NULL;
-        if ($dao->auto_renew && $dao->membership_id) {
-          // its auto-renewal membership mode
-          $membershipTypes = CRM_Member_PseudoConstant::membershipType();
-          $membershipType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $dao->membership_id, 'membership_type_id');
-          $membershipType = $membershipTypes[$membershipType] ?? NULL;
-          $details .= '
+    $transaction = new CRM_Core_Transaction();
+    ContributionRecur::update(FALSE)
+      ->addWhere('id', '=', $params['id'])
+      ->setValues([
+        'contribution_status_id:name' => 'Cancelled',
+        'cancel_reason' => $params['cancel_reason'] ?? NULL,
+        'cancel_date' => $params['cancel_date'] ?? 'now',
+      ])->execute();
+
+    // @todo - all of this should be moved to the post hook.
+    // It seems to just create activities.
+    $dao = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params['id']);
+    if ($dao && $dao->recur_id) {
+      $details = $params['processor_message'] ?? NULL;
+      if ($dao->auto_renew && $dao->membership_id) {
+        // its auto-renewal membership mode
+        $membershipTypes = CRM_Member_PseudoConstant::membershipType();
+        $membershipType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $dao->membership_id, 'membership_type_id');
+        $membershipType = $membershipTypes[$membershipType] ?? NULL;
+        $details .= '
 <br/>' . ts('Automatic renewal of %1 membership cancelled.', [1 => $membershipType]);
-        }
-        else {
-          $details .= '<br/>' . ts('The recurring contribution of %1, every %2 %3 has been cancelled.', [
-            1 => $dao->amount,
-            2 => $dao->frequency_interval,
-            3 => $dao->frequency_unit,
-          ]);
-        }
-        $activityParams = [
-          'source_contact_id' => $dao->contact_id,
-          'source_record_id' => $dao->recur_id,
-          'activity_type_id' => 'Cancel Recurring Contribution',
-          'subject' => CRM_Utils_Array::value('subject', $activityParams, ts('Recurring contribution cancelled')),
-          'details' => $details,
-          'status_id' => 'Completed',
-        ];
-
-        $cid = CRM_Core_Session::singleton()->get('userID');
-        if ($cid) {
-          $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
-          $activityParams['source_contact_id'] = $cid;
-        }
-        civicrm_api3('Activity', 'create', $activityParams);
       }
-
-      $transaction->commit();
-      return TRUE;
+      else {
+        $details .= '<br/>' . ts('The recurring contribution of %1, every %2 %3 has been cancelled.', [
+          1 => $dao->amount,
+          2 => $dao->frequency_interval,
+          3 => $dao->frequency_unit,
+        ]);
+      }
+      $activityParams = [
+        'source_contact_id' => $dao->contact_id,
+        'source_record_id' => $dao->recur_id,
+        'activity_type_id' => 'Cancel Recurring Contribution',
+        'subject' => !empty($params['membership_id']) ? ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
+        'details' => $details,
+        'status_id' => 'Completed',
+      ];
+
+      $cid = CRM_Core_Session::singleton()->get('userID');
+      if ($cid) {
+        $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
+        $activityParams['source_contact_id'] = $cid;
+      }
+      civicrm_api3('Activity', 'create', $activityParams);
     }
+    $transaction->commit();
+    return TRUE;
   }
 
   /**
index 9abeccdb6ee707f2a4c31775a7c8ca04bf207cfe..b85d212c0c8498d8a3fb5eb25e22253c0da309bb 100644 (file)
@@ -71,7 +71,8 @@ function civicrm_api3_contribution_recur_get($params) {
  * @throws \CRM_Core_Exception
  */
 function civicrm_api3_contribution_recur_cancel(array $params): array {
-  $existing = ContributionRecur::get(TRUE)
+  $params['check_permissions'] = $params['check_permissions'] ?? FALSE;
+  $existing = ContributionRecur::get($params['check_permissions'])
     ->addWhere('id', '=', $params['id'])
     ->addSelect('contribution_status_id:name')
     ->execute()->first();