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
{
39 protected $_userContext = NULL;
41 protected $_mode = NULL;
43 protected $_selfService = FALSE;
46 * Set variables up before form is built.
48 * @throws \CRM_Core_Exception
50 public function preProcess() {
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
);
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
;
65 $this->_mode
= 'auto_renew';
66 // CRM-18468: crid is more accurate than mid for getting
67 // subscriptionDetails, so don't get them again.
69 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
70 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
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));
79 if (CRM_Contribute_BAO_Contribution
::isSubscriptionCancelled($this->_coid
)) {
80 CRM_Core_Error
::fatal(ts('The recurring contribution looks to have been cancelled already.'));
82 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
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
);
91 (!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
92 (!$this->_subscriptionDetails
)
94 CRM_Core_Error
::fatal('Required information missing.');
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.'));
101 $this->_selfService
= TRUE;
103 $this->assign('self_service', $this->_selfService
);
105 // handle context redirection
106 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
108 CRM_Utils_System
::setTitle($this->_mid ?
ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
109 $this->assign('mode', $this->_mode
);
111 if ($this->_subscriptionDetails
->contact_id
) {
112 list($this->_donorDisplayName
, $this->_donorEmail
)
113 = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
118 * Build the form object.
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) {
125 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
126 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
130 'send_cancel_request',
131 ts('Send cancellation request to %1 ?',
132 [1 => $this->_paymentProcessorObj
->_processorName
])
135 $this->assign('cancelSupported', $cancelSupported);
137 if ($this->_donorEmail
) {
138 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
141 $cancelButton = ts('Cancel Automatic Membership Renewal');
144 $cancelButton = ts('Cancel Recurring Contribution');
148 if ($this->_selfService
) {
155 'name' => $cancelButton,
156 'spacing' => ' ',
161 'name' => ts('Not Now'),
167 * Set default values for the form.
170 * array of default values
172 public function setDefaultValues() {
175 'send_cancel_request' => 1,
180 * Process the form submission.
182 public function postProcess() {
183 $status = $message = NULL;
184 $cancelSubscription = TRUE;
185 $params = $this->controller
->exportValues($this->_name
);
187 if ($this->_selfService
) {
188 // for self service force sending-request & notify
189 if ($this->_paymentProcessorObj
->supports('cancelRecurring')) {
190 $params['send_cancel_request'] = 1;
193 if ($this->_donorEmail
) {
194 $params['is_notify'] = 1;
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);
203 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
204 CRM_Core_Error
::displaySessionError($cancelSubscription);
206 elseif ($cancelSubscription) {
208 civicrm_api3('ContributionRecur', 'cancel', [
209 'id' => $this->_subscriptionDetails
->recur_id
,
210 'membership_id' => $this->_mid
,
211 'processor_message' => $message,
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';
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.',
234 1 => $this->_subscriptionDetails
->amount
,
235 2 => $this->_subscriptionDetails
->frequency_interval
,
236 3 => $this->_subscriptionDetails
->frequency_unit
,
239 $msgTitle = 'Contribution Cancelled';
240 $msgType = 'success';
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',
248 $this->_subscriptionDetails
->contribution_page_id
,
250 ['title', 'receipt_from_name', 'receipt_from_email']
253 = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) .
255 $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] .
259 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
260 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
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
,
276 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
279 catch (CiviCRM_API3_Exception
$e) {
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.');
286 $status = ts('Recurring contribution could not be cancelled in the database.');
291 $status = ts('The recurring contribution could not be cancelled.');
292 $msgTitle = 'Error Cancelling Contribution';
296 $session = CRM_Core_Session
::singleton();
297 $userID = $session->get('userID');
298 if ($userID && $status) {
299 $session->setStatus($status, $msgTitle, $msgType);
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"));