You can configure your WHMCS in order to automatically apply any available credit on a user's account to new recurring invoices immediately upon generation.
(For further details about usual WHMCS management of prepaid credit: https://docs.whmcs.com/Credit/Prefunding
Usually it works fine, and it's useful for current service management: the user does a prepaid credit refill, and then just waits for his services and domains going to automatically renew when necessary, using this available prepaid credit.
But an issue occurs when the available is not sufficient to fully pay an invoice; even more, in such a case many different issues may occur.
Example:
- the user has a domain going to be renewed; the renewal cost is 15.00
- the user has just 14.90 of available prepaid credit
- the system issues an invoice for the renewal of the invoice; total invoice amount 15.00, the system applies the available credit (14.90), total due 0.10
- at the best, the user will log in, and pay the missing 0.10 (and PayPal will apply 0.35 to you for this single transaction: so you'll have a loss of 0.25 ...)
- If the user does not pay, the domain will expire; the invoice will be canceled, but the applied credit will not be refunded to the user (who, when will discover it, will legitly claim)
This is just an example: many other issues may occur, even more difficult than this...
In order to fix this issue, and allow WHMCS to apply credit only when it's sufficient to fully pay an invoice, it's sufficient this hook:
//
// remove applied credit to invoices, when credit applied is not sufficient to fully pay
//
function remove_credit_from_new_invoice($vars) {
if ($vars['status']=="Paid") { return; }
$postData=array('invoiceid' => $vars['invoiceid'], );
$invoicedata = localAPI('GetInvoice', $postData);
if ( $invoicedata['credit'] != 0 AND $invoicedata['balance'] != 0 ) {
$postData = array(
'invoiceid' => $vars['invoiceid'],
'amount' => - $invoicedata['credit'],
);
$res = localAPI('ApplyCredit', $postData);
}
}
add_hook('InvoiceCreationPreEmail', 5, "remove_credit_from_new_invoice" );