Merge pull request #21513 from JKingsnorth/core-2846-1-improve-start-end-date-validation
[civicrm-core.git] / CRM / Contribute / Form / Contribution / Main.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for processing a Contribution.
20 */
21 class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase {
22
23 /**
24 * Define default MembershipType Id.
25 * @var int
26 */
27 public $_defaultMemTypeId;
28
29 public $_paymentProcessors;
30
31 public $_membershipTypeValues;
32
33 public $_useForMember;
34
35 /**
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
37 * @var array
38 */
39 public $_paymentFields = [];
40
41 protected $_paymentProcessorID;
42 protected $_snippet;
43
44 /**
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?"
49 *
50 * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
51 *
52 * @return array
53 */
54 public function getUFGroupIDs() {
55 $ufGroupIDs = [];
56 if (!empty($this->_values['custom_pre_id'])) {
57 $ufGroupIDs[] = $this->_values['custom_pre_id'];
58 }
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'];
64 }
65 else {
66 $ufGroupIDs = array_merge($ufGroupIDs, $this->_values['custom_post_id']);
67 }
68 }
69
70 return $ufGroupIDs;
71 }
72
73 /**
74 * Set variables up before form is built.
75 */
76 public function preProcess() {
77 parent::preProcess();
78
79 $this->_paymentProcessors = $this->get('paymentProcessors');
80 $this->preProcessPaymentOptions();
81
82 $this->assignFormVariablesByContributionID();
83
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));
89
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));
96
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));
100
101 if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) {
102 $this->assign('intro_text', $this->_pcpInfo['intro_text']);
103 }
104 elseif (!empty($this->_values['intro_text'])) {
105 $this->assign('intro_text', $this->_values['intro_text']);
106 }
107
108 $qParams = "reset=1&amp;id={$this->_id}";
109 if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) {
110 $qParams .= "&amp;pcpId={$pcpId}";
111 }
112 $this->assign('qParams', $qParams);
113
114 if (!empty($this->_values['footer_text'])) {
115 $this->assign('footer_text', $this->_values['footer_text']);
116 }
117 }
118
119 /**
120 * Set the default values.
121 */
122 public function setDefaultValues() {
123 // check if the user is registered and we have a contact ID
124 $contactID = $this->getContactID();
125
126 if (!empty($contactID)) {
127 $fields = [];
128 $removeCustomFieldTypes = ['Contribution', 'Membership'];
129 $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields();
130
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)) {
137 continue;
138 }
139 // ignore component fields
140 }
141 elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) {
142 continue;
143 }
144 $fields[$name] = $fieldInfo;
145 }
146
147 if (!empty($fields)) {
148 CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
149 }
150
151 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
152 $this->_defaults = array_merge($this->_defaults, $billingDefaults);
153 }
154 if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
155 $this->_defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->_pendingAmount);
156 }
157
158 /*
159 * hack to simplify credit card entry for testing
160 *
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';
171 */
172
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']);
178
179 $paymentAmount = 0;
180 $duePayment = FALSE;
181 foreach ($pledgePayments as $payId => $value) {
182 if ($value['status'] == 'Overdue') {
183 $this->_defaults['pledge_amount'][$payId] = 1;
184 $paymentAmount += $value['scheduled_amount'];
185 }
186 elseif (!$duePayment && $value['status'] == 'Pending') {
187 $this->_defaults['pledge_amount'][$payId] = 1;
188 $paymentAmount += $value['scheduled_amount'];
189 $duePayment = TRUE;
190 }
191 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
192 $completedContributionIds[] = $value['contribution_id'];
193 }
194 }
195 $this->_defaults['price_' . $this->_priceSetId] = $paymentAmount;
196
197 if (count($completedContributionIds)) {
198 $softCredit = [];
199 foreach ($completedContributionIds as $id) {
200 $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id);
201 }
202 if (isset($softCredit['soft_credit'])) {
203 $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
204
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),
210 $defaults
211 );
212 foreach ($defaults as $fieldName => $value) {
213 $this->_defaults['honor[' . $fieldName . ']'] = $value;
214 }
215 }
216 }
217 }
218 elseif (!empty($this->_values['pledge_block_id'])) {
219 //set default to one time contribution.
220 $this->_defaults['is_pledge'] = 0;
221 }
222
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);
226
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;
231 }
232
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;
236 }
237
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.
253 break;
254 }
255 elseif ($opMemTypeId &&
256 in_array($opMemTypeId, $this->_currentMemberships) &&
257 !in_array($opMemTypeId, $selectedCurrentMemTypes)
258 ) {
259 CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
260 $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id'];
261 }
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');
266 }
267 }
268 }
269 $entityId = CRM_Utils_Array::value('id', CRM_Member_BAO_Membership::getContactMembership($contactID, $memtypeID, NULL));
270 }
271 else {
272 CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults);
273 }
274 }
275
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;
281 }
282
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
289 );
290 }
291 }
292 }
293 }
294
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;
299 }
300 }
301 }
302
303 return $this->_defaults;
304 }
305
306 /**
307 * Build the form object.
308 */
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');
315
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']);
318
319 $this->buildComponentForm($this->_id, $this);
320 }
321
322 if (\Civi::settings()->get('forceRecaptcha')) {
323 if (!$this->_userID) {
324 CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this);
325 }
326 }
327
328 // Build payment processor form
329 CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
330
331 $config = CRM_Core_Config::singleton();
332
333 $contactID = $this->getContactID();
334 if ($contactID) {
335 $this->assign('contact_id', $contactID);
336 $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
337 }
338
339 $this->applyFilter('__ALL__', 'trim');
340 if (empty($this->_ccid)) {
341 if ($this->_emailExists == FALSE) {
342 $this->add('text', "email-{$this->_bltID}",
343 ts('Email Address'),
344 ['size' => 30, 'maxlength' => 60, 'class' => 'email'],
345 TRUE
346 );
347 $this->assign('showMainEmail', TRUE);
348 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
349 }
350 }
351 else {
352 $this->addElement('hidden', "email-{$this->_bltID}", 1);
353 $this->add('text', 'total_amount', ts('Total Amount'), ['readonly' => TRUE], FALSE);
354 }
355
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);
359
360 if ($contactID === 0) {
361 $this->addCidZeroOptions();
362
363 }
364
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')) {
371 $isTest = 0;
372 if ($this->_action & CRM_Core_Action::PREVIEW) {
373 $isTest = 1;
374 }
375
376 if ($this->_priceSetId &&
377 (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet))
378 ) {
379 $this->_useForMember = 1;
380 $this->set('useForMember', $this->_useForMember);
381 }
382
383 $this->_separateMembershipPayment = $this->buildMembershipBlock(
384 $this->_membershipContactID,
385 NULL,
386 $isTest
387 );
388 }
389 $this->set('separateMembershipPayment', $this->_separateMembershipPayment);
390 }
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);
401 }
402 if ($this->_values['is_monetary'] &&
403 $this->_values['is_recur'] && empty($this->_values['pledge_id'])
404 ) {
405 self::buildRecur($this);
406 }
407 }
408
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);
414 }
415 }
416
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);
420 }
421
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);
426 }
427 }
428
429 //to create an cms user
430 if (!$this->_contactID && empty($this->_ccid)) {
431 $createCMSUser = FALSE;
432
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');
436 }
437
438 if (!$createCMSUser &&
439 $this->_values['custom_post_id']
440 ) {
441 if (!is_array($this->_values['custom_post_id'])) {
442 $profileIDs = [$this->_values['custom_post_id']];
443 }
444 else {
445 $profileIDs = $this->_values['custom_post_id'];
446 }
447 foreach ($profileIDs as $pid) {
448 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
449 $profileID = $pid;
450 $createCMSUser = TRUE;
451 break;
452 }
453 }
454 }
455
456 if ($createCMSUser) {
457 CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
458 }
459 }
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);
464 }
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( );"]
471 );
472 $extraOption = ['onclick' => "return pcpAnonymous( );"];
473 $this->addRadio('pcp_is_anonymous', NULL, [ts('Include my name and message'), ts('List my contribution anonymously')], [], '&nbsp;&nbsp;&nbsp;', FALSE, [$extraOption, $extraOption]);
474
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;']);
477 }
478 }
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.'));
481 }
482
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++;
491 }
492 }
493 }
494
495 if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) {
496 $allAreBillingModeProcessors = TRUE;
497 }
498 else {
499 $allAreBillingModeProcessors = FALSE;
500 }
501
502 if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) {
503 $submitButton = [
504 'type' => 'upload',
505 'name' => ts('Contribute'),
506 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
507 'isDefault' => TRUE,
508 ];
509 if (!empty($this->_values['is_confirm_enabled'])) {
510 $submitButton['name'] = ts('Review your contribution');
511 $submitButton['icon'] = 'fa-chevron-right';
512 }
513 // Add submit-once behavior when confirm page disabled
514 if (empty($this->_values['is_confirm_enabled'])) {
515 $this->submitOnce = TRUE;
516 }
517 //change button name for updating contribution
518 if (!empty($this->_ccid)) {
519 $submitButton['name'] = ts('Confirm Payment');
520 }
521 $this->addButtons([$submitButton]);
522 }
523
524 $this->addFormRule(['CRM_Contribute_Form_Contribution_Main', 'formRule'], $this);
525 }
526
527 /**
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)
531 *
532 * @param int $cid
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
537 *
538 * @return bool
539 * Is this a separate membership payment
540 *
541 * @throws \CiviCRM_API3_Exception
542 * @throws \CRM_Core_Exception
543 */
544 private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) {
545 $separateMembershipPayment = FALSE;
546 $this->addOptionalQuickFormElement('auto_renew');
547 if ($this->_membershipBlock) {
548 $this->_currentMemberships = [];
549
550 $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = [];
551 $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember);
552
553 $allowAutoRenewMembership = $autoRenewOption = FALSE;
554 $autoRenewMembershipTypeOptions = [];
555
556 $separateMembershipPayment = $this->_membershipBlock['is_separate_payment'] ?? NULL;
557
558 if ($membershipPriceset) {
559 foreach ($this->_priceSet['fields'] as $pField) {
560 if (empty($pField['options'])) {
561 continue;
562 }
563 foreach ($pField['options'] as $opId => $opValues) {
564 if (empty($opValues['membership_type_id'])) {
565 continue;
566 }
567 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
568 }
569 }
570 }
571 elseif (!empty($this->_membershipBlock['membership_types'])) {
572 $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']);
573 }
574
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',
579 $this->_mid,
580 'membership_type_id'
581 );
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');
584 }
585 }
586
587 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds);
588 $this->_membershipTypeValues = $membershipTypeValues;
589 $endDate = NULL;
590
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;
599 }
600 }
601 }
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']);
608 if ($cid) {
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;
616 }
617 }
618 $membershipTypes[] = $memType;
619 }
620 }
621 elseif ($memType['is_active']) {
622
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;
628 }
629 else {
630 $javascriptMethod = NULL;
631 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = 0;
632 }
633
634 //add membership type.
635 $radio[$memType['id']] = NULL;
636 $radioOptAttrs[$memType['id']] = $javascriptMethod;
637 if ($cid) {
638 $membership = new CRM_Member_DAO_Membership();
639 $membership->contact_id = $cid;
640 $membership->membership_type_id = $memType['id'];
641
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');
645
646 if (!is_null($isTest)) {
647 $membership->is_test = $isTest;
648 }
649
650 //CRM-4297
651 $membership->orderBy('end_date DESC');
652
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);
658 continue;
659 }
660 $this->assign('renewal_mode', TRUE);
661 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
662 $memType['current_membership'] = $membership->end_date;
663 if (!$endDate) {
664 $endDate = $memType['current_membership'];
665 $this->_defaultMemTypeId = $memType['id'];
666 }
667 if ($memType['current_membership'] < $endDate) {
668 $endDate = $memType['current_membership'];
669 $this->_defaultMemTypeId = $memType['id'];
670 }
671 }
672 }
673 $membershipTypes[] = $memType;
674 }
675 }
676 }
677
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);
686
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']);
693 }
694 else {
695 $this->assign('autoRenewOption', $autoRenewOption);
696 }
697
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);
703 }
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);
709 }
710 else {
711 foreach ($radioOptAttrs as $opt => $attrs) {
712 $attrs['class'] = ' required';
713 }
714 $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
715 }
716
717 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
718 }
719
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.'));
723 }
724 else {
725 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
726 }
727 }
728
729 }
730
731 return $separateMembershipPayment;
732 }
733
734 /**
735 * Build elements to collect information for recurring contributions.
736 *
737 *
738 * @param CRM_Core_Form $form
739 */
740 public static function buildRecur(&$form) {
741 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur');
742 $className = get_class($form);
743
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']),
751 ]));
752 }
753
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'
758 ) {
759 $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR,
760 CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, FALSE, NULL, 'value')
761 );
762 }
763
764 $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits);
765
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( );
770 //}
771
772 $form->add('text', 'installments', ts('installments'),
773 $attributes['installments']
774 );
775 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
776
777 $is_recur_label = ts('I want to contribute this amount every');
778
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);
786 }
787 else {
788 $is_recur_label = ts('I want to contribute this amount every %1',
789 [1 => $frequencyUnits[$unitVals[0]]]
790 );
791 $form->assign('all_text_recur', TRUE);
792 }
793 }
794 else {
795 $form->assign('one_frequency_unit', FALSE);
796 $units = [];
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);
802 $unit = ts('Every');
803 }
804 }
805 }
806 $frequencyUnit = &$form->addElement('select', 'frequency_unit', NULL, $units, ['aria-label' => ts('Frequency Unit')]);
807 }
808
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');
812 }
813 else {
814 // make sure frequency_interval is submitted as 1 if given no choice to user.
815 $form->add('hidden', 'frequency_interval', 1);
816 }
817
818 $form->add('checkbox', 'is_recur', $is_recur_label, NULL);
819 }
820
821 /**
822 * Global form rule.
823 *
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
829 *
830 * @return bool|array
831 * true if no errors, else array of errors
832 */
833 public static function formRule($fields, $files, $self) {
834 $errors = [];
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.');
838 }
839
840 if ((!empty($fields['selectMembership']) &&
841 $fields['selectMembership'] != 'no_thanks'
842 ) ||
843 (!empty($fields['priceSetId']) &&
844 $self->_useForMember
845 )
846 ) {
847 $isTest = $self->_action & CRM_Core_Action::PREVIEW;
848 $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_membershipContactID, $isTest, TRUE);
849
850 $membershipOrgDetails = CRM_Member_BAO_MembershipType::getAllMembershipTypes();
851 $unallowedOrgs = [];
852 foreach (array_keys($lifeMember) as $memTypeId) {
853 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId]['member_of_contact_id'];
854 }
855 }
856
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');
862 $priceField->find();
863
864 $check = [];
865 $membershipIsActive = TRUE;
866 $previousId = $otherAmount = FALSE;
867 while ($priceField->fetch()) {
868
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;
873 }
874 }
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}"])
878 ) {
879 $otherAmount = $priceField->id;
880 }
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',
887 [1 => $min]
888 );
889 }
890 if ($max && $otherAmountVal > $max) {
891 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
892 [1 => $max]
893 );
894 }
895 }
896 }
897 if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"])
898 && empty($fields["price_{$previousId}"]))
899 ) {
900 $check[] = $priceField->id;
901 }
902 }
903
904 $currentMemberships = NULL;
905 if ($membershipIsActive) {
906 $is_test = $self->_mode != 'live' ? 1 : 0;
907 $memContactID = $self->_membershipContactID;
908
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);
913 }
914 $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID,
915 $is_test
916 );
917
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)
923 ) {
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'])]);
925 }
926 }
927 else {
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)
932 ) {
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'])]);
934 }
935 }
936 }
937 }
938 }
939 }
940 }
941
942 // CRM-12233
943 if ($membershipIsActive && empty($self->_membershipBlock['is_required'])
944 && $self->isFormSupportsNonMembershipContributions()
945 ) {
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;
951 }
952 elseif ($membershipFieldId) {
953 if ($fieldValue['name'] == 'other_amount') {
954 $otherFieldId = $fieldKey;
955 }
956 elseif ($fieldValue['name'] == 'contribution_amount') {
957 $contributionFieldId = $fieldKey;
958 }
959
960 if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') {
961 $errorKey = $fieldKey;
962 }
963 }
964 }
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.');
968 }
969 }
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).');
973 }
974 else {
975 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
976 }
977 }
978 if ($otherAmount && !empty($check)) {
979 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
980 }
981
982 if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) {
983 $priceFieldIDS = [];
984 $priceFieldMemTypes = [];
985
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) {
990 if ($isSet) {
991 $priceFieldIDS[] = $priceFldVal;
992 }
993 }
994 }
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];
999 }
1000
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']]))
1006 )
1007 ) {
1008 $priceFieldMemTypes[] = $val['membership_type_id'];
1009 }
1010 }
1011 }
1012 }
1013 }
1014
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)
1019 ) {
1020 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
1021 break;
1022 }
1023 }
1024 }
1025
1026 if (!empty($priceFieldIDS)) {
1027 $ids = implode(',', $priceFieldIDS);
1028
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.');
1035 break;
1036 }
1037 }
1038 }
1039
1040 if (empty($priceFieldMemTypes) && $self->_membershipBlock['is_required'] == 1) {
1041 $errors['_qf_default'] = ts('Please select at least one membership option.');
1042 }
1043 }
1044
1045 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'],
1046 $fields, $lineItem
1047 );
1048
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');
1052 }
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),
1056 ]);
1057 }
1058
1059 $amount = $fields['amount'];
1060 }
1061
1062 if (isset($fields['selectProduct']) &&
1063 $fields['selectProduct'] != 'no_thanks'
1064 ) {
1065 $productDAO = new CRM_Contribute_DAO_Product();
1066 $productDAO->id = $fields['selectProduct'];
1067 $productDAO->find(TRUE);
1068 $min_amount = $productDAO->min_contribution;
1069
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']);
1073 }
1074 }
1075
1076 //CRM-16285 - Function to handle validation errors on form, for recurring contribution field.
1077 CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors);
1078
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.');
1081 }
1082
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) == ''
1087 ) {
1088 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
1089 }
1090
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
1095 ) {
1096 if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) ||
1097 !empty($fields[$self->_expressButtonName])
1098 ) {
1099 return $errors;
1100 }
1101 }
1102
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.');
1109 }
1110 }
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.');
1114 }
1115 else {
1116 if (!isset($fields['pledge_installments'])) {
1117 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
1118 }
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.');
1121 }
1122 elseif (empty($fields['pledge_installments'])) {
1123 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
1124 }
1125 }
1126
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.');
1130 }
1131 else {
1132 if (!isset($fields['pledge_frequency_interval'])) {
1133 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
1134 }
1135 elseif (empty($fields['pledge_frequency_interval'])) {
1136 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
1137 }
1138 }
1139 }
1140 }
1141
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) {
1145 return $errors;
1146 }
1147
1148 if (!isset($fields['payment_processor_id'])) {
1149 $errors['payment_processor_id'] = ts('Payment Method is a required field.');
1150 }
1151 else {
1152 CRM_Core_Payment_Form::validatePaymentInstrument(
1153 $fields['payment_processor_id'],
1154 $fields,
1155 $errors,
1156 (!$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing')
1157 );
1158 }
1159
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))]
1166 );
1167 }
1168 }
1169 }
1170
1171 return empty($errors) ? TRUE : $errors;
1172 }
1173
1174 /**
1175 * Compute amount to be paid.
1176 *
1177 * @param array $params
1178 * @param array $formValues
1179 *
1180 * @return int|mixed|null|string
1181 */
1182 public static function computeAmount($params, $formValues) {
1183 $amount = 0;
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']);
1187 }
1188
1189 if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) {
1190 $amount = $params['amount_other'];
1191 }
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');
1195 }
1196 }
1197 else {
1198 if (!empty($formValues['amount'])) {
1199 $amountID = $params['amount'] ?? NULL;
1200
1201 if ($amountID) {
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;
1208 }
1209 }
1210 }
1211 return $amount;
1212 }
1213
1214 /**
1215 * Process the form submission.
1216 */
1217 public function postProcess() {
1218 // we first reset the confirm page so it accepts new values
1219 $this->controller->resetPage('Confirm');
1220
1221 // get the submitted form values.
1222 $params = $this->controller->exportValues($this->_name);
1223 $this->submit($params);
1224
1225 if (empty($this->_values['is_confirm_enabled'])) {
1226 $this->skipToThankYouPage();
1227 }
1228
1229 }
1230
1231 /**
1232 * Submit function.
1233 *
1234 * This is the guts of the postProcess made also accessible to the test suite.
1235 *
1236 * @param array $params
1237 * Submitted values.
1238 *
1239 * @throws \CiviCRM_API3_Exception
1240 */
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'];
1245 }
1246
1247 $params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency;
1248
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();
1257
1258 $priceOptions = [];
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']);
1266
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)
1272 ) {
1273 $this->_values['amount'] = $priceOptions[$selectedPriceOptionID]['amount'] ?? NULL;
1274 }
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;
1279 break;
1280
1281 case 'other_amount':
1282 $params['amount_other'] = $selectedPriceOptionID;
1283 break;
1284 }
1285 }
1286 }
1287 }
1288 }
1289
1290 if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
1291 $params['amount'] = $this->_pendingAmount;
1292 }
1293 else {
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);
1296 }
1297
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'];
1313 $memPresent = TRUE;
1314 }
1315 else {
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];
1320 }
1321 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) {
1322 $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
1323 }
1324 unset($params['price_' . $fieldId]);
1325 break;
1326 }
1327 }
1328 }
1329 }
1330 }
1331
1332 if (!isset($params['amount_other'])) {
1333 $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params));
1334 }
1335
1336 if (!empty($this->_ccid)) {
1337 $this->set('lineItem', $this->_lineItem);
1338 }
1339 elseif ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) {
1340 $lineItem = [];
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;
1350 break;
1351 }
1352 }
1353 $params['price_' . $key] = 1;
1354 break;
1355 }
1356 }
1357 }
1358
1359 if ($this->_membershipBlock) {
1360 $this->processAmountAndGetAutoRenew($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
1361 }
1362 else {
1363 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
1364 }
1365
1366 if ($params['tax_amount']) {
1367 $this->set('tax_amount', $params['tax_amount']);
1368 }
1369
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'];
1375 }
1376 if (!$this->_membershipBlock['is_separate_payment']) {
1377 //require when separate membership not used
1378 $params['amount'] = $proceFieldAmount;
1379 }
1380 }
1381 $this->set('lineItem', $lineItem);
1382 }
1383
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']))
1388 ) {
1389 $params['is_pay_later'] = 1;
1390 }
1391 else {
1392 $params['is_pay_later'] = 0;
1393 }
1394
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);
1402
1403 if ($this->_membershipBlock && $this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) {
1404 $this->set('amount', $params['separate_amount']);
1405 }
1406 else {
1407 $this->set('amount', $params['amount']);
1408 }
1409
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))
1421 ) {
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
1427 // as possible.
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')
1434 ) {
1435 $this->handlePreApproval($this->_params);
1436 }
1437 }
1438 }
1439
1440 /**
1441 * Assign the billing mode to the template.
1442 *
1443 * This is required for legacy support for contributeMode in templates.
1444 *
1445 * The goal is to remove this parameter & use more relevant parameters.
1446 */
1447 protected function setContributeMode() {
1448 switch ($this->_paymentProcessor['billing_mode']) {
1449 case CRM_Core_Payment::BILLING_MODE_FORM:
1450 $this->set('contributeMode', 'direct');
1451 break;
1452
1453 case CRM_Core_Payment::BILLING_MODE_BUTTON:
1454 $this->set('contributeMode', 'express');
1455 break;
1456
1457 case CRM_Core_Payment::BILLING_MODE_NOTIFY:
1458 $this->set('contributeMode', 'notify');
1459 break;
1460 }
1461
1462 }
1463
1464 /**
1465 * Process confirm function and pass browser to the thank you page.
1466 */
1467 protected function skipToThankYouPage() {
1468 // call the post process hook for the main page before we switch to confirm
1469 $this->postProcessHook();
1470
1471 // build the confirm page
1472 $confirmForm = &$this->controller->_pages['Confirm'];
1473 $confirmForm->preProcess();
1474 $confirmForm->buildQuickForm();
1475
1476 // the confirmation page is valid
1477 $data = &$this->controller->container();
1478 $data['valid']['Confirm'] = 1;
1479
1480 // confirm the contribution
1481 // mainProcess calls the hook also
1482 $confirmForm->mainProcess();
1483 $qfKey = $this->controller->_key;
1484
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));
1487 }
1488
1489 /**
1490 * Set form variables if contribution ID is found
1491 */
1492 public function assignFormVariablesByContributionID() {
1493 if (empty($this->_ccid)) {
1494 return;
1495 }
1496 if (!$this->getContactID()) {
1497 CRM_Core_Error::statusBounce(ts("Returning since there is no contact attached to this contribution id."));
1498 }
1499
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."));
1504 }
1505 if (!empty($paymentBalance)) {
1506 $this->_pendingAmount = $paymentBalance;
1507 $this->assign('pendingAmount', $this->_pendingAmount);
1508 }
1509
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);
1513 }
1514
1515 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_ccid);
1516 foreach (array_keys($lineItems) as $id) {
1517 $lineItems[$id]['id'] = $id;
1518 }
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');
1522 }
1523
1524 if (!empty($lineItems[$itemId]['price_field_id'])) {
1525 $this->_lineItem[$this->_priceSetId] = $lineItems;
1526 }
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);
1531 }
1532
1533 /**
1534 * Function for unit tests on the postProcess function.
1535 *
1536 * @param array $params
1537 *
1538 * @throws \CiviCRM_API3_Exception
1539 */
1540 public function testSubmit($params) {
1541 $_SERVER['REQUEST_METHOD'] = 'GET';
1542 $this->controller = new CRM_Contribute_Controller_Contribution();
1543 $this->submit($params);
1544 }
1545
1546 /**
1547 * Has a separate membership payment amount been configured.
1548 *
1549 * @param array $params
1550 *
1551 * @return mixed
1552 * @throws \CiviCRM_API3_Exception
1553 */
1554 protected function hasSeparateMembershipPaymentAmount($params) {
1555 return $this->_separateMembershipPayment && (int) CRM_Member_BAO_MembershipType::getMembershipType($params['selectMembership'])['minimum_fee'];
1556 }
1557
1558 /**
1559 * Get the loaded payment processor - the default for the form.
1560 *
1561 * If the form is using 'pay later' then the value for the manual
1562 * pay later processor is 0.
1563 *
1564 * @return int|null
1565 */
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);
1571 }
1572 // It seems like this might be un=reachable as there should always be a processor...
1573 return NULL;
1574 }
1575
1576 }