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