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