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 generates form components for processing a contribution.
37 class CRM_Contribute_Form_UpdateBilling
extends CRM_Contribute_Form_ContributionRecur
{
38 protected $_mode = NULL;
40 protected $_subscriptionDetails = NULL;
42 protected $_selfService = FALSE;
44 public $_bltID = NULL;
47 * @var array current payment processor including a copy of the object in 'object' key
49 public $_paymentProcessor = array();
52 * Set variables up before form is built.
54 public function preProcess() {
55 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE);
56 $this->_crid
= CRM_Utils_Request
::retrieve('crid', 'Integer', $this, FALSE);
58 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_crid
, 'recur', 'info');
59 $this->_paymentProcessor
['object'] = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_crid
, 'recur', 'obj');
60 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_crid
);
62 // Are we cancelling a recurring contribution that is linked to an auto-renew membership?
63 if ($this->_subscriptionDetails
->membership_id
) {
64 $this->_mid
= $this->_subscriptionDetails
->membership_id
;
68 $this->_coid
= CRM_Utils_Request
::retrieve('coid', 'Integer', $this, FALSE);
70 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'info');
71 $this->_paymentProcessor
['object'] = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
72 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_coid
, 'contribution');
76 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'info');
77 $this->_paymentProcessor
['object'] = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
78 $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
79 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
80 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_mid
, 'membership_type_id');
81 $this->assign('membershipType', CRM_Utils_Array
::value($membershipTypeId, $membershipTypes));
82 $this->_mode
= 'auto_renew';
85 if ((!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
(!$this->_subscriptionDetails
)) {
86 CRM_Core_Error
::fatal('Required information missing.');
88 if (!CRM_Core_Permission
::check('edit contributions')) {
89 if ($this->_subscriptionDetails
->contact_id
!= $this->getContactID()) {
90 CRM_Core_Error
::statusBounce(ts('You do not have permission to cancel subscription.'));
92 $this->_selfService
= TRUE;
95 if (!$this->_paymentProcessor
['object']->supports('updateSubscriptionBillingInfo')) {
96 CRM_Core_Error
::fatal(ts("%1 processor doesn't support updating subscription billing details.",
97 array(1 => $this->_paymentProcessor
['object']->_processorName
)
100 $this->assign('paymentProcessor', $this->_paymentProcessor
);
102 $this->assignBillingType();
104 $this->assign('frequency_unit', $this->_subscriptionDetails
->frequency_unit
);
105 $this->assign('frequency_interval', $this->_subscriptionDetails
->frequency_interval
);
106 $this->assign('amount', $this->_subscriptionDetails
->amount
);
107 $this->assign('installments', $this->_subscriptionDetails
->installments
);
108 $this->assign('mode', $this->_mode
);
110 // handle context redirection
111 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
115 * Set the default values of various form elements.
120 public function setDefaultValues() {
121 $this->_defaults
= array();
123 if ($this->_subscriptionDetails
->contact_id
) {
129 "street_address-{$this->_bltID}",
130 "city-{$this->_bltID}",
131 "postal_code-{$this->_bltID}",
132 "country_id-{$this->_bltID}",
133 "state_province_id-{$this->_bltID}",
135 foreach ($names as $name) {
138 $fields["state_province-{$this->_bltID}"] = 1;
139 $fields["country-{$this->_bltID}"] = 1;
140 $fields["email-{$this->_bltID}"] = 1;
141 $fields['email-Primary'] = 1;
143 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_subscriptionDetails
->contact_id
, $fields, $this->_defaults
);
145 // use primary email address if billing email address is empty
146 if (empty($this->_defaults
["email-{$this->_bltID}"]) &&
147 !empty($this->_defaults
['email-Primary'])
149 $this->_defaults
["email-{$this->_bltID}"] = $this->_defaults
['email-Primary'];
152 foreach ($names as $name) {
153 if (!empty($this->_defaults
[$name])) {
154 $this->_defaults
['billing_' . $name] = $this->_defaults
[$name];
159 $config = CRM_Core_Config
::singleton();
160 // set default country from config if no country set
161 if (empty($this->_defaults
["billing_country_id-{$this->_bltID}"])) {
162 $this->_defaults
["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
165 return $this->_defaults
;
169 * Build the form object.
171 public function buildQuickForm() {
173 if ($this->_selfService
) {
177 $this->addButtons(array(
180 'name' => ts('Save'),
185 'name' => ts('Cancel'),
190 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, TRUE, TRUE);
191 $this->addFormRule(array('CRM_Contribute_Form_UpdateBilling', 'formRule'), $this);
197 * @param array $fields
198 * The input form values.
199 * @param array $files
200 * The uploaded files if any.
201 * @param CRM_Core_Form $self
205 * true if no errors, else array of errors
207 public static function formRule($fields, $files, $self) {
209 CRM_Core_Form
::validateMandatoryFields($self->_fields
, $fields, $errors);
211 // validate the payment instrument values (e.g. credit card number)
212 CRM_Core_Payment_Form
::validatePaymentInstrument($self->_paymentProcessor
['id'], $fields, $errors, NULL);
214 return empty($errors) ?
TRUE : $errors;
220 public function postProcess() {
221 $params = $this->controller
->exportValues($this->_name
);
224 // now set the values for the billing location.
225 foreach ($this->_fields
as $name => $value) {
228 $fields["email-{$this->_bltID}"] = 1;
230 $processorParams = array();
231 foreach ($params as $key => $val) {
232 $key = str_replace('billing_', '', $key);
233 list($key) = explode('-', $key);
234 $processorParams[$key] = $val;
236 $processorParams['state_province'] = CRM_Core_PseudoConstant
::stateProvince($params["billing_state_province_id-{$this->_bltID}"], FALSE);
237 $processorParams['country'] = CRM_Core_PseudoConstant
::country($params["billing_country_id-{$this->_bltID}"], FALSE);
238 $processorParams['month'] = $processorParams['credit_card_exp_date']['M'];
239 $processorParams['year'] = $processorParams['credit_card_exp_date']['Y'];
240 $processorParams['subscriptionId'] = $this->_subscriptionDetails
->subscription_id
;
241 $processorParams['amount'] = $this->_subscriptionDetails
->amount
;
242 $updateSubscription = $this->_paymentProcessor
['object']->updateSubscriptionBillingInfo($message, $processorParams);
243 if (is_a($updateSubscription, 'CRM_Core_Error')) {
244 CRM_Core_Error
::displaySessionError($updateSubscription);
246 elseif ($updateSubscription) {
247 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_subscriptionDetails
->contact_id
, 'contact_type');
248 $contact = &CRM_Contact_BAO_Contact
::createProfileContact($params,
250 $this->_subscriptionDetails
->contact_id
,
257 if ($this->_subscriptionDetails
->membership_id
) {
258 $inputParams = array('id' => $this->_subscriptionDetails
->membership_id
);
259 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
260 $tplParams = $tplParams[$this->_subscriptionDetails
->membership_id
];
261 $tplParams['membership_status'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
262 $tplParams['membershipType'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
263 $status = ts('Billing details for your automatically renewed %1 membership have been updated.',
264 array(1 => $tplParams['membershipType'])
266 $msgTitle = ts('Details Updated');
267 $msgType = 'success';
270 $status = ts('Billing details for the recurring contribution of %1, every %2 %3 have been updated.',
272 1 => $this->_subscriptionDetails
->amount
,
273 2 => $this->_subscriptionDetails
->frequency_interval
,
274 3 => $this->_subscriptionDetails
->frequency_unit
,
277 $msgTitle = ts('Details Updated');
278 $msgType = 'success';
281 'recur_frequency_interval' => $this->_subscriptionDetails
->frequency_interval
,
282 'recur_frequency_unit' => $this->_subscriptionDetails
->frequency_unit
,
283 'amount' => $this->_subscriptionDetails
->amount
,
287 // format new address for display
288 $addressParts = array("street_address", "city", "postal_code", "state_province", "country");
289 foreach ($addressParts as $part) {
290 $addressParts[$part] = CRM_Utils_Array
::value($part, $processorParams);
292 $tplParams['address'] = CRM_Utils_Address
::format($addressParts);
294 // format old address to store in activity details
295 $this->_defaults
["state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvince($this->_defaults
["state_province-{$this->_bltID}"], FALSE);
296 $this->_defaults
["country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::country($this->_defaults
["country-{$this->_bltID}"], FALSE);
297 $addressParts = array("street_address", "city", "postal_code", "state_province", "country");
298 foreach ($addressParts as $part) {
299 $key = "{$part}-{$this->_bltID}";
300 $addressParts[$part] = CRM_Utils_Array
::value($key, $this->_defaults
);
302 $this->_defaults
['address'] = CRM_Utils_Address
::format($addressParts);
304 // format new billing name
305 $name = $processorParams['first_name'];
306 if (!empty($processorParams['middle_name'])) {
307 $name .= " {$processorParams['middle_name']}";
309 $name .= ' ' . $processorParams['last_name'];
311 $tplParams['billingName'] = $name;
313 // format old billing name
314 $name = $this->_defaults
['first_name'];
315 if (!empty($this->_defaults
['middle_name'])) {
316 $name .= " {$this->_defaults['middle_name']}";
318 $name .= ' ' . $this->_defaults
['last_name'];
320 $this->_defaults
['billingName'] = $name;
323 <br/><br/>New Billing Name and Address
324 <br/>==============================
325 <br/>{$tplParams['billingName']}
326 <br/>{$tplParams['address']}
328 <br/><br/>Previous Billing Name and Address
329 <br/>==================================
330 <br/>{$this->_defaults['billingName']}
331 <br/>{$this->_defaults['address']}";
333 $activityParams = array(
334 'source_contact_id' => $this->_subscriptionDetails
->contact_id
,
335 'activity_type_id' => CRM_Core_PseudoConstant
::getKey(
336 'CRM_Activity_BAO_Activity',
338 'Update Recurring Contribution Billing Details'
340 'subject' => ts('Recurring Contribution Billing Details Updated'),
341 'details' => $message,
342 'activity_date_time' => date('YmdHis'),
343 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'),
345 $session = CRM_Core_Session
::singleton();
346 $cid = $session->get('userID');
348 $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
349 $activityParams['source_contact_id'] = $cid;
351 CRM_Activity_BAO_Activity
::create($activityParams);
354 if ($this->_subscriptionDetails
->contribution_page_id
) {
355 CRM_Core_DAO
::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
356 $this->_subscriptionDetails
->contribution_page_id
, $value, array(
359 'receipt_from_email',
362 $receiptFrom = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) . '" <' . $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] . '>';
365 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
366 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
368 list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
369 $tplParams['contact'] = array('display_name' => $donorDisplayName);
371 $date = CRM_Utils_Date
::format($processorParams['credit_card_exp_date']);
372 $tplParams['credit_card_exp_date'] = CRM_Utils_Date
::mysqlToIso($date);
373 $tplParams['credit_card_number'] = CRM_Utils_System
::mungeCreditCard($processorParams['credit_card_number']);
374 $tplParams['credit_card_type'] = $processorParams['credit_card_type'];
376 $sendTemplateParams = array(
377 'groupName' => $this->_subscriptionDetails
->membership_id ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
378 'valueName' => $this->_subscriptionDetails
->membership_id ?
'membership_autorenew_billing' : 'contribution_recurring_billing',
379 'contactId' => $this->_subscriptionDetails
->contact_id
,
380 'tplParams' => $tplParams,
381 'isTest' => $this->_subscriptionDetails
->is_test
,
382 'PDFFilename' => 'receipt.pdf',
383 'from' => $receiptFrom,
384 'toName' => $donorDisplayName,
385 'toEmail' => $donorEmail,
387 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
390 $status = ts('There was some problem updating the billing details.');
391 $msgTitle = ts('Update Error');
395 $session = CRM_Core_Session
::singleton();
396 $userID = $session->get('userID');
397 if ($userID && $status) {
398 $session->setStatus($status, $msgTitle, $msgType);
402 CRM_Utils_System
::setUFMessage($status);
404 $result = (int) ($updateSubscription && isset($ctype));
405 if (isset($tplParams)) {
406 $session->set('resultParams', $tplParams);
408 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
409 "reset=1&task=billing&result={$result}"));