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 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-2015
35 * This class generates form components for processing a contribution
36 * CRM-16229 - During the event registration bulk action via search we
37 * need to inherit CRM_Contact_Form_Task so that we can inherit functions
38 * like getContactIds and make use of controller state. But this is not possible
39 * because CRM_Event_Form_Participant inherits this class.
40 * Ideal situation would be something like
41 * CRM_Event_Form_Participant extends CRM_Contact_Form_Task,
42 * CRM_Contribute_Form_AbstractEditPayment
43 * However this is not possible. Currently PHP does not support multiple
44 * inheritance. So work around solution is to extend this class with
45 * CRM_Contact_Form_Task which further extends CRM_Core_Form.
48 class CRM_Contribute_Form_AbstractEditPayment
extends CRM_Contact_Form_Task
{
58 * @var array current payment processor including a copy of the object in 'object' key
60 public $_paymentProcessor;
61 public $_recurPaymentProcessors;
64 * Array of processor options in the format id => array($id => $label)
65 * WARNING it appears that the format used to differ to this and there are places in the code that
66 * expect the old format. $this->_paymentProcessors provides the additional data which this
67 * array seems to have provided in the past
73 * Available payment processors with full details including the key 'object' indexed by their id
76 protected $_paymentProcessors = array();
79 * Instance of the payment processor object.
81 * @var CRM_Core_Payment
83 protected $_paymentObject;
86 * The id of the contribution that we are processing.
93 * The id of the premium that we are proceessing.
97 public $_premiumID = NULL;
100 * @var CRM_Contribute_DAO_ContributionProduct
102 public $_productDAO = NULL;
112 * The id of the contact associated with this contribution
119 * The id of the pledge payment that we are processing
126 * The id of the pledge that we are processing
133 * Is this contribution associated with an online
134 * financial transaction
138 public $_online = FALSE;
141 * Stores all product option
148 * Stores the honor id
152 public $_honorID = NULL;
155 * Store the financial Type ID
159 public $_contributionType;
162 * The contribution values if an existing contribution
167 * The pledge values if this contribution is associated with pledge
169 public $_pledgeValues;
171 public $_contributeMode = 'direct';
178 * Store the line items if price set used.
183 * Is this a backoffice form
184 * (this will affect whether paypal express code is displayed)
187 public $isBackOffice = TRUE;
189 protected $_formType;
192 * @var mystery variable screaming out for documentation
197 * Array of fields to display on billingBlock.tpl - this is not fully implemented but basically intent is the panes/fieldsets on this page should
198 * be all in this array in order like
199 * 'credit_card' => array('credit_card_number' ...
200 * 'billing_details' => array('first_name' ...
202 * such that both the fields and the order can be more easily altered by payment processors & other extensions
205 public $billingFieldSets = array();
208 * Pre process function with common actions.
210 public function preProcess() {
211 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
212 $this->assign('contactID', $this->_contactID
);
214 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
220 public function showRecordLinkMesssage($id) {
221 $statusId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
222 if (CRM_Contribute_PseudoConstant
::contributionStatus($statusId, 'name') == 'Partially paid') {
223 if ($pid = CRM_Core_DAO
::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
224 $recordPaymentLink = CRM_Utils_System
::url('civicrm/payment',
225 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
227 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');
236 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
238 $params = array('id' => $id);
239 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
241 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
242 $this->_online
= FALSE;
243 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
244 if (!empty($fids['financialTrxnId'])) {
245 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
248 // Also don't allow user to update some fields for recurring contributions.
249 if (!$this->_online
) {
250 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
253 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
256 $daoNote = new CRM_Core_BAO_Note();
257 $daoNote->entity_table
= 'civicrm_contribution';
258 $daoNote->entity_id
= $id;
259 if ($daoNote->find(TRUE)) {
260 $this->_noteID
= $daoNote->id
;
261 $values['note'] = $daoNote->note
;
263 $this->_contributionType
= $values['financial_type_id'];
267 * @param string $type
269 * @param string $subType
270 * @param int $entityId
272 public function applyCustomData($type, $subType, $entityId) {
273 $this->set('type', $type);
274 $this->set('subType', $subType);
275 $this->set('entityId', $entityId);
277 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
278 CRM_Custom_Form_CustomData
::buildQuickForm($this);
279 CRM_Custom_Form_CustomData
::setDefaultValues($this);
284 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
286 public function assignPremiumProduct($id) {
289 FROM civicrm_contribution_product
290 WHERE contribution_id = {$id}
292 $dao = CRM_Core_DAO
::executeQuery($sql,
293 CRM_Core_DAO
::$_nullArray
296 $this->_premiumID
= $dao->id
;
297 $this->_productDAO
= $dao;
303 * This function process contribution related objects.
305 * @param int $contributionId
306 * @param int $statusId
307 * @param int|null $previousStatusId
309 * @return null|string
311 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL, $receiveDate = NULL) {
313 if (!$contributionId ||
!$statusId) {
318 'contribution_id' => $contributionId,
319 'contribution_status_id' => $statusId,
320 'previous_contribution_status_id' => $previousStatusId,
321 'receive_date' => $receiveDate,
324 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
326 if (!is_array($updateResult) ||
327 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
328 !is_array($updatedComponents) ||
329 empty($updatedComponents)
334 // get the user display name.
336 SELECT display_name as displayName
338 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
339 WHERE civicrm_contribution.id = {$contributionId}";
340 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
342 // get the status message for user.
343 foreach ($updatedComponents as $componentName => $updatedStatusId) {
345 if ($componentName == 'CiviMember') {
346 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
347 CRM_Member_PseudoConstant
::membershipStatus()
349 if ($updatedStatusName == 'Cancelled') {
350 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
352 elseif ($updatedStatusName == 'Expired') {
353 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
356 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
358 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
360 1 => $userDisplayName,
368 if ($componentName == 'CiviEvent') {
369 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
370 CRM_Event_PseudoConstant
::participantStatus()
372 if ($updatedStatusName == 'Cancelled') {
373 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
375 elseif ($updatedStatusName == 'Registered') {
376 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
380 if ($componentName == 'CiviPledge') {
381 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
382 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
384 if ($updatedStatusName == 'Cancelled') {
385 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
387 elseif ($updatedStatusName == 'Failed') {
388 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
390 elseif ($updatedStatusName == 'Completed') {
391 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
401 * Array of valid processors. The array resembles the DB table but also has 'object' as a key
404 public function getValidProcessors() {
406 $capabilities = array('BackOffice');
408 $capabilities[] = (ucfirst($this->_mode
) . 'Mode');
410 $processors = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors($capabilities);
416 * Assign billing type id to bltID.
418 * @throws CRM_Core_Exception
421 public function assignBillingType() {
422 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
423 $this->_bltID
= array_search('Billing', $locationTypes);
424 if (!$this->_bltID
) {
425 throw new CRM_Core_Exception(ts('Please set a location type of %1', array(1 => 'Billing')));
427 $this->set('bltID', $this->_bltID
);
428 $this->assign('bltID', $this->_bltID
);
432 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
434 public function assignProcessors() {
435 //ensure that processor has a valid config
436 //only valid processors get display to user
439 $this->assign('processorSupportsFutureStartDate', CRM_Financial_BAO_PaymentProcessor
::hasPaymentProcessorSupporting(array('supportsFutureRecurStartDate')));
440 $this->_paymentProcessors
= $this->getValidProcessors();
441 if (!isset($this->_paymentProcessor
['id'])) {
442 // 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
443 $this->_paymentProcessor
= reset($this->_paymentProcessors
);
445 if (empty($this->_paymentProcessors
)) {
446 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
)));
448 $this->_processors
= array();
449 foreach ($this->_paymentProcessors
as $id => $processor) {
450 $this->_processors
[$id] = ts($processor['name']);
451 if (!empty($processor['description'])) {
452 $this->_processors
[$id] .= ' : ' . ts($processor['description']);
455 //get the valid recurring processors.
456 $test = strtolower($this->_mode
) == 'test' ?
TRUE : FALSE;
457 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, $test, 'is_recur = 1');
458 $this->_recurPaymentProcessors
= array_intersect_key($this->_processors
, $recurring);
460 $this->assign('recurringPaymentProcessorIds',
461 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
464 // this required to show billing block
465 // @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
466 $this->assign_by_ref('paymentProcessor', $processor);
470 * Get current currency from DB or use default currency.
472 * @param $submittedValues
476 public function getCurrency($submittedValues) {
477 $config = CRM_Core_Config
::singleton();
479 $currentCurrency = CRM_Utils_Array
::value('currency',
481 $config->defaultCurrency
484 // use submitted currency if present else use current currency
485 $result = CRM_Utils_Array
::value('currency',
493 * @param int $financialTypeId
497 public function getFinancialAccounts($financialTypeId) {
498 $financialAccounts = array();
499 CRM_Core_PseudoConstant
::populate($financialAccounts,
500 'CRM_Financial_DAO_EntityFinancialAccount',
502 $retrieve = 'financial_account_id',
504 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
505 return $financialAccounts;
509 * @param int $financialTypeId
510 * @param int $relationTypeId
514 public function getFinancialAccount($financialTypeId, $relationTypeId) {
515 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
516 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
519 public function preProcessPledge() {
520 //get the payment values associated with given pledge payment id OR check for payments due.
521 $this->_pledgeValues
= array();
523 $payParams = array('id' => $this->_ppID
);
525 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
526 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
527 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
528 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
531 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
532 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
533 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
536 //get the pledge values associated with given pledge payment.
539 $pledgeParams = array('id' => $this->_pledgeID
);
540 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
541 $this->assign('ppID', $this->_ppID
);
544 // 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
545 if (isset($this->_contactID
)) {
546 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
548 if (!empty($contactPledges)) {
549 $payments = $paymentsDue = NULL;
550 $multipleDue = FALSE;
551 foreach ($contactPledges as $key => $pledgeId) {
552 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
559 $paymentsDue = $payments;
564 // Show link to pledge tab since more than one pledge has a payment due
565 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
566 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
568 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');
570 elseif ($paymentsDue) {
571 // Show user link to oldest Pending or Overdue pledge payment
572 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
573 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
575 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
576 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
580 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
581 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
584 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(
588 )), ts('Notice'), 'alert');
596 * @param $submittedValues
600 public function unsetCreditCardFields($submittedValues) {
601 //Offline Contribution.
602 $unsetParams = array(
603 'payment_processor_id',
604 "email-{$this->_bltID}",
605 'hidden_buildCreditCard',
606 'hidden_buildDirectDebit',
607 'billing_first_name',
608 'billing_middle_name',
611 "city-{$this->_bltID}",
612 "state_province_id-{$this->_bltID}",
613 "postal_code-{$this->_bltID}",
614 "country_id-{$this->_bltID}",
615 'credit_card_number',
617 'credit_card_exp_date',
620 foreach ($unsetParams as $key) {
621 if (isset($submittedValues[$key])) {
622 unset($submittedValues[$key]);
625 return $submittedValues;
629 * Common block for setting up the parts of a form that relate to credit / debit card
632 protected function assignPaymentRelatedVariables() {
634 if ($this->_contactID
) {
635 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
636 $this->assign('displayName', $this->userDisplayName
);
639 $this->assignProcessors();
641 $this->assignBillingType();
643 $this->_fields
= array();
644 CRM_Core_Payment_Form
::setPaymentFieldsByProcessor($this, $this->_paymentProcessor
);
647 catch (CRM_Core_Exception
$e) {
648 CRM_Core_Error
::fatal($e->getMessage());