Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setting to control whether event payment is on main or confirm page
Browse files Browse the repository at this point in the history
mattwire committed Nov 24, 2022
1 parent 9ed1bee commit e9ef826
Showing 5 changed files with 96 additions and 23 deletions.
16 changes: 16 additions & 0 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
@@ -322,6 +322,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.
*
9 changes: 9 additions & 0 deletions CRM/Event/Form/Registration.php
Original file line number Diff line number Diff line change
@@ -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')) {
67 changes: 47 additions & 20 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
@@ -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,18 +272,17 @@ public function buildQuickForm() {
$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 @@ 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);
@@ -345,33 +366,39 @@ public function buildQuickForm() {
*
* @param array $fields
* @param array $files
* @param CRM_Core_Form $self
* @param \CRM_Event_Form_Registration_Confirm $form
*
* @return array|bool
*/
public static function formRule($fields, $files, $self) {
public static function formRule($fields, $files, $form) {
$errors = [];
$eventFull = CRM_Event_BAO_Participant::eventFull($self->_eventId, FALSE, CRM_Utils_Array::value('has_waitlist', $self->_values['event']));
if ($eventFull && empty($self->_allowConfirmation)) {
if (empty($self->_allowWaitlist)) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "reset=1&id={$self->_eventId}", FALSE, NULL, FALSE, TRUE));
$eventFull = CRM_Event_BAO_Participant::eventFull($form->_eventId, FALSE, CRM_Utils_Array::value('has_waitlist', $form->_values['event']));
if ($eventFull && empty($form->_allowConfirmation)) {
if (empty($form->_allowWaitlist)) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "reset=1&id={$form->_eventId}", FALSE, NULL, FALSE, TRUE));
}
}
$self->_feeBlock = $self->_values['fee'];
CRM_Event_Form_Registration_Register::formatFieldsForOptionFull($self);
$form->_feeBlock = $form->_values['fee'];
CRM_Event_Form_Registration_Register::formatFieldsForOptionFull($form);

if (!empty($self->_priceSetId) &&
!$self->_requireApproval && !$self->_allowWaitlist
if (!empty($form->_priceSetId) &&
!$form->_requireApproval && !$form->_allowWaitlist
) {
$priceSetErrors = self::validatePriceSet($self, $self->_params);
if (!empty($priceSetErrors)) {
$errors = self::validatePriceSet($form, $form->_params);
if (!empty($errors)) {
CRM_Core_Session::setStatus(ts('You have been returned to the start of the registration process and any sold out events have been removed from your selections. You will not be able to continue until you review your booking and select different events if you wish.'), ts('Unfortunately some of your options have now sold out for one or more participants.'), 'error');
CRM_Core_Session::setStatus(ts('Please note that the options which are marked or selected are sold out for participant being viewed.'), ts('Sold out:'), 'error');
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "_qf_Register_display=true&qfKey={$fields['qfKey']}"));
}
}

return empty($priceSetErrors) ? TRUE : $priceSetErrors;
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;
}

/**
10 changes: 7 additions & 3 deletions CRM/Event/Form/Registration/Register.php
Original file line number Diff line number Diff line change
@@ -428,8 +428,10 @@ public function buildQuickForm() {
// 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 @@ 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)) {
17 changes: 17 additions & 0 deletions settings/Event.setting.php
Original file line number Diff line number Diff line change
@@ -36,4 +36,21 @@
'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' => [],
'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'],
],
];

0 comments on commit e9ef826

Please sign in to comment.