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