3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_Contribution
extends CRM_Contribute_Form_AbstractEditPayment
{
42 * the id of the contribution that we are processing
50 * the id of the premium that we are processing
55 public $_premiumID = NULL;
58 * @var CRM_Contribute_DAO_ContributionProduct
60 public $_productDAO = NULL;
71 * the id of the contact associated with this contribution
79 * the id of the pledge payment that we are processing
87 * the id of the pledge that we are processing
95 * is this contribution associated with an online
96 * financial transaction
101 public $_online = FALSE;
104 * Stores all product option
112 * Storage of parameters from form
120 * Store the contribution Type ID
124 public $_contributionType;
127 * The contribution values if an existing contribution
132 * The pledge values if this contribution is associated with pledge
134 public $_pledgeValues;
136 public $_contributeMode = 'direct';
141 * Parameter with confusing name
145 public $_compContext;
150 * Possible From email addresses
161 * Store the line items if price set used.
167 * @todo explain why we use lineItem & lineItems
173 * @var array soft credit info
175 public $_softCreditInfo;
177 protected $_formType;
180 * @todo what on earth does cdType stand for????
184 public $_honoreeProfileType;
187 * logged in user's email
200 * Price set as an array
207 * @todo can we define this a as protected? can we define higher up the chain
217 public $userDisplayName;
219 * Function to set variables up before form is built
224 public function preProcess() {
226 //check permission for action.
227 if (!CRM_Core_Permission
::checkActionPermission('CiviContribute', $this->_action
)) {
228 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
231 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
233 $this->assign('cdType', FALSE);
234 if ($this->_cdType
) {
235 $this->assign('cdType', TRUE);
236 CRM_Custom_Form_CustomData
::preProcess($this);
240 $this->_formType
= CRM_Utils_Array
::value('formType', $_GET);
243 $this->_priceSetId
= CRM_Utils_Array
::value('priceSetId', $_GET);
244 $this->set('priceSetId', $this->_priceSetId
);
245 $this->assign('priceSetId', $this->_priceSetId
);
247 //get the pledge payment id
248 $this->_ppID
= CRM_Utils_Request
::retrieve('ppid', 'Positive', $this);
251 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
254 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
255 $this->assign('action', $this->_action
);
257 //get the contribution id if update
258 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
259 if (!empty($this->_id
)) {
260 $this->assign('contribID', $this->_id
);
263 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
264 $this->assign('context', $this->_context
);
266 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
268 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
270 //set the contribution mode.
271 $this->_mode
= CRM_Utils_Request
::retrieve('mode', 'String', $this);
273 $this->assign('contributionMode', $this->_mode
);
275 $this->_paymentProcessor
= array('billing_mode' => 1);
277 $this->assign('showCheckNumber', TRUE);
279 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
280 $this->assignProcessors();
282 if ($this->_contactID
) {
283 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
284 $this->assign('displayName', $this->userDisplayName
);
287 // also check for billing information
288 // get the billing location type
289 $this->assignBillingType();
291 $this->_fields
= array();
293 CRM_Core_Payment_Form
::setPaymentFieldsByType(CRM_Utils_Array
::value('payment_type', $this->_processors
), $this);
295 if ($this->_action
& CRM_Core_Action
::DELETE
) {
299 if (in_array('CiviPledge', CRM_Core_Config
::singleton()->enableComponents
) && !$this->_formType
) {
300 $this->preProcessPledge();
304 $this->showRecordLinkMesssage($this->_id
);
306 $this->_values
= array();
308 // current contribution id
310 $this->assignPremiumProduct($this->_id
);
311 $this->buildValuesAndAssignOnline_Note_Type($this->_id
, $this->_values
);
314 // when custom data is included in this page
315 if (!empty($_POST['hidden_custom'])) {
316 $this->applyCustomData('Contribution', CRM_Utils_Array
::value('financial_type_id', $_POST), $this->_id
);
319 $this->_lineItems
= array();
321 if (!empty($this->_compId
) && $this->_compContext
== 'participant') {
322 $this->assign('compId', $this->_compId
);
323 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_compId
);
326 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution', 1, TRUE, TRUE);
328 empty($lineItem) ?
NULL : $this->_lineItems
[] = $lineItem;
331 $this->assign('lineItem', empty($this->_lineItems
) ?
FALSE : $this->_lineItems
);
335 $this->setPageTitle($this->_ppID ?
ts('Credit Card Pledge Payment') : ts('Credit Card Contribution'));
338 $this->setPageTitle($this->_ppID ?
ts('Pledge Payment') : ts('Contribution'));
342 CRM_Contribute_Form_SoftCredit
::preprocess($this);
346 function setDefaultValues() {
347 if ($this->_cdType
) {
348 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
351 $defaults = $this->_values
;
353 //set defaults for pledge payment.
355 $defaults['total_amount'] = CRM_Utils_Array
::value('scheduled_amount', $this->_pledgeValues
['pledgePayment']);
356 $defaults['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $this->_pledgeValues
);
357 $defaults['currency'] = CRM_Utils_Array
::value('currency', $this->_pledgeValues
);
358 $defaults['option_type'] = 1;
361 if ($this->_action
& CRM_Core_Action
::DELETE
) {
365 // set soft credit defaults
366 CRM_Contribute_Form_SoftCredit
::setDefaultValues($defaults, $this);
369 $config = CRM_Core_Config
::singleton();
370 // set default country from config if no country set
371 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
372 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
375 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
376 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
379 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID
);
380 $defaults = array_merge($defaults, $billingDefaults);
384 $this->_contactID
= $defaults['contact_id'];
387 // Set $newCredit variable in template to control whether link to credit card mode is included
388 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
390 // fix the display of the monetary value, CRM-4038
391 if (isset($defaults['total_amount'])) {
392 $defaults['total_amount'] = CRM_Utils_Money
::format($defaults['total_amount'], NULL, '%a');
395 if (isset($defaults['non_deductible_amount'])) {
396 $defaults['non_deductible_amount'] = CRM_Utils_Money
::format($defaults['non_deductible_amount'], NULL, '%a');
399 if (isset($defaults['fee_amount'])) {
400 $defaults['fee_amount'] = CRM_Utils_Money
::format($defaults['fee_amount'], NULL, '%a');
403 if (isset($defaults['net_amount'])) {
404 $defaults['net_amount'] = CRM_Utils_Money
::format($defaults['net_amount'], NULL, '%a');
407 if ($this->_contributionType
) {
408 $defaults['financial_type_id'] = $this->_contributionType
;
411 if (empty($defaults['payment_instrument_id'])) {
412 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
415 if (!empty($defaults['is_test'])) {
416 $this->assign('is_test', TRUE);
419 $this->assign('showOption', TRUE);
420 // for Premium section
421 if ($this->_premiumID
) {
422 $this->assign('showOption', FALSE);
423 $options = isset($this->_options
[$this->_productDAO
->product_id
]) ?
$this->_options
[$this->_productDAO
->product_id
] : "";
425 $this->assign('showOption', TRUE);
427 $options_key = CRM_Utils_Array
::key($this->_productDAO
->product_option
, $options);
429 $defaults['product_name'] = array($this->_productDAO
->product_id
, trim($options_key));
432 $defaults['product_name'] = array($this->_productDAO
->product_id
);
434 if ($this->_productDAO
->fulfilled_date
) {
435 list($defaults['fulfilled_date']) = CRM_Utils_Date
::setDateDefaults($this->_productDAO
->fulfilled_date
);
439 if (isset($this->userEmail
)) {
440 $this->assign('email', $this->userEmail
);
443 if (!empty($defaults['is_pay_later'])) {
444 $this->assign('is_pay_later', TRUE);
446 $this->assign('contribution_status_id', CRM_Utils_Array
::value('contribution_status_id', $defaults));
448 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
449 foreach ($dates as $key) {
450 if (!empty($defaults[$key])) {
451 list($defaults[$key],
452 $defaults[$key . '_time']
453 ) = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Array
::value($key, $defaults),
459 if (!$this->_id
&& empty($defaults['receive_date'])) {
460 list($defaults['receive_date'],
461 $defaults['receive_date_time']
462 ) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
465 $this->assign('receive_date', CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('receive_date', $defaults),
466 CRM_Utils_Array
::value('receive_date_time', $defaults)
468 $currency = CRM_Utils_Array
::value('currency', $defaults);
469 $this->assign('currency', $currency);
470 // Hack to get currency info to the js layer. CRM-11440
471 CRM_Utils_Money
::format(1);
472 $this->assign('currencySymbol', CRM_Utils_Array
::value($currency, CRM_Utils_Money
::$_currencySymbols));
473 $this->assign('totalAmount', CRM_Utils_Array
::value('total_amount', $defaults));
475 //inherit campaign from pledge.
476 if ($this->_ppID
&& !empty($this->_pledgeValues
['campaign_id'])) {
477 $defaults['campaign_id'] = $this->_pledgeValues
['campaign_id'];
480 $this->_defaults
= $defaults;
485 * Function to build the form
490 public function buildQuickForm() {
491 if ($this->_cdType
) {
492 CRM_Custom_Form_CustomData
::buildQuickForm($this);
497 // build price set form.
498 $buildPriceSet = FALSE;
499 if (empty($this->_lineItems
) &&
500 ($this->_priceSetId ||
!empty($_POST['price_set_id']))
502 $buildPriceSet = TRUE;
503 $getOnlyPriceSetElements = TRUE;
504 if (!$this->_priceSetId
) {
505 $this->_priceSetId
= $_POST['price_set_id'];
506 $getOnlyPriceSetElements = FALSE;
509 $this->set('priceSetId', $this->_priceSetId
);
510 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
512 // get only price set form elements.
513 if ($getOnlyPriceSetElements) {
517 // use to build form during form rule.
518 $this->assign('buildPriceSet', $buildPriceSet);
520 $showAdditionalInfo = FALSE;
522 $defaults = $this->_values
;
523 $additionalDetailFields = array(
527 'non_deductible_amount',
531 foreach ($additionalDetailFields as $key) {
532 if (!empty($defaults[$key])) {
533 $defaults['hidden_AdditionalDetail'] = 1;
538 if ($this->_productDAO
) {
539 if ($this->_productDAO
->product_id
) {
540 $defaults['hidden_Premium'] = 1;
544 if ($this->_noteID
&&
545 isset($this->_values
['note'])
547 $defaults['hidden_AdditionalDetail'] = 1;
551 ts('Additional Details') => 'AdditionalDetail',
554 //Add Premium pane only if Premium is exists.
555 $dao = new CRM_Contribute_DAO_Product();
558 if ($dao->find(TRUE)) {
559 $paneNames[ts('Premium Information')] = 'Premium';
564 if (CRM_Utils_Array
::value('payment_type', $this->_processors
) & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
566 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
569 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
572 if (is_array($ccPane)) {
573 $paneNames = array_merge($ccPane, $paneNames);
576 $buildRecurBlock = FALSE;
577 foreach ($paneNames as $name => $type) {
578 $urlParams = "snippet=4&formType={$type}";
580 $urlParams .= "&mode={$this->_mode}";
584 if ($type == 'CreditCard' ||
585 $type == 'DirectDebit'
590 $allPanes[$name] = array(
591 'url' => CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams),
596 // see if we need to include this paneName in the current form
597 if ($this->_formType
== $type ||
!empty($_POST["hidden_{$type}"]) ||
598 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
600 $showAdditionalInfo = TRUE;
601 $allPanes[$name]['open'] = 'true';
604 if ($type == 'CreditCard') {
605 $buildRecurBlock = TRUE;
606 $this->add('hidden', 'hidden_CreditCard', 1);
607 CRM_Core_Payment_Form
::buildCreditCard($this, TRUE);
609 elseif ($type == 'DirectDebit') {
610 $buildRecurBlock = TRUE;
611 $this->add('hidden', 'hidden_DirectDebit', 1);
612 CRM_Core_Payment_Form
::buildDirectDebit($this, TRUE);
615 $additionalInfoFormFunction = 'build' . $type;
616 CRM_Contribute_Form_AdditionalInfo
::$additionalInfoFormFunction($this);
619 if (empty($this->_recurPaymentProcessors
)) {
620 $buildRecurBlock = FALSE;
622 if ($buildRecurBlock) {
623 CRM_Contribute_Form_Contribution_Main
::buildRecur($this);
624 $this->setDefaults(array('is_recur' => 0));
626 $this->assign('buildRecurBlock', $buildRecurBlock);
627 $qfKey = $this->controller
->_key
;
628 $this->assign('qfKey', $qfKey);
629 $this->assign('allPanes', $allPanes);
630 $this->assign('showAdditionalInfo', $showAdditionalInfo);
632 if ($this->_formType
) {
633 $this->assign('formType', $this->_formType
);
637 $this->applyFilter('__ALL__', 'trim');
639 if ($this->_action
& CRM_Core_Action
::DELETE
) {
640 $this->addButtons(array(
643 'name' => ts('Delete'),
644 'spacing' => ' ',
649 'name' => ts('Cancel')
656 //need to assign custom data type and subtype to the template
657 $this->assign('customDataType', 'Contribution');
658 $this->assign('customDataSubType', $this->_contributionType
);
659 $this->assign('entityID', $this->_id
);
661 if ($this->_context
== 'standalone') {
662 $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE, 'api' => array('extra' => array('email'))), TRUE);
665 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
667 $financialType = $this->add('select', 'financial_type_id',
668 ts('Financial Type'),
669 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
671 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
673 $paymentInstrument = FALSE;
675 $paymentInstrument = $this->add('select', 'payment_instrument_id',
677 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
678 TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
682 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') +
$attributes['trxn_id']);
684 //add receipt for offline contribution
685 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
687 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
689 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
691 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
692 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
699 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
702 elseif ((!$this->_ppID
&& $this->_id
) ||
!$this->_id
) {
703 $suppressFlag = FALSE;
705 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
706 if (CRM_Utils_Array
::value('membership', $componentDetails) || CRM_Utils_Array
::value('participant', $componentDetails)) {
707 $suppressFlag = TRUE;
710 if (!$suppressFlag) {
715 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
719 unset($status[CRM_Utils_Array
::key('Overdue', $statusName)]);
724 $contributionStatus = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id
, 'contribution_status_id');
725 $name = CRM_Utils_Array
::value($contributionStatus, $statusName);
730 unset($status[CRM_Utils_Array
::key('In Progress', $statusName)]);
731 unset($status[CRM_Utils_Array
::key('Pending', $statusName)]);
732 unset($status[CRM_Utils_Array
::key('Failed', $statusName)]);
736 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
746 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
752 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
755 $this->add('select', 'contribution_status_id',
756 ts('Contribution Status'),
762 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
764 if ($this->_online
) {
765 $this->assign('hideCalender', TRUE);
767 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
769 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
770 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
772 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
775 if ($buildRecurBlock) {
776 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
778 $element = $this->add('select',
779 'payment_processor_id',
780 ts('Payment Processor'),
786 if ($this->_online
) {
790 if (empty($this->_lineItems
)) {
791 $buildPriceSet = FALSE;
792 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviContribute');
793 if (!empty($priceSets) && !$this->_ppID
) {
794 $buildPriceSet = TRUE;
797 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
798 // and if we already have line items for that participant. CRM-5095
799 if ($buildPriceSet && $this->_id
) {
800 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
801 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
806 if ($pledgePaymentId) {
807 $buildPriceSet = FALSE;
809 if ($participantID = CRM_Utils_Array
::value('participant', $componentDetails)) {
810 $participantLI = CRM_Price_BAO_LineItem
::getLineItems($participantID);
811 if (!CRM_Utils_System
::isNull($participantLI)) {
812 $buildPriceSet = FALSE;
817 $hasPriceSets = FALSE;
818 if ($buildPriceSet) {
819 $hasPriceSets = TRUE;
820 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
822 '' => ts('Choose price set')
824 NULL, array('onchange' => "buildAmount( this.value );")
826 if ($this->_online
&& !($this->_action
& CRM_Core_Action
::UPDATE
)) {
830 $this->assign('hasPriceSets', $hasPriceSets);
831 $currencyFreeze = FALSE;
832 if (!($this->_action
& CRM_Core_Action
::UPDATE
)) {
833 if ($this->_online ||
$this->_ppID
) {
834 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
836 'style' => "background-color:#EBECE4"
838 $optionTypes = array(
839 '1' => ts('Adjust Pledge Payment Schedule?'),
840 '2' => ts('Adjust Total Pledge Amount?'),
842 $this->addRadio('option_type',
848 $currencyFreeze = TRUE;
852 $totalAmount = $this->addMoney('total_amount',
854 ($hasPriceSets) ?
FALSE : TRUE,
855 $attributes['total_amount'],
856 TRUE, 'currency', NULL, $currencyFreeze
860 $this->add('text', 'source', ts('Source'), CRM_Utils_Array
::value('source', $attributes));
862 //CRM-7362 --add campaigns.
863 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
865 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
869 $js = array('onclick' => "return verify( );");
872 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
875 $this->assign('outBound_option', $mailingInfo['outBound_option']);
877 $this->addButtons(array(
880 'name' => ts('Save'),
886 'name' => ts('Save and New'),
892 'name' => ts('Cancel')
897 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
898 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
899 if ($this->_id
&& $this->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
901 $totalAmount->freeze();
903 $checkNumber->freeze();
904 $paymentInstrument->freeze();
906 $financialType->freeze();
909 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
911 if ($this->_action
& CRM_Core_Action
::VIEW
) {
919 * @param array $fields the input form values
920 * @param array $files the uploaded files if any
923 * @internal param array $options additional user data
925 * @return true if no errors, else array of errors
929 static function formRule($fields, $files, $self) {
932 //check for Credit Card Contribution.
934 if (empty($fields['payment_processor_id'])) {
935 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
939 // do the amount validations.
940 if (empty($fields['total_amount']) && empty($self->_lineItems
)) {
941 if ($priceSetId = CRM_Utils_Array
::value('price_set_id', $fields)) {
942 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $fields, $errors);
946 $softErrors = CRM_Contribute_Form_SoftCredit
::formRule($fields, $errors, $self);
948 if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) ||
!empty($fields['fee_amount']))) {
949 $sum = CRM_Utils_Rule
::cleanMoney($fields['net_amount']) + CRM_Utils_Rule
::cleanMoney($fields['fee_amount']);
950 if (CRM_Utils_Rule
::cleanMoney($fields['total_amount']) != $sum) {
951 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
954 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
955 if ($self->_id
&& $self->_values
['contribution_status_id'] != $fields['contribution_status_id']) {
956 CRM_Contribute_BAO_Contribution
::checkStatusValidation($self->_values
, $fields, $errors);
959 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
960 if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($fields['financial_type_id'])) {
961 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
964 // $trxn_id must be unique CRM-13919
965 if (!empty($fields['trxn_id'])) {
966 $queryParams = array(1 => array($fields['trxn_id'], 'String'));
967 $query = 'select count(*) from civicrm_contribution where trxn_id = %1';
969 $queryParams[2] = array((int)$self->_id
, 'Integer');
970 $query .= ' and id !=%2';
972 $tCnt = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
974 $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id']));
978 $errors = array_merge($errors, $softErrors);
983 * Function to process the form
989 public function postProcess() {
990 $session = CRM_Core_Session
::singleton();
991 $sendReceipt = $pId = $contribution = $isRelatedId = FALSE;
992 $softParams = $softIDs =array();
994 if ($this->_action
& CRM_Core_Action
::DELETE
) {
995 CRM_Contribute_BAO_Contribution
::deleteContribution($this->_id
);
996 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
997 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1002 // get the submitted form values.
1003 $submittedValues = $this->controller
->exportValues($this->_name
);
1004 if (!empty($submittedValues['price_set_id']) && $this->_action
& CRM_Core_Action
::UPDATE
) {
1005 $line = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution');
1006 $lineID = key($line);
1007 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $line[$lineID]), 'price_set_id');
1008 $quickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
1010 CRM_Price_BAO_LineItem
::deleteLineItems($this->_id
, 'civicrm_contribution');
1014 // process price set and get total amount and line items.
1015 $lineItem = array();
1016 $priceSetId = CRM_Utils_Array
::value('price_set_id', $submittedValues);
1017 if (empty($priceSetId) && !$this->_id
) {
1018 $this->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
1019 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
1020 $fieldID = key($this->_priceSet
['fields']);
1021 $fieldValueId = key($this->_priceSet
['fields'][$fieldID]['options']);
1022 $this->_priceSet
['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
1023 $submittedValues['price_' . $fieldID] = 1;
1027 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
1028 $submittedValues, $lineItem[$priceSetId]);
1030 $submittedValues['total_amount'] = CRM_Utils_Array
::value('amount', $submittedValues);
1033 if ($this->_compId
) {
1034 if ($this->_context
== 'participant') {
1035 $pId = $this->_compId
;
1037 elseif ($this->_context
== 'membership') {
1038 $isRelatedId = TRUE;
1042 $contributionDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
1043 if (array_key_exists('membership', $contributionDetails)) {
1044 $isRelatedId = TRUE;
1046 elseif (array_key_exists('participant', $contributionDetails)) {
1047 $pId = current($contributionDetails['participant']);
1051 if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id
) {
1052 // 10117 update th line items for participants
1054 $entityTable = 'participant';
1056 $participantParams = array(
1057 'fee_amount' => $submittedValues['total_amount'],
1060 CRM_Event_BAO_Participant
::add($participantParams);
1061 if (empty($this->_lineItems
)) {
1062 $this->_lineItems
[] = CRM_Price_BAO_LineItem
::getLineItems($entityID, 'participant', 1);
1066 $entityTable = 'contribution';
1067 $entityID = $this->_id
;
1070 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($entityID, NULL, TRUE, $isRelatedId);
1071 foreach (array_keys($lineItems) as $id) {
1072 $lineItems[$id]['id'] = $id;
1074 $itemId = key($lineItems);
1075 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
1076 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1079 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1080 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('total_amount', $submittedValues));
1082 // 10117 update th line items for participants
1083 if (!empty($lineItems[$itemId]['price_field_id'])) {
1084 $lineItem[$this->_priceSetId
] = $lineItems;
1088 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1091 //CRM-11529 for quick config back office transactions
1092 //when financial_type_id is passed in form, update the
1093 //line items with the financial type selected in form
1094 if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array
::value($this->_priceSetId
, $lineItem)
1096 foreach ($lineItem[$this->_priceSetId
] as &$values) {
1097 $values['financial_type_id'] = $submittedValues['financial_type_id'];
1101 if (!isset($submittedValues['total_amount'])) {
1102 $submittedValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_values
);
1104 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1106 if (!empty($submittedValues['pcp_made_through_id'])) {
1109 'pcp_made_through_id',
1110 'pcp_display_in_roll',
1111 'pcp_roll_nickname',
1112 'pcp_personal_note',
1114 foreach ($fields as $f) {
1115 $pcp[$f] = CRM_Utils_Array
::value($f, $submittedValues);
1119 $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id']));
1120 if ($this->_id
&& count($isEmpty) == 1 && key($isEmpty) == NULL) {
1121 //Delete existing soft credit records if soft credit list is empty on update
1122 CRM_Contribute_BAO_ContributionSoft
::del(array('contribution_id' => $this->_id
));
1125 //build soft credit params
1126 foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) {
1127 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1128 $softParams[$key]['contact_id'] = $val;
1129 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1130 $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key];
1131 if (!empty($submittedValues['soft_credit_id'][$key])) {
1132 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
1138 // set the contact, when contact is selected
1139 if (!empty($submittedValues['contact_id'])) {
1140 $this->_contactID
= $submittedValues['contact_id'];
1143 $config = CRM_Core_Config
::singleton();
1145 //Credit Card Contribution.
1147 $this->processCreditCard($submittedValues, $config, $session, $lineItem);
1150 //Offline Contribution.
1151 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1153 // get the required field value only.
1154 $formValues = $submittedValues;
1155 $params = $ids = array();
1157 $params['contact_id'] = $this->_contactID
;
1159 $params['currency'] = $this->getCurrency($submittedValues);
1162 'financial_type_id',
1163 'contribution_status_id',
1164 'payment_instrument_id',
1169 foreach ($fields as $f) {
1170 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
1174 $params['pcp'] = $pcp;
1176 if (!empty($softParams)) {
1177 $params['soft_credit'] = $softParams;
1178 $params['soft_credit_ids'] = $softIDs;
1181 //if priceset is used, no need to cleanup money
1184 $params['skipCleanMoney'] = 1;
1193 foreach ($dates as $d) {
1194 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1197 if (!empty($formValues['is_email_receipt'])) {
1198 $params['receipt_date'] = date("Y-m-d");
1201 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Cancelled', 'name')
1202 ||
$params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name')
1204 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value('cancel_date', $params))) {
1205 $params['cancel_date'] = date('Y-m-d');
1209 $params['cancel_date'] = $params['cancel_reason'] = 'null';
1212 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
1213 // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041
1214 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Pending', 'name')) {
1215 $params['is_pay_later'] = 1;
1217 elseif ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Completed', 'name')) {
1218 $params['is_pay_later'] = 0;
1221 $ids['contribution'] = $params['id'] = $this->_id
;
1223 //Add Additional common information to formatted params
1224 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($formValues, $params, $this);
1226 $params['contribution_mode'] = 'participant';
1227 $params['participant_id'] = $pId;
1228 $params['skipLineItem'] = 1;
1230 elseif ($isRelatedId) {
1231 $params['contribution_mode'] = 'membership';
1233 $params['line_item'] = $lineItem;
1234 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array
::value('id', $this->_paymentProcessor
);
1235 //create contribution.
1236 if ($isQuickConfig) {
1237 $params['is_quick_config'] = 1;
1241 // if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] -
1242 // if non_deductible_amount does NOT exist - then calculate it depending on:
1243 // $ContributionType->is_deductible and whether there is a product (premium).
1244 if (!isset($params['non_deductible_amount']) ||
(!empty($params['non_deductible_amount']))) {
1245 $contributionType = new CRM_Financial_DAO_FinancialType();
1246 $contributionType->id
= $params['financial_type_id'];
1247 if (!$contributionType->find(TRUE)) {
1248 CRM_Core_Error
::fatal('Could not find a system table');
1250 if ($contributionType->is_deductible
) {
1252 if (isset($formValues['product_name'][0])) {
1253 $selectProduct = $formValues['product_name'][0];
1255 // if there is a product - compare the value to the contribution amount
1256 if (isset($selectProduct)) {
1257 $productDAO = new CRM_Contribute_DAO_Product();
1258 $productDAO->id
= $selectProduct;
1259 $productDAO->find(TRUE);
1260 // product value exceeds contribution amount
1261 if ($params['total_amount'] < $productDAO->price
) {
1262 $params['non_deductible_amount'] = $params['total_amount'];
1264 // product value does NOT exceed contribution amount
1266 $params['non_deductible_amount'] = $productDAO->price
;
1269 // contribution is deductible - but there is no product
1271 $params['non_deductible_amount'] = '0.00';
1274 // contribution is NOT deductible
1276 $params['non_deductible_amount'] = $params['total_amount'];
1280 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
1282 // process associated membership / participant, CRM-4395
1283 $relatedComponentStatusMsg = NULL;
1284 if ($contribution->id
&& $this->_action
& CRM_Core_Action
::UPDATE
) {
1285 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id
,
1286 $contribution->contribution_status_id
,
1287 CRM_Utils_Array
::value('contribution_status_id',
1294 if ($contribution->id
&& isset($formValues['note'])) {
1295 CRM_Contribute_Form_AdditionalInfo
::processNote($formValues, $this->_contactID
, $contribution->id
, $this->_noteID
);
1299 if ($contribution->id
&& isset($formValues['product_name'][0])) {
1300 CRM_Contribute_Form_AdditionalInfo
::processPremium($formValues, $contribution->id
,
1301 $this->_premiumID
, $this->_options
1305 //send receipt mail.
1306 if ($contribution->id
&& !empty($formValues['is_email_receipt'])) {
1307 $formValues['contact_id'] = $this->_contactID
;
1308 $formValues['contribution_id'] = $contribution->id
;
1310 $formValues +
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($contribution->id
);
1312 // to get 'from email id' for send receipt
1313 $this->fromEmailId
= $formValues['from_email_address'];
1314 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $formValues);
1317 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1323 //update pledge payment status.
1324 if ((($this->_ppID
&& $contribution->id
) && $this->_action
& CRM_Core_Action
::ADD
) ||
1325 (($pledgePaymentId) && $this->_action
& CRM_Core_Action
::UPDATE
)
1329 //store contribution id in payment record.
1330 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1333 $this->_ppID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1338 $this->_pledgeID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1345 $adjustTotalAmount = FALSE;
1346 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
1347 $adjustTotalAmount = TRUE;
1350 $updatePledgePaymentStatus = FALSE;
1351 //do only if either the status or the amount has changed
1352 if ($this->_action
& CRM_Core_Action
::ADD
) {
1353 $updatePledgePaymentStatus = TRUE;
1355 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& (($this->_defaults
['contribution_status_id'] != $formValues['contribution_status_id']) ||
1356 ($this->_defaults
['total_amount'] != $formValues['total_amount']))
1358 $updatePledgePaymentStatus = TRUE;
1361 if ($updatePledgePaymentStatus) {
1362 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1363 array($this->_ppID
),
1364 $contribution->contribution_status_id
,
1366 $contribution->total_amount
,
1372 $statusMsg = ts('The contribution record has been saved.');
1373 if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
1374 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1377 if ($relatedComponentStatusMsg) {
1378 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1381 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1382 //Offline Contribution ends.
1384 $buttonName = $this->controller
->getButtonName();
1385 if ($this->_context
== 'standalone') {
1386 if ($buttonName == $this->getButtonName('upload', 'new')) {
1387 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/add',
1388 'reset=1&action=add&context=standalone'
1392 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
1393 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1397 elseif ($this->_context
== 'contribution' && $this->_mode
&& $buttonName == $this->getButtonName('upload', 'new')) {
1398 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1399 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1402 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1403 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1404 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1408 //store contribution ID if not yet set (on create)
1409 if ( empty($this->_id
) && !empty($contribution->id
) ) {
1410 $this->_id
= $contribution->id
;
1415 * @param $submittedValues
1417 * @param $session CRM_Core_Session
1420 * @throws CRM_Core_Exception
1422 public function processCreditCard($submittedValues, $config, $session, $lineItem) {
1423 $sendReceipt = $contribution = FALSE;
1424 $unsetParams = array(
1426 'payment_instrument_id',
1427 'contribution_status_id',
1431 foreach ($unsetParams as $key) {
1432 if (isset($submittedValues[$key])) {
1433 unset($submittedValues[$key]);
1436 $isTest = ($this->_mode
== 'test') ?
1 : 0;
1437 // CRM-12680 set $_lineItem if its not set
1438 if (empty($this->_lineItem
) && !empty($lineItem)) {
1439 $this->_lineItem
= $lineItem;
1442 //Get the require fields value only.
1443 $params = $this->_params
= $submittedValues;
1445 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_params
['payment_processor_id'],
1449 //get the payment processor id as per mode.
1450 $this->_params
['payment_processor'] = $params['payment_processor_id'] =
1451 $this->_params
['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor
['id'];
1453 $now = date('YmdHis');
1456 // we need to retrieve email address
1457 if ($this->_context
== 'standalone' && !empty($submittedValues['is_email_receipt'])) {
1458 list($this->userDisplayName
,
1460 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
1461 $this->assign('displayName', $this->userDisplayName
);
1464 //set email for primary location.
1465 $fields['email-Primary'] = 1;
1466 $params['email-Primary'] = $this->userEmail
;
1468 // now set the values for the billing location.
1469 foreach (array_keys($this->_fields
) as $name) {
1473 // also add location name to the array
1474 $params["address_name-{$this->_bltID}"] = CRM_Utils_Array
::value('billing_first_name', $params) . ' ' . CRM_Utils_Array
::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array
::value('billing_last_name', $params);
1475 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1476 $fields["address_name-{$this->_bltID}"] = 1;
1478 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1483 $nameFields = array('first_name', 'middle_name', 'last_name');
1484 foreach ($nameFields as $name) {
1486 if (array_key_exists("billing_$name", $params)) {
1487 $params[$name] = $params["billing_{$name}"];
1488 $params['preserveDBName'] = TRUE;
1492 if (!empty($params['source'])) {
1493 unset($params['source']);
1495 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $fields,
1501 // add all the additional payment params we need
1502 $this->_params
["state_province-{$this->_bltID}"] = $this->_params
["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($this->_params
["billing_state_province_id-{$this->_bltID}"]);
1503 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
1505 if ($this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_CREDIT_CARD
) {
1506 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
1507 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
1509 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
1510 $this->_params
['amount'] = $this->_params
['total_amount'];
1511 $this->_params
['amount_level'] = 0;
1512 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
1514 $config->defaultCurrency
1516 $this->_params
['payment_action'] = 'Sale';
1517 if (!empty($this->_params
['receive_date'])) {
1518 $this->_params
['receive_date'] = CRM_Utils_Date
::processDate($this->_params
['receive_date'], $this->_params
['receive_date_time']);
1521 if (!empty($params['soft_credit_to'])) {
1522 $this->_params
['soft_credit_to'] = $params['soft_credit_to'];
1523 $this->_params
['pcp_made_through_id'] = $params['pcp_made_through_id'];
1526 $this->_params
['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $params);
1527 $this->_params
['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $params);
1528 $this->_params
['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $params);
1530 //Add common data to formatted params
1531 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
1533 if (empty($this->_params
['invoice_id'])) {
1534 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
1537 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
1540 // at this point we've created a contact and stored its address etc
1541 // all the payment processors expect the name and address to be in the
1542 // so we copy stuff over to first_name etc.
1543 $paymentParams = $this->_params
;
1544 $paymentParams['contactID'] = $this->_contactID
;
1545 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
1547 $contributionType = new CRM_Financial_DAO_FinancialType();
1548 $contributionType->id
= $params['financial_type_id'];
1549 if (!$contributionType->find(TRUE)) {
1550 CRM_Core_Error
::fatal('Could not find a system table');
1553 // add some financial type details to the params list
1554 // if folks need to use it
1555 $paymentParams['contributionType_name'] = $this->_params
['contributionType_name'] = $contributionType->name
;
1556 $paymentParams['contributionPageID'] = NULL;
1557 if (!empty($this->_params
['is_email_receipt'])) {
1558 $paymentParams['email'] = $this->userEmail
;
1559 $paymentParams['is_email_receipt'] = 1;
1562 $paymentParams['is_email_receipt'] = 0;
1563 $this->_params
['is_email_receipt'] = 0;
1565 if (!empty($this->_params
['receive_date'])) {
1566 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1569 // For recurring contribution, create Contribution Record first.
1570 // Contribution ID, Recurring ID and Contact ID needed
1571 // When we get a callback from the payment processor, CRM-7115
1572 if (!empty($paymentParams['is_recur'])) {
1573 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1583 $paymentParams['contributionID'] = $contribution->id
;
1584 $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
1585 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
1586 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
1589 if ($paymentParams['amount'] > 0.0) {
1590 // force a re-get of the payment processor in case the form changed it, CRM-7179
1591 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this, TRUE);
1592 $result = $payment->doDirectPayment($paymentParams);
1595 if (is_a($result, 'CRM_Core_Error')) {
1596 //make sure to cleanup db for recurring case.
1597 if (!empty($paymentParams['contributionID'])) {
1598 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1599 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
1601 if (!empty($paymentParams['contributionRecurID'])) {
1602 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1603 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
1606 //set the contribution mode.
1607 $urlParams = "action=add&cid={$this->_contactID}";
1609 $urlParams .= "&mode={$this->_mode}";
1611 if (!empty($this->_ppID
)) {
1612 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1614 CRM_Core_Error
::displaySessionError($result);
1615 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams));
1619 $this->_params
= array_merge($this->_params
, $result);
1622 $this->_params
['receive_date'] = $now;
1624 if (!empty($this->_params
['is_email_receipt'])) {
1625 $this->_params
['receipt_date'] = $now;
1628 $this->_params
['receipt_date'] = CRM_Utils_Date
::processDate($this->_params
['receipt_date'],
1629 $params['receipt_date_time'], TRUE
1633 $this->set('params', $this->_params
);
1634 $this->assign('trxn_id', $result['trxn_id']);
1635 $this->assign('receive_date', $this->_params
['receive_date']);
1637 // result has all the stuff we need
1638 // lets archive it to a financial transaction
1639 if ($contributionType->is_deductible
) {
1640 $this->assign('is_deductible', TRUE);
1641 $this->set('is_deductible', TRUE);
1644 // set source if not set
1645 if (empty($this->_params
['source'])) {
1646 $userID = $session->get('userID');
1647 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1650 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1653 // build custom data getFields array
1654 $customFieldsContributionType = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE,
1655 CRM_Utils_Array
::value('financial_type_id', $params)
1657 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsContributionType,
1658 CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1660 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
1667 if (empty($paymentParams['is_recur'])) {
1668 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1679 //send receipt mail.
1680 if ($contribution->id
&& !empty($this->_params
['is_email_receipt'])) {
1681 $this->_params
['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $result);
1682 $this->_params
['contact_id'] = $this->_contactID
;
1683 $this->_params
['contribution_id'] = $contribution->id
;
1684 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $this->_params
, TRUE);
1688 if ($contribution->id
&& isset($params['note'])) {
1689 CRM_Contribute_Form_AdditionalInfo
::processNote($params, $contactID, $contribution->id
, NULL);
1692 if ($contribution->id
&& isset($params['product_name'][0])) {
1693 CRM_Contribute_Form_AdditionalInfo
::processPremium($params, $contribution->id
, NULL, $this->_options
);
1696 //update pledge payment status.
1697 if ($this->_ppID
&& $contribution->id
) {
1698 //store contribution id in payment record.
1699 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1701 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1702 array($this->_ppID
),
1703 $contribution->contribution_status_id
,
1705 $contribution->total_amount
1709 if ($contribution->id
) {
1710 $statusMsg = ts('The contribution record has been processed.');
1711 if (!empty($this->_params
['is_email_receipt']) && $sendReceipt) {
1712 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1714 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');