Merge pull request #14154 from greenpeace-cee/fix-quicksearch-font
[civicrm-core.git] / CRM / Contribute / Form / CancelSubscription.php
CommitLineData
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 37class 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.
86 if (!$this->_crid) {
87 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj');
88 $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_mid, 'membership');
89 }
6a488035
TO
90
91 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
92 $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id');
93 $this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes));
94 }
95
6a488035
TO
96 if ($this->_coid) {
97 if (CRM_Contribute_BAO_Contribution::isSubscriptionCancelled($this->_coid)) {
98 CRM_Core_Error::fatal(ts('The recurring contribution looks to have been cancelled already.'));
99 }
100 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj');
d947cfb5 101
6a488035
TO
102 $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit);
103 $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval);
104 $this->assign('amount', $this->_subscriptionDetails->amount);
105 $this->assign('installments', $this->_subscriptionDetails->installments);
106 }
107
d947cfb5
DL
108 if (
109 (!$this->_crid && !$this->_coid && !$this->_mid) ||
1273d77c 110 (!$this->_subscriptionDetails)
6a488035 111 ) {
f1105edc 112 CRM_Core_Error::statusBounce('Required information missing.');
6a488035
TO
113 }
114
6a488035
TO
115 // handle context redirection
116 CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext();
117
118 CRM_Utils_System::setTitle($this->_mid ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
119 $this->assign('mode', $this->_mode);
120
f1105edc 121 if ($this->isSelfService()) {
122 unset($this->entityFields['send_cancel_request'], $this->entityFields['is_notify']);
123 }
124
6a488035 125 if ($this->_subscriptionDetails->contact_id) {
7c550ca0
WA
126 list($this->_donorDisplayName, $this->_donorEmail)
127 = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
6a488035
TO
128 }
129 }
130
f1105edc 131 /**
132 * Set entity fields for this cancellation.
133 */
134 public function setEntityFields() {
135 $this->entityFields = [
136 'cancel_reason' => ['name' => 'cancel_reason'],
137 ];
138 $this->entityFields['send_cancel_request'] = [
139 'title' => ts('Send cancellation request to %1 ?', [1 => $this->_paymentProcessorObj->_processorName]),
140 'name' => 'send_cancel_request',
141 'not-auto-addable' => TRUE,
142 ];
143 $this->entityFields['is_notify'] = [
144 'title' => ts('Notify Contributor?'),
145 'name' => 'is_notify',
146 'not-auto-addable' => TRUE,
147 ];
148 }
149
6a488035 150 /**
fe482240 151 * Build the form object.
6a488035
TO
152 */
153 public function buildQuickForm() {
f1105edc 154 $this->buildQuickEntityForm();
6a488035 155 // Determine if we can cancel recurring contribution via API with this processor
1524a007 156 $cancelSupported = $this->_paymentProcessorObj->supports('CancelRecurring');
6a488035 157 if ($cancelSupported) {
be2fb01f 158 $searchRange = [];
6a488035
TO
159 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
160 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
161
d947cfb5
DL
162 $this->addGroup(
163 $searchRange,
164 'send_cancel_request',
165 ts('Send cancellation request to %1 ?',
be2fb01f 166 [1 => $this->_paymentProcessorObj->_processorName])
d947cfb5 167 );
6a488035
TO
168 }
169 $this->assign('cancelSupported', $cancelSupported);
d947cfb5 170
6a488035
TO
171 if ($this->_donorEmail) {
172 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
173 }
174 if ($this->_mid) {
175 $cancelButton = ts('Cancel Automatic Membership Renewal');
176 }
177 else {
178 $cancelButton = ts('Cancel Recurring Contribution');
179 }
180
181 $type = 'next';
f1105edc 182 if ($this->isSelfService()) {
6a488035
TO
183 $type = 'submit';
184 }
185
be2fb01f 186 $this->addButtons([
1330f57a
SL
187 [
188 'type' => $type,
189 'name' => $cancelButton,
190 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
191 'isDefault' => TRUE,
192 ],
193 [
194 'type' => 'cancel',
195 'name' => ts('Not Now'),
196 ],
197 ]);
6a488035
TO
198 }
199
200 /**
1c4a09f4 201 * Set default values for the form.
6a488035 202 *
a6c01b45
CW
203 * @return array
204 * array of default values
6a488035 205 */
00be9182 206 public function setDefaultValues() {
be2fb01f 207 return [
1c4a09f4 208 'is_notify' => 1,
209 'send_cancel_request' => 1,
be2fb01f 210 ];
6a488035
TO
211 }
212
213 /**
fe482240 214 * Process the form submission.
6a488035
TO
215 */
216 public function postProcess() {
353ffa53 217 $status = $message = NULL;
6a488035 218 $cancelSubscription = TRUE;
353ffa53 219 $params = $this->controller->exportValues($this->_name);
6a488035 220
f1105edc 221 if ($this->isSelfService()) {
6a488035 222 // for self service force sending-request & notify
1524a007 223 if ($this->_paymentProcessorObj->supports('cancelRecurring')) {
6a488035 224 $params['send_cancel_request'] = 1;
d947cfb5
DL
225 }
226
227 if ($this->_donorEmail) {
6a488035 228 $params['is_notify'] = 1;
d947cfb5 229 }
6a488035
TO
230 }
231
232 if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) {
be2fb01f 233 $cancelParams = ['subscriptionId' => $this->_subscriptionDetails->subscription_id];
6a488035
TO
234 $cancelSubscription = $this->_paymentProcessorObj->cancelSubscription($message, $cancelParams);
235 }
236
237 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
238 CRM_Core_Error::displaySessionError($cancelSubscription);
239 }
240 elseif ($cancelSubscription) {
c7d9325a 241 try {
242 civicrm_api3('ContributionRecur', 'cancel', [
243 'id' => $this->_subscriptionDetails->recur_id,
244 'membership_id' => $this->_mid,
245 'processor_message' => $message,
f1105edc 246 'cancel_reason' => $params['cancel_reason'],
c7d9325a 247 ]);
d947cfb5 248
be2fb01f 249 $tplParams = [];
6a488035 250 if ($this->_mid) {
be2fb01f 251 $inputParams = ['id' => $this->_mid];
6a488035
TO
252 CRM_Member_BAO_Membership::getValues($inputParams, $tplParams);
253 $tplParams = $tplParams[$this->_mid];
7c550ca0
WA
254 $tplParams['membership_status']
255 = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
256 $tplParams['membershipType']
257 = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
be2fb01f 258 $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
259 $msgTitle = 'Membership Renewal Cancelled';
260 $msgType = 'info';
261 }
262 else {
263 $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails->frequency_interval;
264 $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails->frequency_unit;
265 $tplParams['amount'] = $this->_subscriptionDetails->amount;
be2fb01f 266 $tplParams['contact'] = ['display_name' => $this->_donorDisplayName];
6a488035 267 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
be2fb01f 268 [
6a488035
TO
269 1 => $this->_subscriptionDetails->amount,
270 2 => $this->_subscriptionDetails->frequency_interval,
21dfd5f5 271 3 => $this->_subscriptionDetails->frequency_unit,
be2fb01f 272 ]
6a488035
TO
273 );
274 $msgTitle = 'Contribution Cancelled';
275 $msgType = 'success';
276 }
277
278 if (CRM_Utils_Array::value('is_notify', $params) == 1) {
279 if ($this->_subscriptionDetails->contribution_page_id) {
d947cfb5
DL
280 CRM_Core_DAO::commonRetrieveAll(
281 'CRM_Contribute_DAO_ContributionPage',
282 'id',
283 $this->_subscriptionDetails->contribution_page_id,
284 $value,
be2fb01f 285 ['title', 'receipt_from_name', 'receipt_from_email']
d947cfb5 286 );
7c550ca0
WA
287 $receiptFrom
288 = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) .
d947cfb5
DL
289 '" <' .
290 $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] .
291 '>';
6a488035
TO
292 }
293 else {
294 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
295 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
296 }
d947cfb5 297
6a488035 298 // send notification
7c550ca0 299 $sendTemplateParams
be2fb01f 300 = [
353ffa53
TO
301 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
302 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
303 'contactId' => $this->_subscriptionDetails->contact_id,
304 'tplParams' => $tplParams,
305 //'isTest' => $isTest, set this from _objects
306 'PDFFilename' => 'receipt.pdf',
307 'from' => $receiptFrom,
308 'toName' => $this->_donorDisplayName,
309 'toEmail' => $this->_donorEmail,
be2fb01f 310 ];
c6327d7d 311 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
312 }
313 }
c7d9325a 314 catch (CiviCRM_API3_Exception $e) {
6a488035
TO
315 $msgType = 'error';
316 $msgTitle = ts('Error');
317 if ($params['send_cancel_request'] == 1) {
318 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
319 }
320 else {
321 $status = ts('Recurring contribution could not be cancelled in the database.');
322 }
323 }
324 }
325 else {
326 $status = ts('The recurring contribution could not be cancelled.');
327 $msgTitle = 'Error Cancelling Contribution';
328 $msgType = 'error';
329 }
330
331 $session = CRM_Core_Session::singleton();
353ffa53 332 $userID = $session->get('userID');
481a74f4 333 if ($userID && $status) {
6a488035
TO
334 $session->setStatus($status, $msgTitle, $msgType);
335 }
336 elseif (!$userID) {
7c550ca0 337 if ($status) {
6a488035 338 CRM_Utils_System::setUFMessage($status);
7c550ca0
WA
339 // keep result as 1, since we not displaying anything on the redirected page anyway
340 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus',
353ffa53 341 "reset=1&task=cancel&result=1"));
7c550ca0 342 }
6a488035
TO
343 }
344 }
96025800 345
f1105edc 346 /**
347 * Is this being used by a front end user to update their own recurring.
348 *
349 * @return bool
350 */
351 protected function isSelfService() {
352 if (!is_null($this->selfService)) {
353 return $this->selfService;
354 }
355 $this->selfService = FALSE;
356 if (!CRM_Core_Permission::check('edit contributions')) {
357 if ($this->_subscriptionDetails->contact_id != $this->getContactID()) {
358 CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
359 }
360 $this->selfService = TRUE;
361 }
362 return $this->selfService;
363 }
364
6a488035 365}