3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
11 use Civi\Payment\PropertyBag
;
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This class provides support for canceling recurring subscriptions.
22 class CRM_Contribute_Form_CancelSubscription
extends CRM_Contribute_Form_ContributionRecur
{
24 protected $_userContext = NULL;
26 protected $_mode = NULL;
29 * Should custom data be suppressed on this form.
31 * We override to suppress custom data because historically it has not been
32 * shown on this form & we don't want to expose it as a by-product of
33 * other change without establishing that it would be good on this form.
37 protected function isSuppressCustomData() {
42 * Set variables up before form is built.
44 * @throws \CRM_Core_Exception
46 public function preProcess() {
49 $this->assign('frequency_unit', $this->getSubscriptionDetails()->frequency_unit
);
50 $this->assign('frequency_interval', $this->getSubscriptionDetails()->frequency_interval
);
51 $this->assign('amount', $this->getSubscriptionDetails()->amount
);
52 $this->assign('installments', $this->getSubscriptionDetails()->installments
);
54 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
55 if ($this->getSubscriptionDetails()->membership_id
) {
56 $this->_mid
= $this->getSubscriptionDetails()->membership_id
;
61 $this->_mode
= 'auto_renew';
62 // CRM-18468: crid is more accurate than mid for getting
63 // subscriptionDetails, so don't get them again.
65 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
66 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_mid
, 'membership_type_id');
67 $this->assign('membershipType', CRM_Utils_Array
::value($membershipTypeId, $membershipTypes));
71 if (CRM_Contribute_BAO_Contribution
::isSubscriptionCancelled($this->_coid
)) {
72 CRM_Core_Error
::statusBounce(ts('The recurring contribution looks to have been cancelled already.'));
74 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
76 $this->assign('frequency_unit', $this->getSubscriptionDetails()->frequency_unit
);
77 $this->assign('frequency_interval', $this->getSubscriptionDetails()->frequency_interval
);
78 $this->assign('amount', $this->getSubscriptionDetails()->amount
);
79 $this->assign('installments', $this->getSubscriptionDetails()->installments
);
83 (!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
84 (!$this->getSubscriptionDetails())
86 CRM_Core_Error
::statusBounce('Required information missing.');
89 // handle context redirection
90 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
92 CRM_Utils_System
::setTitle($this->_mid ?
ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
93 $this->assign('mode', $this->_mode
);
95 if ($this->isSelfService()) {
96 unset($this->entityFields
['send_cancel_request'], $this->entityFields
['is_notify']);
99 if ($this->getSubscriptionDetails()->contact_id
) {
100 list($this->_donorDisplayName
, $this->_donorEmail
)
101 = CRM_Contact_BAO_Contact
::getContactDetails($this->getSubscriptionDetails()->contact_id
);
106 * Set entity fields for this cancellation.
108 public function setEntityFields() {
109 $this->entityFields
= [
110 'cancel_reason' => ['name' => 'cancel_reason'],
112 $this->entityFields
['send_cancel_request'] = [
113 'title' => ts('Send cancellation request?'),
114 'name' => 'send_cancel_request',
115 'not-auto-addable' => TRUE,
117 $this->entityFields
['is_notify'] = [
118 'title' => ts('Notify Contributor?'),
119 'name' => 'is_notify',
120 'not-auto-addable' => TRUE,
125 * Build the form object.
127 public function buildQuickForm() {
128 $this->buildQuickEntityForm();
129 // Determine if we can cancel recurring contribution via API with this processor
130 $cancelSupported = $this->_paymentProcessorObj
->supports('CancelRecurring');
131 if ($cancelSupported) {
133 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
134 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
138 'send_cancel_request',
139 ts('Send cancellation request to %1 ?',
140 [1 => $this->_paymentProcessorObj
->_processorName
])
143 $this->assign('cancelSupported', $cancelSupported);
145 if ($this->_donorEmail
) {
146 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
149 $cancelButton = ts('Cancel Automatic Membership Renewal');
152 $cancelButton = ts('Cancel Recurring Contribution');
156 if ($this->isSelfService()) {
163 'name' => $cancelButton,
164 'spacing' => ' ',
169 'name' => ts('Not Now'),
175 * Set default values for the form.
178 * array of default values
180 public function setDefaultValues() {
183 'send_cancel_request' => 1,
188 * Process the form submission.
190 public function postProcess() {
192 $cancelSubscription = TRUE;
193 $params = $this->controller
->exportValues($this->_name
);
195 if ($this->isSelfService()) {
196 // for self service force sending-request & notify
197 if ($this->_paymentProcessorObj
->supports('cancelRecurring')) {
198 $params['send_cancel_request'] = 1;
201 if ($this->_donorEmail
) {
202 $params['is_notify'] = 1;
206 if (CRM_Utils_Array
::value('send_cancel_request', $params) == 1) {
208 $propertyBag = new PropertyBag();
209 $propertyBag->setContributionRecurID($this->getSubscriptionDetails()->recur_id
);
210 $propertyBag->setRecurProcessorID($this->getSubscriptionDetails()->subscription_id
);
211 $message = $this->_paymentProcessorObj
->doCancelRecurring($propertyBag)['message'];
213 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
214 CRM_Core_Error
::statusBounce($e->getMessage());
218 if ($cancelSubscription) {
220 civicrm_api3('ContributionRecur', 'cancel', [
221 'id' => $this->getSubscriptionDetails()->recur_id
,
222 'membership_id' => $this->_mid
,
223 'processor_message' => $message,
224 'cancel_reason' => $params['cancel_reason'],
229 $inputParams = ['id' => $this->_mid
];
230 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
231 $tplParams = $tplParams[$this->_mid
];
232 $tplParams['membership_status']
233 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
234 $tplParams['membershipType']
235 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
236 $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']]);
237 $msgTitle = 'Membership Renewal Cancelled';
241 $tplParams['recur_frequency_interval'] = $this->getSubscriptionDetails()->frequency_interval
;
242 $tplParams['recur_frequency_unit'] = $this->getSubscriptionDetails()->frequency_unit
;
243 $tplParams['amount'] = $this->getSubscriptionDetails()->amount
;
244 $tplParams['contact'] = ['display_name' => $this->_donorDisplayName
];
245 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
247 1 => $this->getSubscriptionDetails()->amount
,
248 2 => $this->getSubscriptionDetails()->frequency_interval
,
249 3 => $this->getSubscriptionDetails()->frequency_unit
,
252 $msgTitle = 'Contribution Cancelled';
253 $msgType = 'success';
256 if (CRM_Utils_Array
::value('is_notify', $params) == 1) {
257 if ($this->getSubscriptionDetails()->contribution_page_id
) {
258 CRM_Core_DAO
::commonRetrieveAll(
259 'CRM_Contribute_DAO_ContributionPage',
261 $this->getSubscriptionDetails()->contribution_page_id
,
263 ['title', 'receipt_from_name', 'receipt_from_email']
266 = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id
]) .
268 $value[$this->getSubscriptionDetails()->contribution_page_id
]['receipt_from_email'] .
272 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
273 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
279 'groupName' => $this->_mode
== 'auto_renew' ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
280 'valueName' => $this->_mode
== 'auto_renew' ?
'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
281 'contactId' => $this->getSubscriptionDetails()->contact_id
,
282 'tplParams' => $tplParams,
283 //'isTest' => $isTest, set this from _objects
284 'PDFFilename' => 'receipt.pdf',
285 'from' => $receiptFrom,
286 'toName' => $this->_donorDisplayName
,
287 'toEmail' => $this->_donorEmail
,
289 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
292 catch (CiviCRM_API3_Exception
$e) {
294 $msgTitle = ts('Error');
295 if ($params['send_cancel_request'] == 1) {
296 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
299 $status = ts('Recurring contribution could not be cancelled in the database.');
304 $status = ts('The recurring contribution could not be cancelled.');
305 $msgTitle = 'Error Cancelling Contribution';
309 $session = CRM_Core_Session
::singleton();
310 $userID = $session->get('userID');
311 if ($userID && $status) {
312 $session->setStatus($status, $msgTitle, $msgType);
316 CRM_Utils_System
::setUFMessage($status);
317 // keep result as 1, since we not displaying anything on the redirected page anyway
318 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
319 "reset=1&task=cancel&result=1"));