We have a Custom Hook for Mass Payment Merged Invoice Auto Cancellation.
Reason?
This issue arises due to a known bug in the WHMCS Mass Payment feature. The feature enables clients to pay multiple unpaid invoices simultaneously. However, the problem occurs when a client selects multiple invoices (e.g., Invoice1 and Invoice2) for payment using Mass Pay. WHMCS generates a third invoice (Invoice3) to consolidate the payment. If the client decides not to proceed with the payment, Invoice3 remains in the system. Additionally, Invoice3 does not appear in the client area, causing complications (e.g. the invoice3 will not be showing in the invoice page but will be shown in the client area home page as a unpaid/due amount causing confusion).
Workarounds
We have made a WHMCS hook to automatically cancel Invoice3 if the Any order/invoice from MassPay invoice (invoice3) are cancelled, the merged order in Mass Payment will be automatically cancelled. This prevents such invoices from lingering in the system and being shown in the client area home page.
Hook Code.
add_hook('InvoiceCancelled', 1, function($vars) {
$invoice_id = $vars['invoiceid'];
$invoice_data = localAPI('GetInvoice',['invoiceid' => $invoice_id]);
$client_id = $invoice_data['userid'];
$mass_invoice_id = Capsule::table('tblinvoiceitems')
->where('userid', $client_id)
->where('type', 'Invoice')
->where('relid', $invoice_id)
->value('invoiceid');
Capsule::table('tblinvoices')->where('id', $mass_invoice_id)->update([
'status' => 'Cancelled'
]);
});