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 useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * form to process actions on the group aspect of Custom Data
39 class CRM_Contribute_Form_Contribution_Confirm
extends CRM_Contribute_Form_ContributionBase
{
42 * the id of the contact associated with this contribution
51 * The id of the contribution object that is created when the form is submitted
56 public $_contributionID;
59 * Set the parameters to be passed to contribution create function
61 * @param array $params
62 * @param integer $contactID
63 * @param $financialTypeID
65 * @param $contributionPageId
66 * @param $nonDeductibleAmount
72 * @param $paymentProcessorOutcome
75 * @param $recurringContributionID
80 * @param $softCreditToID
84 * @internal param $financialType
87 public static function getContributionParams($params, $contactID, $financialTypeID, $online, $contributionPageId, $nonDeductibleAmount, $campaignId, $isMonetary, $pending,
88 $paymentProcessorOutcome, $receiptDate, $recurringContributionID, $isTest, $addressID, $softCreditToID, $lineItems)
90 $contributionParams = array(
91 'contact_id' => $contactID,
92 'financial_type_id' => $financialTypeID,
93 'contribution_page_id' => $contributionPageId,
94 'receive_date' => (CRM_Utils_Array
::value('receive_date', $params)) ? CRM_Utils_Date
::processDate($params['receive_date']) : date('YmdHis'),
95 'non_deductible_amount' => $nonDeductibleAmount,
96 'total_amount' => $params['amount'],
97 'amount_level' => CRM_Utils_Array
::value('amount_level', $params),
98 'invoice_id' => $params['invoiceID'],
99 'currency' => $params['currencyID'],
101 (!$online ||
!empty($params['source'])) ?
102 CRM_Utils_Array
::value('source', $params) :
103 CRM_Utils_Array
::value('description', $params),
104 'is_pay_later' => CRM_Utils_Array
::value('is_pay_later', $params, 0),
105 //configure cancel reason, cancel date and thankyou date
106 //from 'contribution' type profile if included
107 'cancel_reason' => CRM_Utils_Array
::value('cancel_reason', $params, 0),
109 isset($params['cancel_date']) ?
110 CRM_Utils_Date
::format($params['cancel_date']) :
113 isset($params['thankyou_date']) ?
114 CRM_Utils_Date
::format($params['thankyou_date']) :
116 'campaign_id' => $campaignId,
117 'is_test' => $isTest,
118 'address_id' => $addressID,
119 //setting to make available to hook - although seems wrong to set on form for BAO hook availability
120 'soft_credit_to' => $softCreditToID,
121 'line_item' => $lineItems,
122 'skipLineItem' => CRM_Utils_Array
::value('skipLineItem', $params, 0)
124 if (!$online && isset($params['thankyou_date'])) {
125 $contributionParam['thankyou_date'] = $params['thankyou_date'];
127 if (!$online ||
$isMonetary) {
128 if (empty($params['is_pay_later'])) {
129 $contributionParams['payment_instrument_id'] = 1;
132 if (!$pending && $paymentProcessorOutcome) {
133 $contributionParams +
= array(
134 'fee_amount' => CRM_Utils_Array
::value('fee_amount', $paymentProcessorOutcome),
135 'net_amount' => CRM_Utils_Array
::value('net_amount', $paymentProcessorOutcome, $params['amount']),
136 'trxn_id' => $paymentProcessorOutcome['trxn_id'],
137 'receipt_date' => $receiptDate,
138 // also add financial_trxn details as part of fix for CRM-4724
139 'trxn_result_code' => CRM_Utils_Array
::value('trxn_result_code', $paymentProcessorOutcome),
140 'payment_processor' => CRM_Utils_Array
::value('payment_processor', $paymentProcessorOutcome),
144 // CRM-4038: for non-en_US locales, CRM_Contribute_BAO_Contribution::add() expects localised amounts
145 $contributionParams['non_deductible_amount'] = trim(CRM_Utils_Money
::format($contributionParams['non_deductible_amount'], ' '));
146 $contributionParams['total_amount'] = trim(CRM_Utils_Money
::format($contributionParams['total_amount'], ' '));
148 if ($recurringContributionID) {
149 $contributionParams['contribution_recur_id'] = $recurringContributionID;
152 $contributionParams['contribution_status_id'] = $pending ?
2 : 1;
153 if (isset($contributionParams['invoice_id'])) {
154 $contributionParams['id'] = CRM_Core_DAO
::getFieldValue(
155 'CRM_Contribute_DAO_Contribution',
156 $contributionParams['invoice_id'],
162 return $contributionParams;
166 * Function to set variables up before form is built
171 public function preProcess() {
172 $config = CRM_Core_Config
::singleton();
173 parent
::preProcess();
175 // lineItem isn't set until Register postProcess
176 $this->_lineItem
= $this->get('lineItem');
177 $this->_paymentProcessor
= $this->get('paymentProcessor');
179 if ($this->_contributeMode
== 'express') {
180 // rfp == redirect from paypal
181 $rfp = CRM_Utils_Request
::retrieve('rfp', 'Boolean',
182 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'GET'
185 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this);
186 $expressParams = $payment->getExpressCheckoutDetails($this->get('token'));
188 $this->_params
['payer'] = $expressParams['payer'];
189 $this->_params
['payer_id'] = $expressParams['payer_id'];
190 $this->_params
['payer_status'] = $expressParams['payer_status'];
192 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $expressParams, $this->_params
, FALSE);
194 // fix state and country id if present
195 if (!empty($this->_params
["billing_state_province_id-{$this->_bltID}"])) {
196 $this->_params
["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($this->_params
["billing_state_province_id-{$this->_bltID}"]);
198 if (!empty($this->_params
["billing_country_id-{$this->_bltID}"]) && $this->_params
["billing_country_id-{$this->_bltID}"]) {
199 $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
202 // set a few other parameters for PayPal
203 $this->_params
['token'] = $this->get('token');
205 $this->_params
['amount'] = $this->get('amount');
207 if (!empty($this->_membershipBlock
)){
208 $this->_params
['selectMembership'] = $this->get('selectMembership');
210 // we use this here to incorporate any changes made by folks in hooks
211 $this->_params
['currencyID'] = $config->defaultCurrency
;
213 $this->_params
['payment_action'] = 'Sale';
215 // also merge all the other values from the profile fields
216 $values = $this->controller
->exportValues('Main');
218 'amount', 'amount_other',
219 "billing_street_address-{$this->_bltID}",
220 "billing_city-{$this->_bltID}",
221 "billing_state_province_id-{$this->_bltID}",
222 "billing_postal_code-{$this->_bltID}",
223 "billing_country_id-{$this->_bltID}",
225 foreach ($values as $name => $value) {
227 if (!in_array($name, $skipFields)) {
228 $this->_params
[$name] = $value;
231 $this->set('getExpressCheckoutDetails', $this->_params
);
234 $this->_params
= $this->get('getExpressCheckoutDetails');
238 $this->_params
= $this->controller
->exportValues('Main');
240 if (!empty($this->_params
["billing_state_province_id-{$this->_bltID}"])) {
241 $this->_params
["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($this->_params
["billing_state_province_id-{$this->_bltID}"]);
243 if (!empty($this->_params
["billing_country_id-{$this->_bltID}"])) {
244 $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
247 if (isset($this->_params
['credit_card_exp_date'])) {
248 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
249 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
252 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
253 $this->_params
['amount'] = $this->get('amount');
255 $this->_useForMember
= $this->get('useForMember');
257 if (isset($this->_params
['amount'])) {
258 $this->setFormAmountFields($this->_params
['priceSetId']);
260 $this->_params
['currencyID'] = $config->defaultCurrency
;
261 $this->_params
['payment_action'] = 'Sale';
264 $this->_params
['is_pay_later'] = $this->get('is_pay_later');
265 $this->assign('is_pay_later', $this->_params
['is_pay_later']);
266 if ($this->_params
['is_pay_later']) {
267 $this->assign('pay_later_receipt', $this->_values
['pay_later_receipt']);
269 // if onbehalf-of-organization
270 if (!empty($this->_params
['hidden_onbehalf_profile'])) {
272 if (empty($this->_params
['org_option']) && empty($this->_params
['organization_id'])) {
273 if (!empty($this->_params
['onbehalfof_id'])) {
274 $this->_params
['organization_id'] = $this->_params
['onbehalfof_id'];
277 $this->_params
['organization_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_params
['onbehalf']['organization_name'], 'id', 'display_name');
281 $this->_params
['organization_name'] = $this->_params
['onbehalf']['organization_name'];
282 $addressBlocks = array(
283 'street_address', 'city', 'state_province',
284 'postal_code', 'country', 'supplemental_address_1',
285 'supplemental_address_2', 'supplemental_address_3',
286 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'address_name',
289 $blocks = array('email', 'phone', 'im', 'url', 'openid');
290 foreach ($this->_params
['onbehalf'] as $loc => $value) {
291 $field = $typeId = NULL;
292 if (strstr($loc, '-')) {
293 list($field, $locType) = explode('-', $loc);
296 if (in_array($field, $addressBlocks)) {
297 if ($locType == 'Primary') {
298 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
299 $locType = $defaultLocationType->id
;
302 if ($field == 'country') {
303 $value = CRM_Core_PseudoConstant
::countryIsoCode($value);
305 elseif ($field == 'state_province') {
306 $value = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value);
310 if (isset($this->_params
['onbehalf_location']['address'])
311 && count($this->_params
['onbehalf_location']['address']) > 0) {
315 $this->_params
['onbehalf_location']['address'][$locType][$field] = $value;
316 if (empty($this->_params
['onbehalf_location']['address'][$locType]['is_primary'])) {
317 $this->_params
['onbehalf_location']['address'][$locType]['is_primary'] = $isPrimary;
319 $this->_params
['onbehalf_location']['address'][$locType]['location_type_id'] = $locType;
321 elseif (in_array($field, $blocks)) {
322 if (!$typeId ||
is_numeric($typeId)) {
323 $blockName = $fieldName = $field;
324 $locationType = 'location_type_id';
325 if ( $locType == 'Primary' ) {
326 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
327 $locationValue = $defaultLocationType->id
;
330 $locationValue = $locType;
333 $phoneExtField = array();
335 if ($field == 'url') {
336 $blockName = 'website';
337 $locationType = 'website_type_id';
338 list($field, $locationValue) = explode('-', $loc);
340 elseif ($field == 'im') {
342 $locTypeId = 'provider_id';
343 $typeId = $this->_params
['onbehalf']["{$loc}-provider_id"];
345 elseif ($field == 'phone') {
346 list($field, $locType, $typeId) = explode('-', $loc);
347 $locTypeId = 'phone_type_id';
349 //check if extension field exists
350 $extField = str_replace('phone','phone_ext', $loc);
351 if (isset($this->_params
['onbehalf'][$extField])) {
352 $phoneExtField = array('phone_ext' => $this->_params
['onbehalf'][$extField]);
357 if ( isset ($this->_params
['onbehalf_location'][$blockName] )
358 && count( $this->_params
['onbehalf_location'][$blockName] ) > 0 ) {
361 if ($locationValue) {
362 $blockValues = array(
363 $fieldName => $value,
364 $locationType => $locationValue,
365 'is_primary' => $isPrimary,
369 $blockValues = array_merge($blockValues, array($locTypeId => $typeId));
371 if (!empty($phoneExtField)) {
372 $blockValues = array_merge($blockValues, $phoneExtField);
375 $this->_params
['onbehalf_location'][$blockName][] = $blockValues;
379 elseif (strstr($loc, 'custom')) {
380 if ($value && isset($this->_params
['onbehalf']["{$loc}_id"])) {
381 $value = $this->_params
['onbehalf']["{$loc}_id"];
383 $this->_params
['onbehalf_location']["{$loc}"] = $value;
386 if ($loc == 'contact_sub_type') {
387 $this->_params
['onbehalf_location'][$loc] = $value;
390 $this->_params
['onbehalf_location'][$field] = $value;
395 elseif (!empty($this->_values
['is_for_organization'])) {
396 // no on behalf of an organization, CRM-5519
397 // so reset loc blocks from main params.
399 'phone', 'email', 'address') as $blk) {
400 if (isset($this->_params
[$blk])) {
401 unset($this->_params
[$blk]);
406 // if auto renew checkbox is set, initiate a open-ended recurring membership
407 if ((!empty($this->_params
['selectMembership']) ||
!empty($this->_params
['priceSetId'])) && !empty($this->_paymentProcessor
['is_recur']) &&
408 CRM_Utils_Array
::value('auto_renew', $this->_params
) && empty($this->_params
['is_recur']) && empty($this->_params
['frequency_interval'])) {
410 $this->_params
['is_recur'] = $this->_values
['is_recur'] = 1;
411 // check if price set is not quick config
412 if (!empty($this->_params
['priceSetId']) && !CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params
['priceSetId'], 'is_quick_config')) {
413 list($this->_params
['frequency_interval'], $this->_params
['frequency_unit']) = CRM_Price_BAO_PriceSet
::getRecurDetails($this->_params
['priceSetId']);
416 // FIXME: set interval and unit based on selected membership type
417 $this->_params
['frequency_interval'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
418 $this->_params
['selectMembership'], 'duration_interval'
420 $this->_params
['frequency_unit'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
421 $this->_params
['selectMembership'], 'duration_unit'
427 $params = $this->processPcp($this, $this->_params
);
428 $this->_params
= $params;
430 $this->_params
['invoiceID'] = $this->get('invoiceID');
432 //carry campaign from profile.
433 if (array_key_exists('contribution_campaign_id', $this->_params
)) {
434 $this->_params
['campaign_id'] = $this->_params
['contribution_campaign_id'];
437 // assign contribution page id to the template so we can add css class for it
438 $this->assign('contributionPageID', $this->_id
);
440 $this->set('params', $this->_params
);
444 * Function to actually build the form
449 public function buildQuickForm() {
450 $this->assignToTemplate();
452 $params = $this->_params
;
453 // make sure we have values for it
454 if ($this->_honor_block_is_active
&& !empty($params['soft_credit_type_id'])) {
456 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
458 $this->assign('honor_block_is_active', $this->_honor_block_is_active
);
459 $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]);
460 CRM_Contribute_BAO_ContributionSoft
::formatHonoreeProfileFields($this, $params['honor'], $params['honoree_profile_id']);
462 $fieldTypes = array('Contact');
463 $fieldTypes[] = CRM_Core_BAO_UFGroup
::getContactType($params['honoree_profile_id']);
464 $this->buildCustom($params['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes);
466 $this->assign('receiptFromEmail', CRM_Utils_Array
::value('receipt_from_email', $this->_values
));
467 $amount_block_is_active = $this->get('amount_block_is_active');
468 $this->assign('amount_block_is_active', $amount_block_is_active);
470 if (!empty($params['selectProduct']) && $params['selectProduct'] != 'no_thanks') {
471 $option = CRM_Utils_Array
::value('options_' . $params['selectProduct'], $params);
472 $productID = $params['selectProduct'];
473 CRM_Contribute_BAO_Premium
::buildPremiumBlock($this, $this->_id
, FALSE,
476 $this->set('productID', $productID);
477 $this->set('option', $option);
479 $config = CRM_Core_Config
::singleton();
480 if (in_array('CiviMember', $config->enableComponents
)) {
481 if (isset($params['selectMembership']) &&
482 $params['selectMembership'] != 'no_thanks'
484 CRM_Member_BAO_Membership
::buildMembershipBlock($this,
486 $this->_membershipContactID
,
488 $params['selectMembership'],
493 $this->assign('membershipBlock', FALSE);
496 $this->buildCustom($this->_values
['custom_pre_id'], 'customPre', TRUE);
497 $this->buildCustom($this->_values
['custom_post_id'], 'customPost', TRUE);
499 if (!empty($params['hidden_onbehalf_profile'])) {
500 $ufJoinParams = array(
501 'module' => 'onBehalf',
502 'entity_table' => 'civicrm_contribution_page',
503 'entity_id' => $this->_id
,
505 $OnBehalfProfile = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
506 $profileId = $OnBehalfProfile[0];
508 $fieldTypes = array('Contact', 'Organization');
509 $contactSubType = CRM_Contact_BAO_ContactType
::subTypes('Organization');
510 $fieldTypes = array_merge($fieldTypes, $contactSubType);
511 if (is_array($this->_membershipBlock
) && !empty($this->_membershipBlock
)) {
512 $fieldTypes = array_merge($fieldTypes, array('Membership'));
515 $fieldTypes = array_merge($fieldTypes, array('Contribution'));
518 $this->buildCustom($profileId, 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes);
521 $this->_separateMembershipPayment
= $this->get('separateMembershipPayment');
522 $this->assign('is_separate_payment', $this->_separateMembershipPayment
);
523 if ($this->_priceSetId
&& !CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
524 $this->assign('lineItem', $this->_lineItem
);
526 $this->assign('is_quick_config', 1);
527 $this->_params
['is_quick_config'] = 1;
529 $this->assign('priceSetID', $this->_priceSetId
);
530 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
531 if ($this->_paymentProcessor
&&
532 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('Google_Checkout', $paymentProcessorType)
533 && !$this->_params
['is_pay_later'] && !($this->_amount
== 0)
535 $this->_checkoutButtonName
= $this->getButtonName('next', 'checkout');
537 $this->_checkoutButtonName
,
538 $this->_paymentProcessor
['url_button'],
539 array('class' => 'crm-form-submit')
542 $this->addButtons(array(
545 'name' => ts('<< Go Back'),
551 if ($this->_contributeMode
== 'notify' ||
!$this->_values
['is_monetary'] ||
552 $this->_amount
<= 0.0 ||
$this->_params
['is_pay_later'] ||
553 ($this->_separateMembershipPayment
&& $this->_amount
<= 0.0)
555 $contribButton = ts('Continue >>');
556 $this->assign('button', ts('Continue'));
559 $contribButton = ts('Make Contribution');
560 $this->assign('button', ts('Make Contribution'));
562 $this->addButtons(array(
565 'name' => $contribButton,
566 'spacing' => ' ',
568 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name
. "','" . ts('Processing') . "');"),
572 'name' => ts('Go Back'),
580 foreach ($this->_fields
as $name => $dontCare) {
581 if ($name != 'onbehalf' ||
$name != 'honor') {
585 $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1;
587 $contact = $this->_params
;
588 foreach ($fields as $name => $dontCare) {
589 if (isset($contact[$name])) {
590 $defaults[$name] = $contact[$name];
591 if (substr($name, 0, 7) == 'custom_') {
592 $timeField = "{$name}_time";
593 if (isset($contact[$timeField])) {
594 $defaults[$timeField] = $contact[$timeField];
596 if (isset($contact["{$name}_id"])) {
597 $defaults["{$name}_id"] = $contact["{$name}_id"];
600 elseif (in_array($name, array('addressee', 'email_greeting', 'postal_greeting')) && !empty($contact[$name . '_custom'])) {
601 $defaults[$name . '_custom'] = $contact[$name . '_custom'];
606 $this->assign('useForMember', $this->get('useForMember'));
608 $this->setDefaults($defaults);
614 * overwrite action, since we are only showing elements in frozen mode
615 * no help display needed
620 function getAction() {
621 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
622 return CRM_Core_Action
::VIEW | CRM_Core_Action
::PREVIEW
;
625 return CRM_Core_Action
::VIEW
;
630 * This function sets the default values for the form. Note that in edit/view mode
631 * the default values are retrieved from the database
637 function setDefaultValues() {}
645 public function postProcess() {
646 $contactID = $this->getContactID();
648 // add a description field at the very beginning
649 $this->_params
['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo
['title']) ?
$this->_pcpInfo
['title'] : $this->_values
['title']);
651 // also add accounting code
652 $this->_params
['accountingCode'] = CRM_Utils_Array
::value('accountingCode',
657 $this->_params
['currencyID'] = CRM_Core_Config
::singleton()->defaultCurrency
;
659 //carry payment processor id.
660 if ($paymentProcessorId = CRM_Utils_Array
::value('id', $this->_paymentProcessor
)) {
661 $this->_params
['payment_processor_id'] = $paymentProcessorId;
663 if (!empty($params['image_URL'])) {
664 CRM_Contact_BAO_Contact
::processImageParams($params);
666 $premiumParams = $membershipParams = $params = $this->_params
;
667 $fields = array('email-Primary' => 1);
669 // get the add to groups
670 $addToGroups = array();
672 // now set the values for the billing location.
673 foreach ($this->_fields
as $name => $value) {
676 // get the add to groups for uf fields
677 if (!empty($value['add_to_group_id'])) {
678 $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
682 if (!array_key_exists('first_name', $fields)) {
683 $nameFields = array('first_name', 'middle_name', 'last_name');
684 foreach ($nameFields as $name) {
686 if (array_key_exists("billing_$name", $params)) {
687 $params[$name] = $params["billing_{$name}"];
688 $params['preserveDBName'] = TRUE;
693 // billing email address
694 $fields["email-{$this->_bltID}"] = 1;
696 //unset the billing parameters if it is pay later mode
697 //to avoid creation of billing location
698 if ($params['is_pay_later']) {
699 $billingFields = array(
700 'billing_first_name',
701 'billing_middle_name',
703 "billing_street_address-{$this->_bltID}",
704 "billing_city-{$this->_bltID}",
705 "billing_state_province-{$this->_bltID}",
706 "billing_state_province_id-{$this->_bltID}",
707 "billing_postal_code-{$this->_bltID}",
708 "billing_country-{$this->_bltID}",
709 "billing_country_id-{$this->_bltID}",
712 foreach ($billingFields as $value) {
713 unset($params[$value]);
714 unset($fields[$value]);
718 // if onbehalf-of-organization contribution, take out
719 // organization params in a separate variable, to make sure
720 // normal behavior is continued. And use that variable to
721 // process on-behalf-of functionality.
722 if (!empty($this->_params
['hidden_onbehalf_profile'])) {
723 $behalfOrganization = array();
724 $orgFields = array('organization_name', 'organization_id', 'org_option');
725 foreach ($orgFields as $fld) {
726 if (array_key_exists($fld, $params)) {
727 $behalfOrganization[$fld] = $params[$fld];
728 unset($params[$fld]);
732 if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) {
733 foreach ($params['onbehalf'] as $fld => $values) {
734 if (strstr($fld, 'custom_')) {
735 $behalfOrganization[$fld] = $values;
737 elseif (!(strstr($fld, '-'))) {
738 if (in_array($fld, array(
739 'contribution_campaign_id', 'member_campaign_id'))) {
740 $fld = 'campaign_id';
743 $behalfOrganization[$fld] = $values;
745 $this->_params
[$fld] = $values;
750 if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) {
751 foreach ($params['onbehalf_location'] as $block => $vals) {
752 //fix for custom data (of type checkbox, multi-select)
753 if ( substr($block, 0, 7) == 'custom_' ) {
756 // fix the index of block elements
757 if (is_array($vals) ) {
758 foreach ( $vals as $key => $val ) {
759 //dont adjust the index of address block as
760 //it's index is WRT to location type
761 $newKey = ($block == 'address') ?
$key : ++
$key;
762 $behalfOrganization[$block][$newKey] = $val;
766 unset($params['onbehalf_location']);
768 if (!empty($params['onbehalf[image_URL]'])) {
769 $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]'];
773 // check for profile double opt-in and get groups to be subscribed
774 $subscribeGroupIds = CRM_Core_BAO_UFGroup
::getDoubleOptInGroupIds($params, $contactID);
776 // since we are directly adding contact to group lets unset it from mailing
777 if (!empty($addToGroups)) {
778 foreach ($addToGroups as $groupId) {
779 if (isset($subscribeGroupIds[$groupId])) {
780 unset($subscribeGroupIds[$groupId]);
785 foreach ($addToGroups as $k) {
786 if (array_key_exists($k, $subscribeGroupIds)) {
787 unset($addToGroups[$k]);
791 if (empty($contactID)) {
792 $dupeParams = $params;
793 if (!empty($dupeParams['onbehalf'])) {
794 unset($dupeParams['onbehalf']);
797 $dedupeParams = CRM_Dedupe_Finder
::formatParams($dupeParams, 'Individual');
798 $dedupeParams['check_permission'] = FALSE;
799 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
801 // if we find more than one contact, use the first one
802 $contactID = CRM_Utils_Array
::value(0, $ids);
804 // Fetch default greeting id's if creating a contact
806 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
807 if (!isset($params[$greeting])) {
808 $params[$greeting] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
815 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type');
817 $contactID = CRM_Contact_BAO_Contact
::createProfileContact(
827 // Make the contact ID associated with the contribution available at the Class level.
828 // Also make available to the session.
829 //@todo consider handling this in $this->getContactID();
830 $this->set('contactID', $contactID);
831 $this->_contactID
= $contactID;
833 //get email primary first if exist
834 $subscriptionEmail = array('email' => CRM_Utils_Array
::value('email-Primary', $params));
835 if (!$subscriptionEmail['email']) {
836 $subscriptionEmail['email'] = CRM_Utils_Array
::value("email-{$this->_bltID}", $params);
838 // subscribing contact to groups
839 if (!empty($subscribeGroupIds) && $subscriptionEmail['email']) {
840 CRM_Mailing_Event_BAO_Subscribe
::commonSubscribe($subscribeGroupIds, $subscriptionEmail, $contactID);
843 // If onbehalf-of-organization contribution / signup, add organization
844 // and it's location.
845 if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) {
847 foreach ($this->_fields
['onbehalf'] as $name => $value) {
848 $ufFields[$name] = 1;
850 self
::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values
,
851 $this->_params
, $ufFields
853 } else if (!empty($this->_membershipContactID
) && $contactID != $this->_membershipContactID
) {
854 // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household,
855 // store current user id as related contact for later use for mailing / activity..
856 $this->_values
['related_contact'] = $contactID;
857 $this->_params
['related_contact'] = $contactID;
858 // swap contact like we do for on-behalf-org case, so parent/primary membership is affected
859 $contactID = $this->_membershipContactID
;
862 // lets store the contactID in the session
863 // for things like tell a friend
864 $session = CRM_Core_Session
::singleton();
865 if (!$session->get('userID')) {
866 $session->set('transaction.userID', $contactID);
869 $session->set('transaction.userID', NULL);
872 $this->_useForMember
= $this->get('useForMember');
874 // store the fact that this is a membership and membership type is selected
875 $processMembership = FALSE;
876 if ((!empty($membershipParams['selectMembership']) &&
877 $membershipParams['selectMembership'] != 'no_thanks'
881 $processMembership = TRUE;
883 if (!$this->_useForMember
) {
884 $this->assign('membership_assign', TRUE);
885 $this->set('membershipTypeID', $this->_params
['selectMembership']);
888 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
889 $membershipParams['is_test'] = 1;
891 if ($this->_params
['is_pay_later']) {
892 $membershipParams['is_pay_later'] = 1;
896 if ($processMembership) {
897 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $membershipParams, TRUE);
899 // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups
900 if (isset($this->_params
['related_contact'])) {
901 $membershipParams['cms_contactID'] = $this->_params
['related_contact'];
904 $membershipParams['cms_contactID'] = $contactID;
907 //inherit campaign from contribution page.
908 if (!array_key_exists('campaign_id', $membershipParams)) {
909 $membershipParams['campaign_id'] = CRM_Utils_Array
::value('campaign_id', $this->_values
);
912 if (!empty($membershipParams['onbehalf']) &&
913 is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) {
914 $this->_params
['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id'];
917 $customFieldsFormatted = $fieldTypes = array();
918 if (!empty($membershipParams['onbehalf']) &&
919 is_array($membershipParams['onbehalf'])) {
920 foreach ($membershipParams['onbehalf'] as $key => $value) {
921 if (strstr($key, 'custom_')) {
922 $customFieldId = explode('_', $key);
923 CRM_Core_BAO_CustomField
::formatCustomField(
925 $customFieldsFormatted,
933 $fieldTypes = array('Contact', 'Organization', 'Membership');
936 $priceFieldIds = $this->get('memberPriceFieldIDS');
938 if (!empty($priceFieldIds)) {
939 $contributionTypeID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id');
940 unset($priceFieldIds['id']);
941 $membershipTypeIds = array();
942 $membershipTypeTerms = array();
943 foreach ($priceFieldIds as $priceFieldId) {
944 if ($id = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) {
945 $membershipTypeIds[] = $id;
946 //@todo the value for $term is immediately overwritten. It is unclear from the code whether it was intentional to
947 // do this or a double = was intended (this ambiguity is the reason many IDEs complain about 'assignment in condition'
949 if ($term = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) {
950 $membershipTypeTerms[$id] = ($term > 1) ?
$term : 1;
953 $membershipTypeTerms[$id] = 1;
957 $membershipParams['selectMembership'] = $membershipTypeIds;
958 $membershipParams['financial_type_id'] = $contributionTypeID;
959 $membershipParams['types_terms'] = $membershipTypeTerms;
961 if (!empty($membershipParams['selectMembership'])) {
963 $membershipLineItems = array();
964 if ($this->_separateMembershipPayment
&& $this->_values
['amount_block_is_active']) {
965 foreach ($this->_values
['fee'] as $key => $feeValues) {
966 if ($feeValues['name'] == 'membership_amount') {
967 $fieldId = $this->_params
['price_' . $key];
968 $membershipLineItems[$this->_priceSetId
][$fieldId] = $this->_lineItem
[$this->_priceSetId
][$fieldId];
969 unset($this->_lineItem
[$this->_priceSetId
][$fieldId]);
974 $this->processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems);
975 if (!$this->_amount
> 0.0 ||
!$membershipParams['amount']) {
976 // we need to explicitly create a CMS user in case of free memberships
977 // since it is done under processConfirm for paid memberships
978 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($membershipParams,
979 $membershipParams['cms_contactID'],
980 'email-' . $this->_bltID
986 // at this point we've created a contact and stored its address etc
987 // all the payment processors expect the name and address to be in the
988 // so we copy stuff over to first_name etc.
989 $paymentParams = $this->_params
;
990 $contributionTypeId = $this->_values
['financial_type_id'];
992 $fieldTypes = array();
993 if (!empty($paymentParams['onbehalf']) &&
994 is_array($paymentParams['onbehalf'])
996 foreach ($paymentParams['onbehalf'] as $key => $value) {
997 if (strstr($key, 'custom_')) {
998 $this->_params
[$key] = $value;
1001 $fieldTypes = array('Contact', 'Organization', 'Contribution');
1004 CRM_Contribute_BAO_Contribution_Utils
::processConfirm($this, $paymentParams,
1005 $premiumParams, $contactID,
1006 $contributionTypeId,
1016 * @param $premiumParams
1017 * @param $contribution
1022 public function postProcessPremium($premiumParams, $contribution) {
1023 $hour = $minute = $second = 0;
1024 // assigning Premium information to receipt tpl
1025 $selectProduct = CRM_Utils_Array
::value('selectProduct', $premiumParams);
1026 if ($selectProduct &&
1027 $selectProduct != 'no_thanks'
1029 $startDate = $endDate = "";
1030 $this->assign('selectPremium', TRUE);
1031 $productDAO = new CRM_Contribute_DAO_Product();
1032 $productDAO->id
= $selectProduct;
1033 $productDAO->find(TRUE);
1034 $this->assign('product_name', $productDAO->name
);
1035 $this->assign('price', $productDAO->price
);
1036 $this->assign('sku', $productDAO->sku
);
1037 $this->assign('option', CRM_Utils_Array
::value('options_' . $premiumParams['selectProduct'], $premiumParams));
1039 $periodType = $productDAO->period_type
;
1042 $fixed_period_start_day = $productDAO->fixed_period_start_day
;
1043 $duration_unit = $productDAO->duration_unit
;
1044 $duration_interval = $productDAO->duration_interval
;
1045 if ($periodType == 'rolling') {
1046 $startDate = date('Y-m-d');
1048 elseif ($periodType == 'fixed') {
1049 if ($fixed_period_start_day) {
1050 $date = explode('-', date('Y-m-d'));
1051 $month = substr($fixed_period_start_day, 0, strlen($fixed_period_start_day) - 2);
1052 $day = substr($fixed_period_start_day, -2) . "<br>";
1054 $startDate = $year . '-' . $month . '-' . $day;
1057 $startDate = date('Y-m-d');
1061 $date = explode('-', $startDate);
1066 switch ($duration_unit) {
1068 $year = $year +
$duration_interval;
1072 $month = $month +
$duration_interval;
1076 $day = $day +
$duration_interval;
1080 $day = $day +
($duration_interval * 7);
1082 $endDate = date('Y-m-d H:i:s', mktime($hour, $minute, $second, $month, $day, $year));
1083 $this->assign('start_date', $startDate);
1084 $this->assign('end_date', $endDate);
1087 $dao = new CRM_Contribute_DAO_Premium();
1088 $dao->entity_table
= 'civicrm_contribution_page';
1089 $dao->entity_id
= $this->_id
;
1091 $this->assign('contact_phone', $dao->premiums_contact_phone
);
1092 $this->assign('contact_email', $dao->premiums_contact_email
);
1094 //create Premium record
1096 'product_id' => $premiumParams['selectProduct'],
1097 'contribution_id' => $contribution->id
,
1098 'product_option' => CRM_Utils_Array
::value('options_' . $premiumParams['selectProduct'], $premiumParams),
1100 'start_date' => CRM_Utils_Date
::customFormat($startDate, '%Y%m%d'),
1101 'end_date' => CRM_Utils_Date
::customFormat($endDate, '%Y%m%d'),
1103 if (!empty($premiumParams['selectProduct'])){
1104 $daoPremiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
1105 $daoPremiumsProduct->product_id
= $premiumParams['selectProduct'];
1106 $daoPremiumsProduct->premiums_id
= $dao->id
;
1107 $daoPremiumsProduct->find(true);
1108 $params['financial_type_id'] = $daoPremiumsProduct->financial_type_id
;
1110 //Fixed For CRM-3901
1111 $daoContrProd = new CRM_Contribute_DAO_ContributionProduct();
1112 $daoContrProd->contribution_id
= $contribution->id
;
1113 if ($daoContrProd->find(TRUE)) {
1114 $params['id'] = $daoContrProd->id
;
1117 CRM_Contribute_BAO_Contribution
::addPremium($params);
1118 if ($productDAO->cost
&& !empty($params['financial_type_id'])) {
1119 $trxnParams = array(
1120 'cost' => $productDAO->cost
,
1121 'currency' => $productDAO->currency
,
1122 'financial_type_id' => $params['financial_type_id'],
1123 'contributionId' => $contribution->id
1125 CRM_Core_BAO_FinancialTrxn
::createPremiumTrxn($trxnParams);
1128 elseif ($selectProduct == 'no_thanks') {
1129 //Fixed For CRM-3901
1130 $daoContrProd = new CRM_Contribute_DAO_ContributionProduct();
1131 $daoContrProd->contribution_id
= $contribution->id
;
1132 if ($daoContrProd->find(TRUE)) {
1133 $daoContrProd->delete();
1139 * Process the contribution
1142 * @param array $params
1143 * @param array $result
1144 * @param integer $contactID
1145 * @param CRM_Financial_DAO_FinancialType $financialType
1146 * @param bool $pending
1147 * @param bool $online
1149 * @param bool $isTest
1150 * @param array $lineItems
1153 * @return CRM_Contribute_DAO_Contribution
1156 static function processContribution(
1167 $transaction = new CRM_Core_Transaction();
1168 $contribSoftContactId = $addressID = NULL;
1170 // add these values for the recurringContrib function ,CRM-10188
1171 $params['financial_type_id'] = $financialType->id
;
1173 //create an contribution address
1174 if ($form->_contributeMode
!= 'notify' && empty($params['is_pay_later']) && !empty($form->_values
['is_monetary'])) {
1175 $addressID = CRM_Contribute_BAO_Contribution
::createAddress($params, $form->_bltID
);
1178 //@todo - this is being set from the form to resolve CRM-10188 - an
1179 // eNotice caused by it not being set @ the front end
1180 // however, we then get it being over-written with null for backend contributions
1181 // a better fix would be to set the values in the respective forms rather than require
1182 // a function being shared by two forms to deal with their respective values
1183 // moving it to the BAO & not taking the $form as a param would make sense here.
1184 if(!isset($params['is_email_receipt'])){
1185 $params['is_email_receipt'] = CRM_Utils_Array
::value( 'is_email_receipt', $form->_values
);
1187 $recurringContributionID = self
::processRecurringContribution($form, $params, $contactID, $financialType, $online);
1190 // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it.
1191 if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) {
1192 $nonDeductibleAmount = $params['non_deductible_amount'];
1194 // if non_deductible_amount does NOT exist - then calculate it depending on:
1195 // $contributionType->is_deductible and whether there is a product (premium).
1197 //if ($contributionType->is_deductible && $deductibleMode) {
1198 if ($financialType->is_deductible
) {
1199 if ($online && isset($params['selectProduct'])) {
1200 $selectProduct = CRM_Utils_Array
::value('selectProduct', $params);
1202 if (!$online && isset($params['product_name'][0])) {
1203 $selectProduct = $params['product_name'][0];
1205 // if there is a product - compare the value to the contribution amount
1206 if (isset($selectProduct) &&
1207 $selectProduct != 'no_thanks'
1209 $productDAO = new CRM_Contribute_DAO_Product();
1210 $productDAO->id
= $selectProduct;
1211 $productDAO->find(TRUE);
1212 // product value exceeds contribution amount
1213 if ($params['amount'] < $productDAO->price
) {
1214 $nonDeductibleAmount = $params['amount'];
1216 // product value does NOT exceed contribution amount
1218 $nonDeductibleAmount = $productDAO->price
;
1221 // contribution is deductible - but there is no product
1223 $nonDeductibleAmount = '0.00';
1226 // contribution is NOT deductible
1228 $nonDeductibleAmount = $params['amount'];
1232 $now = date('YmdHis');
1233 $receiptDate = CRM_Utils_Array
::value('receipt_date', $params);
1234 if (!empty($form->_values
['is_email_receipt'])) {
1235 $receiptDate = $now;
1238 //get the contrib page id.
1239 $contributionPageId = NULL;
1241 $contributionPageId = $form->_id
;
1242 $campaignId = CRM_Utils_Array
::value('campaign_id', $params);
1243 if (!array_key_exists('campaign_id', $params)) {
1244 $campaignId = CRM_Utils_Array
::value('campaign_id', $form->_values
);
1248 //also for offline we do support - CRM-7290
1249 $contributionPageId = CRM_Utils_Array
::value('contribution_page_id', $params);
1250 $campaignId = CRM_Utils_Array
::value('campaign_id', $params);
1253 // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
1254 if (!empty($params['pcp_made_through_id']) ||
!empty($params['soft_credit_to'])) {
1255 // if its due to pcp
1256 if (!empty($params['pcp_made_through_id'])) {
1257 $contribSoftContactId = CRM_Core_DAO
::getFieldValue(
1259 $params['pcp_made_through_id'],
1264 $contribSoftContactId = CRM_Utils_Array
::value('soft_credit_to', $params);
1267 // Pass these details onto with the contribution to make them
1268 // available at hook_post_process, CRM-8908
1269 $params['soft_credit_to'] = $contribSoftContactId;
1272 if (isset($params['amount'])) {
1273 $contribParams = self
::getContributionParams(
1274 $params, $contactID, $financialType->id
, $online, $contributionPageId, $nonDeductibleAmount, $campaignId, $form->_values
['is_monetary'], $pending, $result, $receiptDate,
1275 $recurringContributionID, $isTest, $addressID, $contribSoftContactId, $lineItems
1277 $contribution = CRM_Contribute_BAO_Contribution
::add($contribParams);
1278 if (is_a($contribution, 'CRM_Core_Error')) {
1279 $message = CRM_Core_Error
::getMessages($contribution);
1280 CRM_Core_Error
::fatal($message);
1283 // lets store it in the form variable so postProcess hook can get to this and use it
1284 $form->_contributionID
= $contribution->id
;
1287 //CRM-13981, processing honor contact into soft-credit contribution
1288 CRM_Contact_Form_ProfileContact
::postProcess($form);
1290 // process soft credit / pcp pages
1291 CRM_Contribute_Form_Contribution_Confirm
::processPcpSoft($params, $contribution);
1293 //handle pledge stuff.
1294 if (empty($form->_params
['separate_membership_payment']) && !empty($form->_values
['pledge_block_id']) &&
1295 (!empty($form->_params
['is_pledge']) ||
!empty($form->_values
['pledge_id']))
1298 if (!empty($form->_values
['pledge_id'])) {
1300 //when user doing pledge payments.
1301 //update the schedule when payment(s) are made
1302 foreach ($form->_params
['pledge_amount'] as $paymentId => $dontCare) {
1303 $scheduledAmount = CRM_Core_DAO
::getFieldValue(
1304 'CRM_Pledge_DAO_PledgePayment',
1310 $pledgePaymentParams = array(
1312 'contribution_id' => $contribution->id
,
1313 'status_id' => $contribution->contribution_status_id
,
1314 'actual_amount' => $scheduledAmount,
1318 CRM_Pledge_BAO_PledgePayment
::add($pledgePaymentParams);
1321 //update pledge status according to the new payment statuses
1322 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($form->_values
['pledge_id']);
1325 //when user creating pledge record.
1326 $pledgeParams = array();
1327 $pledgeParams['contact_id'] = $contribution->contact_id
;
1328 $pledgeParams['installment_amount'] = $pledgeParams['actual_amount'] = $contribution->total_amount
;
1329 $pledgeParams['contribution_id'] = $contribution->id
;
1330 $pledgeParams['contribution_page_id'] = $contribution->contribution_page_id
;
1331 $pledgeParams['financial_type_id'] = $contribution->financial_type_id
;
1332 $pledgeParams['frequency_interval'] = $params['pledge_frequency_interval'];
1333 $pledgeParams['installments'] = $params['pledge_installments'];
1334 $pledgeParams['frequency_unit'] = $params['pledge_frequency_unit'];
1335 if ($pledgeParams['frequency_unit'] == 'month') {
1336 $pledgeParams['frequency_day'] = intval(date("d"));
1339 $pledgeParams['frequency_day'] = 1;
1341 $pledgeParams['create_date'] = $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date("Ymd");
1342 $pledgeParams['status_id'] = $contribution->contribution_status_id
;
1343 $pledgeParams['max_reminders'] = $form->_values
['max_reminders'];
1344 $pledgeParams['initial_reminder_day'] = $form->_values
['initial_reminder_day'];
1345 $pledgeParams['additional_reminder_day'] = $form->_values
['additional_reminder_day'];
1346 $pledgeParams['is_test'] = $contribution->is_test
;
1347 $pledgeParams['acknowledge_date'] = date('Ymd');
1348 $pledgeParams['original_installment_amount'] = $pledgeParams['installment_amount'];
1350 //inherit campaign from contirb page.
1351 $pledgeParams['campaign_id'] = $campaignId;
1353 $pledge = CRM_Pledge_BAO_Pledge
::create($pledgeParams);
1355 $form->_params
['pledge_id'] = $pledge->id
;
1357 //send acknowledgment email. only when pledge is created
1359 //build params to send acknowledgment.
1360 $pledgeParams['id'] = $pledge->id
;
1361 $pledgeParams['receipt_from_name'] = $form->_values
['receipt_from_name'];
1362 $pledgeParams['receipt_from_email'] = $form->_values
['receipt_from_email'];
1364 //scheduled amount will be same as installment_amount.
1365 $pledgeParams['scheduled_amount'] = $pledgeParams['installment_amount'];
1367 //get total pledge amount.
1368 $pledgeParams['total_pledge_amount'] = $pledge->amount
;
1370 CRM_Pledge_BAO_Pledge
::sendAcknowledgment($form, $pledgeParams);
1375 if ($online && $contribution) {
1376 CRM_Core_BAO_CustomValueTable
::postProcess($form->_params
,
1377 CRM_Core_DAO
::$_nullArray,
1378 'civicrm_contribution',
1383 elseif ($contribution) {
1384 //handle custom data.
1385 $params['contribution_id'] = $contribution->id
;
1386 if (!empty($params['custom']) &&
1387 is_array($params['custom']) &&
1388 !is_a($contribution, 'CRM_Core_Error')
1390 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_contribution', $contribution->id
);
1394 if ($contribution && !empty($params['contribution_note'])) {
1395 $noteParams = array(
1396 'entity_table' => 'civicrm_contribution',
1397 'note' => $params['contribution_note'],
1398 'entity_id' => $contribution->id
,
1399 'contact_id' => $contribution->contact_id
,
1400 'modified_date' => date('Ymd'),
1403 CRM_Core_BAO_Note
::add($noteParams, array());
1407 if (isset($params['related_contact'])) {
1408 $contactID = $params['related_contact'];
1410 elseif (isset($params['cms_contactID'])) {
1411 $contactID = $params['cms_contactID'];
1414 //create contribution activity w/ individual and target
1415 //activity w/ organisation contact id when onbelf, CRM-4027
1416 $targetContactID = NULL;
1417 if (!empty($params['hidden_onbehalf_profile'])) {
1418 $targetContactID = $contribution->contact_id
;
1419 $contribution->contact_id
= $contactID;
1422 // create an activity record
1423 if ($contribution) {
1424 CRM_Activity_BAO_Activity
::addActivity($contribution, NULL, $targetContactID);
1427 $transaction->commit();
1428 // CRM-13074 - create the CMSUser after the transaction is completed as it
1429 // is not appropriate to delete a valid contribution if a user create problem occurs
1430 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($params,
1432 'email-' . $form->_bltID
1434 return $contribution;
1438 * Create the recurring contribution record
1441 static function processRecurringContribution(&$form, &$params, $contactID, $contributionType, $online = TRUE) {
1442 // return if this page is not set for recurring
1443 // or the user has not chosen the recurring option
1445 //this is online case validation.
1446 if ((empty($form->_values
['is_recur']) && $online) ||
empty($params['is_recur'])) {
1450 $recurParams = array('contact_id' => $contactID);
1451 $recurParams['amount'] = CRM_Utils_Array
::value('amount', $params);
1452 $recurParams['auto_renew'] = CRM_Utils_Array
::value('auto_renew', $params);
1453 $recurParams['frequency_unit'] = CRM_Utils_Array
::value('frequency_unit', $params);
1454 $recurParams['frequency_interval'] = CRM_Utils_Array
::value('frequency_interval', $params);
1455 $recurParams['installments'] = CRM_Utils_Array
::value('installments', $params);
1456 $recurParams['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params);
1458 // CRM-14354: For an auto-renewing membership with an additional contribution,
1459 // if separate payments is not enabled, make sure only the membership fee recurs
1460 if ($form->_membershipBlock
['is_separate_payment'] === '0'
1461 && isset($params['selectMembership'])
1462 && $form->_values
['is_allow_other_amount'] == '1'
1464 $recurParams['amount'] = $form->_membershipTypeValues
[$params['selectMembership']]['minimum_fee'];
1467 $recurParams['is_test'] = 0;
1468 if (($form->_action
& CRM_Core_Action
::PREVIEW
) ||
1469 (isset($form->_mode
) && ($form->_mode
== 'test'))
1471 $recurParams['is_test'] = 1;
1474 $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis');
1475 if (!empty($params['receive_date'])) {
1476 $recurParams['start_date'] = $params['receive_date'];
1478 $recurParams['invoice_id'] = CRM_Utils_Array
::value('invoiceID', $params);
1479 $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
1480 $recurParams['payment_processor_id'] = CRM_Utils_Array
::value('payment_processor_id', $params);
1481 $recurParams['is_email_receipt'] = CRM_Utils_Array
::value('is_email_receipt', $params);
1482 // we need to add a unique trxn_id to avoid a unique key error
1483 // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991
1484 $recurParams['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $params, $params['invoiceID']);
1485 $recurParams['financial_type_id'] = $contributionType->id
;
1487 if (!$online ||
$form->_values
['is_monetary']) {
1488 $recurParams['payment_instrument_id'] = 1;
1491 $campaignId = CRM_Utils_Array
::value('campaign_id', $params);
1493 if (!array_key_exists('campaign_id', $params)) {
1494 $campaignId = CRM_Utils_Array
::value('campaign_id', $form->_values
);
1497 $recurParams['campaign_id'] = $campaignId;
1499 $recurring = CRM_Contribute_BAO_ContributionRecur
::add($recurParams);
1500 if (is_a($recurring, 'CRM_Core_Error')) {
1501 CRM_Core_Error
::displaySessionError($recurring);
1502 $urlString = 'civicrm/contribute/transact';
1503 $urlParams = '_qf_Main_display=true';
1504 if (get_class($form) == 'CRM_Contribute_Form_Contribution') {
1505 $urlString = 'civicrm/contact/view/contribution';
1506 $urlParams = "action=add&cid={$form->_contactID}";
1508 $urlParams .= "&mode={$form->_mode}";
1511 CRM_Utils_System
::redirect(CRM_Utils_System
::url($urlString, $urlParams));
1514 return $recurring->id
;
1518 * Function to add on behalf of organization and it's location
1520 * @param $behalfOrganization array array of organization info
1521 * @param $contactID int individual contact id. One
1522 * who is doing the process of signup / contribution.
1524 * @param $values array form values array
1526 * @param null $fields
1531 static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) {
1532 $isCurrentEmployer = FALSE;
1535 if (!empty($behalfOrganization['organization_id']) && empty($behalfOrganization['org_option'])) {
1536 $orgID = $behalfOrganization['organization_id'];
1537 unset($behalfOrganization['organization_id']);
1538 $isCurrentEmployer = TRUE;
1541 // formalities for creating / editing organization.
1542 $behalfOrganization['contact_type'] = 'Organization';
1544 // get the relationship type id
1545 $relType = new CRM_Contact_DAO_RelationshipType();
1546 $relType->name_a_b
= 'Employee of';
1547 $relType->find(TRUE);
1548 $relTypeId = $relType->id
;
1550 // keep relationship params ready
1551 $relParams['relationship_type_id'] = $relTypeId . '_a_b';
1552 $relParams['is_permission_a_b'] = 1;
1553 $relParams['is_active'] = 1;
1556 // check if matching organization contact exists
1557 $dedupeParams = CRM_Dedupe_Finder
::formatParams($behalfOrganization, 'Organization');
1558 $dedupeParams['check_permission'] = FALSE;
1559 $dupeIDs = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Organization', 'Unsupervised');
1561 // CRM-6243 says to pick the first org even if more than one match
1562 if (count($dupeIDs) >= 1) {
1563 $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0];
1564 // don't allow name edit
1565 unset($behalfOrganization['organization_name']);
1569 // if found permissioned related organization, allow location edit
1570 $behalfOrganization['contact_id'] = $orgID;
1571 // don't allow name edit
1572 unset($behalfOrganization['organization_name']);
1575 // handling for image url
1576 if (!empty($behalfOrganization['image_URL'])) {
1577 CRM_Contact_BAO_Contact
::processImageParams($behalfOrganization);
1580 // create organization, add location
1581 $orgID = CRM_Contact_BAO_Contact
::createProfileContact($behalfOrganization, $fields, $orgID,
1582 NULL, NULL, 'Organization'
1584 // create relationship
1585 $relParams['contact_check'][$orgID] = 1;
1586 $cid = array('contact' => $contactID);
1587 CRM_Contact_BAO_Relationship
::create($relParams, $cid);
1589 // if multiple match - send a duplicate alert
1590 if ($dupeIDs && (count($dupeIDs) > 1)) {
1591 $values['onbehalf_dupe_alert'] = 1;
1593 $params['onbehalf_dupe_alert'] = 1;
1596 // make sure organization-contact-id is considered for recording
1597 // contribution/membership etc..
1598 if ($contactID != $orgID) {
1599 // take a note of contact-id, so we can send the
1600 // receipt to individual contact as well.
1602 // required for mailing/template display ..etc
1603 $values['related_contact'] = $contactID;
1605 $params['related_contact'] = $contactID;
1607 //make this employee of relationship as current
1608 //employer / employee relationship, CRM-3532
1609 if ($isCurrentEmployer &&
1610 ($orgID != CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id'))
1612 $isCurrentEmployer = FALSE;
1615 if (!$isCurrentEmployer && $orgID) {
1616 //build current employer params
1617 $currentEmpParams[$contactID] = $orgID;
1618 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
1621 // contribution / signup will be done using this
1623 $contactID = $orgID;
1628 * Function used to save pcp / soft credit entry
1629 * This is used by contribution and also event pcps
1631 * @param array $params associated array
1632 * @param object $contribution contribution object
1637 static function processPcpSoft(&$params, &$contribution) {
1638 //add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
1639 if (!empty($params['soft_credit_to'])) {
1640 $contributionSoftParams = array();
1642 'pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note', 'amount') as $val) {
1643 if (!empty($params[$val])) {
1644 $contributionSoftParams[$val] = $params[$val];
1648 $contributionSoftParams['contact_id'] = $params['soft_credit_to'];
1649 // add contribution id
1650 $contributionSoftParams['contribution_id'] = $contribution->id
;
1652 $contributionSoftParams['pcp_id'] = $params['pcp_made_through_id'];
1654 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup
::getValue('soft_credit_type', 'pcp', 'name');
1656 CRM_Contribute_BAO_ContributionSoft
::add($contributionSoftParams);
1661 * Function used to se pcp related defaults / params
1662 * This is used by contribution and also event pcps
1664 * @param object $page form object
1665 * @param array $params associated array
1671 static function processPcp(&$page, $params) {
1672 $params['pcp_made_through_id'] = $page->_pcpId
;
1673 $page->assign('pcpBlock', TRUE);
1674 if (!empty($params['pcp_display_in_roll']) && empty($params['pcp_roll_nickname'])) {
1675 $params['pcp_roll_nickname'] = ts('Anonymous');
1676 $params['pcp_is_anonymous'] = 1;
1679 $params['pcp_is_anonymous'] = 0;
1682 'pcp_display_in_roll',
1684 'pcp_roll_nickname',
1687 if (!empty($params[$val])) {
1688 $page->assign($val, $params[$val]);
1696 * @param array $membershipParams
1697 * @param integer $contactID
1698 * @param array $customFieldsFormatted
1699 * @param array $fieldTypes
1700 * @param array $premiumParams
1701 * @param array $membershipLineItems line items specifically relating to memberships
1703 public function processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems) {
1705 $membershipTypeIDs = (array) $membershipParams['selectMembership'];
1706 $membershipTypes = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, $membershipTypeIDs);
1707 $membershipType = empty($membershipTypes) ?
array() : reset($membershipTypes);
1708 $this->assign('membership_name', CRM_Utils_Array
::value('name', $membershipType));
1710 $isPaidMembership = FALSE;
1711 if($this->_amount
>= 0.0 && isset($membershipParams['amount'])) {
1712 //amount must be greater than zero for
1713 //adding contribution record to contribution table.
1714 //this condition arises when separate membership payment is
1715 //enabled and contribution amount is not selected. fix for CRM-3010
1716 $isPaidMembership = TRUE;
1718 $isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id
, $this->_values
['amount_block_is_active']);
1720 if ($this->_values
['amount_block_is_active']) {
1721 $contributionTypeId = $this->_values
['financial_type_id'];
1724 $contributionTypeId = CRM_Utils_Array
::value('financial_type_id', $membershipType, CRM_Utils_Array
::value('financial_type_id' ,$membershipParams));
1727 CRM_Member_BAO_Membership
::postProcessMembership($membershipParams, $contactID,
1728 $this, $premiumParams, $customFieldsFormatted, $fieldTypes, $membershipType, $membershipTypeIDs, $isPaidMembership, $this->_membershipId
, $isProcessSeparateMembershipTransaction, $contributionTypeId,
1729 $membershipLineItems
1731 $this->assign('membership_assign', TRUE);
1732 $this->set('membershipTypeID', $membershipParams['selectMembership']);
1734 catch (CRM_Core_Exception
$e) {
1735 CRM_Core_Session
::singleton()->setStatus($e->getMessage());
1736 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}"));
1741 * Are we going to do 2 financial transactions?
1742 * ie the membership block supports a separate transactions AND the contribution form has been configured for a contribution
1743 * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferPayment style)
1746 * @param integer $formID
1747 * @param bool $amountBlockActiveOnForm
1751 public function isSeparateMembershipTransaction($formID, $amountBlockActiveOnForm) {
1752 $memBlockDetails = CRM_Member_BAO_Membership
::getMembershipBlock($formID);
1753 if (!empty($memBlockDetails['is_separate_payment']) && $amountBlockActiveOnForm) {
1760 * This function sets the fields
1761 * - $this->_params['amount_level']
1762 * - $this->_params['selectMembership']
1763 * And under certain circumstances sets
1764 * $this->_params['amount'] = null;
1766 * @param $priceSetID
1768 * @internal param $isQuickConfig
1769 * @internal param $priceField
1771 public function setFormAmountFields($priceSetID) {
1772 $isQuickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params
['priceSetId'], 'is_quick_config');
1773 $priceField = new CRM_Price_DAO_PriceField();
1774 $priceField->price_set_id
= $priceSetID;
1775 $priceField->orderBy('weight');
1776 $priceField->find();
1777 $paramWeDoNotUnderstand = NULL;
1779 while ($priceField->fetch()) {
1780 if ($priceField->name
== "contribution_amount") {
1781 $paramWeDoNotUnderstand = $priceField->id
;
1783 if ($isQuickConfig && !empty($this->_params
["price_{$priceField->id}"])) {
1784 if ($this->_values
['fee'][$priceField->id
]['html_type'] != 'Text') {
1785 $this->_params
['amount_level'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue',
1786 $this->_params
["price_{$priceField->id}"], 'label');
1788 if ($priceField->name
== "membership_amount") {
1789 $this->_params
['selectMembership'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue',
1790 $this->_params
["price_{$priceField->id}"], 'membership_type_id');
1792 } // if separate payment we set contribution amount to be null, so that it will not show contribution amount same as membership amount.
1793 // @todo - this needs more documentation - it appears the setting to null is tied up with separate membership payments
1794 // but the circumstances are very confusing. Many of these conditions are repeated in the next conditional
1795 // so we should merge them together
1796 // the quick config seems like a red-herring - if this is about a separate membership payment then there
1797 // are 2 types of line items - membership ones & non-membership ones - regardless of whether quick config is set
1799 CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
)
1800 && !empty($this->_values
['fee'][$priceField->id
])
1801 && ($this->_values
['fee'][$priceField->id
]['name'] == "other_amount")
1802 && CRM_Utils_Array
::value("price_{$paramWeDoNotUnderstand}", $this->_params
) < 1
1803 && empty($this->_params
["price_{$priceField->id}"])
1805 $this->_params
['amount'] = null;
1808 // Fix for CRM-14375 - If we are using separate payments and "no
1809 // thank you" is selected for the additional contribution, set
1810 // contribution amount to be null, so that it will not show
1811 // contribution amount same as membership amount.
1812 //@todo - merge with section above
1813 if ($this->_membershipBlock
['is_separate_payment']
1814 && !empty($this->_values
['fee'][$priceField->id
])
1815 && CRM_Utils_Array
::value('name', $this->_values
['fee'][$priceField->id
]) == 'contribution_amount'
1816 && CRM_Utils_Array
::value("price_{$priceField->id}", $this->_params
) == '-1'
1818 $this->_params
['amount'] = null;
1824 * Static submit function allowing tests (& api access although this is being built slowly)
1827 static function submit($params) {
1828 $form = new CRM_Contribute_Form_Contribution_Confirm();
1829 $form->_id
= $params['id'];
1830 CRM_Contribute_BAO_ContributionPage
::setValues($form->_id
, $form->_values
);
1831 $form->_separateMembershipPayment
= CRM_Contribute_BAO_ContributionPage
::getIsMembershipPayment($form->_id
);
1832 //this way the mocked up controller ignores the session stuff
1833 $_SERVER['REQUEST_METHOD'] = 'GET';
1834 $form->controller
= new CRM_Contribute_Controller_Contribution();
1835 $params['invoiceID'] = md5(uniqid(rand(), TRUE));
1836 $paramsProcessedForForm = $form->_params
= self
::getFormParams($params['id'], $params);
1837 $form->_amount
= $params['amount'];
1839 $priceSetID = $form->_params
['priceSetId'] = $paramsProcessedForForm['price_set_id'];
1840 $priceFields = CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
1841 $priceSetFields = reset($priceFields);
1842 $form->_values
['fee'] = $priceSetFields['fields'];
1843 $form->_priceSetId
= $priceSetID;
1844 $form->setFormAmountFields($priceSetID);
1845 if (!empty($params['payment_processor'])) {
1846 $form->_paymentProcessor
= civicrm_api3('payment_processor', 'getsingle', array('id' => $params['payment_processor']));
1848 $priceFields = $priceFields[$priceSetID]['fields'];
1849 CRM_Price_BAO_PriceSet
::processAmount($priceFields, $paramsProcessedForForm, $lineItems, 'civicrm_contribution');
1850 $form->_lineItem
= array($priceSetID => $lineItems);
1851 $form->postProcess();
1855 * Helper function for static submit function - set relevant params - help us to build up an array that we can pass in
1857 * @param array $params
1860 * @throws CiviCRM_API3_Exception
1862 static function getFormParams($id, array $params) {
1863 if(!isset($params['is_pay_later'])) {
1864 if (!empty($params['payment_processor'])) {
1865 $params['is_pay_later'] = 0;
1868 $params['is_pay_later'] = civicrm_api3('contribution_page', 'getvalue', array(
1870 'return' => 'is_pay_later'
1874 if(empty($params['price_set_id'])) {
1875 $params['price_set_id'] = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $params['id']);