3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
35 * This class provides support for canceling recurring subscriptions.
37 class CRM_Contribute_Form_CancelSubscription
extends CRM_Core_Form
{
38 protected $_paymentProcessorObj = NULL;
40 protected $_userContext = NULL;
42 protected $_mode = NULL;
44 protected $_mid = NULL;
46 protected $_coid = NULL;
48 protected $_crid = NULL;
50 protected $_selfService = FALSE;
53 * Set variables up before form is built.
55 public function preProcess() {
56 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE);
58 $this->_crid
= CRM_Utils_Request
::retrieve('crid', 'Integer', $this, FALSE);
60 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_crid
, 'recur', 'obj');
61 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_crid
);
62 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
63 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
64 $this->assign('amount', $this->_subscriptionDetails
->amount
);
65 $this->assign('installments', $this->_subscriptionDetails
->installments
);
67 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
68 if ($this->_subscriptionDetails
->membership_id
) {
69 $this->_mid
= $this->_subscriptionDetails
->membership_id
;
74 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($this->_mid
)) {
75 CRM_Core_Error
::fatal(ts('The auto renewal option for this membership looks to have been cancelled already.'));
77 $this->_mode
= 'auto_renew';
78 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
79 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
81 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
82 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_mid
, 'membership_type_id');
83 $this->assign('membershipType', CRM_Utils_Array
::value($membershipTypeId, $membershipTypes));
86 $this->_coid
= CRM_Utils_Request
::retrieve('coid', 'Integer', $this, FALSE);
88 if (CRM_Contribute_BAO_Contribution
::isSubscriptionCancelled($this->_coid
)) {
89 CRM_Core_Error
::fatal(ts('The recurring contribution looks to have been cancelled already.'));
91 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
92 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_coid
, 'contribution');
94 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
95 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
96 $this->assign('amount', $this->_subscriptionDetails
->amount
);
97 $this->assign('installments', $this->_subscriptionDetails
->installments
);
101 (!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
102 ($this->_subscriptionDetails
== CRM_Core_DAO
::$_nullObject)
104 CRM_Core_Error
::fatal('Required information missing.');
107 if (!CRM_Core_Permission
::check('edit contributions')) {
108 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this, FALSE);
109 if (!CRM_Contact_BAO_Contact_Utils
::validChecksum($this->_subscriptionDetails
->contact_id
, $userChecksum)) {
110 CRM_Core_Error
::fatal(ts('You do not have permission to cancel this recurring contribution.'));
112 $this->_selfService
= TRUE;
114 $this->assign('self_service', $this->_selfService
);
116 // handle context redirection
117 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
119 CRM_Utils_System
::setTitle($this->_mid ?
ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
120 $this->assign('mode', $this->_mode
);
122 if ($this->_subscriptionDetails
->contact_id
) {
123 list($this->_donorDisplayName
, $this->_donorEmail
)
124 = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
129 * Build the form object.
131 public function buildQuickForm() {
132 // Determine if we can cancel recurring contribution via API with this processor
133 $cancelSupported = $this->_paymentProcessorObj
->isSupported('cancelSubscription');
134 if ($cancelSupported) {
135 $searchRange = array();
136 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
137 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
141 'send_cancel_request',
142 ts('Send cancellation request to %1 ?',
143 array(1 => $this->_paymentProcessorObj
->_processorName
))
146 $this->assign('cancelSupported', $cancelSupported);
148 if ($this->_donorEmail
) {
149 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
152 $cancelButton = ts('Cancel Automatic Membership Renewal');
155 $cancelButton = ts('Cancel Recurring Contribution');
159 if ($this->_selfService
) {
163 $this->addButtons(array(
166 'name' => $cancelButton,
167 'spacing' => ' ',
172 'name' => ts('Not Now'),
179 * Set default values for the form.
182 * array of default values
184 public function setDefaultValues() {
187 'send_cancel_request' => 1,
192 * Process the form submission.
194 public function postProcess() {
195 $status = $message = NULL;
196 $cancelSubscription = TRUE;
197 $params = $this->controller
->exportValues($this->_name
);
199 if ($this->_selfService
) {
200 // for self service force sending-request & notify
201 if ($this->_paymentProcessorObj
->isSupported('cancelSubscription')) {
202 $params['send_cancel_request'] = 1;
205 if ($this->_donorEmail
) {
206 $params['is_notify'] = 1;
210 if (CRM_Utils_Array
::value('send_cancel_request', $params) == 1) {
211 $cancelParams = array('subscriptionId' => $this->_subscriptionDetails
->subscription_id
);
212 $cancelSubscription = $this->_paymentProcessorObj
->cancelSubscription($message, $cancelParams);
215 if (is_a($cancelSubscription, 'CRM_Core_Error')) {
216 CRM_Core_Error
::displaySessionError($cancelSubscription);
218 elseif ($cancelSubscription) {
221 'subject' => $this->_mid ?
ts('Auto-renewal membership cancelled') : ts('Recurring contribution cancelled'),
222 'details' => $message,
224 $cancelStatus = CRM_Contribute_BAO_ContributionRecur
::cancelRecurContribution(
225 $this->_subscriptionDetails
->recur_id
,
226 CRM_Core_DAO
::$_nullObject,
231 $tplParams = array();
233 $inputParams = array('id' => $this->_mid
);
234 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
235 $tplParams = $tplParams[$this->_mid
];
236 $tplParams['membership_status']
237 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
238 $tplParams['membershipType']
239 = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
240 $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']));
241 $msgTitle = 'Membership Renewal Cancelled';
245 $tplParams['recur_frequency_interval'] = $this->_subscriptionDetails
->frequency_interval
;
246 $tplParams['recur_frequency_unit'] = $this->_subscriptionDetails
->frequency_unit
;
247 $tplParams['amount'] = $this->_subscriptionDetails
->amount
;
248 $tplParams['contact'] = array('display_name' => $this->_donorDisplayName
);
249 $status = ts('The recurring contribution of %1, every %2 %3 has been cancelled.',
251 1 => $this->_subscriptionDetails
->amount
,
252 2 => $this->_subscriptionDetails
->frequency_interval
,
253 3 => $this->_subscriptionDetails
->frequency_unit
,
256 $msgTitle = 'Contribution Cancelled';
257 $msgType = 'success';
260 if (CRM_Utils_Array
::value('is_notify', $params) == 1) {
261 if ($this->_subscriptionDetails
->contribution_page_id
) {
262 CRM_Core_DAO
::commonRetrieveAll(
263 'CRM_Contribute_DAO_ContributionPage',
265 $this->_subscriptionDetails
->contribution_page_id
,
267 array('title', 'receipt_from_name', 'receipt_from_email')
270 = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) .
272 $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] .
276 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
277 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
283 'groupName' => $this->_mode
== 'auto_renew' ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
284 'valueName' => $this->_mode
== 'auto_renew' ?
'membership_autorenew_cancelled' : 'contribution_recurring_cancelled',
285 'contactId' => $this->_subscriptionDetails
->contact_id
,
286 'tplParams' => $tplParams,
287 //'isTest' => $isTest, set this from _objects
288 'PDFFilename' => 'receipt.pdf',
289 'from' => $receiptFrom,
290 'toName' => $this->_donorDisplayName
,
291 'toEmail' => $this->_donorEmail
,
293 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
298 $msgTitle = ts('Error');
299 if ($params['send_cancel_request'] == 1) {
300 $status = ts('Recurring contribution was cancelled successfully by the processor, but could not be marked as cancelled in the database.');
303 $status = ts('Recurring contribution could not be cancelled in the database.');
308 $status = ts('The recurring contribution could not be cancelled.');
309 $msgTitle = 'Error Cancelling Contribution';
313 $session = CRM_Core_Session
::singleton();
314 $userID = $session->get('userID');
315 if ($userID && $status) {
316 $session->setStatus($status, $msgTitle, $msgType);
320 CRM_Utils_System
::setUFMessage($status);
321 // keep result as 1, since we not displaying anything on the redirected page anyway
322 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
323 "reset=1&task=cancel&result=1"));