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