Merge pull request #14438 from eileenmcnaughton/display
[civicrm-core.git] / CRM / Contribute / Form / CancelSubscription.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class provides support for canceling recurring subscriptions.
36 */
37 class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_ContributionRecur {
38
39 protected $_userContext = NULL;
40
41 protected $_mode = NULL;
42
43 protected $_selfService = FALSE;
44
45 /**
46 * Set variables up before form is built.
47 *
48 * @throws \CRM_Core_Exception
49 */
50 public function preProcess() {
51 parent::preProcess();
52 if ($this->_crid) {
53 $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit);
54 $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval);
55 $this->assign('amount', $this->_subscriptionDetails->amount);
56 $this->assign('installments', $this->_subscriptionDetails->installments);
57
58 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
59 if ($this->_subscriptionDetails->membership_id) {
60 $this->_mid = $this->_subscriptionDetails->membership_id;
61 }
62 }
63
64 if ($this->_mid) {
65 $this->_mode = 'auto_renew';
66 // CRM-18468: crid is more accurate than mid for getting
67 // subscriptionDetails, so don't get them again.
68 if (!$this->_crid) {
69 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj');
70 $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_mid, 'membership');
71 }
72
73 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
74 $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id');
75 $this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes));
76 }
77
78 if ($this->_coid) {
79 if (CRM_Contribute_BAO_Contribution::isSubscriptionCancelled($this->_coid)) {
80 CRM_Core_Error::fatal(ts('The recurring contribution looks to have been cancelled already.'));
81 }
82 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj');
83
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
90 if (
91 (!$this->_crid && !$this->_coid && !$this->_mid) ||
92 (!$this->_subscriptionDetails)
93 ) {
94 CRM_Core_Error::fatal('Required information missing.');
95 }
96
97 if (!CRM_Core_Permission::check('edit contributions')) {
98 if ($this->_subscriptionDetails->contact_id != $this->getContactID()) {
99 CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
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) {
112 list($this->_donorDisplayName, $this->_donorEmail)
113 = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
114 }
115 }
116
117 /**
118 * Build the form object.
119 */
120 public function buildQuickForm() {
121 // Determine if we can cancel recurring contribution via API with this processor
122 $cancelSupported = $this->_paymentProcessorObj->supports('CancelRecurring');
123 if ($cancelSupported) {
124 $searchRange = [];
125 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
126 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
127
128 $this->addGroup(
129 $searchRange,
130 'send_cancel_request',
131 ts('Send cancellation request to %1 ?',
132 [1 => $this->_paymentProcessorObj->_processorName])
133 );
134 }
135 $this->assign('cancelSupported', $cancelSupported);
136
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';
148 if ($this->_selfService) {
149 $type = 'submit';
150 }
151
152 $this->addButtons([
153 [
154 'type' => $type,
155 'name' => $cancelButton,
156 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
157 'isDefault' => TRUE,
158 ],
159 [
160 'type' => 'cancel',
161 'name' => ts('Not Now'),
162 ],
163 ]);
164 }
165
166 /**
167 * Set default values for the form.
168 *
169 * @return array
170 * array of default values
171 */
172 public function setDefaultValues() {
173 return [
174 'is_notify' => 1,
175 'send_cancel_request' => 1,
176 ];
177 }
178
179 /**
180 * Process the form submission.
181 */
182 public function postProcess() {
183 $status = $message = NULL;
184 $cancelSubscription = TRUE;
185 $params = $this->controller->exportValues($this->_name);
186
187 if ($this->_selfService) {
188 // for self service force sending-request & notify
189 if ($this->_paymentProcessorObj->supports('cancelRecurring')) {
190 $params['send_cancel_request'] = 1;
191 }
192
193 if ($this->_donorEmail) {
194 $params['is_notify'] = 1;
195 }
196 }
197
198 if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) {
199 $cancelParams = ['subscriptionId' => $this->_subscriptionDetails->subscription_id];
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) {
207 try {
208 civicrm_api3('ContributionRecur', 'cancel', [
209 'id' => $this->_subscriptionDetails->recur_id,
210 'membership_id' => $this->_mid,
211 'processor_message' => $message,
212 ]);
213
214 $tplParams = [];
215 if ($this->_mid) {
216 $inputParams = ['id' => $this->_mid];
217 CRM_Member_BAO_Membership::getValues($inputParams, $tplParams);
218 $tplParams = $tplParams[$this->_mid];
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']);
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']]);
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;
231 $tplParams['contact'] = ['display_name' => $this->_donorDisplayName];
232 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
233 [
234 1 => $this->_subscriptionDetails->amount,
235 2 => $this->_subscriptionDetails->frequency_interval,
236 3 => $this->_subscriptionDetails->frequency_unit,
237 ]
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) {
245 CRM_Core_DAO::commonRetrieveAll(
246 'CRM_Contribute_DAO_ContributionPage',
247 'id',
248 $this->_subscriptionDetails->contribution_page_id,
249 $value,
250 ['title', 'receipt_from_name', 'receipt_from_email']
251 );
252 $receiptFrom
253 = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) .
254 '" <' .
255 $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] .
256 '>';
257 }
258 else {
259 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
260 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
261 }
262
263 // send notification
264 $sendTemplateParams
265 = [
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,
275 ];
276 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
277 }
278 }
279 catch (CiviCRM_API3_Exception $e) {
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();
297 $userID = $session->get('userID');
298 if ($userID && $status) {
299 $session->setStatus($status, $msgTitle, $msgType);
300 }
301 elseif (!$userID) {
302 if ($status) {
303 CRM_Utils_System::setUFMessage($status);
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',
306 "reset=1&task=cancel&result=1"));
307 }
308 }
309 }
310
311 }