Skip to content

Commit

Permalink
Merge pull request #17788 from eileenmcnaughton/update
Browse files Browse the repository at this point in the history
[REF] Follow up cleanup
  • Loading branch information
seamuslee001 authored Jul 13, 2020
2 parents b089734 + 32509f6 commit 53ae66a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 93 deletions.
175 changes: 84 additions & 91 deletions CRM/Contribute/Form/CancelSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,10 +197,10 @@ public function setDefaultValues() {
* 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()) {
Expand All @@ -222,112 +223,104 @@ public function postProcess() {
$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'));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contribute/Form/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -179,7 +179,7 @@ protected function setSubscriptionDetails() {
/**
* Get details for the recurring contribution being altered.
*
* @return array
* @return \CRM_Core_DAO
*/
public function getSubscriptionDetails() {
return $this->subscriptionDetails;
Expand Down

0 comments on commit 53ae66a

Please sign in to comment.