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