3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class provides support for canceling recurring subscriptions.
37 class CRM_Contribute_Form_CancelSubscription
extends CRM_Contribute_Form_ContributionRecur
{
38 protected $_paymentProcessorObj = NULL;
40 protected $_userContext = NULL;
42 protected $_mode = NULL;
44 protected $_selfService = FALSE;
47 * Set variables up before form is built.
49 public function preProcess() {
52 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_crid
, 'recur', 'obj');
53 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_crid
);
54 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
55 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
56 $this->assign('amount', $this->_subscriptionDetails
->amount
);
57 $this->assign('installments', $this->_subscriptionDetails
->installments
);
59 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
60 if ($this->_subscriptionDetails
->membership_id
) {
61 $this->_mid
= $this->_subscriptionDetails
->membership_id
;
66 $this->_mode
= 'auto_renew';
67 // CRM-18468: crid is more accurate than mid for getting
68 // subscriptionDetails, so don't get them again.
70 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
71 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
74 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
75 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_mid
, 'membership_type_id');
76 $this->assign('membershipType', CRM_Utils_Array
::value($membershipTypeId, $membershipTypes));
80 if (CRM_Contribute_BAO_Contribution
::isSubscriptionCancelled($this->_coid
)) {
81 CRM_Core_Error
::fatal(ts('The recurring contribution looks to have been cancelled already.'));
83 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
84 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_coid
, 'contribution');
86 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
87 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
88 $this->assign('amount', $this->_subscriptionDetails
->amount
);
89 $this->assign('installments', $this->_subscriptionDetails
->installments
);
93 (!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
94 (!$this->_subscriptionDetails
)
96 CRM_Core_Error
::fatal('Required information missing.');
99 if (!CRM_Core_Permission
::check('edit contributions')) {
100 if ($this->_subscriptionDetails
->contact_id
!= $this->getContactID()) {
101 CRM_Core_Error
::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
103 $this->_selfService
= TRUE;
105 $this->assign('self_service', $this->_selfService
);
107 // handle context redirection
108 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
110 CRM_Utils_System
::setTitle($this->_mid ?
ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
111 $this->assign('mode', $this->_mode
);
113 if ($this->_subscriptionDetails
->contact_id
) {
114 list($this->_donorDisplayName
, $this->_donorEmail
)
115 = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
120 * Build the form object.
122 public function buildQuickForm() {
123 // Determine if we can cancel recurring contribution via API with this processor
124 $cancelSupported = $this->_paymentProcessorObj
->supports('CancelRecurring');
125 if ($cancelSupported) {
127 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
128 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
132 'send_cancel_request',
133 ts('Send cancellation request to %1 ?',
134 [1 => $this->_paymentProcessorObj
->_processorName
])
137 $this->assign('cancelSupported', $cancelSupported);
139 if ($this->_donorEmail
) {
140 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
143 $cancelButton = ts('Cancel Automatic Membership Renewal');
146 $cancelButton = ts('Cancel Recurring Contribution');
150 if ($this->_selfService
) {
157 'name' => $cancelButton,
158 'spacing' => ' ',
163 'name' => ts('Not Now'),
170 * Set default values for the form.
173 * array of default values
175 public function setDefaultValues() {
178 'send_cancel_request' => 1,
183 * Process the form submission.
185 public function postProcess() {
186 $status = $message = NULL;
187 $cancelSubscription = TRUE;
188 $params = $this->controller
->exportValues($this->_name
);
190 if ($this->_selfService
) {
191 // for self service force sending-request & notify
192 if ($this->_paymentProcessorObj
->supports('cancelRecurring')) {
193 $params['send_cancel_request'] = 1;
196 if ($this->_donorEmail
) {
197 $params['is_notify'] = 1;
201 if (CRM_Utils_Array
::value('send_cancel_request', $params) == 1) {
202 $cancelParams = ['subscriptionId' => $this->_subscriptionDetails
->subscription_id
];
203 $cancelSubscription = $this->_paymentProcessorObj
->cancelSubscription($message, $cancelParams);
206 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
207 CRM_Core_Error
::displaySessionError($cancelSubscription);
209 elseif ($cancelSubscription) {
212 'subject' => $this->_mid ?
ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
213 'details' => $message,
215 $cancelStatus = CRM_Contribute_BAO_ContributionRecur
::cancelRecurContribution(
216 $this->_subscriptionDetails
->recur_id
,
223 $inputParams = ['id' => $this->_mid
];
224 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
225 $tplParams = $tplParams[$this->_mid
];
226 $tplParams['membership_status']
227 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
228 $tplParams['membershipType']
229 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
230 $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']]);
231 $msgTitle = 'Membership Renewal Cancelled';
235 $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails
->frequency_interval
;
236 $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails
->frequency_unit
;
237 $tplParams['amount'] = $this->_subscriptionDetails
->amount
;
238 $tplParams['contact'] = ['display_name' => $this->_donorDisplayName
];
239 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
241 1 => $this->_subscriptionDetails
->amount
,
242 2 => $this->_subscriptionDetails
->frequency_interval
,
243 3 => $this->_subscriptionDetails
->frequency_unit
,
246 $msgTitle = 'Contribution Cancelled';
247 $msgType = 'success';
250 if (CRM_Utils_Array
::value('is_notify', $params) == 1) {
251 if ($this->_subscriptionDetails
->contribution_page_id
) {
252 CRM_Core_DAO
::commonRetrieveAll(
253 'CRM_Contribute_DAO_ContributionPage',
255 $this->_subscriptionDetails
->contribution_page_id
,
257 ['title', 'receipt_from_name', 'receipt_from_email']
260 = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) .
262 $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] .
266 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
267 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
273 'groupName' => $this->_mode
== 'auto_renew' ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
274 'valueName' => $this->_mode
== 'auto_renew' ?
'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
275 'contactId' => $this->_subscriptionDetails
->contact_id
,
276 'tplParams' => $tplParams,
277 //'isTest' => $isTest, set this from _objects
278 'PDFFilename' => 'receipt.pdf',
279 'from' => $receiptFrom,
280 'toName' => $this->_donorDisplayName
,
281 'toEmail' => $this->_donorEmail
,
283 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
288 $msgTitle = ts('Error');
289 if ($params['send_cancel_request'] == 1) {
290 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
293 $status = ts('Recurring contribution could not be cancelled in the database.');
298 $status = ts('The recurring contribution could not be cancelled.');
299 $msgTitle = 'Error Cancelling Contribution';
303 $session = CRM_Core_Session
::singleton();
304 $userID = $session->get('userID');
305 if ($userID && $status) {
306 $session->setStatus($status, $msgTitle, $msgType);
310 CRM_Utils_System
::setUFMessage($status);
311 // keep result as 1, since we not displaying anything on the redirected page anyway
312 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
313 "reset=1&task=cancel&result=1"));