From 7f4af29f7645f33769815a35fe9aca110e657786 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 20 Dec 2023 15:38:03 +1300 Subject: [PATCH] Stop bypassing hooks on ContributionRecur.cancel --- CRM/Contribute/BAO/ContributionRecur.php | 97 +++++++++++------------- api/v3/ContributionRecur.php | 3 +- 2 files changed, 46 insertions(+), 54 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index d2308a9532..df1dea9185 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -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 .= '
' . ts('Automatic renewal of %1 membership cancelled.', [1 => $membershipType]); - } - else { - $details .= '
' . 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 .= '
' . 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; } /** diff --git a/api/v3/ContributionRecur.php b/api/v3/ContributionRecur.php index 9abeccdb6e..b85d212c0c 100644 --- a/api/v3/ContributionRecur.php +++ b/api/v3/ContributionRecur.php @@ -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(); -- 2.25.1