From 7d3506d86d4d7ff8d29781913f07a0a43d6da291 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Thu, 24 Nov 2022 17:57:11 +0000 Subject: [PATCH] Add setting to control whether event payment is on main or confirm page --- CRM/Event/BAO/Event.php | 16 +++++++++ CRM/Event/Form/Registration.php | 9 ++++++ CRM/Event/Form/Registration/Confirm.php | 41 ++++++++++++++++++++---- CRM/Event/Form/Registration/Register.php | 10 ++++-- settings/Event.setting.php | 18 +++++++++++ 5 files changed, 84 insertions(+), 10 deletions(-) diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 4cbc3f023b..7adc12737b 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -314,6 +314,22 @@ WHERE ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0 )"; return $events; } + /** + * Returns an array of event pages [id => title] + * @return array + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public static function getEventsForSelect2() { + return ['all' => ts('- all -')] + + \Civi\Api4\Event::get(FALSE) + ->addSelect('id', 'title') + ->addWhere('is_active', '=', TRUE) + ->execute() + ->indexBy('id') + ->column('title'); + } + /** * Get events Summary. * diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index a6cf7139aa..8cd771834c 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -178,6 +178,13 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { */ public $paymentInstrumentID; + /** + * Should the payment element be shown on the confirm page instead of the first page? + * + * @var bool + */ + protected $showPaymentOnConfirm = FALSE; + /** * Set variables up before form is built. */ @@ -218,6 +225,8 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { //get the additional participant ids. $this->_additionalParticipantIds = $this->get('additionalParticipantIds'); + $this->showPaymentOnConfirm = (in_array($this->_eventId, \Civi::settings()->get('event_show_payment_on_confirm')) || in_array('all', \Civi::settings()->get('event_show_payment_on_confirm'))); + if (!$this->_values) { // this is the first time we are hitting this, so check for permissions here if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) { diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index d129e8b009..303f406bce 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -97,6 +97,25 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $this->set('params', $this->_params); } + public function setDefaultValues() { + if (!$this->showPaymentOnConfirm) { + return []; + } + // Set default payment processor as default payment_processor radio button value + if (!empty($this->_paymentProcessors)) { + foreach ($this->_paymentProcessors as $pid => $value) { + if (!empty($value['is_default'])) { + $defaults['payment_processor_id'] = $pid; + break; + } + } + } + if (!empty($this->_values['event']['is_pay_later']) && empty($this->_defaults['payment_processor_id'])) { + $defaults['is_pay_later'] = 1; + } + return $defaults ?? []; + } + /** * Pre process function for Paypal Express confirm. * @todo this is just a step in refactor as payment processor specific code does not belong in generic forms @@ -253,18 +272,17 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $this->assign('amounts', $this->_amount); $this->assign('totalAmount', $this->_totalAmount); $this->set('totalAmount', $this->_totalAmount); - $this->assign('showPaymentOnConfirm', FALSE); - //if (!empty($this->_values['event']['is_multiple_registrations'])) { + $showPaymentOnConfirm = (in_array($this->_eventId, \Civi::settings()->get('event_show_payment_on_confirm')) || in_array('all', \Civi::settings()->get('event_show_payment_on_confirm'))); + $this->assign('showPaymentOnConfirm', $showPaymentOnConfirm); + if ($showPaymentOnConfirm) { $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later'); $this->setPayLaterLabel($isPayLater ? $this->_values['event']['pay_later_text'] : ''); $this->_paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_values['event']['payment_processor'] ?? NULL); $this->assignPaymentProcessor($isPayLater); - - $this->assign('showPaymentOnConfirm', TRUE); - $this->addPaymentProcessorFieldsToForm(); CRM_Core_Payment_ProcessorForm::buildQuickForm($this); - // } + $this->addPaymentProcessorFieldsToForm(); + } } if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { @@ -329,7 +347,10 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { } $this->setDefaults($defaults); - //$this->freeze(); + $showPaymentOnConfirm = (in_array($this->_eventId, \Civi::settings()->get('event_show_payment_on_confirm')) || in_array('all', \Civi::settings()->get('event_show_payment_on_confirm'))); + if (!$showPaymentOnConfirm) { + $this->freeze(); + } //lets give meaningful status message, CRM-4320. $this->assign('isOnWaitlist', $this->_allowWaitlist); @@ -371,6 +392,12 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { } } + if ($form->showPaymentOnConfirm && empty($form->_requireApproval) && !empty($form->_totalAmount) + && $form->_totalAmount > 0 && !isset($fields['payment_processor_id']) + ) { + $errors['payment_processor_id'] = ts('Please select a Payment Method'); + } + return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index e901dcb12d..3b35e681c0 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -428,8 +428,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { // https://github.com/civicrm/civicrm-core/pull/19151 $this->assign('moneyFormat', CRM_Utils_Money::format(1234.56)); self::buildAmount($this); - //CRM_Core_Payment_ProcessorForm::buildQuickForm($this); - //$this->addPaymentProcessorFieldsToForm(); + if (!$this->showPaymentOnConfirm) { + CRM_Core_Payment_ProcessorForm::buildQuickForm($this); + $this->addPaymentProcessorFieldsToForm(); + } } if ($contactID === 0 && !$this->_values['event']['is_multiple_registrations']) { @@ -905,7 +907,9 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { if ($form->_values['event']['is_monetary']) { if (empty($form->_requireApproval) && !empty($fields['amount']) && $fields['amount'] > 0 && !isset($fields['payment_processor_id'])) { - // $errors['payment_processor_id'] = ts('Please select a Payment Method'); + if (!$form->showPaymentOnConfirm) { + $errors['payment_processor_id'] = ts('Please select a Payment Method'); + } } if (self::isZeroAmount($fields, $form)) { diff --git a/settings/Event.setting.php b/settings/Event.setting.php index c7481cb2f2..d3c871619c 100644 --- a/settings/Event.setting.php +++ b/settings/Event.setting.php @@ -36,4 +36,22 @@ return [ 'help_text' => NULL, 'pseudoconstant' => ['callback' => 'CRM_Core_SelectValues::getDashboardEntriesCount'], ], + 'event_show_payment_on_confirm' => [ + 'name' => 'event_show_payment_on_confirm', + 'settings_pages' => ['event' => ['weight' => 100]], + 'type' => 'Array', + 'default' => [], + 'add' => '5.58', + 'title' => ts('EXPERIMENTAL: Show Event Payment on Confirm?'), + 'html_type' => 'select', + 'html_attributes' => [ + 'class' => 'crm-select2', + 'multiple' => TRUE, + ], + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => ts('Should payment element be shown on the confirmation page instead of the first page?'), + 'help_text' => NULL, + 'pseudoconstant' => ['callback' => 'CRM_Event_BAO_Event::getEventsForSelect2'], + ], ]; -- 2.25.1