LosBooom Posted October 3, 2019 Share Posted October 3, 2019 Hello. Is it possible to automatically repay invoice on the last day of the service, if there are funds on the account? Customers complain about this situation: invoice to pay for the service comes on the 15th. The client does not have funds on the balance, therefore, automatic payment does not work. Before the end of the service, the client replenishes his balance but does not manually pay the bill. As a result, at the end of the month the service is suspended, although there were funds on the account. Maybe there is a standard solution, or can someone help with writing such a script? 0 Quote Link to comment Share on other sites More sharing options...
LosBooom Posted October 3, 2019 Author Share Posted October 3, 2019 So my idea: Go through the list of all unpaid invoice whose duedate = today. The code: $ command = 'GetInvoices'; $ postData = array ( 'status' => 'Unpaid', 'limitnum' => 25, ‘duedate’ => $ today; 'orderby' => 'invoicenumber', ); $ results = localAPI ($ command, $ postData); if ($ results ['result'] == 'success') { $ total_unpaid_invoices = $ results ['totalresults']; echo "Total number of unpaid invoices". $ total_unpaid_invoices. "<br>"; foreach ($ results ['invoices'] ['invoice'] as $ invoice) { echo $ invoice ['userid']; echo $ invoice ['duedate']; echo $ invoice ['total']; } } Is there an api call to automatically pay invoice and debit from the user's balance or do I need to write my own code? 0 Quote Link to comment Share on other sites More sharing options...
LosBooom Posted October 6, 2019 Author Share Posted October 6, 2019 So i managed it myself. If anyone in future will need something similar to this: there is my solution: <?php $whmcsRoot = realpath(dirname(__FILE__)."/../").DIRECTORY_SEPARATOR; require_once($whmcsRoot."init.php"); $today = date('Y-m-d'); $unpaid_invoices = localAPI('GetInvoices', array( 'status' => 'Unpaid', 'limitnum' => 5000, 'orderby' => 'duedate', )); $unpaid_invoices_result = $unpaid_invoices['result']; // get array of all unpaid invoices if ($unpaid_invoices_result == 'success') { foreach ($unpaid_invoices['invoices']['invoice'] as $invoice) { $invoice_id = $invoice['id']; $invoice_total = $invoice['total']; // last day of payment invoice if ($invoice['duedate'] == $today) { // try to pay invoice from user balance $apply_invoice = localAPI('ApplyCredit', array( 'invoiceid' => $invoice_id, 'amount' => $invoice_total, )); if ($apply_invoice['result'] == 'success') { $apply_invoice_id = $apply_invoice['invoiceid']; logActivity("Automatic Invoice Closure: Invoice automatically $apply_invoice_id paid"); } else { $error_msg = $apply_invoice['message']; logActivity("Automatic Invoice Closure: ERROR: $invoice_id: $error_msg"); } } } } else { logActivity("Automatic Invoice Closure: ERROR: $unpaid_invoices_result"); } exit; 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.