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

Add cc fee for additional participants as well #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions CRM/Extrafee/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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]);
Expand Down
35 changes: 32 additions & 3 deletions extrafee.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,38 @@ 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);
$ppExtraFeeSettings = json_decode(Civi::settings()->get('processor_extra_fee_settings'), TRUE);
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();
}
}
}
}

Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions templates/extra_fee.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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) {
Expand Down