3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This class provides support for canceling recurring subscriptions
40 class CRM_Contribute_Form_CancelSubscription
extends CRM_Core_Form
{
41 protected $_paymentProcessorObj = NULL;
43 protected $_userContext = NULL;
45 protected $_mode = NULL;
47 protected $_mid = NULL;
49 protected $_coid = NULL;
51 protected $_crid = NULL;
53 protected $_selfService = FALSE;
56 * Set variables up before form is built.
60 public function preProcess() {
61 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE);
63 $this->_crid
= CRM_Utils_Request
::retrieve('crid', 'Integer', $this, FALSE);
65 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_crid
, 'recur', 'obj');
66 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_crid
);
67 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
68 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
69 $this->assign('amount', $this->_subscriptionDetails
->amount
);
70 $this->assign('installments', $this->_subscriptionDetails
->installments
);
72 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
73 if ($this->_subscriptionDetails
->membership_id
) {
74 $this->_mid
= $this->_subscriptionDetails
->membership_id
;
79 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($this->_mid
)) {
80 CRM_Core_Error
::fatal(ts('The auto renewal option for this membership looks to have been cancelled already.'));
82 $this->_mode
= 'auto_renew';
83 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
84 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
86 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
87 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_mid
, 'membership_type_id');
88 $this->assign('membershipType', CRM_Utils_Array
::value($membershipTypeId, $membershipTypes));
91 $this->_coid
= CRM_Utils_Request
::retrieve('coid', 'Integer', $this, FALSE);
93 if (CRM_Contribute_BAO_Contribution
::isSubscriptionCancelled($this->_coid
)) {
94 CRM_Core_Error
::fatal(ts('The recurring contribution looks to have been cancelled already.'));
96 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
97 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_coid
, 'contribution');
99 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
100 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
101 $this->assign('amount', $this->_subscriptionDetails
->amount
);
102 $this->assign('installments', $this->_subscriptionDetails
->installments
);
106 (!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
107 ($this->_subscriptionDetails
== CRM_Core_DAO
::$_nullObject)
109 CRM_Core_Error
::fatal('Required information missing.');
112 if (!CRM_Core_Permission
::check('edit contributions')) {
113 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this, FALSE);
114 if (!CRM_Contact_BAO_Contact_Utils
::validChecksum($this->_subscriptionDetails
->contact_id
, $userChecksum)) {
115 CRM_Core_Error
::fatal(ts('You do not have permission to cancel this recurring contribution.'));
117 $this->_selfService
= TRUE;
119 $this->assign('self_service', $this->_selfService
);
121 // handle context redirection
122 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
124 CRM_Utils_System
::setTitle($this->_mid ?
ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
125 $this->assign('mode', $this->_mode
);
127 if ($this->_subscriptionDetails
->contact_id
) {
128 list($this->_donorDisplayName
, $this->_donorEmail
)
129 = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
134 * Build the form object.
138 public function buildQuickForm() {
139 // Determine if we can cancel recurring contribution via API with this processor
140 $cancelSupported = $this->_paymentProcessorObj
->isSupported('cancelSubscription');
141 if ($cancelSupported) {
142 $searchRange = array();
143 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
144 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
148 'send_cancel_request',
149 ts('Send cancellation request to %1 ?',
150 array(1 => $this->_paymentProcessorObj
->_processorName
))
153 $this->assign('cancelSupported', $cancelSupported);
155 if ($this->_donorEmail
) {
156 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
159 $cancelButton = ts('Cancel Automatic Membership Renewal');
162 $cancelButton = ts('Cancel Recurring Contribution');
166 if ($this->_selfService
) {
170 $this->addButtons(array(
173 'name' => $cancelButton,
174 'spacing' => ' ',
179 'name' => ts('Not Now'),
186 * Set default values for the form. Note that in edit/view mode
187 * the default values are retrieved from the database
190 * array of default values
192 public function setDefaultValues() {
193 $defaults = array('is_notify' => 1);
198 * Process the form submission.
203 public function postProcess() {
204 $status = $message = NULL;
205 $cancelSubscription = TRUE;
206 $params = $this->controller
->exportValues($this->_name
);
208 if ($this->_selfService
) {
209 // for self service force sending-request & notify
210 if ($this->_paymentProcessorObj
->isSupported('cancelSubscription')) {
211 $params['send_cancel_request'] = 1;
214 if ($this->_donorEmail
) {
215 $params['is_notify'] = 1;
219 if (CRM_Utils_Array
::value('send_cancel_request', $params) == 1) {
220 $cancelParams = array('subscriptionId' => $this->_subscriptionDetails
->subscription_id
);
221 $cancelSubscription = $this->_paymentProcessorObj
->cancelSubscription($message, $cancelParams);
224 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
225 CRM_Core_Error
::displaySessionError($cancelSubscription);
227 elseif ($cancelSubscription) {
230 'subject' => $this->_mid ?
ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
231 'details' => $message,
233 $cancelStatus = CRM_Contribute_BAO_ContributionRecur
::cancelRecurContribution(
234 $this->_subscriptionDetails
->recur_id
,
235 CRM_Core_DAO
::$_nullObject,
240 $tplParams = array();
242 $inputParams = array('id' => $this->_mid
);
243 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
244 $tplParams = $tplParams[$this->_mid
];
245 $tplParams['membership_status']
246 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
247 $tplParams['membershipType']
248 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
249 $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.', array(1 => $tplParams['membershipType']));
250 $msgTitle = 'Membership Renewal Cancelled';
254 $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails
->frequency_interval
;
255 $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails
->frequency_unit
;
256 $tplParams['amount'] = $this->_subscriptionDetails
->amount
;
257 $tplParams['contact'] = array('display_name' => $this->_donorDisplayName
);
258 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
260 1 => $this->_subscriptionDetails
->amount
,
261 2 => $this->_subscriptionDetails
->frequency_interval
,
262 3 => $this->_subscriptionDetails
->frequency_unit
,
265 $msgTitle = 'Contribution Cancelled';
266 $msgType = 'success';
269 if (CRM_Utils_Array
::value('is_notify', $params) == 1) {
270 if ($this->_subscriptionDetails
->contribution_page_id
) {
271 CRM_Core_DAO
::commonRetrieveAll(
272 'CRM_Contribute_DAO_ContributionPage',
274 $this->_subscriptionDetails
->contribution_page_id
,
276 array('title', 'receipt_from_name', 'receipt_from_email')
279 = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) .
281 $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] .
285 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
286 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
292 'groupName' => $this->_mode
== 'auto_renew' ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
293 'valueName' => $this->_mode
== 'auto_renew' ?
'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
294 'contactId' => $this->_subscriptionDetails
->contact_id
,
295 'tplParams' => $tplParams,
296 //'isTest' => $isTest, set this from _objects
297 'PDFFilename' => 'receipt.pdf',
298 'from' => $receiptFrom,
299 'toName' => $this->_donorDisplayName
,
300 'toEmail' => $this->_donorEmail
,
302 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
307 $msgTitle = ts('Error');
308 if ($params['send_cancel_request'] == 1) {
309 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
312 $status = ts('Recurring contribution could not be cancelled in the database.');
317 $status = ts('The recurring contribution could not be cancelled.');
318 $msgTitle = 'Error Cancelling Contribution';
322 $session = CRM_Core_Session
::singleton();
323 $userID = $session->get('userID');
324 if ($userID && $status) {
325 $session->setStatus($status, $msgTitle, $msgType);
329 CRM_Utils_System
::setUFMessage($status);
330 // keep result as 1, since we not displaying anything on the redirected page anyway
331 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
332 "reset=1&task=cancel&result=1"));