3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 usefusul, 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-2014
37 * This class generates form components for processing a contribution
40 class CRM_Contribute_Form_AbstractEditPayment
extends CRM_Core_Form
{
50 * @var array current payment processor including a copy of the object in 'object' key
52 public $_paymentProcessor;
53 public $_recurPaymentProcessors;
56 * Array of processor options in the format id => array($id => $label)
57 * WARNING it appears that the format used to differ to this and there are places in the code that
58 * expect the old format. $this->_paymentProcessors provides the additional data which this
59 * array seems to have provided in the past
65 * Available payment processors with full details including the key 'object' indexed by their id
68 protected $_paymentProcessors = array();
70 * The id of the contribution that we are processing
77 * The id of the premium that we are proceessing
81 public $_premiumID = NULL;
84 * @var CRM_Contribute_DAO_ContributionProduct
86 public $_productDAO = NULL;
96 * The id of the contact associated with this contribution
103 * The id of the pledge payment that we are processing
110 * The id of the pledge that we are processing
117 * Is this contribution associated with an online
118 * financial transaction
122 public $_online = FALSE;
125 * Stores all product option
132 * Stores the honor id
136 public $_honorID = NULL;
139 * Store the financial Type ID
143 public $_contributionType;
146 * The contribution values if an existing contribution
151 * The pledge values if this contribution is associated with pledge
153 public $_pledgeValues;
155 public $_contributeMode = 'direct';
162 * Store the line items if price set used.
167 * Is this a backoffice form
168 * (this will affect whether paypal express code is displayed)
171 public $isBackOffice = TRUE;
173 protected $_formType;
176 * @var mystery variable screaming out for documentation
181 * Array of fields to display on billingBlock.tpl - this is not fully implemented but basically intent is the panes/fieldsets on this page should
182 * be all in this array in order like
183 * 'credit_card' => array('credit_card_number' ...
184 * 'billing_details' => array('first_name' ...
186 * such that both the fields and the order can be more easily altered by payment processors & other extensions
189 public $billingFieldSets = array();
194 public function showRecordLinkMesssage($id) {
195 $statusId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
196 if (CRM_Contribute_PseudoConstant
::contributionStatus($statusId, 'name') == 'Partially paid') {
197 if ($pid = CRM_Core_DAO
::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
198 $recordPaymentLink = CRM_Utils_System
::url('civicrm/payment',
199 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
201 CRM_Core_Session
::setStatus(ts('Please use the <a href="%1">Record Payment</a> form if you have received an additional payment for this Partially paid contribution record.', array(1 => $recordPaymentLink)), ts('Notice'), 'alert');
210 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
212 $params = array('id' => $id);
213 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
215 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
216 $this->_online
= FALSE;
217 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
218 if (!empty($fids['financialTrxnId'])) {
219 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
222 // Also don't allow user to update some fields for recurring contributions.
223 if (!$this->_online
) {
224 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
227 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
230 $daoNote = new CRM_Core_BAO_Note();
231 $daoNote->entity_table
= 'civicrm_contribution';
232 $daoNote->entity_id
= $id;
233 if ($daoNote->find(TRUE)) {
234 $this->_noteID
= $daoNote->id
;
235 $values['note'] = $daoNote->note
;
237 $this->_contributionType
= $values['financial_type_id'];
241 * @param string $type
243 * @param string $subType
244 * @param int $entityId
246 public function applyCustomData($type, $subType, $entityId) {
247 $this->set('type', $type);
248 $this->set('subType', $subType);
249 $this->set('entityId', $entityId);
251 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
252 CRM_Custom_Form_CustomData
::buildQuickForm($this);
253 CRM_Custom_Form_CustomData
::setDefaultValues($this);
258 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
260 public function assignPremiumProduct($id) { //to get Premium id
263 FROM civicrm_contribution_product
264 WHERE contribution_id = {$id}
266 $dao = CRM_Core_DAO
::executeQuery($sql,
267 CRM_Core_DAO
::$_nullArray
270 $this->_premiumID
= $dao->id
;
271 $this->_productDAO
= $dao;
277 * This function process contribution related objects.
279 * @param int $contributionId
280 * @param int $statusId
281 * @param int|null $previousStatusId
283 * @return null|string
285 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
287 if (!$contributionId ||
!$statusId) {
292 'contribution_id' => $contributionId,
293 'contribution_status_id' => $statusId,
294 'previous_contribution_status_id' => $previousStatusId,
297 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
299 if (!is_array($updateResult) ||
300 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
301 !is_array($updatedComponents) ||
302 empty($updatedComponents)
307 // get the user display name.
309 SELECT display_name as displayName
311 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
312 WHERE civicrm_contribution.id = {$contributionId}";
313 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
315 // get the status message for user.
316 foreach ($updatedComponents as $componentName => $updatedStatusId) {
318 if ($componentName == 'CiviMember') {
319 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
320 CRM_Member_PseudoConstant
::membershipStatus()
322 if ($updatedStatusName == 'Cancelled') {
323 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
325 elseif ($updatedStatusName == 'Expired') {
326 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
329 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
331 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
333 1 => $userDisplayName,
341 if ($componentName == 'CiviEvent') {
342 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
343 CRM_Event_PseudoConstant
::participantStatus()
345 if ($updatedStatusName == 'Cancelled') {
346 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
348 elseif ($updatedStatusName == 'Registered') {
349 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
353 if ($componentName == 'CiviPledge') {
354 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
355 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
357 if ($updatedStatusName == 'Cancelled') {
358 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
360 elseif ($updatedStatusName == 'Failed') {
361 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
363 elseif ($updatedStatusName == 'Completed') {
364 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
373 * @return array of valid processors. The array resembles the DB table but also has 'object' as a key
376 public function getValidProcessors() {
378 $capabilities = array('BackOffice');
380 $capabilities[] = (ucfirst($this->_mode
) . 'Mode');
382 $processors = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors($capabilities);
388 * Assign billing type id to bltID
390 * @throws CRM_Core_Exception
393 public function assignBillingType() {
394 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
395 $this->_bltID
= array_search('Billing', $locationTypes);
396 if (!$this->_bltID
) {
397 throw new CRM_Core_Exception(ts('Please set a location type of %1', array(1 => 'Billing')));
399 $this->set('bltID', $this->_bltID
);
400 $this->assign('bltID', $this->_bltID
);
404 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
406 public function assignProcessors() {
407 //ensure that processor has a valid config
408 //only valid processors get display to user
411 $this->assign('processorSupportsFutureStartDate', CRM_Financial_BAO_PaymentProcessor
::hasPaymentProcessorSupporting(array('supportsFutureRecurStartDate')));
412 $this->_paymentProcessors
= $this->getValidProcessors();
413 if (!isset($this->_paymentProcessor
['id'])) {
414 // if the payment processor isn't set yet (as indicated by the presence of an id,) we'll grab the first one which should be the default
415 $this->_paymentProcessor
= reset($this->_paymentProcessors
);
417 if (empty($this->_paymentProcessors
)) {
418 throw new CRM_Core_Exception(ts('You will need to configure the %1 settings for your Payment Processor before you can submit a credit card transactions.', array(1 => $this->_mode
)));
420 $this->_processors
= array();
421 foreach ($this->_paymentProcessors
as $id => $processor) {
422 $this->_processors
[$id] = ts($processor['name']);
424 //get the valid recurring processors.
425 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
426 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
428 $this->assign('recurringPaymentProcessorIds',
429 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
432 // this required to show billing block
433 // @todo remove this assignment the billing block is now designed to be always included but will not show fieldsets unless those sets of fields are assigned
434 $this->assign_by_ref('paymentProcessor', $processor);
438 * @param $submittedValues
442 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
443 $config = CRM_Core_Config
::singleton();
445 $currentCurrency = CRM_Utils_Array
::value('currency',
447 $config->defaultCurrency
450 // use submitted currency if present else use current currency
451 $result = CRM_Utils_Array
::value('currency',
459 * @param int $financialTypeId
463 public function getFinancialAccounts($financialTypeId) {
464 $financialAccounts = array();
465 CRM_Core_PseudoConstant
::populate($financialAccounts,
466 'CRM_Financial_DAO_EntityFinancialAccount',
468 $retrieve = 'financial_account_id',
470 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
471 return $financialAccounts;
475 * @param int $financialTypeId
476 * @param int $relationTypeId
480 public function getFinancialAccount($financialTypeId, $relationTypeId) {
481 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
482 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
485 public function preProcessPledge() {
486 //get the payment values associated with given pledge payment id OR check for payments due.
487 $this->_pledgeValues
= array();
489 $payParams = array('id' => $this->_ppID
);
491 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
492 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
493 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
494 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
497 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
498 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
499 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
502 //get the pledge values associated with given pledge payment.
505 $pledgeParams = array('id' => $this->_pledgeID
);
506 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
507 $this->assign('ppID', $this->_ppID
);
510 // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206
511 if (isset($this->_contactID
)) {
512 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
514 if (!empty($contactPledges)) {
515 $payments = $paymentsDue = NULL;
516 $multipleDue = FALSE;
517 foreach ($contactPledges as $key => $pledgeId) {
518 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
525 $paymentsDue = $payments;
530 // Show link to pledge tab since more than one pledge has a payment due
531 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
532 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
534 CRM_Core_Session
::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)), ts('Notice'), 'alert');
536 elseif ($paymentsDue) {
537 // Show user link to oldest Pending or Overdue pledge payment
538 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
539 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
541 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
542 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
546 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
547 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
550 CRM_Core_Session
::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array(
554 )), ts('Notice'), 'alert');
562 * @param $submittedValues
566 public function unsetCreditCardFields($submittedValues) {
567 //Offline Contribution.
568 $unsetParams = array(
569 'payment_processor_id',
570 "email-{$this->_bltID}",
571 'hidden_buildCreditCard',
572 'hidden_buildDirectDebit',
573 'billing_first_name',
574 'billing_middle_name',
577 "city-{$this->_bltID}",
578 "state_province_id-{$this->_bltID}",
579 "postal_code-{$this->_bltID}",
580 "country_id-{$this->_bltID}",
581 'credit_card_number',
583 'credit_card_exp_date',
586 foreach ($unsetParams as $key) {
587 if (isset($submittedValues[$key])) {
588 unset($submittedValues[$key]);
591 return $submittedValues;
595 * Common block for setting up the parts of a form that relate to credit / debit card
598 protected function assignPaymentRelatedVariables() {
600 if ($this->_contactID
) {
601 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
602 $this->assign('displayName', $this->userDisplayName
);
605 $this->assignProcessors();
607 $this->assignBillingType();
609 $this->_fields
= array();
610 CRM_Core_Payment_Form
::setPaymentFieldsByProcessor($this, $this->_paymentProcessor
);
613 catch (CRM_Core_Exception
$e) {
614 CRM_Core_Error
::fatal($e->getMessage());