Merge pull request #42 from pradpnayak/CRM-11983
[civicrm-core.git] / CRM / Contribute / Form / CancelSubscription.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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);
72
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');
99
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
106 if ((!$this->_crid && !$this->_coid && !$this->_mid) ||
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) {
128 list($this->_donorDisplayName, $this->_donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
129 }
130 }
131
132 /**
133 * Function to build the form
134 *
135 * @return None
136 * @access public
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) {
142 $searchRange = array();
143 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
144 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
145
146 $this->addGroup($searchRange, 'send_cancel_request', ts('Send cancellation request to %1 ?', array(1 => $this->_paymentProcessorObj->_processorName)));
147 }
148 $this->assign('cancelSupported', $cancelSupported);
149
150 if ($this->_donorEmail) {
151 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
152 }
153 if ($this->_mid) {
154 $cancelButton = ts('Cancel Automatic Membership Renewal');
155 }
156 else {
157 $cancelButton = ts('Cancel Recurring Contribution');
158 }
159
160 $type = 'next';
161 if ( $this->_selfService ) {
162 $type = 'submit';
163 }
164
165 $this->addButtons(array(
166 array(
167 'type' => $type,
168 'name' => $cancelButton,
169 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
170 'isDefault' => TRUE,
171 ),
172 array(
173 'type' => 'cancel',
174 'name' => ts('Not Now'),
175 ),
176 )
177 );
178 }
179
180 /**
181 * This function sets the default values for the form. Note that in edit/view mode
182 * the default values are retrieved from the database
183 *
184 * @param null
185 *
186 * @return array array of default values
187 * @access public
188 */
189 function setDefaultValues() {
190 $defaults = array();
191 $defaults['is_notify'] = 1;
192 return $defaults;
193 }
194
195 /**
196 * Function to process the form
197 *
198 * @access public
199 *
200 * @return None
201 */
202 public function postProcess() {
203 $status = $message = NULL;
204 $cancelSubscription = TRUE;
205 $params = $this->controller->exportValues($this->_name);
206
207 if ($this->_selfService) {
208 // for self service force sending-request & notify
209 if ($this->_paymentProcessorObj->isSupported('cancelSubscription'))
210 $params['send_cancel_request'] = 1;
211 if ($this->_donorEmail)
212 $params['is_notify'] = 1;
213 }
214
215 if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) {
216 $cancelParams = array('subscriptionId' => $this->_subscriptionDetails->subscription_id);
217 $cancelSubscription = $this->_paymentProcessorObj->cancelSubscription($message, $cancelParams);
218 }
219
220 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
221 CRM_Core_Error::displaySessionError($cancelSubscription);
222 }
223 elseif ($cancelSubscription) {
224 $activityParams = array('subject' => $this->_mid ? ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
225 'details' => $message,
226 );
227 $cancelStatus = CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($this->_subscriptionDetails->recur_id,
228 CRM_Core_DAO::$_nullObject, $activityParams
229 );
230 if ($cancelStatus) {
231 $tplParams = array();
232 if ($this->_mid) {
233 $inputParams = array('id' => $this->_mid);
234 CRM_Member_BAO_Membership::getValues($inputParams, $tplParams);
235 $tplParams = $tplParams[$this->_mid];
236 $tplParams['membership_status'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
237 $tplParams['membershipType'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
238 $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']));
239 $msgTitle = 'Membership Renewal Cancelled';
240 $msgType = 'info';
241 }
242 else {
243 $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails->frequency_interval;
244 $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails->frequency_unit;
245 $tplParams['amount'] = $this->_subscriptionDetails->amount;
246 $tplParams['contact'] = array('display_name' => $this->_donorDisplayName);
247 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
248 array(
249 1 => $this->_subscriptionDetails->amount,
250 2 => $this->_subscriptionDetails->frequency_interval,
251 3 => $this->_subscriptionDetails->frequency_unit
252 )
253 );
254 $msgTitle = 'Contribution Cancelled';
255 $msgType = 'success';
256 }
257
258 if (CRM_Utils_Array::value('is_notify', $params) == 1) {
259 if ($this->_subscriptionDetails->contribution_page_id) {
260 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
261 $this->_subscriptionDetails->contribution_page_id,
262 $value, array('title',
263 'receipt_from_name',
264 'receipt_from_email',
265 )
266 );
267 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>';
268 }
269 else {
270 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
271 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
272 }
273
274 // send notification
275 $sendTemplateParams =
276 array(
277 'groupName' => $this->_mode == 'auto_renew' ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
278 'valueName' => $this->_mode == 'auto_renew' ? 'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
279 'contactId' => $this->_subscriptionDetails->contact_id,
280 'tplParams' => $tplParams,
281 //'isTest' => $isTest, set this from _objects
282 'PDFFilename' => 'receipt.pdf',
283 'from' => $receiptFrom,
284 'toName' => $this->_donorDisplayName,
285 'toEmail' => $this->_donorEmail,
286 );
287 list($sent) = CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
288 }
289 }
290 else {
291 $msgType = 'error';
292 $msgTitle = ts('Error');
293 if ($params['send_cancel_request'] == 1) {
294 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
295 }
296 else {
297 $status = ts('Recurring contribution could not be cancelled in the database.');
298 }
299 }
300 }
301 else {
302 $status = ts('The recurring contribution could not be cancelled.');
303 $msgTitle = 'Error Cancelling Contribution';
304 $msgType = 'error';
305 }
306
307 $session = CRM_Core_Session::singleton();
308 $userID = $session->get('userID');
309 if ( $userID && $status) {
310 $session->setStatus($status, $msgTitle, $msgType);
311 }
312 elseif (!$userID) {
313 if ($status)
314 CRM_Utils_System::setUFMessage($status);
315 // keep result as 1, since we not displaying anything on the redirected page anyway
316 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus',
317 "reset=1&task=cancel&result=1"));
318 }
319 }
320}
321