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