Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move event payment to the confirmation page #24781

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
@@ -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.
*
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')) {
41 changes: 40 additions & 1 deletion 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,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;
}

30 changes: 13 additions & 17 deletions CRM/Event/Form/Registration/Register.php
Original file line number Diff line number Diff line change
@@ -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,20 +421,25 @@ 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']) {
//@todo we are blocking for multiple registrations because we haven't tested
$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)) {
18 changes: 18 additions & 0 deletions settings/Event.setting.php
Original file line number Diff line number Diff line change
@@ -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'],
],
];
20 changes: 20 additions & 0 deletions templates/CRM/Event/Form/Registration/Confirm.tpl
Original file line number Diff line number Diff line change
@@ -46,6 +46,26 @@
</div>
{/if}

{if $showPaymentOnConfirm}
<div class="crm-group event_info-group">
<div class="header-dark">
{ts}Payment details{/ts}
</div>
{if !empty($form.payment_processor_id.label)}
<fieldset class="crm-public-form-item crm-group payment_options-group" style="display:none;">
<legend>{ts}Payment Options{/ts}</legend>
<div class="crm-section payment_processor-section">
<div class="label">{$form.payment_processor_id.label}</div>
<div class="content">{$form.payment_processor_id.html}</div>
<div class="clear"></div>
</div>
</fieldset>
{/if}
{include file='CRM/Core/BillingBlockWrapper.tpl'}
{literal}<script>function calculateTotalFee() { return {/literal}{$totalAmount}{literal} }</script>{/literal}
</div>
{/if}

<div class="crm-group event_info-group">
<div class="header-dark">
{ts}Event Information{/ts}