Jump to content

Automatically withdraw funds on the last day


LosBooom

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated