From 5fce47a29f500f45c9ac31fdc515f118de642bfe Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Sat, 22 Apr 2023 19:16:37 +0530 Subject: [PATCH 1/3] Add cc fee for additional participants as well --- CRM/Extrafee/Fee.php | 15 ++++++++------- extrafee.php | 20 ++++++++++++++++++-- templates/extra_fee.tpl | 10 ++++++++-- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CRM/Extrafee/Fee.php b/CRM/Extrafee/Fee.php index 570a8bc..8d26a9f 100644 --- a/CRM/Extrafee/Fee.php +++ b/CRM/Extrafee/Fee.php @@ -73,14 +73,15 @@ public static function modifyTotalAmountInParams($formName, &$form, $extraFeeSet $form->set('amount', $form->_amount); } } - elseif ($formName == 'CRM_Event_Form_Registration_Register') { + elseif ($formName == 'CRM_Event_Form_Registration_Register' || $formName == 'CRM_Event_Form_Registration_AdditionalParticipant') { $params = $form->getVar('_params'); - if (!empty($params[0]['amount'])) { - $params[0]['amount'] += $params[0]['amount'] * $percent/100 + $processingFee; - $params[0]['amount'] = number_format($params[0]['amount'], 2); - $form->setVar('_params', $params); - $form->set('params', $params); + foreach ($params as $key => &$value) { + if ($key == array_key_last($params) && !empty($value['amount'])) { + $value['amount'] += $value['amount'] * $percent/100 + $processingFee; + } } + $form->setVar('_params', $params); + $form->set('params', $params); } } @@ -118,7 +119,7 @@ public static function isFormEligibleForExtraFee($form, $extraFeeSettings, $ppEx * Get Extra fees overriden by payment processor. */ public static function getProcessorExtraFees() { - $ppExtraFeeSettings = json_decode(Civi::settings()->get('processor_extra_fee_settings'), TRUE); + $ppExtraFeeSettings = (array) json_decode(Civi::settings()->get('processor_extra_fee_settings'), TRUE); foreach ($ppExtraFeeSettings as $ppID => $pp) { if (empty($pp['percent']) && empty($pp['processing_fee'])) { unset($ppExtraFeeSettings[$ppID]); diff --git a/extrafee.php b/extrafee.php index b016f21..a42a93f 100644 --- a/extrafee.php +++ b/extrafee.php @@ -57,7 +57,11 @@ function extrafee_civicrm_buildForm($formName, &$form) { 'template' => "{$templatePath}/CRM/Extrafee/Form/processor_extra_fee.tpl" )); } - if (!in_array($formName, ['CRM_Contribute_Form_Contribution_Main', 'CRM_Event_Form_Registration_Register'])) { + if (!in_array($formName, [ + 'CRM_Contribute_Form_Contribution_Main', + 'CRM_Event_Form_Registration_Register', + 'CRM_Event_Form_Registration_AdditionalParticipant' + ])) { return; } $extraFeeSettings = json_decode(Civi::settings()->get('extra_fee_settings'), TRUE); @@ -66,6 +70,10 @@ function extrafee_civicrm_buildForm($formName, &$form) { return; } if (!empty($extraFeeSettings['percent']) || !empty($extraFeeSettings['processing_fee'])) { + $params = $form->getVar('_params'); + if ($formName == 'CRM_Event_Form_Registration_AdditionalParticipant' && !empty($params[0]['payment_processor_id'])) { + $form->assign('selected_payment_processor', $params[0]['payment_processor_id']); + } CRM_Extrafee_Fee::displayFeeMessage($form, $extraFeeSettings); CRM_Extrafee_Fee::addOptionalFeeCheckbox($form, $extraFeeSettings); } @@ -87,7 +95,11 @@ function extrafee_civicrm_postProcess($formName, &$form) { Civi::settings()->set('processor_extra_fee_settings', json_encode($ppExtraFeeSettings)); } - if (!in_array($formName, ['CRM_Contribute_Form_Contribution_Main', 'CRM_Event_Form_Registration_Register'])) { + if (!in_array($formName, [ + 'CRM_Contribute_Form_Contribution_Main', + 'CRM_Event_Form_Registration_Register', + 'CRM_Event_Form_Registration_AdditionalParticipant' + ])) { return; } $extraFeeSettings = json_decode(Civi::settings()->get('extra_fee_settings'), TRUE); @@ -96,6 +108,10 @@ function extrafee_civicrm_postProcess($formName, &$form) { return; } $ppID = $form->getVar('_paymentProcessorID'); + $params = $form->getVar('_params'); + if ($formName == 'CRM_Event_Form_Registration_AdditionalParticipant' && empty($ppID) && !empty($params[0]['payment_processor_id'])) { + $ppID = $params[0]['payment_processor_id']; + } if ((!empty($extraFeeSettings['percent']) || !empty($extraFeeSettings['processing_fee'])) && !empty($ppID) && empty($form->_ccid)) { CRM_Extrafee_Fee::modifyTotalAmountInParams($formName, $form, $extraFeeSettings, $ppID); } diff --git a/templates/extra_fee.tpl b/templates/extra_fee.tpl index 040acfc..90d8aaf 100644 --- a/templates/extra_fee.tpl +++ b/templates/extra_fee.tpl @@ -3,6 +3,7 @@ CRM.$(function($) { var processor_extrafee = {/literal} {if $processor_extra_fee_values} {$processor_extra_fee_values} {else} 0 {/if}{literal}; var extra_fee_settings = {/literal} {if $extra_fee_settings} {$extra_fee_settings} {else} 0 {/if}{literal}; + var selected_payment_processor = {/literal} {if $selected_payment_processor} {$selected_payment_processor} {else} '' {/if}{literal}; var isQuickConfig = {/literal}{$quick_config_display}{literal}; var payNowPayment = {/literal} {if $payNowPayment} {$payNowPayment} {else} 0 {/if}{literal}; @@ -35,7 +36,9 @@ CRM.$(function($) { $('#pricesetTotal').append(optional_input + msg); } - $('input#extra_fee_add').on('change', function() { displayTotalAmount(calculateTotalFee()); }); + $('input#extra_fee_add').on('change', function() { + displayTotalAmount(calculateTotalFee()); + }); /* * Thanks: https://stackoverflow.com/a/59268677/11400326 @@ -60,7 +63,7 @@ CRM.$(function($) { ( totalfee < 0 ) ? $('#pricelabel, #pricevalue').hide() : $('#pricelabel, #pricevalue').show(); } - function formatExtraFee(amount, c, d, t){ + function formatExtraFee(amount, c, d, t) { var n = amount, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, @@ -87,6 +90,9 @@ CRM.$(function($) { var pp = $('input[name=payment_processor_id]:checked').val(); if (typeof pp === 'undefined') { pp = $('input[name=payment_processor_id]').val(); + if (selected_payment_processor && typeof pp === 'undefined') { + pp = selected_payment_processor; + } } if (typeof pp !== 'undefined' && pp != 0 && totalFee) { if (addExtraFee) { From 61860fa17ba7dd43d2a5384f40d2c554ee7e5518 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Mon, 1 May 2023 13:06:50 +0530 Subject: [PATCH 2/3] Enable optional extrafee for additional participant --- extrafee.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extrafee.php b/extrafee.php index a42a93f..38b03ae 100644 --- a/extrafee.php +++ b/extrafee.php @@ -73,6 +73,10 @@ function extrafee_civicrm_buildForm($formName, &$form) { $params = $form->getVar('_params'); if ($formName == 'CRM_Event_Form_Registration_AdditionalParticipant' && !empty($params[0]['payment_processor_id'])) { $form->assign('selected_payment_processor', $params[0]['payment_processor_id']); + if (!empty($params[0]['extra_fee_add'])) { + $defaults['extra_fee_add'] = 1; + $form->setDefaults($defaults); + } } CRM_Extrafee_Fee::displayFeeMessage($form, $extraFeeSettings); CRM_Extrafee_Fee::addOptionalFeeCheckbox($form, $extraFeeSettings); From ad4220c1a10a2bf2d462de90332f6b31e45c4986 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Mon, 1 May 2023 15:02:41 +0530 Subject: [PATCH 3/3] additional fixes for additional participants --- CRM/Extrafee/Fee.php | 2 +- extrafee.php | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CRM/Extrafee/Fee.php b/CRM/Extrafee/Fee.php index 8d26a9f..c3b628a 100644 --- a/CRM/Extrafee/Fee.php +++ b/CRM/Extrafee/Fee.php @@ -41,7 +41,7 @@ public static function displayFeeMessage($form, $extraFeeSettings) { } public static function addOptionalFeeCheckbox($form, $extraFeeSettings) { - $form->add('checkbox', 'extra_fee_add', $extraFeeSettings['label']); + return $form->add('checkbox', 'extra_fee_add', $extraFeeSettings['label']); } /** diff --git a/extrafee.php b/extrafee.php index 38b03ae..6011fc3 100644 --- a/extrafee.php +++ b/extrafee.php @@ -69,17 +69,26 @@ function extrafee_civicrm_buildForm($formName, &$form) { if (!CRM_Extrafee_Fee::isFormEligibleForExtraFee($form, $extraFeeSettings, $ppExtraFeeSettings)) { return; } + $addOptionalCheckbox = TRUE; + $freeze = FALSE; if (!empty($extraFeeSettings['percent']) || !empty($extraFeeSettings['processing_fee'])) { $params = $form->getVar('_params'); if ($formName == 'CRM_Event_Form_Registration_AdditionalParticipant' && !empty($params[0]['payment_processor_id'])) { + $defaults['extra_fee_add'] = 1; + $form->setDefaults($defaults); + $freeze = TRUE; $form->assign('selected_payment_processor', $params[0]['payment_processor_id']); if (!empty($params[0]['extra_fee_add'])) { - $defaults['extra_fee_add'] = 1; - $form->setDefaults($defaults); + $addOptionalCheckbox = $freeze = FALSE; } } CRM_Extrafee_Fee::displayFeeMessage($form, $extraFeeSettings); - CRM_Extrafee_Fee::addOptionalFeeCheckbox($form, $extraFeeSettings); + if ($addOptionalCheckbox) { + $element = CRM_Extrafee_Fee::addOptionalFeeCheckbox($form, $extraFeeSettings); + if ($freeze) { + $element->freeze(); + } + } } }