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