From 32509f69c42cbf5d610bd69a72914ddf85614111 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 29 Jun 2020 16:38:59 +1200 Subject: [PATCH] [REF] Follow up cleanup This removes an IF block that relies on the always-true parameter I'm pretty sure it just wasn't done last round as all the white space created a lot of noise --- CRM/Contribute/Form/CancelSubscription.php | 175 ++++++++++----------- CRM/Contribute/Form/ContributionRecur.php | 4 +- 2 files changed, 86 insertions(+), 93 deletions(-) diff --git a/CRM/Contribute/Form/CancelSubscription.php b/CRM/Contribute/Form/CancelSubscription.php index 3d0003796e..b1b4b6cb52 100644 --- a/CRM/Contribute/Form/CancelSubscription.php +++ b/CRM/Contribute/Form/CancelSubscription.php @@ -10,15 +10,16 @@ */ use Civi\Payment\PropertyBag; +use Civi\Payment\Exception\PaymentProcessorException; /** * This class provides support for canceling recurring subscriptions. */ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_ContributionRecur { - protected $_userContext = NULL; + protected $_userContext; - protected $_mode = NULL; + protected $_mode; /** * The contributor email @@ -196,10 +197,10 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib * Process the form submission. * * @throws \CRM_Core_Exception + * @throws \API_Exception */ public function postProcess() { $message = NULL; - $cancelSubscription = TRUE; $params = $this->controller->exportValues($this->_name); if ($this->isSelfService()) { @@ -222,112 +223,104 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib $propertyBag->setRecurProcessorID($this->getSubscriptionDetails()->processor_id); $message = $this->_paymentProcessorObj->doCancelRecurring($propertyBag)['message']; } - catch (\Civi\Payment\Exception\PaymentProcessorException $e) { + catch (PaymentProcessorException $e) { CRM_Core_Error::statusBounce($e->getMessage()); } - if ($cancelSubscription) { - try { - civicrm_api3('ContributionRecur', 'cancel', [ - 'id' => $this->getSubscriptionDetails()->recur_id, - 'membership_id' => $this->_mid, - 'processor_message' => $message, - 'cancel_reason' => $params['cancel_reason'], - ]); - - $tplParams = []; - if ($this->_mid) { - $inputParams = ['id' => $this->_mid]; - CRM_Member_BAO_Membership::getValues($inputParams, $tplParams); - $tplParams = $tplParams[$this->_mid]; - $tplParams['membership_status'] - = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']); - $tplParams['membershipType'] - = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']); - $status = ts('The automatic renewal of your %1 membership has been cancelled as requested. This does not affect the status of your membership - you will receive a separate notification when your membership is up for renewal.', [1 => $tplParams['membershipType']]); - $msgTitle = 'Membership Renewal Cancelled'; - $msgType = 'info'; - } - else { - $tplParams['recur_frequency_interval'] = $this->getSubscriptionDetails()->frequency_interval; - $tplParams['recur_frequency_unit'] = $this->getSubscriptionDetails()->frequency_unit; - $tplParams['amount'] = CRM_Utils_Money::format($this->getSubscriptionDetails()->amount, $this->getSubscriptionDetails()->currency); - $tplParams['contact'] = ['display_name' => $this->_donorDisplayName]; - $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.', - [ - 1 => $tplParams['amount'], - 2 => $tplParams['recur_frequency_interval'], - 3 => $tplParams['recur_frequency_unit'], - ] - ); - $msgTitle = 'Contribution Cancelled'; - $msgType = 'success'; - } - - if (CRM_Utils_Array::value('is_notify', $params) == 1) { - if ($this->getSubscriptionDetails()->contribution_page_id) { - CRM_Core_DAO::commonRetrieveAll( - 'CRM_Contribute_DAO_ContributionPage', - 'id', - $this->getSubscriptionDetails()->contribution_page_id, - $value, - ['title', 'receipt_from_name', 'receipt_from_email'] - ); - $receiptFrom - = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) . - '" <' . - $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] . - '>'; - } - else { - $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); - $receiptFrom = "$domainValues[0] <$domainValues[1]>"; - } - - // send notification - $sendTemplateParams - = [ - 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', - 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled', - 'contactId' => $this->getSubscriptionDetails()->contact_id, - 'tplParams' => $tplParams, - //'isTest' => $isTest, set this from _objects - 'PDFFilename' => 'receipt.pdf', - 'from' => $receiptFrom, - 'toName' => $this->_donorDisplayName, - 'toEmail' => $this->_donorEmail, - ]; - list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); - } + try { + civicrm_api3('ContributionRecur', 'cancel', [ + 'id' => $this->getSubscriptionDetails()->recur_id, + 'membership_id' => $this->_mid, + 'processor_message' => $message, + 'cancel_reason' => $params['cancel_reason'], + ]); + + $tplParams = []; + if ($this->_mid) { + $inputParams = ['id' => $this->_mid]; + CRM_Member_BAO_Membership::getValues($inputParams, $tplParams); + $tplParams = $tplParams[$this->_mid]; + $tplParams['membership_status'] + = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']); + $tplParams['membershipType'] + = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']); + $status = ts('The automatic renewal of your %1 membership has been cancelled as requested. This does not affect the status of your membership - you will receive a separate notification when your membership is up for renewal.', [1 => $tplParams['membershipType']]); + $msgTitle = 'Membership Renewal Cancelled'; + $msgType = 'info'; } - catch (CiviCRM_API3_Exception $e) { - $msgType = 'error'; - $msgTitle = ts('Error'); - if ($params['send_cancel_request'] == 1) { - $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.'); + else { + $tplParams['recur_frequency_interval'] = $this->getSubscriptionDetails()->frequency_interval; + $tplParams['recur_frequency_unit'] = $this->getSubscriptionDetails()->frequency_unit; + $tplParams['amount'] = CRM_Utils_Money::format($this->getSubscriptionDetails()->amount, $this->getSubscriptionDetails()->currency); + $tplParams['contact'] = ['display_name' => $this->_donorDisplayName]; + $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.', + [ + 1 => $tplParams['amount'], + 2 => $tplParams['recur_frequency_interval'], + 3 => $tplParams['recur_frequency_unit'], + ] + ); + $msgTitle = 'Contribution Cancelled'; + $msgType = 'success'; + } + + if (CRM_Utils_Array::value('is_notify', $params) == 1) { + if ($this->getSubscriptionDetails()->contribution_page_id) { + CRM_Core_DAO::commonRetrieveAll( + 'CRM_Contribute_DAO_ContributionPage', + 'id', + $this->getSubscriptionDetails()->contribution_page_id, + $value, + ['title', 'receipt_from_name', 'receipt_from_email'] + ); + $receiptFrom + = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) . + '" <' . + $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] . + '>'; } else { - $status = ts('Recurring contribution could not be cancelled in the database.'); + $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); + $receiptFrom = "$domainValues[0] <$domainValues[1]>"; } + + // send notification + $sendTemplateParams + = [ + 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', + 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled', + 'contactId' => $this->getSubscriptionDetails()->contact_id, + 'tplParams' => $tplParams, + //'isTest' => $isTest, set this from _objects + 'PDFFilename' => 'receipt.pdf', + 'from' => $receiptFrom, + 'toName' => $this->_donorDisplayName, + 'toEmail' => $this->_donorEmail, + ]; + list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); } } - else { - $status = ts('The recurring contribution could not be cancelled.'); - $msgTitle = 'Error Cancelling Contribution'; + catch (CiviCRM_API3_Exception $e) { $msgType = 'error'; + $msgTitle = ts('Error'); + if ($params['send_cancel_request'] == 1) { + $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.'); + } + else { + $status = ts('Recurring contribution could not be cancelled in the database.'); + } } - $session = CRM_Core_Session::singleton(); - $userID = $session->get('userID'); + $userID = CRM_Core_Session::getLoggedInContactID(); if ($userID && $status) { - $session->setStatus($status, $msgTitle, $msgType); + CRM_Core_Session::singleton()->setStatus($status, $msgTitle, $msgType); } elseif (!$userID) { if ($status) { CRM_Utils_System::setUFMessage($status); // keep result as 1, since we not displaying anything on the redirected page anyway - return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', - "reset=1&task=cancel&result=1")); + CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', + 'reset=1&task=cancel&result=1')); } } } diff --git a/CRM/Contribute/Form/ContributionRecur.php b/CRM/Contribute/Form/ContributionRecur.php index f313d5be22..8378d60599 100644 --- a/CRM/Contribute/Form/ContributionRecur.php +++ b/CRM/Contribute/Form/ContributionRecur.php @@ -87,7 +87,7 @@ class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form { /** * Details of the subscription (recurring contribution) to be altered. * - * @var array + * @var \CRM_Core_DAO */ protected $subscriptionDetails = []; @@ -179,7 +179,7 @@ class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form { /** * Get details for the recurring contribution being altered. * - * @return array + * @return \CRM_Core_DAO */ public function getSubscriptionDetails() { return $this->subscriptionDetails; -- 2.25.1