Merge pull request #18000 from eileenmcnaughton/brn
[civicrm-core.git] / CRM / Contribute / Form / Contribution / Main.php
CommitLineData
6a488035
TO
1<?php
2/*
bc77d7c0
TO
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 +--------------------------------------------------------------------+
006389de 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
d90a1a52 19 * This class generates form components for processing a Contribution.
6a488035
TO
20 */
21class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase {
22
23 /**
fe482240 24 * Define default MembershipType Id.
1330f57a 25 * @var int
6a488035
TO
26 */
27 public $_defaultMemTypeId;
28
6a488035 29 public $_paymentProcessors;
6a488035
TO
30
31 public $_membershipTypeValues;
32
33 public $_useForMember;
34
dde5a0ef 35 /**
100fef9d 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
dde5a0ef
EM
37 * @var array
38 */
be2fb01f 39 public $_paymentFields = [];
dde5a0ef 40
e50ee6a3 41 protected $_paymentProcessorID;
7c8cc461 42 protected $_snippet;
6a488035
TO
43
44 /**
fe482240 45 * Set variables up before form is built.
6a488035
TO
46 */
47 public function preProcess() {
48 parent::preProcess();
49
42e3a033
EM
50 $this->_paymentProcessors = $this->get('paymentProcessors');
51 $this->preProcessPaymentOptions();
6a488035 52
eccb4502 53 $this->assignFormVariablesByContributionID();
58466af1 54
b44e3f84 55 // Make the contributionPageID available to the template
6a488035 56 $this->assign('contributionPageID', $this->_id);
58466af1 57 $this->assign('ccid', $this->_ccid);
6a488035
TO
58 $this->assign('isShare', CRM_Utils_Array::value('is_share', $this->_values));
59 $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values));
60
a3d827a7 61 $this->assign('reset', CRM_Utils_Request::retrieve('reset', 'Boolean'));
d90a1a52
EM
62 $this->assign('mainDisplay', CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean',
63 CRM_Core_DAO::$_nullObject));
6a488035 64
78f0ee1b 65 if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) {
6a488035
TO
66 $this->assign('intro_text', $this->_pcpInfo['intro_text']);
67 }
78f0ee1b 68 elseif (!empty($this->_values['intro_text'])) {
6a488035
TO
69 $this->assign('intro_text', $this->_values['intro_text']);
70 }
71
72 $qParams = "reset=1&amp;id={$this->_id}";
78f0ee1b 73 if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) {
6a488035
TO
74 $qParams .= "&amp;pcpId={$pcpId}";
75 }
78f0ee1b 76 $this->assign('qParams', $qParams);
6a488035 77
78f0ee1b 78 if (!empty($this->_values['footer_text'])) {
6a488035
TO
79 $this->assign('footer_text', $this->_values['footer_text']);
80 }
6a488035
TO
81 }
82
186c9c17 83 /**
fe482240 84 * Set the default values.
186c9c17 85 */
00be9182 86 public function setDefaultValues() {
6a488035 87 // check if the user is registered and we have a contact ID
da8d9879 88 $contactID = $this->getContactID();
6a488035 89
da8d9879 90 if (!empty($contactID)) {
be2fb01f
CW
91 $fields = [];
92 $removeCustomFieldTypes = ['Contribution', 'Membership'];
6a488035
TO
93 $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields();
94
95 // remove component related fields
b1666977 96 foreach ($this->_fields as $name => $fieldInfo) {
6a488035
TO
97 //don't set custom data Used for Contribution (CRM-1344)
98 if (substr($name, 0, 7) == 'custom_') {
99 $id = substr($name, 7);
100 if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) {
101 continue;
102 }
103 // ignore component fields
104 }
105 elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) {
106 continue;
107 }
b1666977 108 $fields[$name] = $fieldInfo;
6a488035 109 }
fc60f30e
RN
110
111 if (!empty($fields)) {
472561f4 112 CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
fc60f30e
RN
113 }
114
9d665938 115 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
116 $this->_defaults = array_merge($this->_defaults, $billingDefaults);
6a488035 117 }
58466af1 118 if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
fe552de9 119 $this->_defaults['total_amount'] = CRM_Utils_Money::format($this->_pendingAmount, NULL, '%a');
58466af1 120 }
6a488035 121
aefd7f6b
EM
122 /*
123 * hack to simplify credit card entry for testing
124 *
125 * $this->_defaults['credit_card_type'] = 'Visa';
126 * $this->_defaults['amount'] = 168;
127 * $this->_defaults['credit_card_number'] = '4111111111111111';
128 * $this->_defaults['cvv2'] = '000';
129 * $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
130 * // hack to simplify direct debit entry for testing
131 * $this->_defaults['account_holder'] = 'Max Müller';
132 * $this->_defaults['bank_account_number'] = '12345678';
133 * $this->_defaults['bank_identification_number'] = '12030000';
134 * $this->_defaults['bank_name'] = 'Bankname';
135 */
6a488035
TO
136
137 //build set default for pledge overdue payment.
a7488080 138 if (!empty($this->_values['pledge_id'])) {
133e2c99 139 //used to record completed pledge payment ids used later for honor default
be2fb01f 140 $completedContributionIds = [];
133e2c99 141 $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']);
6a488035 142
b8887609 143 $paymentAmount = 0;
6a488035 144 $duePayment = FALSE;
133e2c99 145 foreach ($pledgePayments as $payId => $value) {
146 if ($value['status'] == 'Overdue') {
6a488035 147 $this->_defaults['pledge_amount'][$payId] = 1;
b8887609 148 $paymentAmount += $value['scheduled_amount'];
6a488035 149 }
133e2c99 150 elseif (!$duePayment && $value['status'] == 'Pending') {
6a488035 151 $this->_defaults['pledge_amount'][$payId] = 1;
b8887609 152 $paymentAmount += $value['scheduled_amount'];
6a488035
TO
153 $duePayment = TRUE;
154 }
133e2c99 155 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
156 $completedContributionIds[] = $value['contribution_id'];
157 }
158 }
b8887609 159 $this->_defaults['price_' . $this->_priceSetId] = $paymentAmount;
133e2c99 160
4779abb3 161 if (count($completedContributionIds)) {
be2fb01f 162 $softCredit = [];
133e2c99 163 foreach ($completedContributionIds as $id) {
164 $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id);
165 }
166 if (isset($softCredit['soft_credit'])) {
167 $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
168
169 //since honoree profile fieldname of fields are prefixed with 'honor'
170 //we need to reformat the fieldname to append prefix during setting default values
171 CRM_Core_BAO_UFGroup::setProfileDefaults(
172 $softCredit['soft_credit'][1]['contact_id'],
173 CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId),
174 $defaults
175 );
176 foreach ($defaults as $fieldName => $value) {
177 $this->_defaults['honor[' . $fieldName . ']'] = $value;
178 }
179 }
6a488035
TO
180 }
181 }
a7488080 182 elseif (!empty($this->_values['pledge_block_id'])) {
6a488035
TO
183 //set default to one time contribution.
184 $this->_defaults['is_pledge'] = 0;
185 }
186
187 // to process Custom data that are appended to URL
188 $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
03110609 189 $this->_defaults = array_merge($this->_defaults, $getDefaults);
6a488035
TO
190
191 $config = CRM_Core_Config::singleton();
192 // set default country from config if no country set
a7488080 193 if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) {
6a488035
TO
194 $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
195 }
196
197 // set default state/province from config if no state/province set
a7488080 198 if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) {
6a488035
TO
199 $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
200 }
201
c432c016 202 $entityId = $memtypeID = NULL;
6a488035 203 if ($this->_priceSetId) {
a1a68e64 204 if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) {
be2fb01f 205 $selectedCurrentMemTypes = [];
6a488035
TO
206 foreach ($this->_priceSet['fields'] as $key => $val) {
207 foreach ($val['options'] as $keys => $values) {
9c1bc317 208 $opMemTypeId = $values['membership_type_id'] ?? NULL;
352c43f7 209 $priceFieldName = 'price_' . $values['price_field_id'];
034b5cb6
KJ
210 $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName);
211 if (!empty($priceFieldValue)) {
212 CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults);
213 // break here to prevent overwriting of default due to 'is_default'
214 // option configuration or setting of current membership or
215 // membership for related organization.
216 // The value sent via URL get's higher priority.
217 break;
218 }
219 elseif ($opMemTypeId &&
6a488035
TO
220 in_array($opMemTypeId, $this->_currentMemberships) &&
221 !in_array($opMemTypeId, $selectedCurrentMemTypes)
222 ) {
034b5cb6 223 CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
c432c016 224 $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id'];
6a488035 225 }
1330f57a
SL
226 elseif (!empty($values['is_default']) && !$opMemTypeId && (!isset($this->_defaults[$priceFieldName]) ||
227 ($val['html_type'] == 'CheckBox' && !isset($this->_defaults[$priceFieldName][$keys])))) {
228 CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
229 $memtypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults[$priceFieldName], 'membership_type_id');
6a488035
TO
230 }
231 }
232 }
a97d770e 233 $entityId = CRM_Utils_Array::value('id', CRM_Member_BAO_Membership::getContactMembership($contactID, $memtypeID, NULL));
6a488035
TO
234 }
235 else {
9da8dc8c 236 CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults);
6a488035
TO
237 }
238 }
239
a97d770e 240 //set custom field defaults set by admin if value is not set
241 if (!empty($this->_fields)) {
242 //load default campaign from page.
243 if (array_key_exists('contribution_campaign_id', $this->_fields)) {
9c1bc317 244 $this->_defaults['contribution_campaign_id'] = $this->_values['campaign_id'] ?? NULL;
a97d770e 245 }
246
247 //set custom field defaults
248 foreach ($this->_fields as $name => $field) {
249 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
250 if (!isset($this->_defaults[$name])) {
251 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
252 $entityId, CRM_Profile_Form::MODE_REGISTER
253 );
254 }
255 }
256 }
257 }
258
6a488035
TO
259 if (!empty($this->_paymentProcessors)) {
260 foreach ($this->_paymentProcessors as $pid => $value) {
a7488080 261 if (!empty($value['is_default'])) {
e02d7e96 262 $this->_defaults['payment_processor_id'] = $pid;
6a488035
TO
263 }
264 }
265 }
266
267 return $this->_defaults;
268 }
269
270 /**
fe482240 271 * Build the form object.
6a488035
TO
272 */
273 public function buildQuickForm() {
4839c695
KJ
274 // build profiles first so that we can determine address fields etc
275 // and then show copy address checkbox
2f44fc96 276 if (empty($this->_ccid)) {
277 $this->buildCustom($this->_values['custom_pre_id'], 'customPre');
278 $this->buildCustom($this->_values['custom_post_id'], 'customPost');
4839c695 279
2f44fc96 280 // CRM-18399: used by template to pass pre profile id as a url arg
281 $this->assign('custom_pre_id', $this->_values['custom_pre_id']);
d38c288e 282
2f44fc96 283 $this->buildComponentForm($this->_id, $this);
284 }
bcb8cc84 285
a3d94981 286 if (count($this->_paymentProcessors) >= 1 && !$this->get_template_vars("isCaptcha") && $this->hasToAddForcefully()) {
ce287b85
AP
287 if (!$this->_userID) {
288 $this->enableCaptchaOnForm();
289 }
290 else {
291 $this->displayCaptchaWarning();
292 }
96ea60e0
AP
293 }
294
7bf9cde2 295 // Build payment processor form
6cc679d2 296 CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
6a488035
TO
297
298 $config = CRM_Core_Config::singleton();
78f0ee1b 299
37326fa1
DG
300 $contactID = $this->getContactID();
301 if ($contactID) {
302 $this->assign('contact_id', $contactID);
f498a273 303 $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
37326fa1
DG
304 }
305
6a488035 306 $this->applyFilter('__ALL__', 'trim');
58466af1 307 if (empty($this->_ccid)) {
0c7c9ff7
KC
308 if ($this->_emailExists == FALSE) {
309 $this->add('text', "email-{$this->_bltID}",
310 ts('Email Address'),
be2fb01f 311 ['size' => 30, 'maxlength' => 60, 'class' => 'email'],
0c7c9ff7
KC
312 TRUE
313 );
d08a6665 314 $this->assign('showMainEmail', TRUE);
0c7c9ff7
KC
315 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
316 }
58466af1 317 }
318 else {
319 $this->addElement('hidden', "email-{$this->_bltID}", 1);
be2fb01f 320 $this->add('text', 'total_amount', ts('Total Amount'), ['readonly' => TRUE], FALSE);
58466af1 321 }
bf7ae813 322 $pps = $this->getProcessors();
1421b010
SL
323 $this->addPaymentProcessorFieldsToForm();
324 if (!empty($pps) && count($pps) === 1) {
325 $ppKeys = array_keys($pps);
326 $currentPP = array_pop($ppKeys);
327 if ($currentPP === 0) {
6a488035 328 $this->assign('is_pay_later', $this->_values['is_pay_later']);
bf7ae813 329 $this->assign('pay_later_text', $this->getPayLaterLabel());
6a488035
TO
330 }
331 }
596bff78 332
333 $contactID = $this->getContactID();
aa288d3f 334 if ($this->getContactID() === 0) {
f956cd24 335 $this->addCidZeroOptions();
596bff78 336 }
aa288d3f 337
6a488035
TO
338 //build pledge block.
339 $this->_useForMember = 0;
340 //don't build membership block when pledge_id is passed
58466af1 341 if (empty($this->_values['pledge_id']) && empty($this->_ccid)) {
6a488035
TO
342 $this->_separateMembershipPayment = FALSE;
343 if (in_array('CiviMember', $config->enableComponents)) {
344 $isTest = 0;
345 if ($this->_action & CRM_Core_Action::PREVIEW) {
346 $isTest = 1;
347 }
348
349 if ($this->_priceSetId &&
350 (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet))
351 ) {
352 $this->_useForMember = 1;
353 $this->set('useForMember', $this->_useForMember);
354 }
355
42e3a033 356 $this->_separateMembershipPayment = $this->buildMembershipBlock(
5b757295 357 $this->_membershipContactID,
6a488035 358 TRUE, NULL, FALSE,
5b757295 359 $isTest
6a488035
TO
360 );
361 }
362 $this->set('separateMembershipPayment', $this->_separateMembershipPayment);
363 }
364 $this->assign('useForMember', $this->_useForMember);
365 // If we configured price set for contribution page
366 // we are not allow membership signup as well as any
367 // other contribution amount field, CRM-5095
368 if (isset($this->_priceSetId) && $this->_priceSetId) {
369 $this->add('hidden', 'priceSetId', $this->_priceSetId);
370 // build price set form.
371 $this->set('priceSetId', $this->_priceSetId);
58466af1 372 if (empty($this->_ccid)) {
373 CRM_Price_BAO_PriceSet::buildPriceSet($this);
374 }
6a488035 375 if ($this->_values['is_monetary'] &&
353ffa53
TO
376 $this->_values['is_recur'] && empty($this->_values['pledge_id'])
377 ) {
6a488035
TO
378 self::buildRecur($this);
379 }
380 }
6a488035 381
58466af1 382 if ($this->_priceSetId && empty($this->_ccid)) {
9da8dc8c 383 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035
TO
384 if ($is_quick_config) {
385 $this->_useForMember = 0;
386 $this->set('useForMember', $this->_useForMember);
387 }
388 }
389
6a488035 390 //we allow premium for pledge during pledge creation only.
58466af1 391 if (empty($this->_values['pledge_id']) && empty($this->_ccid)) {
6a488035
TO
392 CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE);
393 }
394
6a488035 395 //don't build pledge block when mid is passed
58466af1 396 if (!$this->_mid && empty($this->_ccid)) {
6a488035 397 $config = CRM_Core_Config::singleton();
8cc574cf 398 if (in_array('CiviPledge', $config->enableComponents) && !empty($this->_values['pledge_block_id'])) {
6a488035
TO
399 CRM_Pledge_BAO_PledgeBlock::buildPledgeBlock($this);
400 }
401 }
402
6a488035 403 //to create an cms user
58466af1 404 if (!$this->_contactID && empty($this->_ccid)) {
6a488035
TO
405 $createCMSUser = FALSE;
406
407 if ($this->_values['custom_pre_id']) {
408 $profileID = $this->_values['custom_pre_id'];
409 $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
410 }
411
412 if (!$createCMSUser &&
413 $this->_values['custom_post_id']
414 ) {
415 if (!is_array($this->_values['custom_post_id'])) {
be2fb01f 416 $profileIDs = [$this->_values['custom_post_id']];
6a488035
TO
417 }
418 else {
419 $profileIDs = $this->_values['custom_post_id'];
420 }
421 foreach ($profileIDs as $pid) {
422 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
423 $profileID = $pid;
424 $createCMSUser = TRUE;
425 break;
426 }
427 }
428 }
429
430 if ($createCMSUser) {
431 CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
432 }
433 }
58466af1 434 if ($this->_pcpId && empty($this->_ccid)) {
e9a23206
MW
435 if (CRM_PCP_BAO_PCP::displayName($this->_pcpId)) {
436 $pcp_supporter_text = CRM_PCP_BAO_PCP::getPcpSupporterText($this->_pcpId, $this->_id, 'contribute');
eea141c0 437 $this->assign('pcpSupporterText', $pcp_supporter_text);
6a488035 438 }
be2fb01f 439 $prms = ['id' => $this->_pcpId];
31ed2806
JM
440 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
441 if ($pcpInfo['is_honor_roll']) {
4d327c2d 442 $this->assign('isHonor', TRUE);
31ed2806 443 $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL,
be2fb01f 444 ['onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );"]
31ed2806 445 );
be2fb01f 446 $extraOption = ['onclick' => "return pcpAnonymous( );"];
39405208 447 $this->addRadio('pcp_is_anonymous', NULL, [ts('Include my name and message'), ts('List my contribution anonymously')], [], '&nbsp;&nbsp;&nbsp;', FALSE, [$extraOption, $extraOption]);
31ed2806 448
be2fb01f
CW
449 $this->add('text', 'pcp_roll_nickname', ts('Name'), ['maxlength' => 30]);
450 $this->addField('pcp_personal_note', ['entity' => 'ContributionSoft', 'context' => 'create', 'style' => 'height: 3em; width: 40em;']);
31ed2806 451 }
6a488035 452 }
58466af1 453 if (empty($this->_values['fee']) && empty($this->_ccid)) {
7980012b 454 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.'));
e1a89dfd 455 }
6a488035
TO
456
457 //we have to load confirm contribution button in template
458 //when multiple payment processor as the user
459 //can toggle with payment processor selection
460 $billingModePaymentProcessors = 0;
481a74f4 461 if (!empty($this->_paymentProcessors)) {
6a488035
TO
462 foreach ($this->_paymentProcessors as $key => $values) {
463 if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) {
464 $billingModePaymentProcessors++;
465 }
466 }
467 }
468
469 if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) {
470 $allAreBillingModeProcessors = TRUE;
0db6c3e1
TO
471 }
472 else {
6a488035
TO
473 $allAreBillingModeProcessors = FALSE;
474 }
475
476 if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) {
be2fb01f 477 $submitButton = [
61189839 478 'type' => 'upload',
1b9aa3aa 479 'name' => ts('Contribute'),
61189839
CW
480 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
481 'isDefault' => TRUE,
be2fb01f 482 ];
1b9aa3aa
AH
483 if (!empty($this->_values['is_confirm_enabled'])) {
484 $submitButton['name'] = ts('Review your contribution');
485 $submitButton['icon'] = 'fa-chevron-right';
486 }
61189839
CW
487 // Add submit-once behavior when confirm page disabled
488 if (empty($this->_values['is_confirm_enabled'])) {
423616fa 489 $this->submitOnce = TRUE;
61189839 490 }
fe552de9 491 //change button name for updating contribution
492 if (!empty($this->_ccid)) {
493 $submitButton['name'] = ts('Confirm Payment');
494 }
be2fb01f 495 $this->addButtons([$submitButton]);
6a488035
TO
496 }
497
be2fb01f 498 $this->addFormRule(['CRM_Contribute_Form_Contribution_Main', 'formRule'], $this);
6a488035
TO
499 }
500
6a488035 501 /**
fe482240 502 * Build elements to collect information for recurring contributions.
6a488035 503 *
03110609
EM
504 *
505 * @param CRM_Core_Form $form
6a488035 506 */
a5aef36c 507 public static function buildRecur(&$form) {
6a488035
TO
508 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur');
509 $className = get_class($form);
510
a5aef36c 511 $form->assign('is_recur_interval', CRM_Utils_Array::value('is_recur_interval', $form->_values));
512 $form->assign('is_recur_installments', CRM_Utils_Array::value('is_recur_installments', $form->_values));
9a0d0452
PN
513 $paymentObject = $form->getVar('_paymentObject');
514 if ($paymentObject) {
be2fb01f 515 $form->assign('recurringHelpText', $paymentObject->getText('contributionPageRecurringHelp', [
9a0d0452
PN
516 'is_recur_installments' => !empty($form->_values['is_recur_installments']),
517 'is_email_receipt' => !empty($form->_values['is_email_receipt']),
be2fb01f 518 ]));
9a0d0452 519 }
6841f76b 520
6a488035
TO
521 $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL);
522
a7488080 523 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
8cdd6191 524 $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval'] + ['aria-label' => ts('Every')]);
6a488035
TO
525 $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer');
526 }
527 else {
528 // make sure frequency_interval is submitted as 1 if given no choice to user.
529 $form->add('hidden', 'frequency_interval', 1);
530 }
531
9c1bc317 532 $frUnits = $form->_values['recur_frequency_unit'] ?? NULL;
6a488035
TO
533 if (empty($frUnits) &&
534 $className == 'CRM_Contribute_Form_Contribution'
535 ) {
536 $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR,
537 CRM_Core_OptionGroup::values('recur_frequency_units')
538 );
539 }
540
541 $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits);
542
543 // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
317fceb4 544 if (count($unitVals) == 1) {
874c9be7 545 $form->assign('one_frequency_unit', TRUE);
6a488035
TO
546 $unit = $unitVals[0];
547 $form->add('hidden', 'frequency_unit', $unit);
a7488080 548 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
6a488035
TO
549 $unit .= "(s)";
550 }
a5aef36c 551 $form->assign('frequency_unit', $unit);
0db6c3e1
TO
552 }
553 else {
874c9be7 554 $form->assign('one_frequency_unit', FALSE);
be2fb01f 555 $units = [];
728ebde6 556 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE);
6a488035
TO
557 foreach ($unitVals as $key => $val) {
558 if (array_key_exists($val, $frequencyUnits)) {
559 $units[$val] = $frequencyUnits[$val];
a7488080 560 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
6a488035
TO
561 $units[$val] = "{$frequencyUnits[$val]}(s)";
562 }
563 }
564 }
8cdd6191 565 $frequencyUnit = &$form->addElement('select', 'frequency_unit', NULL, $units, ['aria-label' => ts('Frequency Unit')]);
6a488035
TO
566 }
567
6a488035
TO
568 // FIXME: Ideally we should freeze select box if there is only
569 // one option but looks there is some problem /w QF freeze.
570 //if ( count( $units ) == 1 ) {
571 //$frequencyUnit->freeze( );
572 //}
573
574 $form->add('text', 'installments', ts('installments'),
575 $attributes['installments']
576 );
577 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
578 }
579
580 /**
fe482240 581 * Global form rule.
6a488035 582 *
014c4014
TO
583 * @param array $fields
584 * The input form values.
585 * @param array $files
586 * The uploaded files if any.
c59e7960 587 * @param \CRM_Contribute_Form_Contribution_Main $self
2a6da8d7 588 *
72b3a70c
CW
589 * @return bool|array
590 * true if no errors, else array of errors
6a488035 591 */
00be9182 592 public static function formRule($fields, $files, $self) {
be2fb01f 593 $errors = [];
aefd7f6b 594 $amount = self::computeAmount($fields, $self->_values);
de6c59ca 595 if (!empty($fields['auto_renew']) && empty($fields['payment_processor_id'])) {
fb357d6e 596 $errors['auto_renew'] = ts('You cannot have auto-renewal on if you are paying later.');
597 }
6a488035 598
8cc574cf 599 if ((!empty($fields['selectMembership']) &&
6a488035
TO
600 $fields['selectMembership'] != 'no_thanks'
601 ) ||
8cc574cf 602 (!empty($fields['priceSetId']) &&
6a488035
TO
603 $self->_useForMember
604 )
605 ) {
3ec3dcbe 606 $isTest = $self->_action & CRM_Core_Action::PREVIEW;
352c43f7 607 $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_membershipContactID, $isTest, TRUE);
6a488035
TO
608
609 $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization();
610
be2fb01f 611 $unallowedOrgs = [];
6a488035
TO
612 foreach (array_keys($lifeMember) as $memTypeId) {
613 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId];
614 }
615 }
616
617 //check for atleast one pricefields should be selected
58466af1 618 if (!empty($fields['priceSetId']) && empty($self->_ccid)) {
9da8dc8c 619 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
620 $priceField->price_set_id = $fields['priceSetId'];
621 $priceField->orderBy('weight');
622 $priceField->find();
623
be2fb01f 624 $check = [];
6a488035
TO
625 $membershipIsActive = TRUE;
626 $previousId = $otherAmount = FALSE;
627 while ($priceField->fetch()) {
628
c59e7960 629 if ($self->isQuickConfig() && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) {
6a488035 630 $previousId = $priceField->id;
481a74f4 631 if ($priceField->name == 'membership_amount' && !$priceField->is_active) {
6a488035
TO
632 $membershipIsActive = FALSE;
633 }
634 }
635 if ($priceField->name == 'other_amount') {
8cc574cf 636 if ($self->_quickConfig && empty($fields["price_{$priceField->id}"]) &&
353ffa53
TO
637 array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])
638 ) {
6a488035
TO
639 $otherAmount = $priceField->id;
640 }
641 elseif (!empty($fields["price_{$priceField->id}"])) {
ef88f444 642 $otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]);
9c1bc317
CW
643 $min = $self->_values['min_amount'] ?? NULL;
644 $max = $self->_values['max_amount'] ?? NULL;
6a488035
TO
645 if ($min && $otherAmountVal < $min) {
646 $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1',
be2fb01f 647 [1 => $min]
6a488035
TO
648 );
649 }
650 if ($max && $otherAmountVal > $max) {
651 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
be2fb01f 652 [1 => $max]
6a488035
TO
653 );
654 }
655 }
656 }
657 if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"])
353ffa53
TO
658 && empty($fields["price_{$previousId}"]))
659 ) {
6a488035
TO
660 $check[] = $priceField->id;
661 }
662 }
663
b5a62499
PN
664 $currentMemberships = NULL;
665 if ($membershipIsActive) {
666 $is_test = $self->_mode != 'live' ? 1 : 0;
667 $memContactID = $self->_membershipContactID;
7f7fa13a
EM
668
669 // For anonymous user check using dedupe rule
b5a62499
PN
670 // if user has Cancelled Membership
671 if (!$memContactID) {
be2fb01f 672 $memContactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', [], FALSE);
b5a62499
PN
673 }
674 $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID,
675 $is_test
676 );
7f7fa13a 677
b5a62499 678 foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
f3acfdd9 679 if ($fieldValue['html_type'] != 'Text' && !empty($fields['price_' . $fieldKey])) {
ca3c9210 680 if (!is_array($fields['price_' . $fieldKey]) && isset($fieldValue['options'][$fields['price_' . $fieldKey]])) {
7f7fa13a 681 if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]])
353ffa53
TO
682 && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)
683 ) {
6dabf459 684 $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'])]);
b5a62499
PN
685 }
686 }
687 else {
ca3c9210 688 if (is_array($fields['price_' . $fieldKey])) {
689 foreach (array_keys($fields['price_' . $fieldKey]) as $key) {
690 if (array_key_exists('membership_type_id', $fieldValue['options'][$key])
691 && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)
692 ) {
6dabf459 693 $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'])]);
ca3c9210 694 }
b5a62499
PN
695 }
696 }
697 }
698 }
699 }
700 }
7f7fa13a 701
9ce8bcfc 702 // CRM-12233
32c3b33f 703 if ($membershipIsActive && empty($self->_membershipBlock['is_required'])
353ffa53
TO
704 && $self->_values['amount_block_is_active']
705 ) {
9ce8bcfc
PN
706 $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
707 foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
41f27e49 708 // if 'No thank you' membership is selected then set $membershipFieldId
9ce8bcfc
PN
709 if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array::value('price_' . $fieldKey, $fields) == 0) {
710 $membershipFieldId = $fieldKey;
711 }
712 elseif ($membershipFieldId) {
713 if ($fieldValue['name'] == 'other_amount') {
714 $otherFieldId = $fieldKey;
715 }
716 elseif ($fieldValue['name'] == 'contribution_amount') {
717 $contributionFieldId = $fieldKey;
718 }
665e5ec7 719
9ce8bcfc
PN
720 if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') {
721 $errorKey = $fieldKey;
722 }
723 }
724 }
41f27e49 725 // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error
8cc574cf 726 if ($membershipFieldId && !(CRM_Utils_Array::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) {
1cd7e100 727 $errors["price_{$errorKey}"] = ts('Additional Contribution is required.');
9ce8bcfc
PN
728 }
729 }
58466af1 730 if (empty($check) && empty($self->_ccid)) {
6a488035
TO
731 if ($self->_useForMember == 1 && $membershipIsActive) {
732 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
733 }
734 else {
735 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
736 }
737 }
22e263ad 738 if ($otherAmount && !empty($check)) {
6a488035
TO
739 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
740 }
741
742 if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) {
be2fb01f
CW
743 $priceFieldIDS = [];
744 $priceFieldMemTypes = [];
6a488035
TO
745
746 foreach ($self->_priceSet['fields'] as $priceId => $value) {
8cc574cf 747 if (!empty($fields['price_' . $priceId]) || ($self->_quickConfig && $value['name'] == 'membership_amount' && empty($self->_membershipBlock['is_required']))) {
a7488080 748 if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) {
6a488035
TO
749 foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
750 if ($isSet) {
751 $priceFieldIDS[] = $priceFldVal;
752 }
753 }
754 }
8cc574cf 755 elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) {
6a488035
TO
756 // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
757 // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
758 $priceFieldIDS[] = $fields['price_' . $priceId];
759 }
760
a7488080 761 if (!empty($value['options'])) {
6a488035 762 foreach ($value['options'] as $val) {
88c6259e 763 if (!empty($val['membership_type_id']) && (
fb4aa4d9 764 ($fields['price_' . $priceId] == $val['id']) ||
765 (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']]))
766 )
767 ) {
6a488035
TO
768 $priceFieldMemTypes[] = $val['membership_type_id'];
769 }
770 }
771 }
772 }
773 }
774
775 if (!empty($lifeMember)) {
776 foreach ($priceFieldIDS as $priceFieldId) {
9da8dc8c 777 if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) &&
6a488035
TO
778 in_array($membershipOrgDetails[$id], $unallowedOrgs)
779 ) {
780 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
781 break;
782 }
783 }
784 }
785
786 if (!empty($priceFieldIDS)) {
787 $ids = implode(',', $priceFieldIDS);
788
789 $priceFieldIDS['id'] = $fields['priceSetId'];
790 $self->set('memberPriceFieldIDS', $priceFieldIDS);
9da8dc8c 791 $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids);
b44e3f84
DS
792 foreach ($count as $id => $occurrence) {
793 if ($occurrence > 1) {
6a488035
TO
794 $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.');
795 }
796 }
797 }
798
0d791f3b 799 if (empty($priceFieldMemTypes) && $self->_membershipBlock['is_required'] == 1) {
6a488035
TO
800 $errors['_qf_default'] = ts('Please select at least one membership option.');
801 }
802 }
803
9da8dc8c 804 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'],
6a488035
TO
805 $fields, $lineItem
806 );
665e5ec7 807
601c7a24 808 $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount');
6a488035
TO
809 if ($fields['amount'] < 0) {
810 $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
811 }
601c7a24 812 elseif (!empty($minAmt) && $fields['amount'] < $minAmt) {
be2fb01f 813 $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Contribution(s).', [
601c7a24 814 1 => CRM_Utils_Money::format($minAmt),
be2fb01f 815 ]);
601c7a24 816 }
817
6a488035
TO
818 $amount = $fields['amount'];
819 }
820
821 if (isset($fields['selectProduct']) &&
353ffa53
TO
822 $fields['selectProduct'] != 'no_thanks'
823 ) {
6a488035
TO
824 $productDAO = new CRM_Contribute_DAO_Product();
825 $productDAO->id = $fields['selectProduct'];
826 $productDAO->find(TRUE);
827 $min_amount = $productDAO->min_contribution;
828
829 if ($amount < $min_amount) {
be2fb01f 830 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', [1 => CRM_Utils_Money::format($min_amount)]);
6a488035
TO
831 CRM_Core_Session::setStatus($errors['selectProduct']);
832 }
833 }
834
577d1236
WA
835 //CRM-16285 - Function to handle validation errors on form, for recurring contribution field.
836 CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors);
6a488035 837
de6c59ca 838 if (!empty($fields['is_recur']) && empty($fields['payment_processor_id'])) {
6a488035
TO
839 $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
840 }
841
6a488035 842 // validate PCP fields - if not anonymous, we need a nick name value
8cc574cf 843 if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) &&
de6c59ca 844 empty($fields['pcp_is_anonymous']) &&
6a488035
TO
845 CRM_Utils_Array::value('pcp_roll_nickname', $fields) == ''
846 ) {
847 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
848 }
849
850 // return if this is express mode
851 $config = CRM_Core_Config::singleton();
f92fc7eb 852 if ($self->_paymentProcessor &&
5e3148f4 853 (int) $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON
f92fc7eb 854 ) {
8cc574cf 855 if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) ||
b99f3e96 856 !empty($fields[$self->_expressButtonName])
6a488035
TO
857 ) {
858 return $errors;
859 }
860 }
861
862 //validate the pledge fields.
a7488080 863 if (!empty($self->_values['pledge_block_id'])) {
6a488035 864 //validation for pledge payment.
a7488080 865 if (!empty($self->_values['pledge_id'])) {
6a488035
TO
866 if (empty($fields['pledge_amount'])) {
867 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
868 }
869 }
a7488080 870 elseif (!empty($fields['is_pledge'])) {
6a488035 871 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) {
917a9954 872 $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
6a488035
TO
873 }
874 else {
de6c59ca 875 if (!isset($fields['pledge_installments'])) {
6a488035
TO
876 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
877 }
35522279 878 elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 1) {
6a488035
TO
879 $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.');
880 }
de6c59ca 881 elseif (empty($fields['pledge_installments'])) {
6a488035
TO
882 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
883 }
884 }
885
886 //validation for Pledge Frequency Interval.
887 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) {
888 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
889 }
890 else {
de6c59ca 891 if (!isset($fields['pledge_frequency_interval'])) {
6a488035
TO
892 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
893 }
de6c59ca 894 elseif (empty($fields['pledge_frequency_interval'])) {
6a488035
TO
895 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
896 }
897 }
898 }
899 }
900
6a488035 901 // if the user has chosen a free membership or the amount is less than zero
1d1fee72 902 // i.e. we don't need to validate payment related fields or profiles.
f92fc7eb 903 if ((float) $amount <= 0.0) {
6a488035
TO
904 return $errors;
905 }
906
de6c59ca 907 if (!isset($fields['payment_processor_id'])) {
986905f5 908 $errors['payment_processor_id'] = ts('Payment Method is a required field.');
ca3c9210 909 }
910 else {
911 CRM_Core_Payment_Form::validatePaymentInstrument(
912 $fields['payment_processor_id'],
913 $fields,
914 $errors,
915 (!$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing')
916 );
917 }
6a488035 918
6a488035
TO
919 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
920 if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
233319bf 921 $customizedValue = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Contact', $greeting . '_id', 'Customized');
6a488035
TO
922 if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
923 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
be2fb01f 924 [1 => ucwords(str_replace('_', " ", $greeting))]
6a488035
TO
925 );
926 }
927 }
928 }
929
930 return empty($errors) ? TRUE : $errors;
931 }
932
186c9c17 933 /**
aefd7f6b
EM
934 * Compute amount to be paid.
935 *
c490a46a 936 * @param array $params
aefd7f6b 937 * @param array $formValues
186c9c17
EM
938 *
939 * @return int|mixed|null|string
940 */
aefd7f6b 941 public static function computeAmount($params, $formValues) {
99b93cf8 942 $amount = 0;
aefd7f6b 943 // First clean up the other amount field if present.
6a488035
TO
944 if (isset($params['amount_other'])) {
945 $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']);
946 }
947
8cc574cf 948 if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) {
6a488035
TO
949 $amount = $params['amount_other'];
950 }
951 elseif (!empty($params['pledge_amount'])) {
6a488035
TO
952 foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
953 $amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
954 }
955 }
956 else {
aefd7f6b 957 if (!empty($formValues['amount'])) {
9c1bc317 958 $amountID = $params['amount'] ?? NULL;
6a488035
TO
959
960 if ($amountID) {
c039f658 961 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
962 // function to get correct amount level consistently. Remove setting of the amount level in
963 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
964 // to cover all variants.
9c1bc317
CW
965 $params['amount_level'] = $formValues[$amountID]['label'] ?? NULL;
966 $amount = $formValues[$amountID]['value'] ?? NULL;
6a488035
TO
967 }
968 }
969 }
970 return $amount;
971 }
972
973 /**
fe482240 974 * Process the form submission.
6a488035
TO
975 */
976 public function postProcess() {
6a488035
TO
977 // we first reset the confirm page so it accepts new values
978 $this->controller->resetPage('Confirm');
979
980 // get the submitted form values.
981 $params = $this->controller->exportValues($this->_name);
8cb12ff1 982 $this->submit($params);
983
984 if (empty($this->_values['is_confirm_enabled'])) {
985 $this->skipToThankYouPage();
986 }
987
988 }
da8d9879 989
8cb12ff1 990 /**
991 * Submit function.
992 *
993 * This is the guts of the postProcess made also accessible to the test suite.
994 *
995 * @param array $params
996 * Submitted values.
ec57bf87 997 *
998 * @throws \CiviCRM_API3_Exception
8cb12ff1 999 */
1000 public function submit($params) {
4625312e
EM
1001 //carry campaign from profile.
1002 if (array_key_exists('contribution_campaign_id', $params)) {
1003 $params['campaign_id'] = $params['contribution_campaign_id'];
1004 }
1005
8cb12ff1 1006 $params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency;
4625312e 1007
65e172a3 1008 // @todo refactor this & leverage it from the unit tests.
a7488080 1009 if (!empty($params['priceSetId'])) {
9da8dc8c 1010 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035 1011 if ($is_quick_config) {
9da8dc8c 1012 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
1013 $priceField->price_set_id = $params['priceSetId'];
1014 $priceField->orderBy('weight');
1015 $priceField->find();
1016
be2fb01f 1017 $priceOptions = [];
6a488035 1018 while ($priceField->fetch()) {
1d9d37c7 1019 CRM_Price_BAO_PriceFieldValue::getValues($priceField->id, $priceOptions);
ca3c9210 1020 if (($selectedPriceOptionID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) != FALSE && $selectedPriceOptionID > 0) {
1d9d37c7
FG
1021 switch ($priceField->name) {
1022 case 'membership_amount':
9c1bc317 1023 $this->_params['selectMembership'] = $params['selectMembership'] = $priceOptions[$selectedPriceOptionID]['membership_type_id'] ?? NULL;
1d9d37c7 1024 $this->set('selectMembership', $params['selectMembership']);
1d9d37c7 1025
d71d328a 1026 case 'contribution_amount':
1d9d37c7 1027 $params['amount'] = $selectedPriceOptionID;
19c22a58 1028 if ($priceField->name == 'contribution_amount' ||
1029 ($priceField->name == 'membership_amount' &&
1030 CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0)
1031 ) {
9c1bc317 1032 $this->_values['amount'] = $priceOptions[$selectedPriceOptionID]['amount'] ?? NULL;
19c22a58 1033 }
9c1bc317
CW
1034 $this->_values[$selectedPriceOptionID]['value'] = $priceOptions[$selectedPriceOptionID]['amount'] ?? NULL;
1035 $this->_values[$selectedPriceOptionID]['label'] = $priceOptions[$selectedPriceOptionID]['label'] ?? NULL;
1036 $this->_values[$selectedPriceOptionID]['amount_id'] = $priceOptions[$selectedPriceOptionID]['id'] ?? NULL;
1037 $this->_values[$selectedPriceOptionID]['weight'] = $priceOptions[$selectedPriceOptionID]['weight'] ?? NULL;
1d9d37c7
FG
1038 break;
1039
1040 case 'other_amount':
1041 $params['amount_other'] = $selectedPriceOptionID;
1042 break;
6a488035
TO
1043 }
1044 }
6a488035
TO
1045 }
1046 }
1047 }
58466af1 1048
1049 if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
1050 $params['amount'] = $this->_pendingAmount;
1051 }
1052 else {
1053 // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
1054 $params['amount'] = self::computeAmount($params, $this->_values);
1055 }
6a488035 1056
6a488035 1057 $params['separate_amount'] = $params['amount'];
ec57bf87 1058 // @todo - stepping through the code indicates that amount is always set before this point so it never matters.
1059 // Move more of the above into this function...
1060 $params['amount'] = $this->getMainContributionAmount($params);
e6d4f555 1061 //If the membership & contribution is used in contribution page & not separate payment
65e172a3 1062 $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL;
9ce8bcfc 1063 $proceFieldAmount = 0;
8af73472 1064 if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment == 0) {
9da8dc8c 1065 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035
TO
1066 if ($is_quick_config) {
1067 foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) {
874c9be7 1068 if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) {
353ffa53 1069 $fieldId = $fieldVal['id'];
6a488035 1070 $fieldOption = $params['price_' . $fieldId];
9ce8bcfc 1071 $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
353ffa53 1072 $memPresent = TRUE;
6a488035
TO
1073 }
1074 else {
a7488080 1075 if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) {
6a488035
TO
1076 $fieldId = $fieldVal['id'];
1077 if ($fieldVal['name'] == 'other_amount') {
9ce8bcfc 1078 $proceFieldAmount += $this->_submitValues['price_' . $fieldId];
6a488035 1079 }
abcf506a 1080 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) {
9ce8bcfc 1081 $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
ce9024ca 1082 }
6a488035
TO
1083 unset($params['price_' . $fieldId]);
1084 break;
1085 }
1086 }
1087 }
1088 }
1089 }
874c9be7 1090
6a488035
TO
1091 if (!isset($params['amount_other'])) {
1092 $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params));
1093 }
1094
2f44fc96 1095 if (!empty($this->_ccid)) {
58466af1 1096 $this->set('lineItem', $this->_lineItem);
1097 }
1098 elseif ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) {
be2fb01f 1099 $lineItem = [];
481a74f4
TO
1100 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
1101 if ($is_quick_config) {
1102 foreach ($this->_values['fee'] as $key => & $val) {
387fd5aa 1103 if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) {
aefd7f6b
EM
1104 // Clean out any currency symbols.
1105 $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]);
353ffa53 1106 if ($params['price_' . $key] != 0) {
481a74f4 1107 foreach ($val['options'] as $optionKey => & $options) {
9c1bc317 1108 $options['amount'] = $params['price_' . $key] ?? NULL;
ef88f444
PC
1109 break;
1110 }
6a488035 1111 }
92fcb95f 1112 $params['price_' . $key] = 1;
6a488035
TO
1113 break;
1114 }
1115 }
1116 }
bdcbbfea 1117
6a488035 1118 if ($this->_membershipBlock) {
bdcbbfea 1119 $this->processAmountAndGetAutoRenew($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
1120 }
1121 else {
1122 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
6a488035 1123 }
ef88f444 1124
dc428161 1125 if ($params['tax_amount']) {
1126 $this->set('tax_amount', $params['tax_amount']);
1127 }
6a488035
TO
1128
1129 if ($proceFieldAmount) {
6a488035 1130 $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
c40e1ff4 1131 $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
dc428161 1132 if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
1133 $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
1134 }
6a488035 1135 if (!$this->_membershipBlock['is_separate_payment']) {
aefd7f6b
EM
1136 //require when separate membership not used
1137 $params['amount'] = $proceFieldAmount;
6a488035
TO
1138 }
1139 }
1140 $this->set('lineItem', $lineItem);
1141 }
1142
3a19e759 1143 if ($params['amount'] != 0 && (($this->_values['is_pay_later'] &&
1144 empty($this->_paymentProcessor) &&
1145 !array_key_exists('hidden_processor', $params)) ||
de6c59ca 1146 empty($params['payment_processor_id']))
3a19e759 1147 ) {
1148 $params['is_pay_later'] = 1;
1149 }
1150 else {
1151 $params['is_pay_later'] = 0;
1152 }
1153
1154 // Would be nice to someday understand the point of this set.
1155 $this->set('is_pay_later', $params['is_pay_later']);
1156 // assign pay later stuff
1157 $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
1158 $this->assign('is_pay_later', $params['is_pay_later']);
1159 if ($params['is_pay_later']) {
1160 $this->assign('pay_later_text', $this->_values['pay_later_text']);
1488ce4d 1161 $this->assign('pay_later_receipt', CRM_Utils_Array::value('pay_later_receipt', $this->_values));
3a19e759 1162 }
1163
8cc574cf 1164 if ($this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) {
6a488035 1165 $this->set('amount', $params['separate_amount']);
0db6c3e1
TO
1166 }
1167 else {
6a488035
TO
1168 $this->set('amount', $params['amount']);
1169 }
1170
1171 // generate and set an invoiceID for this transaction
1172 $invoiceID = md5(uniqid(rand(), TRUE));
1173 $this->set('invoiceID', $invoiceID);
3910048c 1174 $params['invoiceID'] = $invoiceID;
6489e3de
SL
1175 $title = !empty($this->_values['frontend_title']) ? $this->_values['frontend_title'] : $this->_values['title'];
1176 $params['description'] = ts('Online Contribution') . ': ' . ((!empty($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $title));
ec022878 1177 $params['button'] = $this->controller->getButtonName();
e6d4f555 1178 // required only if is_monetary and valid positive amount
6a488035 1179 if ($this->_values['is_monetary'] &&
1d1fee72 1180 !empty($this->_paymentProcessor) &&
ec57bf87 1181 ((float) $params['amount'] > 0.0 || $this->hasSeparateMembershipPaymentAmount($params))
6a488035 1182 ) {
0f2b049e 1183 // The concept of contributeMode is deprecated - as should be the 'is_monetary' setting.
3910048c 1184 $this->setContributeMode();
90102a32
EM
1185 // Really this setting of $this->_params & params within it should be done earlier on in the function
1186 // probably the values determined here should be reused in confirm postProcess as there is no opportunity to alter anything
1187 // on the confirm page. However as we are dealing with a stable release we go as close to where it is used
1188 // as possible.
1189 // In general the form has a lack of clarity of the logic of why things are set on the form in some cases &
1190 // the logic around when $this->_params is used compared to other params arrays.
1191 $this->_params = array_merge($params, $this->_params);
1192 $this->setRecurringMembershipParams();
f92fc7eb 1193 if ($this->_paymentProcessor &&
10cdf4cb 1194 $this->_paymentProcessor['object']->supports('preApproval')
f92fc7eb 1195 ) {
90102a32 1196 $this->handlePreApproval($this->_params);
6a488035 1197 }
38b66756 1198 }
3910048c 1199 }
6a488035 1200
3910048c
EM
1201 /**
1202 * Assign the billing mode to the template.
1203 *
1204 * This is required for legacy support for contributeMode in templates.
1205 *
1206 * The goal is to remove this parameter & use more relevant parameters.
1207 */
1208 protected function setContributeMode() {
1209 switch ($this->_paymentProcessor['billing_mode']) {
1210 case CRM_Core_Payment::BILLING_MODE_FORM:
1211 $this->set('contributeMode', 'direct');
1212 break;
6a488035 1213
3910048c
EM
1214 case CRM_Core_Payment::BILLING_MODE_BUTTON:
1215 $this->set('contributeMode', 'express');
1216 break;
6a488035 1217
3910048c
EM
1218 case CRM_Core_Payment::BILLING_MODE_NOTIFY:
1219 $this->set('contributeMode', 'notify');
1220 break;
6a488035 1221 }
874c9be7 1222
6a488035 1223 }
7bf9cde2 1224
3910048c
EM
1225 /**
1226 * Process confirm function and pass browser to the thank you page.
1227 */
1228 protected function skipToThankYouPage() {
1229 // call the post process hook for the main page before we switch to confirm
1230 $this->postProcessHook();
1231
1232 // build the confirm page
1233 $confirmForm = &$this->controller->_pages['Confirm'];
1234 $confirmForm->preProcess();
1235 $confirmForm->buildQuickForm();
1236
1237 // the confirmation page is valid
1238 $data = &$this->controller->container();
1239 $data['valid']['Confirm'] = 1;
1240
1241 // confirm the contribution
1242 // mainProcess calls the hook also
1243 $confirmForm->mainProcess();
1244 $qfKey = $this->controller->_key;
1245
1246 // redirect to thank you page
1247 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE));
aefd7f6b
EM
1248 }
1249
eccb4502 1250 /**
1251 * Set form variables if contribution ID is found
1252 */
1253 public function assignFormVariablesByContributionID() {
1254 if (empty($this->_ccid)) {
1255 return;
1256 }
58b0963c
JP
1257 if (!$this->getContactID()) {
1258 CRM_Core_Error::statusBounce(ts("Returning since there is no contact attached to this contribution id."));
1259 }
eccb4502 1260
26085eab 1261 $paymentBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($this->_ccid);
eccb4502 1262 //bounce if the contribution is not pending.
26085eab 1263 if ((int) $paymentBalance <= 0) {
eccb4502 1264 CRM_Core_Error::statusBounce(ts("Returning since contribution has already been handled."));
1265 }
26085eab 1266 if (!empty($paymentBalance)) {
1267 $this->_pendingAmount = $paymentBalance;
eccb4502 1268 $this->assign('pendingAmount', $this->_pendingAmount);
1269 }
1270
1271 if ($taxAmount = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_ccid, 'tax_amount')) {
1272 $this->set('tax_amount', $taxAmount);
1273 $this->assign('taxAmount', $taxAmount);
1274 }
1275
1276 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_ccid);
1277 foreach (array_keys($lineItems) as $id) {
1278 $lineItems[$id]['id'] = $id;
1279 }
1280 $itemId = key($lineItems);
1281 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
1282 $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1283 }
1284
1285 if (!empty($lineItems[$itemId]['price_field_id'])) {
1286 $this->_lineItem[$this->_priceSetId] = $lineItems;
1287 }
1288 $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
1289 $this->assign('lineItem', $this->_lineItem);
1290 $this->assign('is_quick_config', $isQuickConfig);
1291 $this->assign('priceSetID', $this->_priceSetId);
1292 }
1293
8cb12ff1 1294 /**
1295 * Function for unit tests on the postProcess function.
1296 *
1297 * @param array $params
ec57bf87 1298 *
1299 * @throws \CiviCRM_API3_Exception
8cb12ff1 1300 */
1301 public function testSubmit($params) {
1302 $_SERVER['REQUEST_METHOD'] = 'GET';
1303 $this->controller = new CRM_Contribute_Controller_Contribution();
1304 $this->submit($params);
1305 }
1306
ec57bf87 1307 /**
1308 * Has a separate membership payment amount been configured.
1309 *
1310 * @param array $params
1311 *
1312 * @return mixed
1313 * @throws \CiviCRM_API3_Exception
1314 */
1315 protected function hasSeparateMembershipPaymentAmount($params) {
1316 return $this->_separateMembershipPayment && (int) CRM_Member_BAO_MembershipType::getMembershipType($params['selectMembership'])['minimum_fee'];
1317 }
1318
6a488035 1319}