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