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 proceessing
50 * the id of the premium that we are proceessing
55 public $_premiumID = NULL;
56 public $_productDAO = NULL;
67 * the id of the contact associated with this contribution
75 * the id of the pledge payment that we are processing
83 * the id of the pledge that we are processing
91 * is this contribution associated with an online
92 * financial transaction
97 public $_online = FALSE;
100 * Stores all product option
108 * Store the contribution Type ID
112 public $_contributionType;
115 * The contribution values if an existing contribution
120 * The pledge values if this contribution is associated with pledge
122 public $_pledgeValues;
124 public $_contributeMode = 'direct';
131 * Store the line items if price set used.
136 * @var soft credit info
138 public $_softCreditInfo;
140 protected $_formType;
142 public $_honoreeProfileType;
145 * Function to set variables up before form is built
150 public function preProcess() {
152 //check permission for action.
153 if (!CRM_Core_Permission
::checkActionPermission('CiviContribute', $this->_action
)) {
154 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
157 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
159 $this->assign('cdType', FALSE);
160 if ($this->_cdType
) {
161 $this->assign('cdType', TRUE);
162 CRM_Custom_Form_CustomData
::preProcess($this);
166 $config = CRM_Core_Config
::singleton();
168 $this->_formType
= CRM_Utils_Array
::value('formType', $_GET);
171 $this->_priceSetId
= CRM_Utils_Array
::value('priceSetId', $_GET);
172 $this->set('priceSetId', $this->_priceSetId
);
173 $this->assign('priceSetId', $this->_priceSetId
);
175 //get the pledge payment id
176 $this->_ppID
= CRM_Utils_Request
::retrieve('ppid', 'Positive', $this);
179 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
182 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
183 $this->assign('action', $this->_action
);
185 //get the contribution id if update
186 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
187 if (!empty($this->_id
)) {
188 $this->assign('contribID', $this->_id
);
191 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
192 $this->assign('context', $this->_context
);
194 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
196 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
198 //set the contribution mode.
199 $this->_mode
= CRM_Utils_Request
::retrieve('mode', 'String', $this);
201 $this->assign('contributionMode', $this->_mode
);
203 $this->_paymentProcessor
= array('billing_mode' => 1);
205 $this->assign('showCheckNumber', TRUE);
207 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
208 $this->assignProcessors();
210 if ($this->_contactID
) {
211 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
212 $this->assign('displayName', $this->userDisplayName
);
215 // also check for billing information
216 // get the billing location type
217 $this->assignBillingType();
219 $this->_fields
= array();
221 CRM_Core_Payment_Form
::setPaymentFieldsByType(CRM_Utils_Array
::value('payment_type', $this->_processors
), $this);
223 if ($this->_action
& CRM_Core_Action
::DELETE
) {
227 if (in_array('CiviPledge', $config->enableComponents
) && !$this->_formType
) {
228 $this->preProcessPledge();
232 $this->showRecordLinkMesssage($this->_id
);
234 $this->_values
= array();
236 // current contribution id
238 $this->assignPremiumProduct($this->_id
);
239 $this->buildValuesAndAssignOnline_Note_Type($this->_id
, $this->_values
);
242 // when custom data is included in this page
243 if (!empty($_POST['hidden_custom'])) {
244 $this->applyCustomData('Contribution', CRM_Utils_Array
::value('financial_type_id', $_POST), $this->_id
);
247 $this->_lineItems
= array();
249 if (!empty($this->_compId
) && $this->_compContext
== 'participant') {
250 $this->assign('compId', $this->_compId
);
251 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_compId
);
254 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution', 1);
256 empty($lineItem) ?
NULL : $this->_lineItems
[] = $lineItem;
259 $this->assign('lineItem', empty($this->_lineItems
) ?
FALSE : $this->_lineItems
);
262 if ($this->_contactID
) {
263 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
265 // Check if this is default domain contact CRM-10482
266 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_contactID
)) {
267 $displayName .= ' (' . ts('default organization') . ')';
270 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
271 CRM_Utils_System
::setTitle(ts('Contribution from') . ' ' . $displayName);
275 CRM_Contribute_Form_SoftCredit
::preprocess($this);
279 function setDefaultValues() {
280 if ($this->_cdType
) {
281 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
284 $defaults = $this->_values
;
286 //set defaults for pledge payment.
288 $defaults['total_amount'] = CRM_Utils_Array
::value('scheduled_amount', $this->_pledgeValues
['pledgePayment']);
289 $defaults['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $this->_pledgeValues
);
290 $defaults['currency'] = CRM_Utils_Array
::value('currency', $this->_pledgeValues
);
291 $defaults['option_type'] = 1;
295 if ($this->_action
& CRM_Core_Action
::DELETE
) {
299 // set soft credit defaults
300 CRM_Contribute_Form_SoftCredit
::setDefaultValues($defaults, $this);
303 $config = CRM_Core_Config
::singleton();
304 // set default country from config if no country set
305 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
306 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
309 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
310 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
313 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID
);
314 $defaults = array_merge($defaults, $billingDefaults);
316 // now fix all state country selectors, set correct state based on country
317 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults);
321 $this->_contactID
= $defaults['contact_id'];
324 // Set $newCredit variable in template to control whether link to credit card mode is included
325 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
327 // fix the display of the monetary value, CRM-4038
328 if (isset($defaults['total_amount'])) {
329 $defaults['total_amount'] = CRM_Utils_Money
::format($defaults['total_amount'], NULL, '%a');
332 if (isset($defaults['non_deductible_amount'])) {
333 $defaults['non_deductible_amount'] = CRM_Utils_Money
::format($defaults['non_deductible_amount'], NULL, '%a');
336 if (isset($defaults['fee_amount'])) {
337 $defaults['fee_amount'] = CRM_Utils_Money
::format($defaults['fee_amount'], NULL, '%a');
340 if (isset($defaults['net_amount'])) {
341 $defaults['net_amount'] = CRM_Utils_Money
::format($defaults['net_amount'], NULL, '%a');
344 if ($this->_contributionType
) {
345 $defaults['financial_type_id'] = $this->_contributionType
;
348 if (empty($defaults['payment_instrument_id'])) {
349 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
352 if (!empty($defaults['is_test'])) {
353 $this->assign('is_test', TRUE);
356 $this->assign('showOption', TRUE);
357 // for Premium section
358 if ($this->_premiumID
) {
359 $this->assign('showOption', FALSE);
360 $options = isset($this->_options
[$this->_productDAO
->product_id
]) ?
$this->_options
[$this->_productDAO
->product_id
] : "";
362 $this->assign('showOption', TRUE);
364 $options_key = CRM_Utils_Array
::key($this->_productDAO
->product_option
, $options);
366 $defaults['product_name'] = array($this->_productDAO
->product_id
, trim($options_key));
369 $defaults['product_name'] = array($this->_productDAO
->product_id
);
371 if ($this->_productDAO
->fulfilled_date
) {
372 list($defaults['fulfilled_date']) = CRM_Utils_Date
::setDateDefaults($this->_productDAO
->fulfilled_date
);
376 if (isset($this->userEmail
)) {
377 $this->assign('email', $this->userEmail
);
380 if (!empty($defaults['is_pay_later'])) {
381 $this->assign('is_pay_later', TRUE);
383 $this->assign('contribution_status_id', CRM_Utils_Array
::value('contribution_status_id', $defaults));
385 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
386 foreach ($dates as $key) {
387 if (!empty($defaults[$key])) {
388 list($defaults[$key],
389 $defaults[$key . '_time']
390 ) = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Array
::value($key, $defaults),
396 if (!$this->_id
&& empty($defaults['receive_date'])) {
397 list($defaults['receive_date'],
398 $defaults['receive_date_time']
399 ) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
402 $this->assign('receive_date', CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('receive_date', $defaults),
403 CRM_Utils_Array
::value('receive_date_time', $defaults)
405 $currency = CRM_Utils_Array
::value('currency', $defaults);
406 $this->assign('currency', $currency);
407 // Hack to get currency info to the js layer. CRM-11440
408 CRM_Utils_Money
::format(1);
409 $this->assign('currencySymbol', CRM_Utils_Array
::value($currency, CRM_Utils_Money
::$_currencySymbols));
410 $this->assign('totalAmount', CRM_Utils_Array
::value('total_amount', $defaults));
412 //inherit campaign from pledge.
413 if ($this->_ppID
&& !empty($this->_pledgeValues
['campaign_id'])) {
414 $defaults['campaign_id'] = $this->_pledgeValues
['campaign_id'];
417 $this->_defaults
= $defaults;
422 * Function to build the form
427 public function buildQuickForm() {
428 if ($this->_cdType
) {
429 CRM_Custom_Form_CustomData
::buildQuickForm($this);
433 // build price set form.
434 $buildPriceSet = FALSE;
435 if (empty($this->_lineItems
) &&
436 ($this->_priceSetId ||
!empty($_POST['price_set_id']))
438 $buildPriceSet = TRUE;
439 $getOnlyPriceSetElements = TRUE;
440 if (!$this->_priceSetId
) {
441 $this->_priceSetId
= $_POST['price_set_id'];
442 $getOnlyPriceSetElements = FALSE;
445 $this->set('priceSetId', $this->_priceSetId
);
446 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
448 // get only price set form elements.
449 if ($getOnlyPriceSetElements) {
453 // use to build form during form rule.
454 $this->assign('buildPriceSet', $buildPriceSet);
456 $showAdditionalInfo = FALSE;
458 $defaults = $this->_values
;
459 $additionalDetailFields = array(
463 'non_deductible_amount',
467 foreach ($additionalDetailFields as $key) {
468 if (!empty($defaults[$key])) {
469 $defaults['hidden_AdditionalDetail'] = 1;
474 if ($this->_productDAO
) {
475 if ($this->_productDAO
->product_id
) {
476 $defaults['hidden_Premium'] = 1;
480 if ($this->_noteID
&&
481 isset($this->_values
['note'])
483 $defaults['hidden_AdditionalDetail'] = 1;
487 ts('Additional Details') => 'AdditionalDetail',
490 //Add Premium pane only if Premium is exists.
491 $dao = new CRM_Contribute_DAO_Product();
494 if ($dao->find(TRUE)) {
495 $paneNames[ts('Premium Information')] = 'Premium';
500 if (CRM_Utils_Array
::value('payment_type', $this->_processors
) & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
502 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
505 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
508 if (is_array($ccPane)) {
509 $paneNames = array_merge($ccPane, $paneNames);
512 $buildRecurBlock = FALSE;
513 foreach ($paneNames as $name => $type) {
514 $urlParams = "snippet=4&formType={$type}";
516 $urlParams .= "&mode={$this->_mode}";
520 if ($type == 'CreditCard' ||
521 $type == 'DirectDebit'
526 $allPanes[$name] = array(
527 'url' => CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams),
532 // see if we need to include this paneName in the current form
533 if ($this->_formType
== $type ||
!empty($_POST["hidden_{$type}"]) ||
534 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
536 $showAdditionalInfo = TRUE;
537 $allPanes[$name]['open'] = 'true';
540 if ($type == 'CreditCard') {
541 $buildRecurBlock = TRUE;
542 $this->add('hidden', 'hidden_CreditCard', 1);
543 CRM_Core_Payment_Form
::buildCreditCard($this, TRUE);
545 elseif ($type == 'DirectDebit') {
546 $buildRecurBlock = TRUE;
547 $this->add('hidden', 'hidden_DirectDebit', 1);
548 CRM_Core_Payment_Form
::buildDirectDebit($this, TRUE);
551 $additionalInfoFormFunction = 'build' . $type;
552 CRM_Contribute_Form_AdditionalInfo
::$additionalInfoFormFunction($this);
555 if (empty($this->_recurPaymentProcessors
)) {
556 $buildRecurBlock = FALSE;
558 if ($buildRecurBlock) {
559 CRM_Contribute_Form_Contribution_Main
::buildRecur($this);
560 $this->setDefaults(array('is_recur' => 0));
562 $this->assign('buildRecurBlock', $buildRecurBlock);
563 $qfKey = $this->controller
->_key
;
564 $this->assign('qfKey', $qfKey);
565 $this->assign('allPanes', $allPanes);
566 $this->assign('showAdditionalInfo', $showAdditionalInfo);
568 if ($this->_formType
) {
569 $this->assign('formType', $this->_formType
);
573 $this->applyFilter('__ALL__', 'trim');
575 if ($this->_action
& CRM_Core_Action
::DELETE
) {
576 $this->addButtons(array(
579 'name' => ts('Delete'),
580 'spacing' => ' ',
585 'name' => ts('Cancel')
592 //need to assign custom data type and subtype to the template
593 $this->assign('customDataType', 'Contribution');
594 $this->assign('customDataSubType', $this->_contributionType
);
595 $this->assign('entityID', $this->_id
);
597 if ($this->_context
== 'standalone') {
598 $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE);
601 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
603 $financialType = $this->add('select', 'financial_type_id',
604 ts('Financial Type'),
605 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
607 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
611 $paymentInstrument = $this->add('select', 'payment_instrument_id',
613 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
614 TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
618 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']);
620 //add receipt for offline contribution
621 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
623 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
625 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
627 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
628 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
635 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
638 elseif ((!$this->_ppID
&& $this->_id
) ||
!$this->_id
) {
639 $suppressFlag = FALSE;
641 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
642 if (CRM_Utils_Array
::value('membership', $componentDetails) || CRM_Utils_Array
::value('participant', $componentDetails)) {
643 $suppressFlag = TRUE;
646 if (!$suppressFlag) {
651 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
655 unset($status[CRM_Utils_Array
::key('Overdue', $statusName)]);
660 $contributionStatus = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id
, 'contribution_status_id');
661 $name = CRM_Utils_Array
::value($contributionStatus, $statusName);
666 unset($status[CRM_Utils_Array
::key('In Progress', $statusName)]);
667 unset($status[CRM_Utils_Array
::key('Pending', $statusName)]);
668 unset($status[CRM_Utils_Array
::key('Failed', $statusName)]);
672 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
682 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
688 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
691 $this->add('select', 'contribution_status_id',
692 ts('Contribution Status'),
698 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
700 if ($this->_online
) {
701 $this->assign('hideCalender', TRUE);
703 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
705 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
706 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
708 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
711 if ($buildRecurBlock) {
712 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
714 $element = $this->add('select',
715 'payment_processor_id',
716 ts('Payment Processor'),
722 if ($this->_online
) {
726 if (empty($this->_lineItems
)) {
727 $buildPriceSet = FALSE;
728 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviContribute');
729 if (!empty($priceSets) && !$this->_ppID
) {
730 $buildPriceSet = TRUE;
733 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
734 // and if we already have line items for that participant. CRM-5095
735 if ($buildPriceSet && $this->_id
) {
736 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
737 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
742 if ($pledgePaymentId) {
743 $buildPriceSet = FALSE;
745 if ($participantID = CRM_Utils_Array
::value('participant', $componentDetails)) {
746 $participantLI = CRM_Price_BAO_LineItem
::getLineItems($participantID);
747 if (!CRM_Utils_System
::isNull($participantLI)) {
748 $buildPriceSet = FALSE;
753 $hasPriceSets = FALSE;
754 if ($buildPriceSet) {
755 $hasPriceSets = TRUE;
756 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
758 '' => ts('Choose price set')
760 NULL, array('onchange' => "buildAmount( this.value );")
762 if ($this->_online
&& !($this->_action
& CRM_Core_Action
::UPDATE
)) {
766 $this->assign('hasPriceSets', $hasPriceSets);
767 $currencyFreeze = FALSE;
768 if (!($this->_action
& CRM_Core_Action
::UPDATE
)) {
769 if ($this->_online ||
$this->_ppID
) {
770 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
772 'style' => "background-color:#EBECE4"
774 $optionTypes = array(
775 '1' => ts('Adjust Pledge Payment Schedule?'),
776 '2' => ts('Adjust Total Pledge Amount?'),
778 $this->addRadio('option_type',
784 $currencyFreeze = TRUE;
788 $totalAmount = $this->addMoney('total_amount',
790 ($hasPriceSets) ?
FALSE : TRUE,
791 $attributes['total_amount'],
792 TRUE, 'currency', NULL, $currencyFreeze
796 $this->add('text', 'source', ts('Source'), CRM_Utils_Array
::value('source', $attributes));
798 //CRM-7362 --add campaigns.
799 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
801 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
805 $js = array('onclick' => "return verify( );");
808 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
811 $this->assign('outBound_option', $mailingInfo['outBound_option']);
813 $this->addButtons(array(
816 'name' => ts('Save'),
822 'name' => ts('Save and New'),
828 'name' => ts('Cancel')
833 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
834 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
835 if ($this->_id
&& $this->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
837 $totalAmount->freeze();
839 $checkNumber->freeze();
840 $paymentInstrument->freeze();
842 $financialType->freeze();
845 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
847 if ($this->_action
& CRM_Core_Action
::VIEW
) {
855 * @param array $fields the input form values
856 * @param array $files the uploaded files if any
859 * @internal param array $options additional user data
861 * @return true if no errors, else array of errors
865 static function formRule($fields, $files, $self) {
868 //check for Credit Card Contribution.
870 if (empty($fields['payment_processor_id'])) {
871 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
875 // do the amount validations.
876 if (empty($fields['total_amount']) && empty($self->_lineItems
)) {
877 if ($priceSetId = CRM_Utils_Array
::value('price_set_id', $fields)) {
878 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $fields, $errors);
882 $softErrors = CRM_Contribute_Form_SoftCredit
::formRule($fields, $errors, $self);
884 if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) ||
!empty($fields['fee_amount']))) {
885 $sum = CRM_Utils_Rule
::cleanMoney($fields['net_amount']) + CRM_Utils_Rule
::cleanMoney($fields['fee_amount']);
886 if (CRM_Utils_Rule
::cleanMoney($fields['total_amount']) != $sum) {
887 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
890 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
891 if ($self->_id
&& $self->_values
['contribution_status_id'] != $fields['contribution_status_id']) {
892 CRM_Contribute_BAO_Contribution
::checkStatusValidation($self->_values
, $fields, $errors);
895 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
896 if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($fields['financial_type_id'])) {
897 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
900 // $trxn_id must be unique CRM-13919
901 if (!empty($fields['trxn_id'])) {
902 $queryParams = array(1 => array($fields['trxn_id'], 'String'));
903 $query = 'select count(*) from civicrm_contribution where trxn_id = %1';
905 $queryParams[2] = array((int)$self->_id
, 'Integer');
906 $query .= ' and id !=%2';
908 $tCnt = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
910 $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id']));
914 $errors = array_merge($errors, $softErrors);
919 * Function to process the form
925 public function postProcess() {
926 $session = CRM_Core_Session
::singleton();
927 if ($this->_action
& CRM_Core_Action
::DELETE
) {
928 CRM_Contribute_BAO_Contribution
::deleteContribution($this->_id
);
929 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
930 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
935 // get the submitted form values.
936 $submittedValues = $this->controller
->exportValues($this->_name
);
937 if (!empty($submittedValues['price_set_id']) && $this->_action
& CRM_Core_Action
::UPDATE
) {
938 $line = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution');
939 $lineID = key($line);
940 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $line[$lineID]), 'price_set_id');
941 $quickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
943 CRM_Price_BAO_LineItem
::deleteLineItems($this->_id
, 'civicrm_contribution');
947 // process price set and get total amount and line items.
949 $priceSetId = $pId = NULL;
950 $priceSetId = CRM_Utils_Array
::value('price_set_id', $submittedValues);
951 if (empty($priceSetId) && !$this->_id
) {
952 $this->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
953 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
954 $fieldID = key($this->_priceSet
['fields']);
955 $fieldValueId = key($this->_priceSet
['fields'][$fieldID]['options']);
956 $this->_priceSet
['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
957 $submittedValues['price_' . $fieldID] = 1;
961 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
962 $submittedValues, $lineItem[$priceSetId]);
964 $submittedValues['total_amount'] = CRM_Utils_Array
::value('amount', $submittedValues);
968 $pId = ($this->_compId
&& $this->_context
== 'participant') ?
$this->_compId
: CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id
, 'participant_id', 'contribution_id');
970 if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id
) {
971 // 10117 update th line items for participants
973 $entityTable = 'participant';
975 $participantParams = array(
976 'fee_amount' => $submittedValues['total_amount'],
979 CRM_Event_BAO_Participant
::add($participantParams);
980 if (empty($this->_lineItems
)) {
981 $this->_lineItems
= CRM_Price_BAO_LineItem
::getLineItems($entityID, 'participant', 1);
985 $entityTable = 'contribution';
986 $entityID = $this->_id
;
989 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($entityID, $entityTable);
990 foreach (array_keys($lineItems) as $id) {
991 $lineItems[$id]['id'] = $id;
993 $itemId = key($lineItems);
995 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
996 $fieldType = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'html_type');
997 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1000 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1001 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('total_amount', $submittedValues));
1003 // 10117 update th line items for participants
1004 if (!empty($lineItems[$itemId]['price_field_id'])) {
1005 $lineItem[$this->_priceSetId
] = $lineItems;
1009 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1012 //CRM-11529 for quick config backoffice transactions
1013 //when financial_type_id is passed in form, update the
1014 //lineitems with the financial type selected in form
1015 if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array
::value($this->_priceSetId
, $lineItem)
1017 foreach ($lineItem[$this->_priceSetId
] as &$values) {
1018 $values['financial_type_id'] = $submittedValues['financial_type_id'];
1022 if (!isset($submittedValues['total_amount'])) {
1023 $submittedValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_values
);
1025 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1027 if (!empty($submittedValues['pcp_made_through_id'])) {
1030 'pcp_made_through_id',
1031 'pcp_display_in_roll',
1032 'pcp_roll_nickname',
1033 'pcp_personal_note',
1035 foreach ($fields as $f) {
1036 $pcp[$f] = CRM_Utils_Array
::value($f, $submittedValues);
1040 $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id']));
1041 if ($this->_id
&& count($isEmpty) == 1 && key($isEmpty) == NULL) {
1042 //Delete existing soft credit records if soft credit list is empty on update
1043 CRM_Contribute_BAO_ContributionSoft
::del(array('contribution_id' => $this->_id
));
1046 //build soft credit params
1047 $softParams = $softIDs =array();
1048 foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) {
1049 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1050 $softParams[$key]['contact_id'] = $val;
1051 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1052 $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key];
1053 if (!empty($submittedValues['soft_credit_id'][$key])) {
1054 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
1060 // set the contact, when contact is selected
1061 if (!empty($submittedValues['contact_id'])) {
1062 $this->_contactID
= $submittedValues['contact_id'];
1065 $config = CRM_Core_Config
::singleton();
1067 //Credit Card Contribution.
1069 $this->processCreditCard($submittedValues, $config, $session, $pId, $lineItem);
1072 //Offline Contribution.
1073 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1075 // get the required field value only.
1076 $formValues = $submittedValues;
1077 $params = $ids = array();
1079 $params['contact_id'] = $this->_contactID
;
1081 $params['currency'] = $this->getCurrency($submittedValues);
1084 'financial_type_id',
1085 'contribution_status_id',
1086 'payment_instrument_id',
1091 foreach ($fields as $f) {
1092 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
1096 $params['pcp'] = $pcp;
1098 if (!empty($softParams)) {
1099 $params['soft_credit'] = $softParams;
1100 $params['soft_credit_ids'] = $softIDs;
1103 //if priceset is used, no need to cleanup money
1106 $params['skipCleanMoney'] = 1;
1115 foreach ($dates as $d) {
1116 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1119 if (!empty($formValues['is_email_receipt'])) {
1120 $params['receipt_date'] = date("Y-m-d");
1123 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Cancelled', 'name')
1124 ||
$params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name')
1126 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value('cancel_date', $params))) {
1127 $params['cancel_date'] = date('Y-m-d');
1131 $params['cancel_date'] = $params['cancel_reason'] = 'null';
1134 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
1135 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Pending', 'name')) {
1136 $params['is_pay_later'] = 1;
1139 $ids['contribution'] = $params['id'] = $this->_id
;
1141 //Add Additional common information to formatted params
1142 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($formValues, $params, $this);
1144 $params['contribution_mode'] = 'participant';
1145 $params['participant_id'] = $pId;
1146 $params['skipLineItem'] = 1;
1148 $params['line_item'] = $lineItem;
1149 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array
::value('id', $this->_paymentProcessor
);
1150 //create contribution.
1151 if ($isQuickConfig) {
1152 $params['is_quick_config'] = 1;
1156 // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it.
1157 if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) {
1158 $params['non_deductible_amount'] = $params['non_deductible_amount'];
1160 // if non_deductible_amount does NOT exist - then calculate it depending on:
1161 // $ContributionType->is_deductible and whether there is a product (premium).
1163 $contributionType = new CRM_Financial_DAO_FinancialType();
1164 $contributionType->id
= $params['financial_type_id'];
1165 if (!$contributionType->find(TRUE)) {
1166 CRM_Core_Error
::fatal('Could not find a system table');
1168 if ($contributionType->is_deductible
) {
1170 if (isset($formValues['product_name'][0])) {
1171 $selectProduct = $formValues['product_name'][0];
1173 // if there is a product - compare the value to the contribution amount
1174 if (isset($selectProduct)) {
1175 $productDAO = new CRM_Contribute_DAO_Product();
1176 $productDAO->id
= $selectProduct;
1177 $productDAO->find(TRUE);
1178 // product value exceeds contribution amount
1179 if ($params['total_amount'] < $productDAO->price
) {
1180 $params['non_deductible_amount'] = $params['total_amount'];
1182 // product value does NOT exceed contribution amount
1184 $params['non_deductible_amount'] = $productDAO->price
;
1187 // contribution is deductible - but there is no product
1189 $params['non_deductible_amount'] = '0.00';
1192 // contribution is NOT deductible
1194 $params['non_deductible_amount'] = $params['total_amount'];
1198 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
1200 // process associated membership / participant, CRM-4395
1201 $relatedComponentStatusMsg = NULL;
1202 if ($contribution->id
&& $this->_action
& CRM_Core_Action
::UPDATE
) {
1203 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id
,
1204 $contribution->contribution_status_id
,
1205 CRM_Utils_Array
::value('contribution_status_id',
1212 if ($contribution->id
&& isset($formValues['note'])) {
1213 CRM_Contribute_Form_AdditionalInfo
::processNote($formValues, $this->_contactID
, $contribution->id
, $this->_noteID
);
1217 if ($contribution->id
&& isset($formValues['product_name'][0])) {
1218 CRM_Contribute_Form_AdditionalInfo
::processPremium($formValues, $contribution->id
,
1219 $this->_premiumID
, $this->_options
1223 //send receipt mail.
1224 if ($contribution->id
&& !empty($formValues['is_email_receipt'])) {
1225 $formValues['contact_id'] = $this->_contactID
;
1226 $formValues['contribution_id'] = $contribution->id
;
1228 $formValues +
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($contribution->id
);
1230 // to get 'from email id' for send receipt
1231 $this->fromEmailId
= $formValues['from_email_address'];
1232 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $formValues);
1235 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1241 //update pledge payment status.
1242 if ((($this->_ppID
&& $contribution->id
) && $this->_action
& CRM_Core_Action
::ADD
) ||
1243 (($pledgePaymentId) && $this->_action
& CRM_Core_Action
::UPDATE
)
1247 //store contribution id in payment record.
1248 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1251 $this->_ppID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1256 $this->_pledgeID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1263 $adjustTotalAmount = FALSE;
1264 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
1265 $adjustTotalAmount = TRUE;
1268 $updatePledgePaymentStatus = FALSE;
1269 //do only if either the status or the amount has changed
1270 if ($this->_action
& CRM_Core_Action
::ADD
) {
1271 $updatePledgePaymentStatus = TRUE;
1273 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& (($this->_defaults
['contribution_status_id'] != $formValues['contribution_status_id']) ||
1274 ($this->_defaults
['total_amount'] != $formValues['total_amount']))
1276 $updatePledgePaymentStatus = TRUE;
1279 if ($updatePledgePaymentStatus) {
1280 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1281 array($this->_ppID
),
1282 $contribution->contribution_status_id
,
1284 $contribution->total_amount
,
1290 $statusMsg = ts('The contribution record has been saved.');
1291 if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
1292 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1295 if ($relatedComponentStatusMsg) {
1296 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1299 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1300 //Offline Contribution ends.
1302 $buttonName = $this->controller
->getButtonName();
1303 if ($this->_context
== 'standalone') {
1304 if ($buttonName == $this->getButtonName('upload', 'new')) {
1305 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/add',
1306 'reset=1&action=add&context=standalone'
1310 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
1311 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1315 elseif ($this->_context
== 'contribution' && $this->_mode
&& $buttonName == $this->getButtonName('upload', 'new')) {
1316 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1317 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1320 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1321 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1322 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1326 //store contribution ID if not yet set (on create)
1327 if ( empty($this->_id
) && !empty($contribution->id
) ) {
1328 $this->_id
= $contribution->id
;
1332 public function processCreditCard($submittedValues, $config, $session, $pId, $lineItem) {
1333 $unsetParams = array(
1335 'payment_instrument_id',
1336 'contribution_status_id',
1340 foreach ($unsetParams as $key) {
1341 if (isset($submittedValues[$key])) {
1342 unset($submittedValues[$key]);
1346 // CRM-12680 set $_lineItem if its not set
1347 if (empty($this->_lineItem
) && !empty($lineItem)) {
1348 $this->_lineItem
= $lineItem;
1351 //Get the rquire fields value only.
1352 $params = $this->_params
= $submittedValues;
1354 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_params
['payment_processor_id'],
1358 //get the payment processor id as per mode.
1359 $this->_params
['payment_processor'] = $params['payment_processor_id'] =
1360 $this->_params
['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor
['id'];
1362 $now = date('YmdHis');
1365 // we need to retrieve email address
1366 if ($this->_context
== 'standalone' && !empty($submittedValues['is_email_receipt'])) {
1367 list($this->userDisplayName
,
1369 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
1370 $this->assign('displayName', $this->userDisplayName
);
1373 //set email for primary location.
1374 $fields['email-Primary'] = 1;
1375 $params['email-Primary'] = $this->userEmail
;
1377 // now set the values for the billing location.
1378 foreach ($this->_fields
as $name => $dontCare) {
1382 // also add location name to the array
1383 $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);
1384 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1385 $fields["address_name-{$this->_bltID}"] = 1;
1387 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1392 $nameFields = array('first_name', 'middle_name', 'last_name');
1393 foreach ($nameFields as $name) {
1395 if (array_key_exists("billing_$name", $params)) {
1396 $params[$name] = $params["billing_{$name}"];
1397 $params['preserveDBName'] = TRUE;
1401 if (!empty($params['source'])) {
1402 unset($params['source']);
1404 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $fields,
1410 // add all the additioanl payment params we need
1411 $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}"]);
1412 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
1414 if ($this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_CREDIT_CARD
) {
1415 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
1416 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
1418 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
1419 $this->_params
['amount'] = $this->_params
['total_amount'];
1420 $this->_params
['amount_level'] = 0;
1421 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
1423 $config->defaultCurrency
1425 $this->_params
['payment_action'] = 'Sale';
1426 if (!empty($this->_params
['receive_date'])) {
1427 $this->_params
['receive_date'] = CRM_Utils_Date
::processDate($this->_params
['receive_date'], $this->_params
['receive_date_time']);
1430 if (!empty($params['soft_credit_to'])) {
1431 $this->_params
['soft_credit_to'] = $params['soft_credit_to'];
1432 $this->_params
['pcp_made_through_id'] = $params['pcp_made_through_id'];
1435 $this->_params
['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $params);
1436 $this->_params
['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $params);
1437 $this->_params
['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $params);
1439 //Add common data to formatted params
1440 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
1442 if (empty($this->_params
['invoice_id'])) {
1443 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
1446 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
1449 // at this point we've created a contact and stored its address etc
1450 // all the payment processors expect the name and address to be in the
1451 // so we copy stuff over to first_name etc.
1452 $paymentParams = $this->_params
;
1453 $paymentParams['contactID'] = $this->_contactID
;
1454 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
1456 $contributionType = new CRM_Financial_DAO_FinancialType();
1457 $contributionType->id
= $params['financial_type_id'];
1458 if (!$contributionType->find(TRUE)) {
1459 CRM_Core_Error
::fatal('Could not find a system table');
1462 // add some financial type details to the params list
1463 // if folks need to use it
1464 $paymentParams['contributionType_name'] = $this->_params
['contributionType_name'] = $contributionType->name
;
1465 $paymentParams['contributionPageID'] = NULL;
1466 if (!empty($this->_params
['is_email_receipt'])) {
1467 $paymentParams['email'] = $this->userEmail
;
1468 $paymentParams['is_email_receipt'] = 1;
1471 $paymentParams['is_email_receipt'] = 0;
1472 $this->_params
['is_email_receipt'] = 0;
1474 if (!empty($this->_params
['receive_date'])) {
1475 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1480 // For recurring contribution, create Contribution Record first.
1481 // Contribution ID, Recurring ID and Contact ID needed
1482 // When we get a callback from the payment processor, CRM-7115
1483 if (!empty($paymentParams['is_recur'])) {
1484 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1493 $paymentParams['contributionID'] = $contribution->id
;
1494 $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
1495 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
1496 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
1499 if ($paymentParams['amount'] > 0.0) {
1500 // force a reget of the payment processor in case the form changed it, CRM-7179
1501 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this, TRUE);
1502 $result = $payment->doDirectPayment($paymentParams);
1505 if (is_a($result, 'CRM_Core_Error')) {
1506 //make sure to cleanup db for recurring case.
1507 if (!empty($paymentParams['contributionID'])) {
1508 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1509 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
1511 if (!empty($paymentParams['contributionRecurID'])) {
1512 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1513 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
1516 //set the contribution mode.
1517 $urlParams = "action=add&cid={$this->_contactID}";
1519 $urlParams .= "&mode={$this->_mode}";
1521 if (!empty($this->_ppID
)) {
1522 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1524 CRM_Core_Error
::displaySessionError($result);
1525 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams));
1529 $this->_params
= array_merge($this->_params
, $result);
1532 $this->_params
['receive_date'] = $now;
1534 if (!empty($this->_params
['is_email_receipt'])) {
1535 $this->_params
['receipt_date'] = $now;
1538 $this->_params
['receipt_date'] = CRM_Utils_Date
::processDate($this->_params
['receipt_date'],
1539 $params['receipt_date_time'], TRUE
1543 $this->set('params', $this->_params
);
1544 $this->assign('trxn_id', $result['trxn_id']);
1545 $this->assign('receive_date', $this->_params
['receive_date']);
1547 // result has all the stuff we need
1548 // lets archive it to a financial transaction
1549 if ($contributionType->is_deductible
) {
1550 $this->assign('is_deductible', TRUE);
1551 $this->set('is_deductible', TRUE);
1554 // set source if not set
1555 if (empty($this->_params
['source'])) {
1556 $userID = $session->get('userID');
1557 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1560 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1563 // build custom data getFields array
1564 $customFieldsContributionType = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE,
1565 CRM_Utils_Array
::value('financial_type_id', $params)
1567 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsContributionType,
1568 CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1570 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
1577 if (empty($paymentParams['is_recur'])) {
1578 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1587 //send receipt mail.
1588 if ($contribution->id
&& !empty($this->_params
['is_email_receipt'])) {
1589 $this->_params
['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $result);
1590 $this->_params
['contact_id'] = $this->_contactID
;
1591 $this->_params
['contribution_id'] = $contribution->id
;
1592 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $this->_params
, TRUE);
1596 if ($contribution->id
&& isset($params['note'])) {
1597 CRM_Contribute_Form_AdditionalInfo
::processNote($params, $contactID, $contribution->id
, NULL);
1600 if ($contribution->id
&& isset($params['product_name'][0])) {
1601 CRM_Contribute_Form_AdditionalInfo
::processPremium($params, $contribution->id
, NULL, $this->_options
);
1604 //update pledge payment status.
1605 if ($this->_ppID
&& $contribution->id
) {
1606 //store contribution id in payment record.
1607 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1609 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1610 array($this->_ppID
),
1611 $contribution->contribution_status_id
,
1613 $contribution->total_amount
1617 if ($contribution->id
) {
1618 $statusMsg = ts('The contribution record has been processed.');
1619 if (!empty($this->_params
['is_email_receipt']) && $sendReceipt) {
1620 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1622 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');