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