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 * This class generates form components for processing a Contribution
40 class CRM_Contribute_Form_Contribution_Main
extends CRM_Contribute_Form_ContributionBase
{
43 *Define default MembershipType Id
46 public $_defaultMemTypeId;
48 public $_relatedOrganizationFound;
50 public $_onBehalfRequired = FALSE;
51 public $_onbehalf = FALSE;
52 public $_paymentProcessors;
55 public $_membershipTypeValues;
57 public $_useForMember;
60 * array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl
63 public $_paymentFields = array();
65 protected $_paymentProcessorID;
69 * Function to set variables up before form is built
74 public function preProcess() {
77 self
::preProcessPaymentOptions($this);
79 // Make the contributionPageID avilable to the template
80 $this->assign('contributionPageID', $this->_id
);
81 $this->assign('isShare', CRM_Utils_Array
::value('is_share', $this->_values
));
82 $this->assign('isConfirmEnabled', CRM_Utils_Array
::value('is_confirm_enabled', $this->_values
));
84 // make sure we have right permission to edit this user
85 $csContactID = $this->getContactID();
86 $reset = CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
87 $mainDisplay = CRM_Utils_Request
::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO
::$_nullObject);
90 $this->assign('reset', $reset);
94 $this->assign('mainDisplay', $mainDisplay);
97 // Possible values for 'is_for_organization':
98 // * 0 - org profile disabled
99 // * 1 - org profile optional
100 // * 2 - org profile required
101 $this->_onbehalf
= FALSE;
102 if (!empty($this->_values
['is_for_organization'])) {
103 if ($this->_values
['is_for_organization'] == 2) {
104 $this->_onBehalfRequired
= TRUE;
106 // Add organization profile if 1 of the following are true:
107 // If the org profile is required
108 if ($this->_onBehalfRequired ||
109 // Or we are building the form for the first time
111 // Or the user has submitted the form and checked the "On Behalf" checkbox
112 !empty($_POST['is_for_organization'])
114 $this->_onbehalf
= TRUE;
115 CRM_Contribute_Form_Contribution_OnBehalfOf
::preProcess($this);
118 $this->assign('onBehalfRequired', $this->_onBehalfRequired
);
120 if ($this->_honor_block_is_active
) {
121 CRM_Contact_Form_ProfileContact
::preprocess($this);
124 if ($this->_snippet
) {
125 $this->assign('isOnBehalfCallback', CRM_Utils_Array
::value('onbehalf', $_GET, FALSE));
129 if (!empty($this->_pcpInfo
['id']) && !empty($this->_pcpInfo
['intro_text'])) {
130 $this->assign('intro_text', $this->_pcpInfo
['intro_text']);
132 elseif (!empty($this->_values
['intro_text'])) {
133 $this->assign('intro_text', $this->_values
['intro_text']);
136 $qParams = "reset=1&id={$this->_id}";
137 if ($pcpId = CRM_Utils_Array
::value('pcp_id', $this->_pcpInfo
)) {
138 $qParams .= "&pcpId={$pcpId}";
140 $this->assign('qParams', $qParams);
142 if (!empty($this->_values
['footer_text'])) {
143 $this->assign('footer_text', $this->_values
['footer_text']);
147 if (!empty($this->_values
['is_for_organization'])) {
148 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
149 $ufJoinParams = array(
150 'module' => 'onBehalf',
151 'entity_table' => 'civicrm_contribution_page',
152 'entity_id' => $this->_id
,
154 $onBehalfProfileIDs = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
155 // getUFGroupIDs returns an array with the first item being the ID we need
156 $onBehalfProfileID = $onBehalfProfileIDs[0];
157 if ($onBehalfProfileID) {
158 $onBehalfProfile = CRM_Core_BAO_UFGroup
::profileGroups($onBehalfProfileID);
160 'Individual', 'Organization', 'Household') as $contactType) {
161 if (in_array($contactType, $onBehalfProfile) &&
162 (in_array('Membership', $onBehalfProfile) ||
163 in_array('Contribution', $onBehalfProfile)
166 CRM_Core_Error
::fatal($msg);
171 if ($postID = CRM_Utils_Array
::value('custom_post_id', $this->_values
)) {
172 $postProfile = CRM_Core_BAO_UFGroup
::profileGroups($postID);
174 'Individual', 'Organization', 'Household') as $contactType) {
175 if (in_array($contactType, $postProfile) &&
176 (in_array('Membership', $postProfile) ||
177 in_array('Contribution', $postProfile)
180 CRM_Core_Error
::fatal($msg);
188 * set the default values
196 function setDefaultValues() {
197 // check if the user is registered and we have a contact ID
198 $contactID = $this->getContactID();
200 if (!empty($contactID)) {
202 $removeCustomFieldTypes = array('Contribution', 'Membership');
203 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields();
205 // remove component related fields
206 foreach ($this->_fields
as $name => $dontCare) {
207 //don't set custom data Used for Contribution (CRM-1344)
208 if (substr($name, 0, 7) == 'custom_') {
209 $id = substr($name, 7);
210 if (!CRM_Core_BAO_CustomGroup
::checkCustomField($id, $removeCustomFieldTypes)) {
213 // ignore component fields
215 elseif (array_key_exists($name, $contribFields) ||
(substr($name, 0, 11) == 'membership_') ||
(substr($name, 0, 13) == 'contribution_')) {
221 if (!empty($fields)) {
222 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactID, $fields, $this->_defaults
);
225 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
226 $this->_defaults
= array_merge($this->_defaults
, $billingDefaults);
229 //set custom field defaults set by admin if value is not set
230 if (!empty($this->_fields
)) {
231 //load default campaign from page.
232 if (array_key_exists('contribution_campaign_id', $this->_fields
)) {
233 $this->_defaults
['contribution_campaign_id'] = CRM_Utils_Array
::value('campaign_id', $this->_values
);
236 //set custom field defaults
237 foreach ($this->_fields
as $name => $field) {
238 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
239 if (!isset($this->_defaults
[$name])) {
240 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID, $name, $this->_defaults
,
241 NULL, CRM_Profile_Form
::MODE_REGISTER
248 // // hack to simplify credit card entry for testing
249 // $this->_defaults['credit_card_type'] = 'Visa';
250 // $this->_defaults['amount'] = 168;
251 // $this->_defaults['credit_card_number'] = '4111111111111111';
252 // $this->_defaults['cvv2'] = '000';
253 // $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
255 // // hack to simplify direct debit entry for testing
256 // $this->_defaults['account_holder'] = 'Max Müller';
257 // $this->_defaults['bank_account_number'] = '12345678';
258 // $this->_defaults['bank_identification_number'] = '12030000';
259 // $this->_defaults['bank_name'] = 'Bankname';
261 //build set default for pledge overdue payment.
262 if (!empty($this->_values
['pledge_id'])) {
263 //get all pledge payment records of current pledge id.
264 $pledgePayments = array();
266 //used to record completed pledge payment ids used later for honor default
267 $completedContributionIds = array();
269 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($this->_values
['pledge_id']);
272 foreach ($pledgePayments as $payId => $value) {
273 if ($value['status'] == 'Overdue') {
274 $this->_defaults
['pledge_amount'][$payId] = 1;
276 elseif (!$duePayment && $value['status'] == 'Pending') {
277 $this->_defaults
['pledge_amount'][$payId] = 1;
280 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
281 $completedContributionIds[] = $value['contribution_id'];
285 if ($this->_honor_block_is_active
&& count($completedContributionIds)) {
286 $softCredit = array();
287 foreach ($completedContributionIds as $id) {
288 $softCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($id);
290 if (isset($softCredit['soft_credit'])) {
291 $this->_defaults
['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
293 //since honoree profile fieldname of fields are prefixed with 'honor'
294 //we need to reformat the fieldname to append prefix during setting default values
295 CRM_Core_BAO_UFGroup
::setProfileDefaults(
296 $softCredit['soft_credit'][1]['contact_id'],
297 CRM_Core_BAO_UFGroup
::getFields($this->_honoreeProfileId
),
300 foreach ($defaults as $fieldName => $value) {
301 $this->_defaults
['honor[' . $fieldName . ']'] = $value;
306 elseif (!empty($this->_values
['pledge_block_id'])) {
307 //set default to one time contribution.
308 $this->_defaults
['is_pledge'] = 0;
311 // to process Custom data that are appended to URL
312 $getDefaults = CRM_Core_BAO_CustomGroup
::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
313 $this->_defaults
= array_merge($this->_defaults
, $getDefaults);
315 $config = CRM_Core_Config
::singleton();
316 // set default country from config if no country set
317 if (empty($this->_defaults
["billing_country_id-{$this->_bltID}"])) {
318 $this->_defaults
["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
321 // set default state/province from config if no state/province set
322 if (empty($this->_defaults
["billing_state_province_id-{$this->_bltID}"])) {
323 $this->_defaults
["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
326 if ($this->_priceSetId
) {
327 if (($this->_useForMember
&& !empty($this->_currentMemberships
)) ||
$this->_defaultMemTypeId
) {
328 $selectedCurrentMemTypes = array();
329 foreach ($this->_priceSet
['fields'] as $key => $val) {
330 foreach ($val['options'] as $keys => $values) {
331 $opMemTypeId = CRM_Utils_Array
::value('membership_type_id', $values);
333 in_array($opMemTypeId, $this->_currentMemberships
) &&
334 !in_array($opMemTypeId, $selectedCurrentMemTypes)
336 if ($val['html_type'] == 'CheckBox') {
337 $this->_defaults
["price_{$key}"][$keys] = 1;
340 $this->_defaults
["price_{$key}"] = $keys;
342 $selectedCurrentMemTypes[] = $values['membership_type_id'];
344 elseif (!empty($values['is_default']) &&
346 (!isset($this->_defaults
["price_{$key}"]) ||
347 ($val['html_type'] == 'CheckBox' && !isset($this->_defaults
["price_{$key}"][$keys]))
350 if ($val['html_type'] == 'CheckBox') {
351 $this->_defaults
["price_{$key}"][$keys] = 1;
354 $this->_defaults
["price_{$key}"] = $keys;
361 CRM_Price_BAO_PriceSet
::setDefaultPriceSet($this, $this->_defaults
);
365 if (!empty($this->_paymentProcessors
)) {
366 foreach ($this->_paymentProcessors
as $pid => $value) {
367 if (!empty($value['is_default'])) {
368 $this->_defaults
['payment_processor'] = $pid;
373 return $this->_defaults
;
377 * Function to build the form
382 public function buildQuickForm() {
383 // build profiles first so that we can determine address fields etc
384 // and then show copy address checkbox
385 $this->buildCustom($this->_values
['custom_pre_id'], 'customPre');
386 $this->buildCustom($this->_values
['custom_post_id'], 'customPost');
388 if (!empty($this->_fields
) && !empty($this->_values
['custom_pre_id'])) {
389 $profileAddressFields = array();
390 foreach ($this->_fields
as $key => $value) {
391 CRM_Core_BAO_UFField
::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $this->_values
['custom_pre_id']));
393 $this->set('profileAddressFields', $profileAddressFields);
396 // Build payment processor form
397 if (empty($_GET['onbehalf'])) {
398 CRM_Core_Payment_ProcessorForm
::buildQuickForm($this);
399 // Return if we are in an ajax callback
400 if ($this->_snippet
) {
405 $config = CRM_Core_Config
::singleton();
407 if ($this->_onbehalf
) {
408 CRM_Contribute_Form_Contribution_OnBehalfOf
::buildQuickForm($this);
409 // Return if we are in an ajax callback
410 if ($this->_snippet
) {
415 $this->applyFilter('__ALL__', 'trim');
416 $this->add('text', "email-{$this->_bltID}",
418 array('size' => 30, 'maxlength' => 60, 'class' => 'email'),
421 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
423 //@todo - this should be replaced by a check as to whether billing fields are set
424 $onlinePaymentProcessorEnabled = FALSE;
425 if (!empty($this->_paymentProcessors
)) {
426 foreach ($this->_paymentProcessors
as $key => $name) {
427 if($name['billing_mode'] == 1) {
428 $onlinePaymentProcessorEnabled = TRUE;
430 $pps[$key] = $name['name'];
433 if (!empty($this->_values
['is_pay_later'])) {
434 $pps[0] = $this->_values
['pay_later_text'];
437 if (count($pps) > 1) {
438 $this->addRadio('payment_processor', ts('Payment Method'), $pps,
442 elseif (!empty($pps)) {
443 $key = array_keys($pps);
444 $key = array_pop($key);
445 $this->addElement('hidden', 'payment_processor', $key);
447 $this->assign('is_pay_later', $this->_values
['is_pay_later']);
448 $this->assign('pay_later_text', $this->_values
['pay_later_text']);
452 $contactID = $this->getContactID();
453 if($this->getContactID() === '0') {
454 $this->addCidZeroOptions($onlinePaymentProcessorEnabled);
456 //build pledge block.
457 $this->_useForMember
= 0;
458 //don't build membership block when pledge_id is passed
459 if (empty($this->_values
['pledge_id'])) {
460 $this->_separateMembershipPayment
= FALSE;
461 if (in_array('CiviMember', $config->enableComponents
)) {
463 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
467 if ($this->_priceSetId
&&
468 (CRM_Core_Component
::getComponentID('CiviMember') == CRM_Utils_Array
::value('extends', $this->_priceSet
))
470 $this->_useForMember
= 1;
471 $this->set('useForMember', $this->_useForMember
);
474 $this->_separateMembershipPayment
= CRM_Member_BAO_Membership
::buildMembershipBlock($this,
476 $this->_membershipContactID
,
481 $this->set('separateMembershipPayment', $this->_separateMembershipPayment
);
483 $this->assign('useForMember', $this->_useForMember
);
484 // If we configured price set for contribution page
485 // we are not allow membership signup as well as any
486 // other contribution amount field, CRM-5095
487 if (isset($this->_priceSetId
) && $this->_priceSetId
) {
488 $this->add('hidden', 'priceSetId', $this->_priceSetId
);
489 // build price set form.
490 $this->set('priceSetId', $this->_priceSetId
);
491 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
492 if ($this->_values
['is_monetary'] &&
493 $this->_values
['is_recur'] && empty($this->_values
['pledge_id'])) {
494 self
::buildRecur($this);
498 if ($this->_priceSetId
) {
499 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
500 if ($is_quick_config) {
501 $this->_useForMember
= 0;
502 $this->set('useForMember', $this->_useForMember
);
506 if ($this->_values
['is_for_organization']) {
507 $this->buildOnBehalfOrganization();
510 //we allow premium for pledge during pledge creation only.
511 if (empty($this->_values
['pledge_id'])) {
512 CRM_Contribute_BAO_Premium
::buildPremiumBlock($this, $this->_id
, TRUE);
516 if ($this->_honor_block_is_active
) {
517 $this->assign('honor_block_is_active', TRUE);
519 //build soft-credit section
520 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
521 //build honoree profile section
522 CRM_Contact_Form_ProfileContact
::buildQuickForm($this);
526 //don't build pledge block when mid is passed
528 $config = CRM_Core_Config
::singleton();
529 if (in_array('CiviPledge', $config->enableComponents
) && !empty($this->_values
['pledge_block_id'])) {
530 CRM_Pledge_BAO_PledgeBlock
::buildPledgeBlock($this);
534 //to create an cms user
535 if (!$this->_userID
) {
536 $createCMSUser = FALSE;
538 if ($this->_values
['custom_pre_id']) {
539 $profileID = $this->_values
['custom_pre_id'];
540 $createCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
543 if (!$createCMSUser &&
544 $this->_values
['custom_post_id']
546 if (!is_array($this->_values
['custom_post_id'])) {
547 $profileIDs = array($this->_values
['custom_post_id']);
550 $profileIDs = $this->_values
['custom_post_id'];
552 foreach ($profileIDs as $pid) {
553 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
555 $createCMSUser = TRUE;
561 if ($createCMSUser) {
562 CRM_Core_BAO_CMSUser
::buildForm($this, $profileID, TRUE);
566 if ($pcpSupporter = CRM_PCP_BAO_PCP
::displayName($this->_pcpId
)) {
567 $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter));
568 // Only tell people that can also create a PCP if the contribution page has a non-empty value in the "Create Personal Campaign Page link" field.
569 $text = CRM_PCP_BAO_PCP
::getPcpBlockStatus($this->_id
, 'contribute');
571 $pcp_supporter_text .= ts("You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!");
573 $this->assign('pcpSupporterText', $pcp_supporter_text);
575 $prms = array('id' => $this->_pcpId
);
576 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
577 if ($pcpInfo['is_honor_roll']) {
578 $this->assign('isHonor', TRUE);
579 $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL,
580 array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );")
582 $extraOption = array('onclick' => "return pcpAnonymous( );");
584 $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption);
585 $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption);
586 $this->addGroup($elements, 'pcp_is_anonymous', NULL, ' ');
587 $this->setDefaults(array('pcp_display_in_roll' => 1));
588 $this->setDefaults(array('pcp_is_anonymous' => 1));
590 $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30));
591 $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;'));
595 //we have to load confirm contribution button in template
596 //when multiple payment processor as the user
597 //can toggle with payment processor selection
598 $billingModePaymentProcessors = 0;
599 if ( !empty( $this->_paymentProcessors
) ) {
600 foreach ($this->_paymentProcessors
as $key => $values) {
601 if ($values['billing_mode'] == CRM_Core_Payment
::BILLING_MODE_BUTTON
) {
602 $billingModePaymentProcessors++
;
607 if ($billingModePaymentProcessors && count($this->_paymentProcessors
) == $billingModePaymentProcessors) {
608 $allAreBillingModeProcessors = TRUE;
610 $allAreBillingModeProcessors = FALSE;
613 if (!($allAreBillingModeProcessors && !$this->_values
['is_pay_later'])) {
614 $submitButton = array(
616 'name' => CRM_Utils_Array
::value('is_confirm_enabled', $this->_values
) ?
ts('Confirm Contribution') : ts('Contribute'),
617 'spacing' => ' ',
620 // Add submit-once behavior when confirm page disabled
621 if (empty($this->_values
['is_confirm_enabled'])) {
622 $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name
. "','" . ts('Processing') . "');");
624 $this->addButtons(array($submitButton));
627 $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this);
631 * build elements to enable pay on behalf of an organization.
635 function buildOnBehalfOrganization() {
636 if ($this->_membershipContactID
) {
637 $entityBlock = array('contact_id' => $this->_membershipContactID
);
638 CRM_Core_BAO_Location
::getValues($entityBlock, $this->_defaults
);
641 if (!$this->_onBehalfRequired
) {
642 $this->addElement('checkbox', 'is_for_organization',
643 $this->_values
['for_organization'],
644 NULL, array('onclick' => "showOnBehalf( );")
648 $this->assign('is_for_organization', TRUE);
649 $this->assign('urlPath', 'civicrm/contribute/transact');
653 * build elements to collect information for recurring contributions
657 * @param CRM_Core_Form $form
659 public static function buildRecur(&$form) {
660 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_ContributionRecur');
661 $className = get_class($form);
663 $form->assign('is_recur_interval', CRM_Utils_Array
::value('is_recur_interval', $form->_values
));
664 $form->assign('is_recur_installments', CRM_Utils_Array
::value('is_recur_installments', $form->_values
));
666 $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL);
668 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
669 $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']);
670 $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer');
673 // make sure frequency_interval is submitted as 1 if given no choice to user.
674 $form->add('hidden', 'frequency_interval', 1);
677 $frUnits = CRM_Utils_Array
::value('recur_frequency_unit', $form->_values
);
678 if (empty($frUnits) &&
679 $className == 'CRM_Contribute_Form_Contribution'
681 $frUnits = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
682 CRM_Core_OptionGroup
::values('recur_frequency_units')
686 $unitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $frUnits);
688 // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
689 if(sizeof($unitVals) == 1) {
690 $form->assign('one_frequency_unit', true);
691 $unit = $unitVals[0];
692 $form->add('hidden', 'frequency_unit', $unit);
693 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
696 $form->assign('frequency_unit', $unit);
698 $form->assign('one_frequency_unit', false);
700 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
701 foreach ($unitVals as $key => $val) {
702 if (array_key_exists($val, $frequencyUnits)) {
703 $units[$val] = $frequencyUnits[$val];
704 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
705 $units[$val] = "{$frequencyUnits[$val]}(s)";
709 $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units);
713 // FIXME: Ideally we should freeze select box if there is only
714 // one option but looks there is some problem /w QF freeze.
715 //if ( count( $units ) == 1 ) {
716 //$frequencyUnit->freeze( );
719 $form->add('text', 'installments', ts('installments'),
720 $attributes['installments']
722 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
728 * @param array $fields the input form values
729 * @param array $files the uploaded files if any
732 * @internal param array $options additional user data
734 * @return true if no errors, else array of errors
738 static function formRule($fields, $files, $self) {
740 $amount = self
::computeAmount($fields, $self);
742 if ((!empty($fields['selectMembership']) &&
743 $fields['selectMembership'] != 'no_thanks'
745 (!empty($fields['priceSetId']) &&
749 $lifeMember = CRM_Member_BAO_Membership
::getAllContactMembership($self->_userID
, FALSE, TRUE);
751 $membershipOrgDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization();
753 $unallowedOrgs = array();
754 foreach (array_keys($lifeMember) as $memTypeId) {
755 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId];
759 //check for atleast one pricefields should be selected
760 if (!empty($fields['priceSetId'])) {
761 $priceField = new CRM_Price_DAO_PriceField();
762 $priceField->price_set_id
= $fields['priceSetId'];
763 $priceField->orderBy('weight');
767 $membershipIsActive = TRUE;
768 $previousId = $otherAmount = FALSE;
769 while ($priceField->fetch()) {
771 if ($self->_quickConfig
&& ($priceField->name
== 'contribution_amount' ||
$priceField->name
== 'membership_amount')) {
772 $previousId = $priceField->id
;
773 if ($priceField->name
== 'membership_amount' && !$priceField->is_active
) {
774 $membershipIsActive = FALSE;
777 if ($priceField->name
== 'other_amount') {
778 if ($self->_quickConfig
&& empty($fields["price_{$priceField->id}"]) &&
779 array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values
['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])) {
780 $otherAmount = $priceField->id
;
782 elseif (!empty($fields["price_{$priceField->id}"])) {
783 $otherAmountVal = CRM_Utils_Rule
::cleanMoney($fields["price_{$priceField->id}"]);
784 $min = CRM_Utils_Array
::value('min_amount', $self->_values
);
785 $max = CRM_Utils_Array
::value('max_amount', $self->_values
);
786 if ($min && $otherAmountVal < $min) {
787 $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1',
791 if ($max && $otherAmountVal > $max) {
792 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
798 if (!empty($fields["price_{$priceField->id}"]) ||
($previousId == $priceField->id
&& isset($fields["price_{$previousId}"])
799 && empty($fields["price_{$previousId}"]))) {
800 $check[] = $priceField->id
;
804 $currentMemberships = NULL;
805 if ($membershipIsActive) {
806 $is_test = $self->_mode
!= 'live' ?
1 : 0;
807 $memContactID = $self->_membershipContactID
;
809 // For anonymous user check using dedupe rule
810 // if user has Cancelled Membership
811 if (!$memContactID) {
812 $dedupeParams = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
813 $dedupeParams['check_permission'] = FALSE;
814 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
815 // if we find more than one contact, use the first one
816 $memContactID = CRM_Utils_Array
::value(0, $ids);
818 $currentMemberships = CRM_Member_BAO_Membership
::getContactsCancelledMembership($memContactID,
822 $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.';
823 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
824 if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array
::value('price_' . $fieldKey, $fields)) {
825 if (!is_array($fields['price_' . $fieldKey])) {
826 if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]])
827 && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)) {
828 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'])));
832 foreach ($fields['price_' . $fieldKey] as $key => $ignore) {
833 if (array_key_exists('membership_type_id', $fieldValue['options'][$key])
834 && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)) {
835 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$key]['membership_type_id'])));
844 if ($membershipIsActive && !$self->_membershipBlock
['is_required']
845 && $self->_values
['amount_block_is_active']) {
846 $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
847 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
848 // if 'No thank you' membership is selected then set $membershipFieldId
849 if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array
::value('price_' . $fieldKey, $fields) == 0) {
850 $membershipFieldId = $fieldKey;
852 elseif ($membershipFieldId) {
853 if ($fieldValue['name'] == 'other_amount') {
854 $otherFieldId = $fieldKey;
856 elseif ($fieldValue['name'] == 'contribution_amount') {
857 $contributionFieldId = $fieldKey;
860 if (!$errorKey || CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields) == '0') {
861 $errorKey = $fieldKey;
865 // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error
866 if ($membershipFieldId && !(CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) {
867 $errors["price_{$errorKey}"] = ts('Additional Contribution is required.');
871 if ($self->_useForMember
== 1 && $membershipIsActive) {
872 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
875 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
878 if($otherAmount && !empty($check)) {
879 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
882 if ($self->_useForMember
== 1 && !empty($check) && $membershipIsActive) {
883 $priceFieldIDS = array();
884 $priceFieldMemTypes = array();
886 foreach ($self->_priceSet
['fields'] as $priceId => $value) {
887 if (!empty($fields['price_' . $priceId]) ||
($self->_quickConfig
&& $value['name'] == 'membership_amount' && empty($self->_membershipBlock
['is_required']))) {
888 if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) {
889 foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
891 $priceFieldIDS[] = $priceFldVal;
895 elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) {
896 // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
897 // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
898 $priceFieldIDS[] = $fields['price_' . $priceId];
901 if (!empty($value['options'])) {
902 foreach ($value['options'] as $val) {
903 if (!empty($val['membership_type_id']) && (
904 ($fields['price_' . $priceId] == $val['id']) ||
905 (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']]))
908 $priceFieldMemTypes[] = $val['membership_type_id'];
915 if (!empty($lifeMember)) {
916 foreach ($priceFieldIDS as $priceFieldId) {
917 if (($id = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) &&
918 in_array($membershipOrgDetails[$id], $unallowedOrgs)
920 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
926 if (!empty($priceFieldIDS)) {
927 $ids = implode(',', $priceFieldIDS);
929 $priceFieldIDS['id'] = $fields['priceSetId'];
930 $self->set('memberPriceFieldIDS', $priceFieldIDS);
931 $count = CRM_Price_BAO_PriceSet
::getMembershipCount($ids);
932 foreach ($count as $id => $occurance) {
933 if ($occurance > 1) {
934 $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.');
939 if (empty($priceFieldMemTypes)) {
940 $errors['_qf_default'] = ts('Please select at least one membership option.');
944 CRM_Price_BAO_PriceSet
::processAmount($self->_values
['fee'],
948 if ($fields['amount'] < 0) {
949 $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
951 $amount = $fields['amount'];
954 if (isset($fields['selectProduct']) &&
955 $fields['selectProduct'] != 'no_thanks') {
956 $productDAO = new CRM_Contribute_DAO_Product();
957 $productDAO->id
= $fields['selectProduct'];
958 $productDAO->find(TRUE);
959 $min_amount = $productDAO->min_contribution
;
961 if ($amount < $min_amount) {
962 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money
::format($min_amount)));
963 CRM_Core_Session
::setStatus($errors['selectProduct']);
967 if (!empty($fields['is_recur'])) {
968 if ($fields['frequency_interval'] <= 0) {
969 $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).');
971 if ($fields['frequency_unit'] == '0') {
972 $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).');
976 if (!empty($fields['is_recur']) &&
977 CRM_Utils_Array
::value('payment_processor', $fields) == 0) {
978 $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
981 if (!empty($fields['is_for_organization']) &&
982 !property_exists($self, 'organizationName')
985 if (empty($fields['onbehalf']['organization_name'])) {
986 if (!empty($fields['org_option']) && !$fields['onbehalfof_id']) {
987 $errors['organization_id'] = ts('Please select an organization or enter a new one.');
989 elseif (empty($fields['org_option'])) {
990 $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.');
994 foreach ($fields['onbehalf'] as $key => $value) {
995 if (strstr($key, 'email')) {
996 $emailLocType = explode('-', $key);
999 if (empty($fields['onbehalf']["email-{$emailLocType[1]}"])) {
1000 $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.');
1004 // validate PCP fields - if not anonymous, we need a nick name value
1005 if ($self->_pcpId
&& !empty($fields['pcp_display_in_roll']) &&
1006 (CRM_Utils_Array
::value('pcp_is_anonymous', $fields) == 0) &&
1007 CRM_Utils_Array
::value('pcp_roll_nickname', $fields) == ''
1009 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
1012 // return if this is express mode
1013 $config = CRM_Core_Config
::singleton();
1014 if ($self->_paymentProcessor
&&
1015 $self->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_BUTTON
1017 if (!empty($fields[$self->_expressButtonName
. '_x']) ||
!empty($fields[$self->_expressButtonName
. '_y']) ||
1018 CRM_Utils_Array
::value($self->_expressButtonName
, $fields)
1024 //validate the pledge fields.
1025 if (!empty($self->_values
['pledge_block_id'])) {
1026 //validation for pledge payment.
1027 if (!empty($self->_values
['pledge_id'])) {
1028 if (empty($fields['pledge_amount'])) {
1029 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
1032 elseif (!empty($fields['is_pledge'])) {
1033 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_installments', $fields)) == FALSE) {
1034 $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
1037 if (CRM_Utils_Array
::value('pledge_installments', $fields) == NULL) {
1038 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
1040 elseif (CRM_Utils_array
::value('pledge_installments', $fields) == 1) {
1041 $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.');
1043 elseif (CRM_Utils_array
::value('pledge_installments', $fields) == 0) {
1044 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
1048 //validation for Pledge Frequency Interval.
1049 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_frequency_interval', $fields)) == FALSE) {
1050 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
1053 if (CRM_Utils_Array
::value('pledge_frequency_interval', $fields) == NULL) {
1054 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
1056 elseif (CRM_Utils_array
::value('pledge_frequency_interval', $fields) == 0) {
1057 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
1063 // also return if paylater mode
1064 if (CRM_Utils_Array
::value('payment_processor', $fields) == 0 && $self->_isBillingAddressRequiredForPayLater
== 0) {
1065 return empty($errors) ?
TRUE : $errors;
1068 // if the user has chosen a free membership or the amount is less than zero
1069 // i.e. we skip calling the payment processor and hence dont need credit card
1071 if ((float) $amount <= 0.0) {
1075 if (!empty($self->_paymentFields
)) {
1076 CRM_Core_Form
::validateMandatoryFields($self->_paymentFields
, $fields, $errors);
1078 CRM_Core_Payment_Form
::validateCreditCard($fields, $errors);
1080 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1081 if ($greetingType = CRM_Utils_Array
::value($greeting, $fields)) {
1082 $customizedValue = CRM_Core_OptionGroup
::getValue($greeting, 'Customized', 'name');
1083 if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
1084 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
1085 array(1 => ucwords(str_replace('_', " ", $greeting)))
1091 return empty($errors) ?
TRUE : $errors;
1098 * @return int|mixed|null|string
1100 public static function computeAmount(&$params, &$form) {
1103 // first clean up the other amount field if present
1104 if (isset($params['amount_other'])) {
1105 $params['amount_other'] = CRM_Utils_Rule
::cleanMoney($params['amount_other']);
1108 if (CRM_Utils_Array
::value('amount', $params) == 'amount_other_radio' ||
!empty($params['amount_other'])) {
1109 $amount = $params['amount_other'];
1111 elseif (!empty($params['pledge_amount'])) {
1113 foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
1114 $amount +
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
1118 if (!empty($form->_values
['amount'])) {
1119 $amountID = CRM_Utils_Array
::value('amount', $params);
1122 $params['amount_level'] = CRM_Utils_Array
::value('label', $form->_values
[$amountID]);
1123 $amount = CRM_Utils_Array
::value('value', $form->_values
[$amountID]);
1131 * Function to process the form
1137 public function postProcess() {
1138 $config = CRM_Core_Config
::singleton();
1139 // we first reset the confirm page so it accepts new values
1140 $this->controller
->resetPage('Confirm');
1142 // get the submitted form values.
1143 $params = $this->controller
->exportValues($this->_name
);
1145 //carry campaign from profile.
1146 if (array_key_exists('contribution_campaign_id', $params)) {
1147 $params['campaign_id'] = $params['contribution_campaign_id'];
1150 if (!empty($params['onbehalfof_id'])) {
1151 $params['organization_id'] = $params['onbehalfof_id'];
1154 $params['currencyID'] = $config->defaultCurrency
;
1156 if (!empty($params['priceSetId'])) {
1157 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1158 if ($is_quick_config) {
1159 $priceField = new CRM_Price_DAO_PriceField();
1160 $priceField->price_set_id
= $params['priceSetId'];
1161 $priceField->orderBy('weight');
1162 $priceField->find();
1164 $priceOptions = array();
1165 while ($priceField->fetch()) {
1166 CRM_Price_BAO_PriceFieldValue
::getValues($priceField->id
, $priceOptions);
1167 if ($selectedPriceOptionID = CRM_Utils_Array
::value("price_{$priceField->id}", $params)) {
1168 switch ($priceField->name
) {
1169 case 'membership_amount':
1170 $this->_params
['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array
::value('membership_type_id', $priceOptions[$selectedPriceOptionID]);
1171 $this->set('selectMembership', $params['selectMembership']);
1172 if (CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
) == 0) {
1173 $this->_values
['amount'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1177 case 'contribution_amount':
1178 $params['amount'] = $selectedPriceOptionID;
1179 $this->_values
['amount'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1180 $this->_values
[$selectedPriceOptionID]['value'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1181 $this->_values
[$selectedPriceOptionID]['label'] = CRM_Utils_Array
::value('label', $priceOptions[$selectedPriceOptionID]);
1182 $this->_values
[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array
::value('id', $priceOptions[$selectedPriceOptionID]);
1183 $this->_values
[$selectedPriceOptionID]['weight'] = CRM_Utils_Array
::value('weight', $priceOptions[$selectedPriceOptionID]);
1186 case 'other_amount':
1187 $params['amount_other'] = $selectedPriceOptionID;
1195 if (($this->_values
['is_pay_later'] &&
1196 empty($this->_paymentProcessor
) &&
1197 !array_key_exists('hidden_processor', $params)) ||
1198 (!empty($params['payment_processor']) && $params['payment_processor'] == 0)) {
1199 $params['is_pay_later'] = 1;
1202 $params['is_pay_later'] = 0;
1205 $this->set('is_pay_later', $params['is_pay_later']);
1206 // assign pay later stuff
1207 $this->_params
['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, FALSE);
1208 $this->assign('is_pay_later', $params['is_pay_later']);
1209 if ($params['is_pay_later']) {
1210 $this->assign('pay_later_text', $this->_values
['pay_later_text']);
1211 $this->assign('pay_later_receipt', $this->_values
['pay_later_receipt']);
1214 // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
1215 $params['amount'] = self
::computeAmount($params, $this);
1216 $params['separate_amount'] = $params['amount'];
1218 if (!empty($params['selectMembership'])) {
1219 if (!empty($this->_membershipTypeValues
)) {
1220 $membershipTypeValues = $this->_membershipTypeValues
[$params['selectMembership']];
1223 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this,
1224 $params['selectMembership']
1227 $memFee = $membershipTypeValues['minimum_fee'];
1228 if (!$params['amount'] && !$this->_separateMembershipPayment
) {
1229 $params['amount'] = $memFee ?
$memFee : 0;
1232 //If the membership & contribution is used in contribution page & not separate payment
1233 $fieldId = $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL;
1234 $proceFieldAmount = 0;
1235 if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment
== 0) {
1236 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1237 if ($is_quick_config) {
1238 foreach ($this->_priceSet
['fields'] as $fieldKey => $fieldVal) {
1239 if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey ])) {
1240 $fieldId = $fieldVal['id'];
1241 $fieldOption = $params['price_' . $fieldId];
1242 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1246 if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' ||
$fieldVal['name'] == 'contribution_amount')) {
1247 $fieldId = $fieldVal['id'];
1248 if ($fieldVal['name'] == 'other_amount') {
1249 $proceFieldAmount +
= $this->_submitValues
['price_' . $fieldId];
1251 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues
['price_' . $fieldId] > 0) {
1252 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1254 unset($params['price_' . $fieldId]);
1262 if (!isset($params['amount_other'])) {
1263 $this->set('amount_level', CRM_Utils_Array
::value('amount_level', $params));
1266 if ($priceSetId = CRM_Utils_Array
::value('priceSetId', $params)) {
1267 $lineItem = array();
1268 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config' );
1269 if ( $is_quick_config ) {
1270 foreach ( $this->_values
['fee'] as $key => & $val ) {
1271 if ( $val['name'] == 'other_amount' && $val['html_type'] == 'Text' && array_key_exists( 'price_'.$key, $params ) ) {
1272 $params['price_'.$key] = CRM_Utils_Rule
::cleanMoney($params['price_'.$key]); //Clean out any currency symbols
1273 if ( $params['price_'.$key] != 0 ) {
1274 foreach ( $val['options'] as $optionKey => & $options ) {
1275 $options['amount'] = CRM_Utils_Array
::value( 'price_'.$key, $params );
1279 $params['price_'.$key] = 1;
1285 if ($this->_membershipBlock
) {
1286 $component = 'membership';
1289 CRM_Price_BAO_PriceSet
::processAmount($this->_values
['fee'], $params, $lineItem[$priceSetId], $component);
1290 if ($params['tax_amount']) {
1291 $this->set('tax_amount', $params['tax_amount']);
1294 if ($proceFieldAmount) {
1295 $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
1296 $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
1297 if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
1298 $proceFieldAmount +
= $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
1300 if (!$this->_membershipBlock
['is_separate_payment']) {
1301 $params['amount'] = $proceFieldAmount; //require when separate membership not used
1304 $this->set('lineItem', $lineItem);
1307 if ($this->_membershipBlock
['is_separate_payment'] && !empty($params['separate_amount'])) {
1308 $this->set('amount', $params['separate_amount']);
1310 $this->set('amount', $params['amount']);
1313 // generate and set an invoiceID for this transaction
1314 $invoiceID = md5(uniqid(rand(), TRUE));
1315 $this->set('invoiceID', $invoiceID);
1317 // required only if is_monetary and valid positive amount
1318 if ($this->_values
['is_monetary'] &&
1319 is_array($this->_paymentProcessor
) &&
1320 ((float ) $params['amount'] > 0.0 ||
$memFee > 0.0)
1323 // default mode is direct
1324 $this->set('contributeMode', 'direct');
1326 if ($this->_paymentProcessor
&&
1327 $this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_BUTTON
1329 //get the button name
1330 $buttonName = $this->controller
->getButtonName();
1331 if (in_array($buttonName,
1332 array($this->_expressButtonName
, $this->_expressButtonName
. '_x', $this->_expressButtonName
. '_y')
1333 ) && empty($params['is_pay_later'])) {
1334 $this->set('contributeMode', 'express');
1336 $donateURL = CRM_Utils_System
::url('civicrm/contribute', '_qf_Contribute_display=1');
1337 $params['cancelURL'] = CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1338 $params['returnURL'] = CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1339 $params['invoiceID'] = $invoiceID;
1340 $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo
['title']) ?
$this->_pcpInfo
['title'] : $this->_values
['title']);
1342 //default action is Sale
1343 $params['payment_action'] = 'Sale';
1345 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this);
1346 $token = $payment->setExpressCheckout($params);
1347 if (is_a($token, 'CRM_Core_Error')) {
1348 CRM_Core_Error
::displaySessionError($token);
1349 CRM_Utils_System
::redirect($params['cancelURL']);
1352 $this->set('token', $token);
1354 $paymentURL = $this->_paymentProcessor
['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token";
1355 CRM_Utils_System
::redirect($paymentURL);
1358 elseif ($this->_paymentProcessor
&&
1359 $this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_NOTIFY
1361 $this->set('contributeMode', 'notify');
1365 // should we skip the confirm page?
1366 if (empty($this->_values
['is_confirm_enabled'])) {
1367 // call the post process hook for the main page before we switch to confirm
1368 $this->postProcessHook();
1370 // build the confirm page
1371 $confirmForm = &$this->controller
->_pages
['Confirm'];
1372 $confirmForm->preProcess();
1373 $confirmForm->buildQuickForm();
1375 // the confirmation page is valid
1376 $data = &$this->controller
->container();
1377 $data['valid']['Confirm'] = 1;
1379 // confirm the contribution
1380 // mainProcess calls the hook also
1381 $confirmForm->mainProcess();
1382 $qfKey = $this->controller
->_key
;
1384 // redirect to thank you page
1385 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE));
1391 * Handle Payment Processor switching
1392 * For contribution and event registration forms
1393 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form
1394 * @param bool $noFees
1396 static function preProcessPaymentOptions(&$form, $noFees = FALSE) {
1397 $form->_snippet
= CRM_Utils_Array
::value('snippet', $_GET);
1399 $form->_paymentProcessors
= $noFees ?
array() : $form->get('paymentProcessors');
1400 $form->_paymentProcessorID
= NULL;
1401 if ($form->_paymentProcessors
) {
1402 // Fetch type during ajax request
1403 if (isset($_GET['type']) && $form->_snippet
) {
1404 $form->_paymentProcessorID
= CRM_Utils_Request
::retrieve('type', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, 0);
1406 // Remember type during form post
1407 elseif (!empty($form->_submitValues
)) {
1408 $form->_paymentProcessorID
= CRM_Utils_Array
::value('payment_processor', $form->_submitValues
);
1409 $form->_paymentProcessor
= CRM_Utils_Array
::value($form->_paymentProcessorID
, $form->_paymentProcessors
);
1410 $form->set('type', $form->_paymentProcessorID
);
1411 $form->set('mode', $form->_mode
);
1412 $form->set('paymentProcessor', $form->_paymentProcessor
);
1414 // Set default payment processor
1416 foreach ($form->_paymentProcessors
as $values) {
1417 if (!empty($values['is_default']) ||
count($form->_paymentProcessors
) == 1) {
1418 $form->_paymentProcessorID
= $values['id'];
1423 if ($form->_paymentProcessorID
) {
1424 CRM_Core_Payment_ProcessorForm
::preProcess($form);
1427 $form->_paymentProcessor
= array();
1430 //get payPal express id and make it available to template
1431 foreach ($form->_paymentProcessors
as $ppId => $values) {
1432 $payPalExpressId = ($values['payment_processor_type'] == 'PayPal_Express') ?
$values['id'] : 0;
1433 $form->assign('payPalExpressId', $payPalExpressId);
1434 if ($payPalExpressId) {
1438 if (!$form->_snippet
) {
1439 // Add JS to show icons for the accepted credit cards
1440 $creditCardTypes = CRM_Core_Payment_Form
::getCreditCardCSSNames();
1441 CRM_Core_Resources
::singleton()
1442 ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10)
1443 // workaround for CRM-13634
1444 // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
1445 ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';');
1448 $form->assign('paymentProcessorID', $form->_paymentProcessorID
);