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