diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 4cbc3f023be3..7adc12737b3a 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -314,6 +314,22 @@ public static function getEvents( 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 a6cf7139aa92..8cd771834c2f 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -178,6 +178,13 @@ protected function getEventID(): int { */ 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 @@ public function preProcess() { //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 3e01e84dcd25..303f406bce84 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -97,6 +97,25 @@ public function preProcess() { $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,6 +272,17 @@ public function buildQuickForm() { $this->assign('amounts', $this->_amount); $this->assign('totalAmount', $this->_totalAmount); $this->set('totalAmount', $this->_totalAmount); + + $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); + CRM_Core_Payment_ProcessorForm::buildQuickForm($this); + $this->addPaymentProcessorFieldsToForm(); + } } if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { @@ -317,7 +347,10 @@ public function buildQuickForm() { } $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); @@ -359,6 +392,12 @@ public static function formRule($fields, $files, $form) { } } + 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 8b0b0263498a..3b35e681c019 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -355,17 +355,6 @@ public function buildQuickForm() { // CRM-18399: used by template to pass pre profile id as a url arg $this->assign('custom_pre_id', $this->_values['custom_pre_id']); - // Required for currency formatting in the JS layer - - // Required for currency formatting in the JS layer - // this is a temporary fix intended to resolve a regression quickly - // And assigning moneyFormat for js layer formatting - // will only work until that is done. - // https://github.com/civicrm/civicrm-core/pull/19151 - $this->assign('moneyFormat', CRM_Utils_Money::format(1234.56)); - - CRM_Core_Payment_ProcessorForm::buildQuickForm($this); - $contactID = $this->getContactID(); $this->assign('contact_id', $contactID); if ($contactID) { @@ -432,7 +421,17 @@ public function buildQuickForm() { $this->assign('isAdditionalParticipants', $isAdditionalParticipants); if ($this->_values['event']['is_monetary']) { + // Required for currency formatting in the JS layer + // this is a temporary fix intended to resolve a regression quickly + // And assigning moneyFormat for js layer formatting + // will only work until that is done. + // https://github.com/civicrm/civicrm-core/pull/19151 + $this->assign('moneyFormat', CRM_Utils_Money::format(1234.56)); self::buildAmount($this); + if (!$this->showPaymentOnConfirm) { + CRM_Core_Payment_ProcessorForm::buildQuickForm($this); + $this->addPaymentProcessorFieldsToForm(); + } } if ($contactID === 0 && !$this->_values['event']['is_multiple_registrations']) { @@ -440,12 +439,7 @@ public function buildQuickForm() { $this->addCIDZeroOptions(); } - if ($this->_values['event']['is_monetary']) { - $this->addPaymentProcessorFieldsToForm(); - } - $this->addElement('hidden', 'bypass_payment', NULL, ['id' => 'bypass_payment']); - $this->assign('bypassPayment', $bypassPayment); if (!$contactID) { @@ -913,7 +907,9 @@ public static function formRule($fields, $files, $form) { 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 c7481cb2f2c0..d3c871619c07 100644 --- a/settings/Event.setting.php +++ b/settings/Event.setting.php @@ -36,4 +36,22 @@ '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'], + ], ]; diff --git a/templates/CRM/Event/Form/Registration/Confirm.tpl b/templates/CRM/Event/Form/Registration/Confirm.tpl index 20fa9774f0be..2cb7a3bded7f 100644 --- a/templates/CRM/Event/Form/Registration/Confirm.tpl +++ b/templates/CRM/Event/Form/Registration/Confirm.tpl @@ -46,6 +46,26 @@ {/if} + {if $showPaymentOnConfirm} +