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