3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
45 public $_defaultMemTypeId;
47 public $_relatedOrganizationFound;
49 public $_onBehalfRequired = FALSE;
50 public $_onbehalf = FALSE;
51 public $_paymentProcessors;
54 public $_membershipTypeValues;
56 public $_useForMember;
59 * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl
62 public $_paymentFields = array();
64 protected $_paymentProcessorID;
68 * Set variables up before form is built
72 public function preProcess() {
75 self
::preProcessPaymentOptions($this);
77 // Make the contributionPageID avilable to the template
78 $this->assign('contributionPageID', $this->_id
);
79 $this->assign('isShare', CRM_Utils_Array
::value('is_share', $this->_values
));
80 $this->assign('isConfirmEnabled', CRM_Utils_Array
::value('is_confirm_enabled', $this->_values
));
82 // make sure we have right permission to edit this user
83 $csContactID = $this->getContactID();
84 $reset = CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
85 $mainDisplay = CRM_Utils_Request
::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO
::$_nullObject);
88 $this->assign('reset', $reset);
92 $this->assign('mainDisplay', $mainDisplay);
95 // Possible values for 'is_for_organization':
96 // * 0 - org profile disabled
97 // * 1 - org profile optional
98 // * 2 - org profile required
99 $this->_onbehalf
= FALSE;
100 if (!empty($this->_values
['is_for_organization'])) {
101 if ($this->_values
['is_for_organization'] == 2) {
102 $this->_onBehalfRequired
= TRUE;
104 // Add organization profile if 1 of the following are true:
105 // If the org profile is required
106 if ($this->_onBehalfRequired ||
107 // Or we are building the form for the first time
109 // Or the user has submitted the form and checked the "On Behalf" checkbox
110 !empty($_POST['is_for_organization'])
112 $this->_onbehalf
= TRUE;
113 CRM_Contribute_Form_Contribution_OnBehalfOf
::preProcess($this);
116 $this->assign('onBehalfRequired', $this->_onBehalfRequired
);
118 if ($this->_honor_block_is_active
) {
119 CRM_Contact_Form_ProfileContact
::preprocess($this);
122 if ($this->_snippet
) {
123 $this->assign('isOnBehalfCallback', CRM_Utils_Array
::value('onbehalf', $_GET, FALSE));
127 if (!empty($this->_pcpInfo
['id']) && !empty($this->_pcpInfo
['intro_text'])) {
128 $this->assign('intro_text', $this->_pcpInfo
['intro_text']);
130 elseif (!empty($this->_values
['intro_text'])) {
131 $this->assign('intro_text', $this->_values
['intro_text']);
134 $qParams = "reset=1&id={$this->_id}";
135 if ($pcpId = CRM_Utils_Array
::value('pcp_id', $this->_pcpInfo
)) {
136 $qParams .= "&pcpId={$pcpId}";
138 $this->assign('qParams', $qParams);
140 if (!empty($this->_values
['footer_text'])) {
141 $this->assign('footer_text', $this->_values
['footer_text']);
146 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
147 if (!empty($this->_values
['is_for_organization']) && empty($member['is_active'])) {
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);
164 if (in_array($contactType, $onBehalfProfile) &&
165 (in_array('Membership', $onBehalfProfile) ||
166 in_array('Contribution', $onBehalfProfile)
169 CRM_Core_Error
::fatal($msg);
174 if ($postID = CRM_Utils_Array
::value('custom_post_id', $this->_values
)) {
175 $postProfile = CRM_Core_BAO_UFGroup
::profileGroups($postID);
181 if (in_array($contactType, $postProfile) &&
182 (in_array('Membership', $postProfile) ||
183 in_array('Contribution', $postProfile)
186 CRM_Core_Error
::fatal($msg);
194 * Set the default values
200 public function setDefaultValues() {
201 // check if the user is registered and we have a contact ID
202 $contactID = $this->getContactID();
204 if (!empty($contactID)) {
206 $removeCustomFieldTypes = array('Contribution', 'Membership');
207 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields();
209 // remove component related fields
210 foreach ($this->_fields
as $name => $dontCare) {
211 //don't set custom data Used for Contribution (CRM-1344)
212 if (substr($name, 0, 7) == 'custom_') {
213 $id = substr($name, 7);
214 if (!CRM_Core_BAO_CustomGroup
::checkCustomField($id, $removeCustomFieldTypes)) {
217 // ignore component fields
219 elseif (array_key_exists($name, $contribFields) ||
(substr($name, 0, 11) == 'membership_') ||
(substr($name, 0, 13) == 'contribution_')) {
225 if (!empty($fields)) {
226 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactID, $fields, $this->_defaults
);
229 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
230 $this->_defaults
= array_merge($this->_defaults
, $billingDefaults);
233 //set custom field defaults set by admin if value is not set
234 if (!empty($this->_fields
)) {
235 //load default campaign from page.
236 if (array_key_exists('contribution_campaign_id', $this->_fields
)) {
237 $this->_defaults
['contribution_campaign_id'] = CRM_Utils_Array
::value('campaign_id', $this->_values
);
240 //set custom field defaults
241 foreach ($this->_fields
as $name => $field) {
242 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
243 if (!isset($this->_defaults
[$name])) {
244 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID, $name, $this->_defaults
,
245 NULL, CRM_Profile_Form
::MODE_REGISTER
252 // // hack to simplify credit card entry for testing
253 // $this->_defaults['credit_card_type'] = 'Visa';
254 // $this->_defaults['amount'] = 168;
255 // $this->_defaults['credit_card_number'] = '4111111111111111';
256 // $this->_defaults['cvv2'] = '000';
257 // $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
259 // // hack to simplify direct debit entry for testing
260 // $this->_defaults['account_holder'] = 'Max Müller';
261 // $this->_defaults['bank_account_number'] = '12345678';
262 // $this->_defaults['bank_identification_number'] = '12030000';
263 // $this->_defaults['bank_name'] = 'Bankname';
265 //build set default for pledge overdue payment.
266 if (!empty($this->_values
['pledge_id'])) {
267 //get all pledge payment records of current pledge id.
268 $pledgePayments = array();
270 //used to record completed pledge payment ids used later for honor default
271 $completedContributionIds = array();
273 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($this->_values
['pledge_id']);
276 foreach ($pledgePayments as $payId => $value) {
277 if ($value['status'] == 'Overdue') {
278 $this->_defaults
['pledge_amount'][$payId] = 1;
280 elseif (!$duePayment && $value['status'] == 'Pending') {
281 $this->_defaults
['pledge_amount'][$payId] = 1;
284 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
285 $completedContributionIds[] = $value['contribution_id'];
289 if ($this->_honor_block_is_active
&& count($completedContributionIds)) {
290 $softCredit = array();
291 foreach ($completedContributionIds as $id) {
292 $softCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($id);
294 if (isset($softCredit['soft_credit'])) {
295 $this->_defaults
['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
297 //since honoree profile fieldname of fields are prefixed with 'honor'
298 //we need to reformat the fieldname to append prefix during setting default values
299 CRM_Core_BAO_UFGroup
::setProfileDefaults(
300 $softCredit['soft_credit'][1]['contact_id'],
301 CRM_Core_BAO_UFGroup
::getFields($this->_honoreeProfileId
),
304 foreach ($defaults as $fieldName => $value) {
305 $this->_defaults
['honor[' . $fieldName . ']'] = $value;
310 elseif (!empty($this->_values
['pledge_block_id'])) {
311 //set default to one time contribution.
312 $this->_defaults
['is_pledge'] = 0;
315 // to process Custom data that are appended to URL
316 $getDefaults = CRM_Core_BAO_CustomGroup
::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
317 $this->_defaults
= array_merge($this->_defaults
, $getDefaults);
319 $config = CRM_Core_Config
::singleton();
320 // set default country from config if no country set
321 if (empty($this->_defaults
["billing_country_id-{$this->_bltID}"])) {
322 $this->_defaults
["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
325 // set default state/province from config if no state/province set
326 if (empty($this->_defaults
["billing_state_province_id-{$this->_bltID}"])) {
327 $this->_defaults
["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
330 if ($this->_priceSetId
) {
331 if (($this->_useForMember
&& !empty($this->_currentMemberships
)) ||
$this->_defaultMemTypeId
) {
332 $selectedCurrentMemTypes = array();
333 foreach ($this->_priceSet
['fields'] as $key => $val) {
334 foreach ($val['options'] as $keys => $values) {
335 $opMemTypeId = CRM_Utils_Array
::value('membership_type_id', $values);
337 in_array($opMemTypeId, $this->_currentMemberships
) &&
338 !in_array($opMemTypeId, $selectedCurrentMemTypes)
340 if ($val['html_type'] == 'CheckBox') {
341 $this->_defaults
["price_{$key}"][$keys] = 1;
344 $this->_defaults
["price_{$key}"] = $keys;
346 $selectedCurrentMemTypes[] = $values['membership_type_id'];
348 elseif (!empty($values['is_default']) &&
350 (!isset($this->_defaults
["price_{$key}"]) ||
351 ($val['html_type'] == 'CheckBox' && !isset($this->_defaults
["price_{$key}"][$keys]))
354 if ($val['html_type'] == 'CheckBox') {
355 $this->_defaults
["price_{$key}"][$keys] = 1;
358 $this->_defaults
["price_{$key}"] = $keys;
365 CRM_Price_BAO_PriceSet
::setDefaultPriceSet($this, $this->_defaults
);
369 if (!empty($this->_paymentProcessors
)) {
370 foreach ($this->_paymentProcessors
as $pid => $value) {
371 if (!empty($value['is_default'])) {
372 $this->_defaults
['payment_processor'] = $pid;
377 return $this->_defaults
;
381 * Build the form object
385 public function buildQuickForm() {
386 // build profiles first so that we can determine address fields etc
387 // and then show copy address checkbox
388 $this->buildCustom($this->_values
['custom_pre_id'], 'customPre');
389 $this->buildCustom($this->_values
['custom_post_id'], 'customPost');
391 if (!empty($this->_fields
) && !empty($this->_values
['custom_pre_id'])) {
392 $profileAddressFields = array();
393 foreach ($this->_fields
as $key => $value) {
394 CRM_Core_BAO_UFField
::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $this->_values
['custom_pre_id']));
396 $this->set('profileAddressFields', $profileAddressFields);
399 // Build payment processor form
400 if (empty($_GET['onbehalf'])) {
401 CRM_Core_Payment_ProcessorForm
::buildQuickForm($this);
402 // Return if we are in an ajax callback
403 if ($this->_snippet
) {
408 $config = CRM_Core_Config
::singleton();
410 if ($this->_onbehalf
) {
411 CRM_Contribute_Form_Contribution_OnBehalfOf
::buildQuickForm($this);
412 // Return if we are in an ajax callback
413 if ($this->_snippet
) {
418 $this->applyFilter('__ALL__', 'trim');
419 $this->add('text', "email-{$this->_bltID}",
421 array('size' => 30, 'maxlength' => 60, 'class' => 'email'),
424 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
426 //@todo - this should be replaced by a check as to whether billing fields are set
427 $onlinePaymentProcessorEnabled = FALSE;
428 if (!empty($this->_paymentProcessors
)) {
429 foreach ($this->_paymentProcessors
as $key => $name) {
430 if ($name['billing_mode'] == 1) {
431 $onlinePaymentProcessorEnabled = TRUE;
433 $pps[$key] = $name['name'];
436 if (!empty($this->_values
['is_pay_later'])) {
437 $pps[0] = $this->_values
['pay_later_text'];
440 if (count($pps) > 1) {
441 $this->addRadio('payment_processor', ts('Payment Method'), $pps,
445 elseif (!empty($pps)) {
446 $key = array_keys($pps);
447 $key = array_pop($key);
448 $this->addElement('hidden', 'payment_processor', $key);
450 $this->assign('is_pay_later', $this->_values
['is_pay_later']);
451 $this->assign('pay_later_text', $this->_values
['pay_later_text']);
455 $contactID = $this->getContactID();
456 if ($this->getContactID() === '0') {
457 $this->addCidZeroOptions($onlinePaymentProcessorEnabled);
459 //build pledge block.
460 $this->_useForMember
= 0;
461 //don't build membership block when pledge_id is passed
462 if (empty($this->_values
['pledge_id'])) {
463 $this->_separateMembershipPayment
= FALSE;
464 if (in_array('CiviMember', $config->enableComponents
)) {
466 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
470 if ($this->_priceSetId
&&
471 (CRM_Core_Component
::getComponentID('CiviMember') == CRM_Utils_Array
::value('extends', $this->_priceSet
))
473 $this->_useForMember
= 1;
474 $this->set('useForMember', $this->_useForMember
);
477 $this->_separateMembershipPayment
= CRM_Member_BAO_Membership
::buildMembershipBlock($this,
479 $this->_membershipContactID
,
484 $this->set('separateMembershipPayment', $this->_separateMembershipPayment
);
486 $this->assign('useForMember', $this->_useForMember
);
487 // If we configured price set for contribution page
488 // we are not allow membership signup as well as any
489 // other contribution amount field, CRM-5095
490 if (isset($this->_priceSetId
) && $this->_priceSetId
) {
491 $this->add('hidden', 'priceSetId', $this->_priceSetId
);
492 // build price set form.
493 $this->set('priceSetId', $this->_priceSetId
);
494 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
495 if ($this->_values
['is_monetary'] &&
496 $this->_values
['is_recur'] && empty($this->_values
['pledge_id'])
498 self
::buildRecur($this);
502 if ($this->_priceSetId
) {
503 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
504 if ($is_quick_config) {
505 $this->_useForMember
= 0;
506 $this->set('useForMember', $this->_useForMember
);
510 if ($this->_values
['is_for_organization']) {
511 $this->buildOnBehalfOrganization();
514 //we allow premium for pledge during pledge creation only.
515 if (empty($this->_values
['pledge_id'])) {
516 CRM_Contribute_BAO_Premium
::buildPremiumBlock($this, $this->_id
, TRUE);
520 if ($this->_honor_block_is_active
) {
521 $this->assign('honor_block_is_active', TRUE);
523 //build soft-credit section
524 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
525 //build honoree profile section
526 CRM_Contact_Form_ProfileContact
::buildQuickForm($this);
529 //don't build pledge block when mid is passed
531 $config = CRM_Core_Config
::singleton();
532 if (in_array('CiviPledge', $config->enableComponents
) && !empty($this->_values
['pledge_block_id'])) {
533 CRM_Pledge_BAO_PledgeBlock
::buildPledgeBlock($this);
537 //to create an cms user
538 if (!$this->_contactID
) {
539 $createCMSUser = FALSE;
541 if ($this->_values
['custom_pre_id']) {
542 $profileID = $this->_values
['custom_pre_id'];
543 $createCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
546 if (!$createCMSUser &&
547 $this->_values
['custom_post_id']
549 if (!is_array($this->_values
['custom_post_id'])) {
550 $profileIDs = array($this->_values
['custom_post_id']);
553 $profileIDs = $this->_values
['custom_post_id'];
555 foreach ($profileIDs as $pid) {
556 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
558 $createCMSUser = TRUE;
564 if ($createCMSUser) {
565 CRM_Core_BAO_CMSUser
::buildForm($this, $profileID, TRUE);
569 if ($pcpSupporter = CRM_PCP_BAO_PCP
::displayName($this->_pcpId
)) {
570 $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter));
571 // 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.
572 $text = CRM_PCP_BAO_PCP
::getPcpBlockStatus($this->_id
, 'contribute');
574 $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!");
576 $this->assign('pcpSupporterText', $pcp_supporter_text);
578 $prms = array('id' => $this->_pcpId
);
579 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
580 if ($pcpInfo['is_honor_roll']) {
581 $this->assign('isHonor', TRUE);
582 $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL,
583 array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );")
585 $extraOption = array('onclick' => "return pcpAnonymous( );");
587 $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption);
588 $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption);
589 $this->addGroup($elements, 'pcp_is_anonymous', NULL, ' ');
590 $this->setDefaults(array('pcp_display_in_roll' => 1));
591 $this->setDefaults(array('pcp_is_anonymous' => 1));
593 $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30));
594 $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;'));
598 //we have to load confirm contribution button in template
599 //when multiple payment processor as the user
600 //can toggle with payment processor selection
601 $billingModePaymentProcessors = 0;
602 if (!empty($this->_paymentProcessors
)) {
603 foreach ($this->_paymentProcessors
as $key => $values) {
604 if ($values['billing_mode'] == CRM_Core_Payment
::BILLING_MODE_BUTTON
) {
605 $billingModePaymentProcessors++
;
610 if ($billingModePaymentProcessors && count($this->_paymentProcessors
) == $billingModePaymentProcessors) {
611 $allAreBillingModeProcessors = TRUE;
614 $allAreBillingModeProcessors = FALSE;
617 if (!($allAreBillingModeProcessors && !$this->_values
['is_pay_later'])) {
618 $submitButton = array(
620 'name' => CRM_Utils_Array
::value('is_confirm_enabled', $this->_values
) ?
ts('Confirm Contribution') : ts('Contribute'),
621 'spacing' => ' ',
624 // Add submit-once behavior when confirm page disabled
625 if (empty($this->_values
['is_confirm_enabled'])) {
626 $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name
. "','" . ts('Processing') . "');");
628 $this->addButtons(array($submitButton));
631 $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this);
635 * Build elements to enable pay on behalf of an organization.
637 public function buildOnBehalfOrganization() {
638 if ($this->_membershipContactID
) {
639 $entityBlock = array('contact_id' => $this->_membershipContactID
);
640 CRM_Core_BAO_Location
::getValues($entityBlock, $this->_defaults
);
643 if (!$this->_onBehalfRequired
) {
644 $this->addElement('checkbox', 'is_for_organization',
645 $this->_values
['for_organization'],
646 NULL, array('onclick' => "showOnBehalf( );")
650 $this->assign('is_for_organization', TRUE);
651 $this->assign('urlPath', 'civicrm/contribute/transact');
655 * Build elements to collect information for recurring contributions
658 * @param CRM_Core_Form $form
660 public static function buildRecur(&$form) {
661 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_ContributionRecur');
662 $className = get_class($form);
664 $form->assign('is_recur_interval', CRM_Utils_Array
::value('is_recur_interval', $form->_values
));
665 $form->assign('is_recur_installments', CRM_Utils_Array
::value('is_recur_installments', $form->_values
));
667 $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL);
669 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
670 $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']);
671 $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer');
674 // make sure frequency_interval is submitted as 1 if given no choice to user.
675 $form->add('hidden', 'frequency_interval', 1);
678 $frUnits = CRM_Utils_Array
::value('recur_frequency_unit', $form->_values
);
679 if (empty($frUnits) &&
680 $className == 'CRM_Contribute_Form_Contribution'
682 $frUnits = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
683 CRM_Core_OptionGroup
::values('recur_frequency_units')
687 $unitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $frUnits);
689 // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
690 if (count($unitVals) == 1) {
691 $form->assign('one_frequency_unit', TRUE);
692 $unit = $unitVals[0];
693 $form->add('hidden', 'frequency_unit', $unit);
694 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
697 $form->assign('frequency_unit', $unit);
700 $form->assign('one_frequency_unit', FALSE);
702 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
703 foreach ($unitVals as $key => $val) {
704 if (array_key_exists($val, $frequencyUnits)) {
705 $units[$val] = $frequencyUnits[$val];
706 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
707 $units[$val] = "{$frequencyUnits[$val]}(s)";
711 $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units);
714 // FIXME: Ideally we should freeze select box if there is only
715 // one option but looks there is some problem /w QF freeze.
716 //if ( count( $units ) == 1 ) {
717 //$frequencyUnit->freeze( );
720 $form->add('text', 'installments', ts('installments'),
721 $attributes['installments']
723 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
729 * @param array $fields
730 * The input form values.
731 * @param array $files
732 * The uploaded files if any.
737 * true if no errors, else array of errors
739 public static function formRule($fields, $files, $self) {
741 $amount = self
::computeAmount($fields, $self);
743 if ((!empty($fields['selectMembership']) &&
744 $fields['selectMembership'] != 'no_thanks'
746 (!empty($fields['priceSetId']) &&
750 $lifeMember = CRM_Member_BAO_Membership
::getAllContactMembership($self->_membershipContactID
, FALSE, TRUE);
752 $membershipOrgDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization();
754 $unallowedOrgs = array();
755 foreach (array_keys($lifeMember) as $memTypeId) {
756 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId];
760 //check for atleast one pricefields should be selected
761 if (!empty($fields['priceSetId'])) {
762 $priceField = new CRM_Price_DAO_PriceField();
763 $priceField->price_set_id
= $fields['priceSetId'];
764 $priceField->orderBy('weight');
768 $membershipIsActive = TRUE;
769 $previousId = $otherAmount = FALSE;
770 while ($priceField->fetch()) {
772 if ($self->_quickConfig
&& ($priceField->name
== 'contribution_amount' ||
$priceField->name
== 'membership_amount')) {
773 $previousId = $priceField->id
;
774 if ($priceField->name
== 'membership_amount' && !$priceField->is_active
) {
775 $membershipIsActive = FALSE;
778 if ($priceField->name
== 'other_amount') {
779 if ($self->_quickConfig
&& empty($fields["price_{$priceField->id}"]) &&
780 array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values
['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])
782 $otherAmount = $priceField->id
;
784 elseif (!empty($fields["price_{$priceField->id}"])) {
785 $otherAmountVal = CRM_Utils_Rule
::cleanMoney($fields["price_{$priceField->id}"]);
786 $min = CRM_Utils_Array
::value('min_amount', $self->_values
);
787 $max = CRM_Utils_Array
::value('max_amount', $self->_values
);
788 if ($min && $otherAmountVal < $min) {
789 $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1',
793 if ($max && $otherAmountVal > $max) {
794 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
800 if (!empty($fields["price_{$priceField->id}"]) ||
($previousId == $priceField->id
&& isset($fields["price_{$previousId}"])
801 && empty($fields["price_{$previousId}"]))
803 $check[] = $priceField->id
;
807 $currentMemberships = NULL;
808 if ($membershipIsActive) {
809 $is_test = $self->_mode
!= 'live' ?
1 : 0;
810 $memContactID = $self->_membershipContactID
;
812 // For anonymous user check using dedupe rule
813 // if user has Cancelled Membership
814 if (!$memContactID) {
815 $dedupeParams = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
816 $dedupeParams['check_permission'] = FALSE;
817 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
818 // if we find more than one contact, use the first one
819 $memContactID = CRM_Utils_Array
::value(0, $ids);
821 $currentMemberships = CRM_Member_BAO_Membership
::getContactsCancelledMembership($memContactID,
825 $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.';
826 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
827 if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array
::value('price_' . $fieldKey, $fields)) {
828 if (!is_array($fields['price_' . $fieldKey])) {
829 if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]])
830 && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)
832 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'])));
836 foreach ($fields['price_' . $fieldKey] as $key => $ignore) {
837 if (array_key_exists('membership_type_id', $fieldValue['options'][$key])
838 && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)
840 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$key]['membership_type_id'])));
849 if ($membershipIsActive && !$self->_membershipBlock
['is_required']
850 && $self->_values
['amount_block_is_active']
852 $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
853 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
854 // if 'No thank you' membership is selected then set $membershipFieldId
855 if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array
::value('price_' . $fieldKey, $fields) == 0) {
856 $membershipFieldId = $fieldKey;
858 elseif ($membershipFieldId) {
859 if ($fieldValue['name'] == 'other_amount') {
860 $otherFieldId = $fieldKey;
862 elseif ($fieldValue['name'] == 'contribution_amount') {
863 $contributionFieldId = $fieldKey;
866 if (!$errorKey || CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields) == '0') {
867 $errorKey = $fieldKey;
871 // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error
872 if ($membershipFieldId && !(CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) {
873 $errors["price_{$errorKey}"] = ts('Additional Contribution is required.');
877 if ($self->_useForMember
== 1 && $membershipIsActive) {
878 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
881 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
884 if ($otherAmount && !empty($check)) {
885 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
888 if ($self->_useForMember
== 1 && !empty($check) && $membershipIsActive) {
889 $priceFieldIDS = array();
890 $priceFieldMemTypes = array();
892 foreach ($self->_priceSet
['fields'] as $priceId => $value) {
893 if (!empty($fields['price_' . $priceId]) ||
($self->_quickConfig
&& $value['name'] == 'membership_amount' && empty($self->_membershipBlock
['is_required']))) {
894 if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) {
895 foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
897 $priceFieldIDS[] = $priceFldVal;
901 elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) {
902 // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
903 // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
904 $priceFieldIDS[] = $fields['price_' . $priceId];
907 if (!empty($value['options'])) {
908 foreach ($value['options'] as $val) {
909 if (!empty($val['membership_type_id']) && (
910 ($fields['price_' . $priceId] == $val['id']) ||
911 (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']]))
914 $priceFieldMemTypes[] = $val['membership_type_id'];
921 if (!empty($lifeMember)) {
922 foreach ($priceFieldIDS as $priceFieldId) {
923 if (($id = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) &&
924 in_array($membershipOrgDetails[$id], $unallowedOrgs)
926 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
932 if (!empty($priceFieldIDS)) {
933 $ids = implode(',', $priceFieldIDS);
935 $priceFieldIDS['id'] = $fields['priceSetId'];
936 $self->set('memberPriceFieldIDS', $priceFieldIDS);
937 $count = CRM_Price_BAO_PriceSet
::getMembershipCount($ids);
938 foreach ($count as $id => $occurance) {
939 if ($occurance > 1) {
940 $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.');
945 if (empty($priceFieldMemTypes)) {
946 $errors['_qf_default'] = ts('Please select at least one membership option.');
950 CRM_Price_BAO_PriceSet
::processAmount($self->_values
['fee'],
954 if ($fields['amount'] < 0) {
955 $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
957 $amount = $fields['amount'];
960 if (isset($fields['selectProduct']) &&
961 $fields['selectProduct'] != 'no_thanks'
963 $productDAO = new CRM_Contribute_DAO_Product();
964 $productDAO->id
= $fields['selectProduct'];
965 $productDAO->find(TRUE);
966 $min_amount = $productDAO->min_contribution
;
968 if ($amount < $min_amount) {
969 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money
::format($min_amount)));
970 CRM_Core_Session
::setStatus($errors['selectProduct']);
974 if (!empty($fields['is_recur'])) {
975 if ($fields['frequency_interval'] <= 0) {
976 $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).');
978 if ($fields['frequency_unit'] == '0') {
979 $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).');
983 if (!empty($fields['is_recur']) &&
984 CRM_Utils_Array
::value('payment_processor', $fields) == 0
986 $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
989 if (!empty($fields['is_for_organization']) &&
990 !property_exists($self, 'organizationName')
993 if (empty($fields['onbehalf']['organization_name'])) {
994 if (!empty($fields['org_option']) && !$fields['onbehalfof_id']) {
995 $errors['organization_id'] = ts('Please select an organization or enter a new one.');
997 elseif (empty($fields['org_option'])) {
998 $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.');
1002 foreach ($fields['onbehalf'] as $key => $value) {
1003 if (strstr($key, 'email')) {
1004 $emailLocType = explode('-', $key);
1007 if (empty($fields['onbehalf']["email-{$emailLocType[1]}"])) {
1008 $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.');
1012 // validate PCP fields - if not anonymous, we need a nick name value
1013 if ($self->_pcpId
&& !empty($fields['pcp_display_in_roll']) &&
1014 (CRM_Utils_Array
::value('pcp_is_anonymous', $fields) == 0) &&
1015 CRM_Utils_Array
::value('pcp_roll_nickname', $fields) == ''
1017 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
1020 // return if this is express mode
1021 $config = CRM_Core_Config
::singleton();
1022 if ($self->_paymentProcessor
&&
1023 $self->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_BUTTON
1025 if (!empty($fields[$self->_expressButtonName
. '_x']) ||
!empty($fields[$self->_expressButtonName
. '_y']) ||
1026 CRM_Utils_Array
::value($self->_expressButtonName
, $fields)
1032 //validate the pledge fields.
1033 if (!empty($self->_values
['pledge_block_id'])) {
1034 //validation for pledge payment.
1035 if (!empty($self->_values
['pledge_id'])) {
1036 if (empty($fields['pledge_amount'])) {
1037 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
1040 elseif (!empty($fields['is_pledge'])) {
1041 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_installments', $fields)) == FALSE) {
1042 $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
1045 if (CRM_Utils_Array
::value('pledge_installments', $fields) == NULL) {
1046 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
1048 elseif (CRM_Utils_array
::value('pledge_installments', $fields) == 1) {
1049 $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.');
1051 elseif (CRM_Utils_array
::value('pledge_installments', $fields) == 0) {
1052 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
1056 //validation for Pledge Frequency Interval.
1057 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_frequency_interval', $fields)) == FALSE) {
1058 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
1061 if (CRM_Utils_Array
::value('pledge_frequency_interval', $fields) == NULL) {
1062 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
1064 elseif (CRM_Utils_array
::value('pledge_frequency_interval', $fields) == 0) {
1065 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
1071 // also return if paylater mode
1072 if (CRM_Utils_Array
::value('payment_processor', $fields) == 0 && $self->_isBillingAddressRequiredForPayLater
== 0) {
1073 return empty($errors) ?
TRUE : $errors;
1076 // if the user has chosen a free membership or the amount is less than zero
1077 // i.e. we skip calling the payment processor and hence dont need credit card
1079 if ((float) $amount <= 0.0) {
1083 if (!empty($self->_paymentFields
)) {
1084 CRM_Core_Form
::validateMandatoryFields($self->_paymentFields
, $fields, $errors);
1086 CRM_Core_Payment_Form
::validateCreditCard($fields, $errors);
1088 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1089 if ($greetingType = CRM_Utils_Array
::value($greeting, $fields)) {
1090 $customizedValue = CRM_Core_OptionGroup
::getValue($greeting, 'Customized', 'name');
1091 if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
1092 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
1093 array(1 => ucwords(str_replace('_', " ", $greeting)))
1099 return empty($errors) ?
TRUE : $errors;
1103 * @param array $params
1104 * @param CRM_Core_Form $form
1106 * @return int|mixed|null|string
1108 public static function computeAmount(&$params, &$form) {
1111 // first clean up the other amount field if present
1112 if (isset($params['amount_other'])) {
1113 $params['amount_other'] = CRM_Utils_Rule
::cleanMoney($params['amount_other']);
1116 if (CRM_Utils_Array
::value('amount', $params) == 'amount_other_radio' ||
!empty($params['amount_other'])) {
1117 $amount = $params['amount_other'];
1119 elseif (!empty($params['pledge_amount'])) {
1121 foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
1122 $amount +
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
1126 if (!empty($form->_values
['amount'])) {
1127 $amountID = CRM_Utils_Array
::value('amount', $params);
1130 $params['amount_level'] = CRM_Utils_Array
::value('label', $form->_values
[$amountID]);
1131 $amount = CRM_Utils_Array
::value('value', $form->_values
[$amountID]);
1139 * Process the form submission
1144 public function postProcess() {
1145 $config = CRM_Core_Config
::singleton();
1146 // we first reset the confirm page so it accepts new values
1147 $this->controller
->resetPage('Confirm');
1149 // get the submitted form values.
1150 $params = $this->controller
->exportValues($this->_name
);
1152 //carry campaign from profile.
1153 if (array_key_exists('contribution_campaign_id', $params)) {
1154 $params['campaign_id'] = $params['contribution_campaign_id'];
1157 if (!empty($params['onbehalfof_id'])) {
1158 $params['organization_id'] = $params['onbehalfof_id'];
1161 $params['currencyID'] = $config->defaultCurrency
;
1163 if (!empty($params['priceSetId'])) {
1164 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1165 if ($is_quick_config) {
1166 $priceField = new CRM_Price_DAO_PriceField();
1167 $priceField->price_set_id
= $params['priceSetId'];
1168 $priceField->orderBy('weight');
1169 $priceField->find();
1171 $priceOptions = array();
1172 while ($priceField->fetch()) {
1173 CRM_Price_BAO_PriceFieldValue
::getValues($priceField->id
, $priceOptions);
1174 if ($selectedPriceOptionID = CRM_Utils_Array
::value("price_{$priceField->id}", $params)) {
1175 switch ($priceField->name
) {
1176 case 'membership_amount':
1177 $this->_params
['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array
::value('membership_type_id', $priceOptions[$selectedPriceOptionID]);
1178 $this->set('selectMembership', $params['selectMembership']);
1179 if (CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
) == 0) {
1180 $this->_values
['amount'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1184 case 'contribution_amount':
1185 $params['amount'] = $selectedPriceOptionID;
1186 $this->_values
['amount'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1187 $this->_values
[$selectedPriceOptionID]['value'] = CRM_Utils_Array
::value('amount', $priceOptions[$selectedPriceOptionID]);
1188 $this->_values
[$selectedPriceOptionID]['label'] = CRM_Utils_Array
::value('label', $priceOptions[$selectedPriceOptionID]);
1189 $this->_values
[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array
::value('id', $priceOptions[$selectedPriceOptionID]);
1190 $this->_values
[$selectedPriceOptionID]['weight'] = CRM_Utils_Array
::value('weight', $priceOptions[$selectedPriceOptionID]);
1193 case 'other_amount':
1194 $params['amount_other'] = $selectedPriceOptionID;
1202 if (($this->_values
['is_pay_later'] &&
1203 empty($this->_paymentProcessor
) &&
1204 !array_key_exists('hidden_processor', $params)) ||
1205 (!empty($params['payment_processor']) && $params['payment_processor'] == 0)
1207 $params['is_pay_later'] = 1;
1210 $params['is_pay_later'] = 0;
1213 $this->set('is_pay_later', $params['is_pay_later']);
1214 // assign pay later stuff
1215 $this->_params
['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, FALSE);
1216 $this->assign('is_pay_later', $params['is_pay_later']);
1217 if ($params['is_pay_later']) {
1218 $this->assign('pay_later_text', $this->_values
['pay_later_text']);
1219 $this->assign('pay_later_receipt', $this->_values
['pay_later_receipt']);
1222 // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
1223 $params['amount'] = self
::computeAmount($params, $this);
1224 $params['separate_amount'] = $params['amount'];
1226 if (!empty($params['selectMembership'])) {
1227 if (!empty($this->_membershipTypeValues
)) {
1228 $membershipTypeValues = $this->_membershipTypeValues
[$params['selectMembership']];
1231 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this,
1232 $params['selectMembership']
1235 $memFee = $membershipTypeValues['minimum_fee'];
1236 if (!$params['amount'] && !$this->_separateMembershipPayment
) {
1237 $params['amount'] = $memFee ?
$memFee : 0;
1240 //If the membership & contribution is used in contribution page & not separate payment
1241 $fieldId = $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL;
1242 $proceFieldAmount = 0;
1243 if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment
== 0) {
1244 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1245 if ($is_quick_config) {
1246 foreach ($this->_priceSet
['fields'] as $fieldKey => $fieldVal) {
1247 if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) {
1248 $fieldId = $fieldVal['id'];
1249 $fieldOption = $params['price_' . $fieldId];
1250 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1254 if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' ||
$fieldVal['name'] == 'contribution_amount')) {
1255 $fieldId = $fieldVal['id'];
1256 if ($fieldVal['name'] == 'other_amount') {
1257 $proceFieldAmount +
= $this->_submitValues
['price_' . $fieldId];
1259 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues
['price_' . $fieldId] > 0) {
1260 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1262 unset($params['price_' . $fieldId]);
1270 if (!isset($params['amount_other'])) {
1271 $this->set('amount_level', CRM_Utils_Array
::value('amount_level', $params));
1274 if ($priceSetId = CRM_Utils_Array
::value('priceSetId', $params)) {
1275 $lineItem = array();
1276 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
1277 if ($is_quick_config) {
1278 foreach ($this->_values
['fee'] as $key => & $val) {
1279 if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && array_key_exists('price_' . $key, $params)) {
1280 $params['price_' . $key] = CRM_Utils_Rule
::cleanMoney($params['price_' . $key]); //Clean out any currency symbols
1281 if ($params['price_' . $key] != 0) {
1282 foreach ($val['options'] as $optionKey => & $options) {
1283 $options['amount'] = CRM_Utils_Array
::value('price_' . $key, $params);
1287 $params['price_' . $key] = 1;
1293 if ($this->_membershipBlock
) {
1294 $component = 'membership';
1297 CRM_Price_BAO_PriceSet
::processAmount($this->_values
['fee'], $params, $lineItem[$priceSetId], $component);
1298 if ($params['tax_amount']) {
1299 $this->set('tax_amount', $params['tax_amount']);
1302 if ($proceFieldAmount) {
1303 $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
1304 $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
1305 if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
1306 $proceFieldAmount +
= $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
1308 if (!$this->_membershipBlock
['is_separate_payment']) {
1309 $params['amount'] = $proceFieldAmount; //require when separate membership not used
1312 $this->set('lineItem', $lineItem);
1315 if ($this->_membershipBlock
['is_separate_payment'] && !empty($params['separate_amount'])) {
1316 $this->set('amount', $params['separate_amount']);
1319 $this->set('amount', $params['amount']);
1322 // generate and set an invoiceID for this transaction
1323 $invoiceID = md5(uniqid(rand(), TRUE));
1324 $this->set('invoiceID', $invoiceID);
1326 // required only if is_monetary and valid positive amount
1327 if ($this->_values
['is_monetary'] &&
1328 is_array($this->_paymentProcessor
) &&
1329 ((float ) $params['amount'] > 0.0 ||
$memFee > 0.0)
1332 // default mode is direct
1333 $this->set('contributeMode', 'direct');
1335 if ($this->_paymentProcessor
&&
1336 $this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_BUTTON
1338 //get the button name
1339 $buttonName = $this->controller
->getButtonName();
1340 if (in_array($buttonName,
1341 array($this->_expressButtonName
, $this->_expressButtonName
. '_x', $this->_expressButtonName
. '_y')
1342 ) && empty($params['is_pay_later'])
1344 $this->set('contributeMode', 'express');
1346 $donateURL = CRM_Utils_System
::url('civicrm/contribute', '_qf_Contribute_display=1');
1347 $params['cancelURL'] = CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1348 $params['returnURL'] = CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1349 $params['invoiceID'] = $invoiceID;
1350 $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo
['title']) ?
$this->_pcpInfo
['title'] : $this->_values
['title']);
1352 //default action is Sale
1353 $params['payment_action'] = 'Sale';
1355 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this);
1356 $token = $payment->setExpressCheckout($params);
1357 if (is_a($token, 'CRM_Core_Error')) {
1358 CRM_Core_Error
::displaySessionError($token);
1359 CRM_Utils_System
::redirect($params['cancelURL']);
1362 $this->set('token', $token);
1364 $paymentURL = $this->_paymentProcessor
['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token";
1365 CRM_Utils_System
::redirect($paymentURL);
1368 elseif ($this->_paymentProcessor
&&
1369 $this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_NOTIFY
1371 $this->set('contributeMode', 'notify');
1375 // should we skip the confirm page?
1376 if (empty($this->_values
['is_confirm_enabled'])) {
1377 // call the post process hook for the main page before we switch to confirm
1378 $this->postProcessHook();
1380 // build the confirm page
1381 $confirmForm = &$this->controller
->_pages
['Confirm'];
1382 $confirmForm->preProcess();
1383 $confirmForm->buildQuickForm();
1385 // the confirmation page is valid
1386 $data = &$this->controller
->container();
1387 $data['valid']['Confirm'] = 1;
1389 // confirm the contribution
1390 // mainProcess calls the hook also
1391 $confirmForm->mainProcess();
1392 $qfKey = $this->controller
->_key
;
1394 // redirect to thank you page
1395 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE));
1401 * Handle Payment Processor switching
1402 * For contribution and event registration forms
1403 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form
1404 * @param bool $noFees
1406 public static function preProcessPaymentOptions(&$form, $noFees = FALSE) {
1407 $form->_snippet
= CRM_Utils_Array
::value('snippet', $_GET);
1409 $form->_paymentProcessors
= $noFees ?
array() : $form->get('paymentProcessors');
1410 $form->_paymentProcessorID
= NULL;
1411 if ($form->_paymentProcessors
) {
1412 // Fetch type during ajax request
1413 if (isset($_GET['type']) && $form->_snippet
) {
1414 $form->_paymentProcessorID
= CRM_Utils_Request
::retrieve('type', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, 0);
1416 // Remember type during form post
1417 elseif (!empty($form->_submitValues
)) {
1418 $form->_paymentProcessorID
= CRM_Utils_Array
::value('payment_processor', $form->_submitValues
);
1419 $form->_paymentProcessor
= CRM_Utils_Array
::value($form->_paymentProcessorID
, $form->_paymentProcessors
);
1420 $form->set('type', $form->_paymentProcessorID
);
1421 $form->set('mode', $form->_mode
);
1422 $form->set('paymentProcessor', $form->_paymentProcessor
);
1424 // Set default payment processor
1426 foreach ($form->_paymentProcessors
as $values) {
1427 if (!empty($values['is_default']) ||
count($form->_paymentProcessors
) == 1) {
1428 $form->_paymentProcessorID
= $values['id'];
1433 if ($form->_paymentProcessorID
) {
1434 CRM_Core_Payment_ProcessorForm
::preProcess($form);
1437 $form->_paymentProcessor
= array();
1440 //get payPal express id and make it available to template
1441 foreach ($form->_paymentProcessors
as $ppId => $values) {
1442 $payPalExpressId = ($values['payment_processor_type'] == 'PayPal_Express') ?
$values['id'] : 0;
1443 $form->assign('payPalExpressId', $payPalExpressId);
1444 if ($payPalExpressId) {
1448 if (!$form->_snippet
) {
1449 // Add JS to show icons for the accepted credit cards
1450 $creditCardTypes = CRM_Core_Payment_Form
::getCreditCardCSSNames();
1451 CRM_Core_Resources
::singleton()
1452 ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10)
1453 // workaround for CRM-13634
1454 // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
1455 ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';');
1458 $form->assign('paymentProcessorID', $form->_paymentProcessorID
);