Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
95cdcc0f | 35 | * This class provides support for canceling recurring subscriptions. |
6a488035 | 36 | */ |
ca809bb6 | 37 | class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_ContributionRecur { |
6a488035 TO |
38 | |
39 | protected $_userContext = NULL; | |
40 | ||
41 | protected $_mode = NULL; | |
42 | ||
f1105edc | 43 | /** |
44 | * Should custom data be suppressed on this form. | |
45 | * | |
46 | * We override to suppress custom data because historically it has not been | |
47 | * shown on this form & we don't want to expose it as a by-product of | |
48 | * other change without establishing that it would be good on this form. | |
49 | * | |
50 | * @return bool | |
51 | */ | |
52 | protected function isSuppressCustomData() { | |
53 | return TRUE; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Is the from being accessed by a front end user to update their own recurring. | |
58 | * | |
59 | * @var bool | |
60 | */ | |
61 | protected $selfService; | |
6a488035 TO |
62 | |
63 | /** | |
fe482240 | 64 | * Set variables up before form is built. |
503699d5 | 65 | * |
66 | * @throws \CRM_Core_Exception | |
6a488035 TO |
67 | */ |
68 | public function preProcess() { | |
1f17c8ef | 69 | parent::preProcess(); |
6a488035 | 70 | if ($this->_crid) { |
6a488035 TO |
71 | $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit); |
72 | $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval); | |
73 | $this->assign('amount', $this->_subscriptionDetails->amount); | |
74 | $this->assign('installments', $this->_subscriptionDetails->installments); | |
d947cfb5 | 75 | |
6a488035 TO |
76 | // Are we cancelling a recurring contribution that is linked to an auto-renew membership? |
77 | if ($this->_subscriptionDetails->membership_id) { | |
78 | $this->_mid = $this->_subscriptionDetails->membership_id; | |
79 | } | |
80 | } | |
81 | ||
82 | if ($this->_mid) { | |
6a488035 | 83 | $this->_mode = 'auto_renew'; |
16381e37 J |
84 | // CRM-18468: crid is more accurate than mid for getting |
85 | // subscriptionDetails, so don't get them again. | |
6a488035 TO |
86 | |
87 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
88 | $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id'); | |
89 | $this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes)); | |
90 | } | |
91 | ||
6a488035 TO |
92 | if ($this->_coid) { |
93 | if (CRM_Contribute_BAO_Contribution::isSubscriptionCancelled($this->_coid)) { | |
94 | CRM_Core_Error::fatal(ts('The recurring contribution looks to have been cancelled already.')); | |
95 | } | |
96 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj'); | |
d947cfb5 | 97 | |
6a488035 TO |
98 | $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit); |
99 | $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval); | |
100 | $this->assign('amount', $this->_subscriptionDetails->amount); | |
101 | $this->assign('installments', $this->_subscriptionDetails->installments); | |
102 | } | |
103 | ||
d947cfb5 DL |
104 | if ( |
105 | (!$this->_crid && !$this->_coid && !$this->_mid) || | |
1273d77c | 106 | (!$this->_subscriptionDetails) |
6a488035 | 107 | ) { |
f1105edc | 108 | CRM_Core_Error::statusBounce('Required information missing.'); |
6a488035 TO |
109 | } |
110 | ||
6a488035 TO |
111 | // handle context redirection |
112 | CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext(); | |
113 | ||
114 | CRM_Utils_System::setTitle($this->_mid ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution')); | |
115 | $this->assign('mode', $this->_mode); | |
116 | ||
f1105edc | 117 | if ($this->isSelfService()) { |
118 | unset($this->entityFields['send_cancel_request'], $this->entityFields['is_notify']); | |
119 | } | |
120 | ||
6a488035 | 121 | if ($this->_subscriptionDetails->contact_id) { |
7c550ca0 WA |
122 | list($this->_donorDisplayName, $this->_donorEmail) |
123 | = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id); | |
6a488035 TO |
124 | } |
125 | } | |
126 | ||
f1105edc | 127 | /** |
128 | * Set entity fields for this cancellation. | |
129 | */ | |
130 | public function setEntityFields() { | |
131 | $this->entityFields = [ | |
132 | 'cancel_reason' => ['name' => 'cancel_reason'], | |
133 | ]; | |
134 | $this->entityFields['send_cancel_request'] = [ | |
703235f6 | 135 | 'title' => ts('Send cancellation request?'), |
f1105edc | 136 | 'name' => 'send_cancel_request', |
137 | 'not-auto-addable' => TRUE, | |
138 | ]; | |
139 | $this->entityFields['is_notify'] = [ | |
140 | 'title' => ts('Notify Contributor?'), | |
141 | 'name' => 'is_notify', | |
142 | 'not-auto-addable' => TRUE, | |
143 | ]; | |
144 | } | |
145 | ||
6a488035 | 146 | /** |
fe482240 | 147 | * Build the form object. |
6a488035 TO |
148 | */ |
149 | public function buildQuickForm() { | |
f1105edc | 150 | $this->buildQuickEntityForm(); |
6a488035 | 151 | // Determine if we can cancel recurring contribution via API with this processor |
1524a007 | 152 | $cancelSupported = $this->_paymentProcessorObj->supports('CancelRecurring'); |
6a488035 | 153 | if ($cancelSupported) { |
be2fb01f | 154 | $searchRange = []; |
6a488035 TO |
155 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1'); |
156 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0'); | |
157 | ||
d947cfb5 DL |
158 | $this->addGroup( |
159 | $searchRange, | |
160 | 'send_cancel_request', | |
161 | ts('Send cancellation request to %1 ?', | |
be2fb01f | 162 | [1 => $this->_paymentProcessorObj->_processorName]) |
d947cfb5 | 163 | ); |
6a488035 TO |
164 | } |
165 | $this->assign('cancelSupported', $cancelSupported); | |
d947cfb5 | 166 | |
6a488035 TO |
167 | if ($this->_donorEmail) { |
168 | $this->add('checkbox', 'is_notify', ts('Notify Contributor?')); | |
169 | } | |
170 | if ($this->_mid) { | |
171 | $cancelButton = ts('Cancel Automatic Membership Renewal'); | |
172 | } | |
173 | else { | |
174 | $cancelButton = ts('Cancel Recurring Contribution'); | |
175 | } | |
176 | ||
177 | $type = 'next'; | |
f1105edc | 178 | if ($this->isSelfService()) { |
6a488035 TO |
179 | $type = 'submit'; |
180 | } | |
181 | ||
be2fb01f | 182 | $this->addButtons([ |
1330f57a SL |
183 | [ |
184 | 'type' => $type, | |
185 | 'name' => $cancelButton, | |
186 | 'spacing' => ' ', | |
187 | 'isDefault' => TRUE, | |
188 | ], | |
189 | [ | |
190 | 'type' => 'cancel', | |
191 | 'name' => ts('Not Now'), | |
192 | ], | |
193 | ]); | |
6a488035 TO |
194 | } |
195 | ||
196 | /** | |
1c4a09f4 | 197 | * Set default values for the form. |
6a488035 | 198 | * |
a6c01b45 CW |
199 | * @return array |
200 | * array of default values | |
6a488035 | 201 | */ |
00be9182 | 202 | public function setDefaultValues() { |
be2fb01f | 203 | return [ |
1c4a09f4 | 204 | 'is_notify' => 1, |
205 | 'send_cancel_request' => 1, | |
be2fb01f | 206 | ]; |
6a488035 TO |
207 | } |
208 | ||
209 | /** | |
fe482240 | 210 | * Process the form submission. |
6a488035 TO |
211 | */ |
212 | public function postProcess() { | |
353ffa53 | 213 | $status = $message = NULL; |
6a488035 | 214 | $cancelSubscription = TRUE; |
353ffa53 | 215 | $params = $this->controller->exportValues($this->_name); |
6a488035 | 216 | |
f1105edc | 217 | if ($this->isSelfService()) { |
6a488035 | 218 | // for self service force sending-request & notify |
1524a007 | 219 | if ($this->_paymentProcessorObj->supports('cancelRecurring')) { |
6a488035 | 220 | $params['send_cancel_request'] = 1; |
d947cfb5 DL |
221 | } |
222 | ||
223 | if ($this->_donorEmail) { | |
6a488035 | 224 | $params['is_notify'] = 1; |
d947cfb5 | 225 | } |
6a488035 TO |
226 | } |
227 | ||
228 | if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) { | |
be2fb01f | 229 | $cancelParams = ['subscriptionId' => $this->_subscriptionDetails->subscription_id]; |
6a488035 TO |
230 | $cancelSubscription = $this->_paymentProcessorObj->cancelSubscription($message, $cancelParams); |
231 | } | |
232 | ||
233 | if (is_a($cancelSubscription, 'CRM_Core_Error')) { | |
234 | CRM_Core_Error::displaySessionError($cancelSubscription); | |
235 | } | |
236 | elseif ($cancelSubscription) { | |
c7d9325a | 237 | try { |
238 | civicrm_api3('ContributionRecur', 'cancel', [ | |
239 | 'id' => $this->_subscriptionDetails->recur_id, | |
240 | 'membership_id' => $this->_mid, | |
241 | 'processor_message' => $message, | |
f1105edc | 242 | 'cancel_reason' => $params['cancel_reason'], |
c7d9325a | 243 | ]); |
d947cfb5 | 244 | |
be2fb01f | 245 | $tplParams = []; |
6a488035 | 246 | if ($this->_mid) { |
be2fb01f | 247 | $inputParams = ['id' => $this->_mid]; |
6a488035 TO |
248 | CRM_Member_BAO_Membership::getValues($inputParams, $tplParams); |
249 | $tplParams = $tplParams[$this->_mid]; | |
7c550ca0 WA |
250 | $tplParams['membership_status'] |
251 | = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']); | |
252 | $tplParams['membershipType'] | |
253 | = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']); | |
be2fb01f | 254 | $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']]); |
6a488035 TO |
255 | $msgTitle = 'Membership Renewal Cancelled'; |
256 | $msgType = 'info'; | |
257 | } | |
258 | else { | |
259 | $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails->frequency_interval; | |
260 | $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails->frequency_unit; | |
261 | $tplParams['amount'] = $this->_subscriptionDetails->amount; | |
be2fb01f | 262 | $tplParams['contact'] = ['display_name' => $this->_donorDisplayName]; |
6a488035 | 263 | $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.', |
be2fb01f | 264 | [ |
6a488035 TO |
265 | 1 => $this->_subscriptionDetails->amount, |
266 | 2 => $this->_subscriptionDetails->frequency_interval, | |
21dfd5f5 | 267 | 3 => $this->_subscriptionDetails->frequency_unit, |
be2fb01f | 268 | ] |
6a488035 TO |
269 | ); |
270 | $msgTitle = 'Contribution Cancelled'; | |
271 | $msgType = 'success'; | |
272 | } | |
273 | ||
274 | if (CRM_Utils_Array::value('is_notify', $params) == 1) { | |
275 | if ($this->_subscriptionDetails->contribution_page_id) { | |
d947cfb5 DL |
276 | CRM_Core_DAO::commonRetrieveAll( |
277 | 'CRM_Contribute_DAO_ContributionPage', | |
278 | 'id', | |
279 | $this->_subscriptionDetails->contribution_page_id, | |
280 | $value, | |
be2fb01f | 281 | ['title', 'receipt_from_name', 'receipt_from_email'] |
d947cfb5 | 282 | ); |
7c550ca0 WA |
283 | $receiptFrom |
284 | = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . | |
d947cfb5 DL |
285 | '" <' . |
286 | $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . | |
287 | '>'; | |
6a488035 TO |
288 | } |
289 | else { | |
290 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); | |
291 | $receiptFrom = "$domainValues[0] <$domainValues[1]>"; | |
292 | } | |
d947cfb5 | 293 | |
6a488035 | 294 | // send notification |
7c550ca0 | 295 | $sendTemplateParams |
be2fb01f | 296 | = [ |
353ffa53 TO |
297 | 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', |
298 | 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled', | |
299 | 'contactId' => $this->_subscriptionDetails->contact_id, | |
300 | 'tplParams' => $tplParams, | |
301 | //'isTest' => $isTest, set this from _objects | |
302 | 'PDFFilename' => 'receipt.pdf', | |
303 | 'from' => $receiptFrom, | |
304 | 'toName' => $this->_donorDisplayName, | |
305 | 'toEmail' => $this->_donorEmail, | |
be2fb01f | 306 | ]; |
c6327d7d | 307 | list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
308 | } |
309 | } | |
c7d9325a | 310 | catch (CiviCRM_API3_Exception $e) { |
6a488035 TO |
311 | $msgType = 'error'; |
312 | $msgTitle = ts('Error'); | |
313 | if ($params['send_cancel_request'] == 1) { | |
314 | $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.'); | |
315 | } | |
316 | else { | |
317 | $status = ts('Recurring contribution could not be cancelled in the database.'); | |
318 | } | |
319 | } | |
320 | } | |
321 | else { | |
322 | $status = ts('The recurring contribution could not be cancelled.'); | |
323 | $msgTitle = 'Error Cancelling Contribution'; | |
324 | $msgType = 'error'; | |
325 | } | |
326 | ||
327 | $session = CRM_Core_Session::singleton(); | |
353ffa53 | 328 | $userID = $session->get('userID'); |
481a74f4 | 329 | if ($userID && $status) { |
6a488035 TO |
330 | $session->setStatus($status, $msgTitle, $msgType); |
331 | } | |
332 | elseif (!$userID) { | |
7c550ca0 | 333 | if ($status) { |
6a488035 | 334 | CRM_Utils_System::setUFMessage($status); |
7c550ca0 WA |
335 | // keep result as 1, since we not displaying anything on the redirected page anyway |
336 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', | |
353ffa53 | 337 | "reset=1&task=cancel&result=1")); |
7c550ca0 | 338 | } |
6a488035 TO |
339 | } |
340 | } | |
96025800 | 341 | |
f1105edc | 342 | /** |
343 | * Is this being used by a front end user to update their own recurring. | |
344 | * | |
345 | * @return bool | |
346 | */ | |
347 | protected function isSelfService() { | |
348 | if (!is_null($this->selfService)) { | |
349 | return $this->selfService; | |
350 | } | |
351 | $this->selfService = FALSE; | |
352 | if (!CRM_Core_Permission::check('edit contributions')) { | |
353 | if ($this->_subscriptionDetails->contact_id != $this->getContactID()) { | |
354 | CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.')); | |
355 | } | |
356 | $this->selfService = TRUE; | |
357 | } | |
358 | return $this->selfService; | |
359 | } | |
360 | ||
6a488035 | 361 | } |