| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | use Civi\Payment\PropertyBag; |
| 12 | |
| 13 | /** |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * This class provides support for canceling recurring subscriptions. |
| 21 | */ |
| 22 | class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_ContributionRecur { |
| 23 | |
| 24 | protected $_userContext = NULL; |
| 25 | |
| 26 | protected $_mode = NULL; |
| 27 | |
| 28 | /** |
| 29 | * Should custom data be suppressed on this form. |
| 30 | * |
| 31 | * We override to suppress custom data because historically it has not been |
| 32 | * shown on this form & we don't want to expose it as a by-product of |
| 33 | * other change without establishing that it would be good on this form. |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | protected function isSuppressCustomData() { |
| 38 | return TRUE; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Set variables up before form is built. |
| 43 | * |
| 44 | * @throws \CRM_Core_Exception |
| 45 | */ |
| 46 | public function preProcess() { |
| 47 | parent::preProcess(); |
| 48 | if ($this->_crid) { |
| 49 | $this->assign('frequency_unit', $this->getSubscriptionDetails()->frequency_unit); |
| 50 | $this->assign('frequency_interval', $this->getSubscriptionDetails()->frequency_interval); |
| 51 | $this->assign('amount', $this->getSubscriptionDetails()->amount); |
| 52 | $this->assign('installments', $this->getSubscriptionDetails()->installments); |
| 53 | |
| 54 | // Are we cancelling a recurring contribution that is linked to an auto-renew membership? |
| 55 | if ($this->getSubscriptionDetails()->membership_id) { |
| 56 | $this->_mid = $this->getSubscriptionDetails()->membership_id; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if ($this->_mid) { |
| 61 | $this->_mode = 'auto_renew'; |
| 62 | // CRM-18468: crid is more accurate than mid for getting |
| 63 | // subscriptionDetails, so don't get them again. |
| 64 | |
| 65 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); |
| 66 | $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id'); |
| 67 | $this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes)); |
| 68 | } |
| 69 | |
| 70 | if ($this->_coid) { |
| 71 | if (CRM_Contribute_BAO_Contribution::isSubscriptionCancelled($this->_coid)) { |
| 72 | CRM_Core_Error::statusBounce(ts('The recurring contribution looks to have been cancelled already.')); |
| 73 | } |
| 74 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj'); |
| 75 | |
| 76 | $this->assign('frequency_unit', $this->getSubscriptionDetails()->frequency_unit); |
| 77 | $this->assign('frequency_interval', $this->getSubscriptionDetails()->frequency_interval); |
| 78 | $this->assign('amount', $this->getSubscriptionDetails()->amount); |
| 79 | $this->assign('installments', $this->getSubscriptionDetails()->installments); |
| 80 | } |
| 81 | |
| 82 | if ( |
| 83 | (!$this->_crid && !$this->_coid && !$this->_mid) || |
| 84 | (!$this->getSubscriptionDetails()) |
| 85 | ) { |
| 86 | CRM_Core_Error::statusBounce('Required information missing.'); |
| 87 | } |
| 88 | |
| 89 | // handle context redirection |
| 90 | CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext(); |
| 91 | |
| 92 | CRM_Utils_System::setTitle($this->_mid ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution')); |
| 93 | $this->assign('mode', $this->_mode); |
| 94 | |
| 95 | if ($this->isSelfService()) { |
| 96 | unset($this->entityFields['send_cancel_request'], $this->entityFields['is_notify']); |
| 97 | } |
| 98 | |
| 99 | if ($this->getSubscriptionDetails()->contact_id) { |
| 100 | list($this->_donorDisplayName, $this->_donorEmail) |
| 101 | = CRM_Contact_BAO_Contact::getContactDetails($this->getSubscriptionDetails()->contact_id); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Set entity fields for this cancellation. |
| 107 | */ |
| 108 | public function setEntityFields() { |
| 109 | $this->entityFields = [ |
| 110 | 'cancel_reason' => ['name' => 'cancel_reason'], |
| 111 | ]; |
| 112 | $this->entityFields['send_cancel_request'] = [ |
| 113 | 'title' => ts('Send cancellation request?'), |
| 114 | 'name' => 'send_cancel_request', |
| 115 | 'not-auto-addable' => TRUE, |
| 116 | ]; |
| 117 | $this->entityFields['is_notify'] = [ |
| 118 | 'title' => ts('Notify Contributor?'), |
| 119 | 'name' => 'is_notify', |
| 120 | 'not-auto-addable' => TRUE, |
| 121 | ]; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Build the form object. |
| 126 | */ |
| 127 | public function buildQuickForm() { |
| 128 | $this->buildQuickEntityForm(); |
| 129 | // Determine if we can cancel recurring contribution via API with this processor |
| 130 | $cancelSupported = $this->_paymentProcessorObj->supports('CancelRecurring'); |
| 131 | if ($cancelSupported) { |
| 132 | $searchRange = []; |
| 133 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1'); |
| 134 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0'); |
| 135 | |
| 136 | $this->addGroup( |
| 137 | $searchRange, |
| 138 | 'send_cancel_request', |
| 139 | ts('Send cancellation request to %1 ?', |
| 140 | [1 => $this->_paymentProcessorObj->_processorName]) |
| 141 | ); |
| 142 | } |
| 143 | $this->assign('cancelSupported', $cancelSupported); |
| 144 | |
| 145 | if ($this->_donorEmail) { |
| 146 | $this->add('checkbox', 'is_notify', ts('Notify Contributor?')); |
| 147 | } |
| 148 | if ($this->_mid) { |
| 149 | $cancelButton = ts('Cancel Automatic Membership Renewal'); |
| 150 | } |
| 151 | else { |
| 152 | $cancelButton = ts('Cancel Recurring Contribution'); |
| 153 | } |
| 154 | |
| 155 | $type = 'next'; |
| 156 | if ($this->isSelfService()) { |
| 157 | $type = 'submit'; |
| 158 | } |
| 159 | |
| 160 | $this->addButtons([ |
| 161 | [ |
| 162 | 'type' => $type, |
| 163 | 'name' => $cancelButton, |
| 164 | 'spacing' => ' ', |
| 165 | 'isDefault' => TRUE, |
| 166 | ], |
| 167 | [ |
| 168 | 'type' => 'cancel', |
| 169 | 'name' => ts('Not Now'), |
| 170 | ], |
| 171 | ]); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Set default values for the form. |
| 176 | * |
| 177 | * @return array |
| 178 | * array of default values |
| 179 | */ |
| 180 | public function setDefaultValues() { |
| 181 | return [ |
| 182 | 'is_notify' => 1, |
| 183 | 'send_cancel_request' => 1, |
| 184 | ]; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Process the form submission. |
| 189 | */ |
| 190 | public function postProcess() { |
| 191 | $message = NULL; |
| 192 | $cancelSubscription = TRUE; |
| 193 | $params = $this->controller->exportValues($this->_name); |
| 194 | |
| 195 | if ($this->isSelfService()) { |
| 196 | // for self service force sending-request & notify |
| 197 | if ($this->_paymentProcessorObj->supports('cancelRecurring')) { |
| 198 | $params['send_cancel_request'] = 1; |
| 199 | } |
| 200 | |
| 201 | if ($this->_donorEmail) { |
| 202 | $params['is_notify'] = 1; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) { |
| 207 | try { |
| 208 | $propertyBag = new PropertyBag(); |
| 209 | $propertyBag->setContributionRecurID($this->getSubscriptionDetails()->recur_id); |
| 210 | $propertyBag->setRecurProcessorID($this->getSubscriptionDetails()->subscription_id); |
| 211 | $message = $this->_paymentProcessorObj->doCancelRecurring($propertyBag)['message']; |
| 212 | } |
| 213 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { |
| 214 | CRM_Core_Error::statusBounce($e->getMessage()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if ($cancelSubscription) { |
| 219 | try { |
| 220 | civicrm_api3('ContributionRecur', 'cancel', [ |
| 221 | 'id' => $this->getSubscriptionDetails()->recur_id, |
| 222 | 'membership_id' => $this->_mid, |
| 223 | 'processor_message' => $message, |
| 224 | 'cancel_reason' => $params['cancel_reason'], |
| 225 | ]); |
| 226 | |
| 227 | $tplParams = []; |
| 228 | if ($this->_mid) { |
| 229 | $inputParams = ['id' => $this->_mid]; |
| 230 | CRM_Member_BAO_Membership::getValues($inputParams, $tplParams); |
| 231 | $tplParams = $tplParams[$this->_mid]; |
| 232 | $tplParams['membership_status'] |
| 233 | = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']); |
| 234 | $tplParams['membershipType'] |
| 235 | = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']); |
| 236 | $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']]); |
| 237 | $msgTitle = 'Membership Renewal Cancelled'; |
| 238 | $msgType = 'info'; |
| 239 | } |
| 240 | else { |
| 241 | $tplParams['recur_frequency_interval'] = $this->getSubscriptionDetails()->frequency_interval; |
| 242 | $tplParams['recur_frequency_unit'] = $this->getSubscriptionDetails()->frequency_unit; |
| 243 | $tplParams['amount'] = $this->getSubscriptionDetails()->amount; |
| 244 | $tplParams['contact'] = ['display_name' => $this->_donorDisplayName]; |
| 245 | $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.', |
| 246 | [ |
| 247 | 1 => $this->getSubscriptionDetails()->amount, |
| 248 | 2 => $this->getSubscriptionDetails()->frequency_interval, |
| 249 | 3 => $this->getSubscriptionDetails()->frequency_unit, |
| 250 | ] |
| 251 | ); |
| 252 | $msgTitle = 'Contribution Cancelled'; |
| 253 | $msgType = 'success'; |
| 254 | } |
| 255 | |
| 256 | if (CRM_Utils_Array::value('is_notify', $params) == 1) { |
| 257 | if ($this->getSubscriptionDetails()->contribution_page_id) { |
| 258 | CRM_Core_DAO::commonRetrieveAll( |
| 259 | 'CRM_Contribute_DAO_ContributionPage', |
| 260 | 'id', |
| 261 | $this->getSubscriptionDetails()->contribution_page_id, |
| 262 | $value, |
| 263 | ['title', 'receipt_from_name', 'receipt_from_email'] |
| 264 | ); |
| 265 | $receiptFrom |
| 266 | = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) . |
| 267 | '" <' . |
| 268 | $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] . |
| 269 | '>'; |
| 270 | } |
| 271 | else { |
| 272 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); |
| 273 | $receiptFrom = "$domainValues[0] <$domainValues[1]>"; |
| 274 | } |
| 275 | |
| 276 | // send notification |
| 277 | $sendTemplateParams |
| 278 | = [ |
| 279 | 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', |
| 280 | 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled', |
| 281 | 'contactId' => $this->getSubscriptionDetails()->contact_id, |
| 282 | 'tplParams' => $tplParams, |
| 283 | //'isTest' => $isTest, set this from _objects |
| 284 | 'PDFFilename' => 'receipt.pdf', |
| 285 | 'from' => $receiptFrom, |
| 286 | 'toName' => $this->_donorDisplayName, |
| 287 | 'toEmail' => $this->_donorEmail, |
| 288 | ]; |
| 289 | list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
| 290 | } |
| 291 | } |
| 292 | catch (CiviCRM_API3_Exception $e) { |
| 293 | $msgType = 'error'; |
| 294 | $msgTitle = ts('Error'); |
| 295 | if ($params['send_cancel_request'] == 1) { |
| 296 | $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.'); |
| 297 | } |
| 298 | else { |
| 299 | $status = ts('Recurring contribution could not be cancelled in the database.'); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | else { |
| 304 | $status = ts('The recurring contribution could not be cancelled.'); |
| 305 | $msgTitle = 'Error Cancelling Contribution'; |
| 306 | $msgType = 'error'; |
| 307 | } |
| 308 | |
| 309 | $session = CRM_Core_Session::singleton(); |
| 310 | $userID = $session->get('userID'); |
| 311 | if ($userID && $status) { |
| 312 | $session->setStatus($status, $msgTitle, $msgType); |
| 313 | } |
| 314 | elseif (!$userID) { |
| 315 | if ($status) { |
| 316 | CRM_Utils_System::setUFMessage($status); |
| 317 | // keep result as 1, since we not displaying anything on the redirected page anyway |
| 318 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', |
| 319 | "reset=1&task=cancel&result=1")); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | } |