3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a Contribution.
21 class CRM_Contribute_Form_Contribution_Main
extends CRM_Contribute_Form_ContributionBase
{
24 * Define default MembershipType Id.
27 public $_defaultMemTypeId;
29 public $_paymentProcessors;
31 public $_membershipTypeValues;
33 public $_useForMember;
36 * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl
39 public $_paymentFields = [];
41 protected $_paymentProcessorID;
45 * Get the active UFGroups (profiles) on this form
46 * Many forms load one or more UFGroups (profiles).
47 * This provides a standard function to retrieve the IDs of those profiles from the form
48 * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
50 * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
54 public function getUFGroupIDs() {
56 if (!empty($this->_values
['custom_pre_id'])) {
57 $ufGroupIDs[] = $this->_values
['custom_pre_id'];
59 if (!empty($this->_values
['custom_post_id'])) {
60 // custom_post_id can be an array (because we can have multiple for events).
61 // It is handled as array for contribution page as well though they don't support multiple profiles.
62 if (!is_array($this->_values
['custom_post_id'])) {
63 $ufGroupIDs[] = $this->_values
['custom_post_id'];
66 $ufGroupIDs = array_merge($ufGroupIDs, $this->_values
['custom_post_id']);
74 * Set variables up before form is built.
76 public function preProcess() {
79 $this->_paymentProcessors
= $this->get('paymentProcessors');
80 $this->preProcessPaymentOptions();
82 $this->assignFormVariablesByContributionID();
84 // Make the contributionPageID available to the template
85 $this->assign('contributionPageID', $this->_id
);
86 $this->assign('ccid', $this->_ccid
);
87 $this->assign('isShare', CRM_Utils_Array
::value('is_share', $this->_values
));
88 $this->assign('isConfirmEnabled', CRM_Utils_Array
::value('is_confirm_enabled', $this->_values
));
90 // Required for currency formatting in the JS layer
91 // this is a temporary fix intended to resolve a regression quickly
92 // And assigning moneyFormat for js layer formatting
93 // will only work until that is done.
94 // https://github.com/civicrm/civicrm-core/pull/19151
95 $this->assign('moneyFormat', CRM_Utils_Money
::format(1234.56));
97 $this->assign('reset', CRM_Utils_Request
::retrieve('reset', 'Boolean'));
98 $this->assign('mainDisplay', CRM_Utils_Request
::retrieve('_qf_Main_display', 'Boolean',
99 CRM_Core_DAO
::$_nullObject));
101 if (!empty($this->_pcpInfo
['id']) && !empty($this->_pcpInfo
['intro_text'])) {
102 $this->assign('intro_text', $this->_pcpInfo
['intro_text']);
104 elseif (!empty($this->_values
['intro_text'])) {
105 $this->assign('intro_text', $this->_values
['intro_text']);
108 $qParams = "reset=1&id={$this->_id}";
109 if ($pcpId = CRM_Utils_Array
::value('pcp_id', $this->_pcpInfo
)) {
110 $qParams .= "&pcpId={$pcpId}";
112 $this->assign('qParams', $qParams);
114 if (!empty($this->_values
['footer_text'])) {
115 $this->assign('footer_text', $this->_values
['footer_text']);
120 * Set the default values.
122 public function setDefaultValues() {
123 // check if the user is registered and we have a contact ID
124 $contactID = $this->getContactID();
126 if (!empty($contactID)) {
128 $removeCustomFieldTypes = ['Contribution', 'Membership'];
129 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields();
131 // remove component related fields
132 foreach ($this->_fields
as $name => $fieldInfo) {
133 //don't set custom data Used for Contribution (CRM-1344)
134 if (substr($name, 0, 7) == 'custom_') {
135 $id = substr($name, 7);
136 if (!CRM_Core_BAO_CustomGroup
::checkCustomField($id, $removeCustomFieldTypes)) {
139 // ignore component fields
141 elseif (array_key_exists($name, $contribFields) ||
(substr($name, 0, 11) == 'membership_') ||
(substr($name, 0, 13) == 'contribution_')) {
144 $fields[$name] = $fieldInfo;
147 if (!empty($fields)) {
148 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactID, $fields, $this->_defaults
);
151 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
152 $this->_defaults
= array_merge($this->_defaults
, $billingDefaults);
154 if (!empty($this->_ccid
) && !empty($this->_pendingAmount
)) {
155 $this->_defaults
['total_amount'] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($this->_pendingAmount
);
159 * hack to simplify credit card entry for testing
161 * $this->_defaults['credit_card_type'] = 'Visa';
162 * $this->_defaults['amount'] = 168;
163 * $this->_defaults['credit_card_number'] = '4111111111111111';
164 * $this->_defaults['cvv2'] = '000';
165 * $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
166 * // hack to simplify direct debit entry for testing
167 * $this->_defaults['account_holder'] = 'Max Müller';
168 * $this->_defaults['bank_account_number'] = '12345678';
169 * $this->_defaults['bank_identification_number'] = '12030000';
170 * $this->_defaults['bank_name'] = 'Bankname';
173 //build set default for pledge overdue payment.
174 if (!empty($this->_values
['pledge_id'])) {
175 //used to record completed pledge payment ids used later for honor default
176 $completedContributionIds = [];
177 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($this->_values
['pledge_id']);
181 foreach ($pledgePayments as $payId => $value) {
182 if ($value['status'] == 'Overdue') {
183 $this->_defaults
['pledge_amount'][$payId] = 1;
184 $paymentAmount +
= $value['scheduled_amount'];
186 elseif (!$duePayment && $value['status'] == 'Pending') {
187 $this->_defaults
['pledge_amount'][$payId] = 1;
188 $paymentAmount +
= $value['scheduled_amount'];
191 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
192 $completedContributionIds[] = $value['contribution_id'];
195 $this->_defaults
['price_' . $this->_priceSetId
] = $paymentAmount;
197 if (count($completedContributionIds)) {
199 foreach ($completedContributionIds as $id) {
200 $softCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($id);
202 if (isset($softCredit['soft_credit'])) {
203 $this->_defaults
['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
205 //since honoree profile fieldname of fields are prefixed with 'honor'
206 //we need to reformat the fieldname to append prefix during setting default values
207 CRM_Core_BAO_UFGroup
::setProfileDefaults(
208 $softCredit['soft_credit'][1]['contact_id'],
209 CRM_Core_BAO_UFGroup
::getFields($this->_honoreeProfileId
),
212 foreach ($defaults as $fieldName => $value) {
213 $this->_defaults
['honor[' . $fieldName . ']'] = $value;
218 elseif (!empty($this->_values
['pledge_block_id'])) {
219 //set default to one time contribution.
220 $this->_defaults
['is_pledge'] = 0;
223 // to process Custom data that are appended to URL
224 $getDefaults = CRM_Core_BAO_CustomGroup
::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
225 $this->_defaults
= array_merge($this->_defaults
, $getDefaults);
227 $config = CRM_Core_Config
::singleton();
228 // set default country from config if no country set
229 if (empty($this->_defaults
["billing_country_id-{$this->_bltID}"])) {
230 $this->_defaults
["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
233 // set default state/province from config if no state/province set
234 if (empty($this->_defaults
["billing_state_province_id-{$this->_bltID}"])) {
235 $this->_defaults
["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
238 $entityId = $memtypeID = NULL;
239 if ($this->_priceSetId
) {
240 if (($this->_useForMember
&& !empty($this->_currentMemberships
)) ||
$this->_defaultMemTypeId
) {
241 $selectedCurrentMemTypes = [];
242 foreach ($this->_priceSet
['fields'] as $key => $val) {
243 foreach ($val['options'] as $keys => $values) {
244 $opMemTypeId = $values['membership_type_id'] ??
NULL;
245 $priceFieldName = 'price_' . $values['price_field_id'];
246 $priceFieldValue = CRM_Price_BAO_PriceSet
::getPriceFieldValueFromURL($this, $priceFieldName);
247 if (!empty($priceFieldValue)) {
248 CRM_Price_BAO_PriceSet
::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults
);
249 // break here to prevent overwriting of default due to 'is_default'
250 // option configuration or setting of current membership or
251 // membership for related organization.
252 // The value sent via URL get's higher priority.
255 elseif ($opMemTypeId &&
256 in_array($opMemTypeId, $this->_currentMemberships
) &&
257 !in_array($opMemTypeId, $selectedCurrentMemTypes)
259 CRM_Price_BAO_PriceSet
::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults
);
260 $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id'];
262 elseif (!empty($values['is_default']) && !$opMemTypeId && (!isset($this->_defaults
[$priceFieldName]) ||
263 ($val['html_type'] == 'CheckBox' && !isset($this->_defaults
[$priceFieldName][$keys])))) {
264 CRM_Price_BAO_PriceSet
::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults
);
265 $memtypeID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults
[$priceFieldName], 'membership_type_id');
269 $entityId = CRM_Utils_Array
::value('id', CRM_Member_BAO_Membership
::getContactMembership($contactID, $memtypeID, NULL));
272 CRM_Price_BAO_PriceSet
::setDefaultPriceSet($this, $this->_defaults
);
276 //set custom field defaults set by admin if value is not set
277 if (!empty($this->_fields
)) {
278 //load default campaign from page.
279 if (array_key_exists('contribution_campaign_id', $this->_fields
)) {
280 $this->_defaults
['contribution_campaign_id'] = $this->_values
['campaign_id'] ??
NULL;
283 //set custom field defaults
284 foreach ($this->_fields
as $name => $field) {
285 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
286 if (!isset($this->_defaults
[$name])) {
287 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID, $name, $this->_defaults
,
288 $entityId, CRM_Profile_Form
::MODE_REGISTER
295 if (!empty($this->_paymentProcessors
)) {
296 foreach ($this->_paymentProcessors
as $pid => $value) {
297 if (!empty($value['is_default'])) {
298 $this->_defaults
['payment_processor_id'] = $pid;
303 return $this->_defaults
;
307 * Build the form object.
309 public function buildQuickForm() {
310 // build profiles first so that we can determine address fields etc
311 // and then show copy address checkbox
312 if (empty($this->_ccid
)) {
313 $this->buildCustom($this->_values
['custom_pre_id'], 'customPre');
314 $this->buildCustom($this->_values
['custom_post_id'], 'customPost');
316 // CRM-18399: used by template to pass pre profile id as a url arg
317 $this->assign('custom_pre_id', $this->_values
['custom_pre_id']);
319 $this->buildComponentForm($this->_id
, $this);
322 if (\Civi
::settings()->get('forceRecaptcha')) {
323 if (!$this->_userID
) {
324 CRM_Utils_ReCAPTCHA
::enableCaptchaOnForm($this);
328 // Build payment processor form
329 CRM_Core_Payment_ProcessorForm
::buildQuickForm($this);
331 $config = CRM_Core_Config
::singleton();
333 $contactID = $this->getContactID();
335 $this->assign('contact_id', $contactID);
336 $this->assign('display_name', CRM_Contact_BAO_Contact
::displayName($contactID));
339 $this->applyFilter('__ALL__', 'trim');
340 if (empty($this->_ccid
)) {
341 if ($this->_emailExists
== FALSE) {
342 $this->add('text', "email-{$this->_bltID}",
344 ['size' => 30, 'maxlength' => 60, 'class' => 'email'],
347 $this->assign('showMainEmail', TRUE);
348 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
352 $this->addElement('hidden', "email-{$this->_bltID}", 1);
353 $this->add('text', 'total_amount', ts('Total Amount'), ['readonly' => TRUE], FALSE);
356 $this->addPaymentProcessorFieldsToForm();
357 $this->assign('is_pay_later', $this->getCurrentPaymentProcessor() === 0 && $this->_values
['is_pay_later']);
358 $this->assign('pay_later_text', $this->getCurrentPaymentProcessor() === 0 ?
$this->getPayLaterLabel() : NULL);
360 if ($contactID === 0) {
361 $this->addCidZeroOptions();
365 //build pledge block.
366 $this->_useForMember
= 0;
367 //don't build membership block when pledge_id is passed
368 if (empty($this->_values
['pledge_id']) && empty($this->_ccid
)) {
369 $this->_separateMembershipPayment
= FALSE;
370 if (CRM_Core_Component
::isEnabled('CiviMember')) {
372 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
376 if ($this->_priceSetId
&&
377 (CRM_Core_Component
::getComponentID('CiviMember') == CRM_Utils_Array
::value('extends', $this->_priceSet
))
379 $this->_useForMember
= 1;
380 $this->set('useForMember', $this->_useForMember
);
383 $this->_separateMembershipPayment
= $this->buildMembershipBlock(
384 $this->_membershipContactID
,
389 $this->set('separateMembershipPayment', $this->_separateMembershipPayment
);
391 $this->assign('useForMember', $this->_useForMember
);
392 // If we configured price set for contribution page
393 // we are not allow membership signup as well as any
394 // other contribution amount field, CRM-5095
395 if (!empty($this->_priceSetId
)) {
396 $this->add('hidden', 'priceSetId', $this->_priceSetId
);
397 // build price set form.
398 $this->set('priceSetId', $this->_priceSetId
);
399 if (empty($this->_ccid
)) {
400 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
402 if ($this->_values
['is_monetary'] &&
403 $this->_values
['is_recur'] && empty($this->_values
['pledge_id'])
405 self
::buildRecur($this);
409 if ($this->_priceSetId
&& empty($this->_ccid
)) {
410 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
411 if ($is_quick_config) {
412 $this->_useForMember
= 0;
413 $this->set('useForMember', $this->_useForMember
);
417 //we allow premium for pledge during pledge creation only.
418 if (empty($this->_values
['pledge_id']) && empty($this->_ccid
)) {
419 CRM_Contribute_BAO_Premium
::buildPremiumBlock($this, $this->_id
, TRUE);
422 //don't build pledge block when mid is passed
423 if (!$this->_mid
&& empty($this->_ccid
)) {
424 if (CRM_Core_Component
::isEnabled('CiviPledge') && !empty($this->_values
['pledge_block_id'])) {
425 CRM_Pledge_BAO_PledgeBlock
::buildPledgeBlock($this);
429 //to create an cms user
430 if (!$this->_contactID
&& empty($this->_ccid
)) {
431 $createCMSUser = FALSE;
433 if ($this->_values
['custom_pre_id']) {
434 $profileID = $this->_values
['custom_pre_id'];
435 $createCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
438 if (!$createCMSUser &&
439 $this->_values
['custom_post_id']
441 if (!is_array($this->_values
['custom_post_id'])) {
442 $profileIDs = [$this->_values
['custom_post_id']];
445 $profileIDs = $this->_values
['custom_post_id'];
447 foreach ($profileIDs as $pid) {
448 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
450 $createCMSUser = TRUE;
456 if ($createCMSUser) {
457 CRM_Core_BAO_CMSUser
::buildForm($this, $profileID, TRUE);
460 if ($this->_pcpId
&& empty($this->_ccid
)) {
461 if (CRM_PCP_BAO_PCP
::displayName($this->_pcpId
)) {
462 $pcp_supporter_text = CRM_PCP_BAO_PCP
::getPcpSupporterText($this->_pcpId
, $this->_id
, 'contribute');
463 $this->assign('pcpSupporterText', $pcp_supporter_text);
465 $prms = ['id' => $this->_pcpId
];
466 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
467 if ($pcpInfo['is_honor_roll']) {
468 $this->assign('isHonor', TRUE);
469 $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL,
470 ['onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );"]
472 $extraOption = ['onclick' => "return pcpAnonymous( );"];
473 $this->addRadio('pcp_is_anonymous', NULL, [ts('Include my name and message'), ts('List my contribution anonymously')], [], ' ', FALSE, [$extraOption, $extraOption]);
475 $this->add('text', 'pcp_roll_nickname', ts('Name'), ['maxlength' => 30]);
476 $this->addField('pcp_personal_note', ['entity' => 'ContributionSoft', 'context' => 'create', 'style' => 'height: 3em; width: 40em;']);
479 if (empty($this->_values
['fee']) && empty($this->_ccid
)) {
480 throw new CRM_Core_Exception(ts('This page does not have any price fields configured or you may not have permission for them. Please contact the site administrator for more details.'));
483 //we have to load confirm contribution button in template
484 //when multiple payment processor as the user
485 //can toggle with payment processor selection
486 $billingModePaymentProcessors = 0;
487 if (!empty($this->_paymentProcessors
)) {
488 foreach ($this->_paymentProcessors
as $key => $values) {
489 if ($values['billing_mode'] == CRM_Core_Payment
::BILLING_MODE_BUTTON
) {
490 $billingModePaymentProcessors++
;
495 if ($billingModePaymentProcessors && count($this->_paymentProcessors
) == $billingModePaymentProcessors) {
496 $allAreBillingModeProcessors = TRUE;
499 $allAreBillingModeProcessors = FALSE;
502 if (!($allAreBillingModeProcessors && !$this->_values
['is_pay_later'])) {
505 'name' => ts('Contribute'),
506 'spacing' => ' ',
509 if (!empty($this->_values
['is_confirm_enabled'])) {
510 $submitButton['name'] = ts('Review your contribution');
511 $submitButton['icon'] = 'fa-chevron-right';
513 // Add submit-once behavior when confirm page disabled
514 if (empty($this->_values
['is_confirm_enabled'])) {
515 $this->submitOnce
= TRUE;
517 //change button name for updating contribution
518 if (!empty($this->_ccid
)) {
519 $submitButton['name'] = ts('Confirm Payment');
521 $this->addButtons([$submitButton]);
524 $this->addFormRule(['CRM_Contribute_Form_Contribution_Main', 'formRule'], $this);
528 * Build Membership Block in Contribution Pages.
529 * @todo this was shared on CRM_Contribute_Form_ContributionBase but we are refactoring and simplifying for each
530 * step (main/confirm/thankyou)
533 * Contact checked for having a current membership for a particular membership.
534 * @param int|array $selectedMembershipTypeID
535 * Selected membership id.
536 * @param null $isTest
539 * Is this a separate membership payment
541 * @throws \CiviCRM_API3_Exception
542 * @throws \CRM_Core_Exception
544 private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) {
545 $separateMembershipPayment = FALSE;
546 $this->addOptionalQuickFormElement('auto_renew');
547 if ($this->_membershipBlock
) {
548 $this->_currentMemberships
= [];
550 $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = [];
551 $membershipPriceset = (!empty($this->_priceSetId
) && $this->_useForMember
);
553 $allowAutoRenewMembership = $autoRenewOption = FALSE;
554 $autoRenewMembershipTypeOptions = [];
556 $separateMembershipPayment = $this->_membershipBlock
['is_separate_payment'] ??
NULL;
558 if ($membershipPriceset) {
559 foreach ($this->_priceSet
['fields'] as $pField) {
560 if (empty($pField['options'])) {
563 foreach ($pField['options'] as $opId => $opValues) {
564 if (empty($opValues['membership_type_id'])) {
567 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
571 elseif (!empty($this->_membershipBlock
['membership_types'])) {
572 $membershipTypeIds = explode(',', $this->_membershipBlock
['membership_types']);
575 if (!empty($membershipTypeIds)) {
576 //set status message if wrong membershipType is included in membershipBlock
577 if (isset($this->_mid
) && !$membershipPriceset) {
578 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
582 if (!in_array($membershipTypeID, $membershipTypeIds)) {
583 CRM_Core_Session
::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error');
587 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, $membershipTypeIds);
588 $this->_membershipTypeValues
= $membershipTypeValues;
591 // Check if we support auto-renew on this contribution page
592 // FIXME: If any of the payment processors do NOT support recurring you cannot setup an
593 // auto-renew payment even if that processor is not selected.
594 $allowAutoRenewOpt = TRUE;
595 if (is_array($this->_paymentProcessors
)) {
596 foreach ($this->_paymentProcessors
as $id => $val) {
597 if ($id && !$val['is_recur']) {
598 $allowAutoRenewOpt = FALSE;
602 foreach ($membershipTypeIds as $value) {
603 $memType = $membershipTypeValues[$value];
604 if ($selectedMembershipTypeID != NULL) {
605 if ($memType['id'] == $selectedMembershipTypeID) {
606 $this->assign('minimum_fee', $memType['minimum_fee'] ??
NULL);
607 $this->assign('membership_name', $memType['name']);
609 $membership = new CRM_Member_DAO_Membership();
610 $membership->contact_id
= $cid;
611 $membership->membership_type_id
= $memType['id'];
612 if ($membership->find(TRUE)) {
613 $this->assign('renewal_mode', TRUE);
614 $memType['current_membership'] = $membership->end_date
;
615 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
618 $membershipTypes[] = $memType;
621 elseif ($memType['is_active']) {
623 if ($allowAutoRenewOpt) {
624 $javascriptMethod = ['onclick' => "return showHideAutoRenew( this.value );"];
625 $isAvailableAutoRenew = $this->_membershipBlock
['auto_renew'][$value] ??
1;
626 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $memType['auto_renew'] * $isAvailableAutoRenew;
627 $allowAutoRenewMembership = TRUE;
630 $javascriptMethod = NULL;
631 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = 0;
634 //add membership type.
635 $radio[$memType['id']] = NULL;
636 $radioOptAttrs[$memType['id']] = $javascriptMethod;
638 $membership = new CRM_Member_DAO_Membership();
639 $membership->contact_id
= $cid;
640 $membership->membership_type_id
= $memType['id'];
642 //show current membership, skip pending and cancelled membership records,
643 //because we take first membership record id for renewal
644 $membership->whereAdd('status_id != 5 AND status_id !=6');
646 if (!is_null($isTest)) {
647 $membership->is_test
= $isTest;
651 $membership->orderBy('end_date DESC');
653 if ($membership->find(TRUE)) {
654 if (!$membership->end_date
) {
655 unset($radio[$memType['id']]);
656 unset($radioOptAttrs[$memType['id']]);
657 $this->assign('islifetime', TRUE);
660 $this->assign('renewal_mode', TRUE);
661 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
662 $memType['current_membership'] = $membership->end_date
;
664 $endDate = $memType['current_membership'];
665 $this->_defaultMemTypeId
= $memType['id'];
667 if ($memType['current_membership'] < $endDate) {
668 $endDate = $memType['current_membership'];
669 $this->_defaultMemTypeId
= $memType['id'];
673 $membershipTypes[] = $memType;
678 $this->assign('membershipBlock', $this->_membershipBlock
);
679 $this->assign('showRadio', TRUE);
680 $this->assign('membershipTypes', $membershipTypes);
681 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
682 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
683 //give preference to user submitted auto_renew value.
684 $takeUserSubmittedAutoRenew = (!empty($_POST) ||
$this->isSubmitted());
685 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
687 // Assign autorenew option (0:hide,1:optional,2:required) so we can use it in confirmation etc.
688 $autoRenewOption = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($this->_priceSetId
);
689 //$selectedMembershipTypeID is retrieved as an array for membership priceset if multiple
690 //options for different organisation is selected on the contribution page.
691 if (is_numeric($selectedMembershipTypeID) && isset($membershipTypeValues[$selectedMembershipTypeID]['auto_renew'])) {
692 $this->assign('autoRenewOption', $membershipTypeValues[$selectedMembershipTypeID]['auto_renew']);
695 $this->assign('autoRenewOption', $autoRenewOption);
698 if (!$membershipPriceset) {
699 if (!$this->_membershipBlock
['is_required']) {
700 $this->assign('showRadioNoThanks', TRUE);
701 $radio['no_thanks'] = NULL;
702 $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
704 elseif ($this->_membershipBlock
['is_required'] && count($radio) == 1) {
705 $temp = array_keys($radio);
706 $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']);
707 $this->assign('singleMembership', TRUE);
708 $this->assign('showRadio', FALSE);
711 foreach ($radioOptAttrs as $opt => $attrs) {
712 $attrs['class'] = ' required';
714 $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
717 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
720 if ((!$this->_values
['is_pay_later'] ||
is_array($this->_paymentProcessors
)) && ($allowAutoRenewMembership ||
$autoRenewOption)) {
721 if ($autoRenewOption == 2) {
722 $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
725 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
731 return $separateMembershipPayment;
735 * Build elements to collect information for recurring contributions.
738 * @param CRM_Core_Form $form
740 public static function buildRecur(&$form) {
741 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_ContributionRecur');
742 $className = get_class($form);
744 $form->assign('is_recur_interval', CRM_Utils_Array
::value('is_recur_interval', $form->_values
));
745 $form->assign('is_recur_installments', CRM_Utils_Array
::value('is_recur_installments', $form->_values
));
746 $paymentObject = $form->getVar('_paymentObject');
747 if ($paymentObject) {
748 $form->assign('recurringHelpText', $paymentObject->getText('contributionPageRecurringHelp', [
749 'is_recur_installments' => !empty($form->_values
['is_recur_installments']),
750 'is_email_receipt' => !empty($form->_values
['is_email_receipt']),
754 $frUnits = $form->_values
['recur_frequency_unit'] ??
NULL;
755 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units', FALSE, FALSE, TRUE);
756 if (empty($frUnits) &&
757 $className == 'CRM_Contribute_Form_Contribution'
759 $frUnits = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
760 CRM_Core_OptionGroup
::values('recur_frequency_units', FALSE, FALSE, FALSE, NULL, 'value')
764 $unitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $frUnits);
766 // FIXME: Ideally we should freeze select box if there is only
767 // one option but looks there is some problem /w QF freeze.
768 //if ( count( $units ) == 1 ) {
769 //$frequencyUnit->freeze( );
772 $form->add('text', 'installments', ts('installments'),
773 $attributes['installments']
775 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
777 $is_recur_label = ts('I want to contribute this amount every');
779 // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
780 if (count($unitVals) == 1) {
781 $form->assign('one_frequency_unit', TRUE);
782 $form->add('hidden', 'frequency_unit', $unitVals[0]);
783 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
784 $unit = CRM_Contribute_BAO_Contribution
::getUnitLabelWithPlural($unitVals[0]);
785 $form->assign('frequency_unit', $unit);
788 $is_recur_label = ts('I want to contribute this amount every %1',
789 [1 => $frequencyUnits[$unitVals[0]]]
791 $form->assign('all_text_recur', TRUE);
795 $form->assign('one_frequency_unit', FALSE);
797 foreach ($unitVals as $key => $val) {
798 if (array_key_exists($val, $frequencyUnits)) {
799 $units[$val] = $frequencyUnits[$val];
800 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
801 $units[$val] = CRM_Contribute_BAO_Contribution
::getUnitLabelWithPlural($val);
806 $frequencyUnit = &$form->addElement('select', 'frequency_unit', NULL, $units, ['aria-label' => ts('Frequency Unit')]);
809 if (!empty($form->_values
['is_recur_interval']) ||
$className == 'CRM_Contribute_Form_Contribution') {
810 $form->add('text', 'frequency_interval', $unit, $attributes['frequency_interval'] +
['aria-label' => ts('Every')]);
811 $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer');
814 // make sure frequency_interval is submitted as 1 if given no choice to user.
815 $form->add('hidden', 'frequency_interval', 1);
818 $form->add('checkbox', 'is_recur', $is_recur_label, NULL);
824 * @param array $fields
825 * The input form values.
826 * @param array $files
827 * The uploaded files if any.
828 * @param \CRM_Contribute_Form_Contribution_Main $self
831 * true if no errors, else array of errors
833 public static function formRule($fields, $files, $self) {
835 $amount = self
::computeAmount($fields, $self->_values
);
836 if (!empty($fields['auto_renew']) && empty($fields['payment_processor_id'])) {
837 $errors['auto_renew'] = ts('You cannot have auto-renewal on if you are paying later.');
840 if ((!empty($fields['selectMembership']) &&
841 $fields['selectMembership'] != 'no_thanks'
843 (!empty($fields['priceSetId']) &&
847 $isTest = $self->_action
& CRM_Core_Action
::PREVIEW
;
848 $lifeMember = CRM_Member_BAO_Membership
::getAllContactMembership($self->_membershipContactID
, $isTest, TRUE);
850 $membershipOrgDetails = CRM_Member_BAO_MembershipType
::getAllMembershipTypes();
852 foreach (array_keys($lifeMember) as $memTypeId) {
853 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId]['member_of_contact_id'];
857 //check for atleast one pricefields should be selected
858 if (!empty($fields['priceSetId']) && empty($self->_ccid
)) {
859 $priceField = new CRM_Price_DAO_PriceField();
860 $priceField->price_set_id
= $fields['priceSetId'];
861 $priceField->orderBy('weight');
865 $membershipIsActive = TRUE;
866 $previousId = $otherAmount = FALSE;
867 while ($priceField->fetch()) {
869 if ($self->isQuickConfig() && ($priceField->name
== 'contribution_amount' ||
$priceField->name
== 'membership_amount')) {
870 $previousId = $priceField->id
;
871 if ($priceField->name
== 'membership_amount' && !$priceField->is_active
) {
872 $membershipIsActive = FALSE;
875 if ($priceField->name
== 'other_amount') {
876 if ($self->_quickConfig
&& empty($fields["price_{$priceField->id}"]) &&
877 array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values
['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])
879 $otherAmount = $priceField->id
;
881 elseif (!empty($fields["price_{$priceField->id}"])) {
882 $otherAmountVal = CRM_Utils_Rule
::cleanMoney($fields["price_{$priceField->id}"]);
883 $min = $self->_values
['min_amount'] ??
NULL;
884 $max = $self->_values
['max_amount'] ??
NULL;
885 if ($min && $otherAmountVal < $min) {
886 $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1',
890 if ($max && $otherAmountVal > $max) {
891 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
897 if (!empty($fields["price_{$priceField->id}"]) ||
($previousId == $priceField->id
&& isset($fields["price_{$previousId}"])
898 && empty($fields["price_{$previousId}"]))
900 $check[] = $priceField->id
;
904 $currentMemberships = NULL;
905 if ($membershipIsActive) {
906 $is_test = $self->_mode
!= 'live' ?
1 : 0;
907 $memContactID = $self->_membershipContactID
;
909 // For anonymous user check using dedupe rule
910 // if user has Cancelled Membership
911 if (!$memContactID) {
912 $memContactID = CRM_Contact_BAO_Contact
::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', [], FALSE);
914 $currentMemberships = CRM_Member_BAO_Membership
::getContactsCancelledMembership($memContactID,
918 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
919 if ($fieldValue['html_type'] != 'Text' && !empty($fields['price_' . $fieldKey])) {
920 if (!is_array($fields['price_' . $fieldKey]) && isset($fieldValue['options'][$fields['price_' . $fieldKey]])) {
921 if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]])
922 && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)
924 $errors['price_' . $fieldKey] = ts('Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.', [1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'])]);
928 if (is_array($fields['price_' . $fieldKey])) {
929 foreach (array_keys($fields['price_' . $fieldKey]) as $key) {
930 if (array_key_exists('membership_type_id', $fieldValue['options'][$key])
931 && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)
933 $errors['price_' . $fieldKey] = ts('Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.', [1 => CRM_Member_PseudoConstant
::membershipType($fieldValue['options'][$key]['membership_type_id'])]);
943 if ($membershipIsActive && empty($self->_membershipBlock
['is_required'])
944 && $self->isFormSupportsNonMembershipContributions()
946 $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
947 foreach ($self->_values
['fee'] as $fieldKey => $fieldValue) {
948 // if 'No thank you' membership is selected then set $membershipFieldId
949 if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array
::value('price_' . $fieldKey, $fields) == 0) {
950 $membershipFieldId = $fieldKey;
952 elseif ($membershipFieldId) {
953 if ($fieldValue['name'] == 'other_amount') {
954 $otherFieldId = $fieldKey;
956 elseif ($fieldValue['name'] == 'contribution_amount') {
957 $contributionFieldId = $fieldKey;
960 if (!$errorKey || CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields) == '0') {
961 $errorKey = $fieldKey;
965 // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error
966 if ($membershipFieldId && !(CRM_Utils_Array
::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) {
967 $errors["price_{$errorKey}"] = ts('Additional Contribution is required.');
970 if (empty($check) && empty($self->_ccid
)) {
971 if ($self->_useForMember
== 1 && $membershipIsActive) {
972 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
975 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
978 if ($otherAmount && !empty($check)) {
979 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
982 if ($self->_useForMember
== 1 && !empty($check) && $membershipIsActive) {
984 $priceFieldMemTypes = [];
986 foreach ($self->_priceSet
['fields'] as $priceId => $value) {
987 if (!empty($fields['price_' . $priceId]) ||
($self->_quickConfig
&& $value['name'] == 'membership_amount' && empty($self->_membershipBlock
['is_required']))) {
988 if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) {
989 foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
991 $priceFieldIDS[] = $priceFldVal;
995 elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) {
996 // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
997 // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
998 $priceFieldIDS[] = $fields['price_' . $priceId];
1001 if (!empty($value['options'])) {
1002 foreach ($value['options'] as $val) {
1003 if (!empty($val['membership_type_id']) && (
1004 ($fields['price_' . $priceId] == $val['id']) ||
1005 (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']]))
1008 $priceFieldMemTypes[] = $val['membership_type_id'];
1015 if (!empty($lifeMember)) {
1016 foreach ($priceFieldIDS as $priceFieldId) {
1017 if (($id = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) &&
1018 in_array($membershipOrgDetails[$id]['member_of_contact_id'], $unallowedOrgs)
1020 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
1026 if (!empty($priceFieldIDS)) {
1027 $ids = implode(',', $priceFieldIDS);
1029 $priceFieldIDS['id'] = $fields['priceSetId'];
1030 $self->set('memberPriceFieldIDS', $priceFieldIDS);
1031 $count = CRM_Price_BAO_PriceSet
::getMembershipCount($ids);
1032 foreach ($count as $id => $occurrence) {
1033 if ($occurrence > 1) {
1034 $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.');
1040 if (empty($priceFieldMemTypes) && $self->_membershipBlock
['is_required'] == 1) {
1041 $errors['_qf_default'] = ts('Please select at least one membership option.');
1045 CRM_Price_BAO_PriceSet
::processAmount($self->_values
['fee'],
1049 $minAmt = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount');
1050 if ($fields['amount'] < 0) {
1051 $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
1053 elseif (!empty($minAmt) && $fields['amount'] < $minAmt) {
1054 $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Contribution(s).', [
1055 1 => CRM_Utils_Money
::format($minAmt),
1059 $amount = $fields['amount'];
1062 if (isset($fields['selectProduct']) &&
1063 $fields['selectProduct'] != 'no_thanks'
1065 $productDAO = new CRM_Contribute_DAO_Product();
1066 $productDAO->id
= $fields['selectProduct'];
1067 $productDAO->find(TRUE);
1068 $min_amount = $productDAO->min_contribution
;
1070 if ($amount < $min_amount) {
1071 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', [1 => CRM_Utils_Money
::format($min_amount)]);
1072 CRM_Core_Session
::setStatus($errors['selectProduct']);
1076 //CRM-16285 - Function to handle validation errors on form, for recurring contribution field.
1077 CRM_Contribute_BAO_ContributionRecur
::validateRecurContribution($fields, $files, $self, $errors);
1079 if (!empty($fields['is_recur']) && empty($fields['payment_processor_id'])) {
1080 $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
1083 // validate PCP fields - if not anonymous, we need a nick name value
1084 if ($self->_pcpId
&& !empty($fields['pcp_display_in_roll']) &&
1085 empty($fields['pcp_is_anonymous']) &&
1086 CRM_Utils_Array
::value('pcp_roll_nickname', $fields) == ''
1088 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
1091 // return if this is express mode
1092 $config = CRM_Core_Config
::singleton();
1093 if ($self->_paymentProcessor
&&
1094 (int) $self->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_BUTTON
1096 if (!empty($fields[$self->_expressButtonName
. '_x']) ||
!empty($fields[$self->_expressButtonName
. '_y']) ||
1097 !empty($fields[$self->_expressButtonName
])
1103 //validate the pledge fields.
1104 if (!empty($self->_values
['pledge_block_id'])) {
1105 //validation for pledge payment.
1106 if (!empty($self->_values
['pledge_id'])) {
1107 if (empty($fields['pledge_amount'])) {
1108 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
1111 elseif (!empty($fields['is_pledge'])) {
1112 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_installments', $fields)) == FALSE) {
1113 $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
1116 if (!isset($fields['pledge_installments'])) {
1117 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
1119 elseif (CRM_Utils_Array
::value('pledge_installments', $fields) == 1) {
1120 $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.');
1122 elseif (empty($fields['pledge_installments'])) {
1123 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
1127 //validation for Pledge Frequency Interval.
1128 if (CRM_Utils_Rule
::positiveInteger(CRM_Utils_Array
::value('pledge_frequency_interval', $fields)) == FALSE) {
1129 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
1132 if (!isset($fields['pledge_frequency_interval'])) {
1133 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
1135 elseif (empty($fields['pledge_frequency_interval'])) {
1136 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
1142 // if the user has chosen a free membership or the amount is less than zero
1143 // i.e. we don't need to validate payment related fields or profiles.
1144 if ((float) $amount <= 0.0) {
1148 if (!isset($fields['payment_processor_id'])) {
1149 $errors['payment_processor_id'] = ts('Payment Method is a required field.');
1152 CRM_Core_Payment_Form
::validatePaymentInstrument(
1153 $fields['payment_processor_id'],
1156 (!$self->_isBillingAddressRequiredForPayLater ?
NULL : 'billing')
1160 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1161 if ($greetingType = CRM_Utils_Array
::value($greeting, $fields)) {
1162 $customizedValue = CRM_Core_PseudoConstant
::getKey('CRM_Contact_BAO_Contact', $greeting . '_id', 'Customized');
1163 if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
1164 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
1165 [1 => ucwords(str_replace('_', " ", $greeting))]
1171 return empty($errors) ?
TRUE : $errors;
1175 * Compute amount to be paid.
1177 * @param array $params
1178 * @param array $formValues
1180 * @return int|mixed|null|string
1182 public static function computeAmount($params, $formValues) {
1184 // First clean up the other amount field if present.
1185 if (isset($params['amount_other'])) {
1186 $params['amount_other'] = CRM_Utils_Rule
::cleanMoney($params['amount_other']);
1189 if (CRM_Utils_Array
::value('amount', $params) == 'amount_other_radio' ||
!empty($params['amount_other'])) {
1190 $amount = $params['amount_other'];
1192 elseif (!empty($params['pledge_amount'])) {
1193 foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
1194 $amount +
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
1198 if (!empty($formValues['amount'])) {
1199 $amountID = $params['amount'] ??
NULL;
1202 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
1203 // function to get correct amount level consistently. Remove setting of the amount level in
1204 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
1205 // to cover all variants.
1206 $params['amount_level'] = $formValues[$amountID]['label'] ??
NULL;
1207 $amount = $formValues[$amountID]['value'] ??
NULL;
1215 * Process the form submission.
1217 public function postProcess() {
1218 // we first reset the confirm page so it accepts new values
1219 $this->controller
->resetPage('Confirm');
1221 // get the submitted form values.
1222 $params = $this->controller
->exportValues($this->_name
);
1223 $this->submit($params);
1225 if (empty($this->_values
['is_confirm_enabled'])) {
1226 $this->skipToThankYouPage();
1234 * This is the guts of the postProcess made also accessible to the test suite.
1236 * @param array $params
1239 * @throws \CiviCRM_API3_Exception
1241 public function submit($params) {
1242 //carry campaign from profile.
1243 if (array_key_exists('contribution_campaign_id', $params)) {
1244 $params['campaign_id'] = $params['contribution_campaign_id'];
1247 $params['currencyID'] = CRM_Core_Config
::singleton()->defaultCurrency
;
1249 // @todo refactor this & leverage it from the unit tests.
1250 if (!empty($params['priceSetId'])) {
1251 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1252 if ($is_quick_config) {
1253 $priceField = new CRM_Price_DAO_PriceField();
1254 $priceField->price_set_id
= $params['priceSetId'];
1255 $priceField->orderBy('weight');
1256 $priceField->find();
1259 while ($priceField->fetch()) {
1260 CRM_Price_BAO_PriceFieldValue
::getValues($priceField->id
, $priceOptions);
1261 if (($selectedPriceOptionID = CRM_Utils_Array
::value("price_{$priceField->id}", $params)) != FALSE && $selectedPriceOptionID > 0) {
1262 switch ($priceField->name
) {
1263 case 'membership_amount':
1264 $this->_params
['selectMembership'] = $params['selectMembership'] = $priceOptions[$selectedPriceOptionID]['membership_type_id'] ??
NULL;
1265 $this->set('selectMembership', $params['selectMembership']);
1267 case 'contribution_amount':
1268 $params['amount'] = $selectedPriceOptionID;
1269 if ($priceField->name
== 'contribution_amount' ||
1270 ($priceField->name
== 'membership_amount' &&
1271 CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
) == 0)
1273 $this->_values
['amount'] = $priceOptions[$selectedPriceOptionID]['amount'] ??
NULL;
1275 $this->_values
[$selectedPriceOptionID]['value'] = $priceOptions[$selectedPriceOptionID]['amount'] ??
NULL;
1276 $this->_values
[$selectedPriceOptionID]['label'] = $priceOptions[$selectedPriceOptionID]['label'] ??
NULL;
1277 $this->_values
[$selectedPriceOptionID]['amount_id'] = $priceOptions[$selectedPriceOptionID]['id'] ??
NULL;
1278 $this->_values
[$selectedPriceOptionID]['weight'] = $priceOptions[$selectedPriceOptionID]['weight'] ??
NULL;
1281 case 'other_amount':
1282 $params['amount_other'] = $selectedPriceOptionID;
1290 if (!empty($this->_ccid
) && !empty($this->_pendingAmount
)) {
1291 $params['amount'] = $this->_pendingAmount
;
1294 // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
1295 $params['amount'] = self
::computeAmount($params, $this->_values
);
1298 $params['separate_amount'] = $params['amount'];
1299 // @todo - stepping through the code indicates that amount is always set before this point so it never matters.
1300 // Move more of the above into this function...
1301 $params['amount'] = $this->getMainContributionAmount($params);
1302 //If the membership & contribution is used in contribution page & not separate payment
1303 $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL;
1304 $proceFieldAmount = 0;
1305 if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment
== 0) {
1306 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1307 if ($is_quick_config) {
1308 foreach ($this->_priceSet
['fields'] as $fieldKey => $fieldVal) {
1309 if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) {
1310 $fieldId = $fieldVal['id'];
1311 $fieldOption = $params['price_' . $fieldId];
1312 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1316 if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' ||
$fieldVal['name'] == 'contribution_amount')) {
1317 $fieldId = $fieldVal['id'];
1318 if ($fieldVal['name'] == 'other_amount') {
1319 $proceFieldAmount +
= $this->_submitValues
['price_' . $fieldId];
1321 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues
['price_' . $fieldId] > 0) {
1322 $proceFieldAmount +
= $fieldVal['options'][$this->_submitValues
['price_' . $fieldId]]['amount'];
1324 unset($params['price_' . $fieldId]);
1332 if (!isset($params['amount_other'])) {
1333 $this->set('amount_level', CRM_Utils_Array
::value('amount_level', $params));
1336 if (!empty($this->_ccid
)) {
1337 $this->set('lineItem', $this->_lineItem
);
1339 elseif ($priceSetId = CRM_Utils_Array
::value('priceSetId', $params)) {
1341 $is_quick_config = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
1342 if ($is_quick_config) {
1343 foreach ($this->_values
['fee'] as $key => & $val) {
1344 if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) {
1345 // Clean out any currency symbols.
1346 $params['price_' . $key] = CRM_Utils_Rule
::cleanMoney($params['price_' . $key]);
1347 if ($params['price_' . $key] != 0) {
1348 foreach ($val['options'] as $optionKey => & $options) {
1349 $options['amount'] = $params['price_' . $key] ??
NULL;
1353 $params['price_' . $key] = 1;
1359 if ($this->_membershipBlock
) {
1360 $this->processAmountAndGetAutoRenew($this->_values
['fee'], $params, $lineItem[$priceSetId], $priceSetId);
1363 CRM_Price_BAO_PriceSet
::processAmount($this->_values
['fee'], $params, $lineItem[$priceSetId], $priceSetId);
1366 if ($params['tax_amount']) {
1367 $this->set('tax_amount', $params['tax_amount']);
1370 if ($proceFieldAmount) {
1371 $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
1372 $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
1373 if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
1374 $proceFieldAmount +
= $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
1376 if (!$this->_membershipBlock
['is_separate_payment']) {
1377 //require when separate membership not used
1378 $params['amount'] = $proceFieldAmount;
1381 $this->set('lineItem', $lineItem);
1384 if ($params['amount'] != 0 && (($this->_values
['is_pay_later'] &&
1385 empty($this->_paymentProcessor
) &&
1386 !array_key_exists('hidden_processor', $params)) ||
1387 empty($params['payment_processor_id']))
1389 $params['is_pay_later'] = 1;
1392 $params['is_pay_later'] = 0;
1395 // Would be nice to someday understand the point of this set.
1396 $this->set('is_pay_later', $params['is_pay_later']);
1397 // assign pay later stuff
1398 $this->_params
['is_pay_later'] = $params['is_pay_later'];
1399 $this->assign('is_pay_later', $params['is_pay_later']);
1400 $this->assign('pay_later_text', $params['is_pay_later'] ?
$this->_values
['pay_later_text'] : NULL);
1401 $this->assign('pay_later_receipt', ($params['is_pay_later'] && isset($this->_values
['pay_later_receipt'])) ?
$this->_values
['pay_later_receipt'] : NULL);
1403 if ($this->_membershipBlock
&& $this->_membershipBlock
['is_separate_payment'] && !empty($params['separate_amount'])) {
1404 $this->set('amount', $params['separate_amount']);
1407 $this->set('amount', $params['amount']);
1410 // generate and set an invoiceID for this transaction
1411 $invoiceID = md5(uniqid(rand(), TRUE));
1412 $this->set('invoiceID', $invoiceID);
1413 $params['invoiceID'] = $invoiceID;
1414 $title = !empty($this->_values
['frontend_title']) ?
$this->_values
['frontend_title'] : $this->_values
['title'];
1415 $params['description'] = ts('Online Contribution') . ': ' . ((!empty($this->_pcpInfo
['title']) ?
$this->_pcpInfo
['title'] : $title));
1416 $params['button'] = $this->controller
->getButtonName();
1417 // required only if is_monetary and valid positive amount
1418 if ($this->_values
['is_monetary'] &&
1419 !empty($this->_paymentProcessor
) &&
1420 ((float) $params['amount'] > 0.0 ||
$this->hasSeparateMembershipPaymentAmount($params))
1422 // The concept of contributeMode is deprecated - as should be the 'is_monetary' setting.
1423 $this->setContributeMode();
1424 // Really this setting of $this->_params & params within it should be done earlier on in the function
1425 // probably the values determined here should be reused in confirm postProcess as there is no opportunity to alter anything
1426 // on the confirm page. However as we are dealing with a stable release we go as close to where it is used
1428 // In general the form has a lack of clarity of the logic of why things are set on the form in some cases &
1429 // the logic around when $this->_params is used compared to other params arrays.
1430 $this->_params
= array_merge($params, $this->_params
);
1431 $this->setRecurringMembershipParams();
1432 if ($this->_paymentProcessor
&&
1433 $this->_paymentProcessor
['object']->supports('preApproval')
1435 $this->handlePreApproval($this->_params
);
1441 * Assign the billing mode to the template.
1443 * This is required for legacy support for contributeMode in templates.
1445 * The goal is to remove this parameter & use more relevant parameters.
1447 protected function setContributeMode() {
1448 switch ($this->_paymentProcessor
['billing_mode']) {
1449 case CRM_Core_Payment
::BILLING_MODE_FORM
:
1450 $this->set('contributeMode', 'direct');
1453 case CRM_Core_Payment
::BILLING_MODE_BUTTON
:
1454 $this->set('contributeMode', 'express');
1457 case CRM_Core_Payment
::BILLING_MODE_NOTIFY
:
1458 $this->set('contributeMode', 'notify');
1465 * Process confirm function and pass browser to the thank you page.
1467 protected function skipToThankYouPage() {
1468 // call the post process hook for the main page before we switch to confirm
1469 $this->postProcessHook();
1471 // build the confirm page
1472 $confirmForm = &$this->controller
->_pages
['Confirm'];
1473 $confirmForm->preProcess();
1474 $confirmForm->buildQuickForm();
1476 // the confirmation page is valid
1477 $data = &$this->controller
->container();
1478 $data['valid']['Confirm'] = 1;
1480 // confirm the contribution
1481 // mainProcess calls the hook also
1482 $confirmForm->mainProcess();
1483 $qfKey = $this->controller
->_key
;
1485 // redirect to thank you page
1486 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE));
1490 * Set form variables if contribution ID is found
1492 public function assignFormVariablesByContributionID() {
1493 if (empty($this->_ccid
)) {
1496 if (!$this->getContactID()) {
1497 CRM_Core_Error
::statusBounce(ts("Returning since there is no contact attached to this contribution id."));
1500 $paymentBalance = CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_ccid
);
1501 //bounce if the contribution is not pending.
1502 if ((float) $paymentBalance <= 0) {
1503 CRM_Core_Error
::statusBounce(ts("Returning since contribution has already been handled."));
1505 if (!empty($paymentBalance)) {
1506 $this->_pendingAmount
= $paymentBalance;
1507 $this->assign('pendingAmount', $this->_pendingAmount
);
1510 if ($taxAmount = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_ccid
, 'tax_amount')) {
1511 $this->set('tax_amount', $taxAmount);
1512 $this->assign('taxAmount', $taxAmount);
1515 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($this->_ccid
);
1516 foreach (array_keys($lineItems) as $id) {
1517 $lineItems[$id]['id'] = $id;
1519 $itemId = key($lineItems);
1520 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
1521 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1524 if (!empty($lineItems[$itemId]['price_field_id'])) {
1525 $this->_lineItem
[$this->_priceSetId
] = $lineItems;
1527 $isQuickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config');
1528 $this->assign('lineItem', $this->_lineItem
);
1529 $this->assign('is_quick_config', $isQuickConfig);
1530 $this->assign('priceSetID', $this->_priceSetId
);
1534 * Function for unit tests on the postProcess function.
1536 * @param array $params
1538 * @throws \CiviCRM_API3_Exception
1540 public function testSubmit($params) {
1541 $_SERVER['REQUEST_METHOD'] = 'GET';
1542 $this->controller
= new CRM_Contribute_Controller_Contribution();
1543 $this->submit($params);
1547 * Has a separate membership payment amount been configured.
1549 * @param array $params
1552 * @throws \CiviCRM_API3_Exception
1554 protected function hasSeparateMembershipPaymentAmount($params) {
1555 return $this->_separateMembershipPayment
&& (int) CRM_Member_BAO_MembershipType
::getMembershipType($params['selectMembership'])['minimum_fee'];
1559 * Get the loaded payment processor - the default for the form.
1561 * If the form is using 'pay later' then the value for the manual
1562 * pay later processor is 0.
1566 protected function getCurrentPaymentProcessor(): ?
int {
1567 $pps = $this->getProcessors();
1568 if (!empty($pps) && count($pps) === 1) {
1569 $ppKeys = array_keys($pps);
1570 return array_pop($ppKeys);
1572 // It seems like this might be un=reachable as there should always be a processor...