diff --git a/CRM/Extrafee/Fee.php b/CRM/Extrafee/Fee.php index 570a8bc..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']); } /** @@ -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..6011fc3 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); @@ -65,9 +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'])) { + $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(); + } + } } } @@ -87,7 +108,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 +121,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) {