I can not comment on the legality of it but I think it is doable with hooks.
<?php
add_hook('InvoiceChangeGateway', 1, function($vars) {
// Perform hook code here...
$invoiceid = $vars['invoiceid'];
$get_invoice_results = localAPI('GetInvoice', ['invoiceid' => $invoiceid]);
if ($get_invoice_results->result == 'success') {
if ($vars['paymentmethod'] == 'CHANGE_ME_expensive_gateway_id') {
// Add additional gateway fee.
$invoice_itmes = $get_invoice_results->items->item;
$number_items = count($invoice_itmes);
$update_invoice_results = localAPI('UpdateInvoice', ['invoiceid' => $invoiceid, 'itemdescription' => [$number_items => 'Gateway Fee Description'], 'itemamount' => [$number_items => 5.00], 'itemtaxed' => [$number_items => true]]);
if (!($update_invoice_results->result == 'success')) {
// We should never get here so notify someone who cares.
}
} else {
// Remove gateway fee from invoice, when client selects alternative gateway.
// You will need to reverse the above code to remove the gateway fee.
}
} else {
// We should never get here so notify someone who cares.
}
});
I have not tested the above code but that is my suggestion.
I think, if I remember, correctly the code will also only work on PHP 5.4+ ... and should be WHMCS 6.x+ safe.