Jump to content
Andrea D.

Hook for WHMCS which will cancel overdue invoices automatically after x days

Recommended Posts

Hello everyone!

I  have found this hook on Github https://github.com/wolkenbauer/whmcs-cancelinvoice-hook and it seems to be not working. I reported that on GitHub on 11 Jun but no one replied to me.

Maybe one of you can easily understand why it is not working?

 

Code:

 

<?php

if (!defined("WHMCS"))
    die("This file cannot be accessed directly");

use WHMCS\Database\Capsule;

add_hook('DailyCronJob',1,function($vars) 
{
//
// Change $overduedays to whatever you need. Default is cancel all invoices after being overdue for 90 days.
//
$overduedays=90;    
$invoices=Capsule::table("tblinvoices")->where("status","Unpaid")->where("duedate","<",date("Y-m-d",strtotime("-$overduedays days")))->get();

foreach($invoices as $invoice)
{
  //use UpdateInvoice API Call for changing the status, otherwise the InvoiceCancelled hook will not trigger 
  //and may affect other modules/apps which rely on this hook.
  $results = localAPI("UpdateInvoice",array("invoiceid"=>$invoice->id,"status"=>"Cancelled"));
  if ($results["result"]=="success") 
   logactivity("[INVOICECANCELHOOK] has cancelled Invoice ID: $invoice->id automatically.");
}
 
}); //hook


//process long pending orders the same way as overdue invoices

add_hook('DailyCronJob',2,function($vars) 
{
    
// As requested by someone, we can use this hook also for cancelling long pending orders (dead orders)
//
// Change $overduedays to whatever you need. Default is cancel all orders after pending for 14 days.
//
$overduedays=14;    
$orders=Capsule::table("tblorders")->where("status","Pending")->where("date","<",date("Y-m-d",strtotime("-$overduedays days")))->get();

foreach($orders as $order)
{
  //use CancelOrder API Call for changing the status
  $results = localAPI("CancelOrder",array("id"=>$order->id,"status"=>"Cancelled"));
  if ($results["result"]=="success") 
   logactivity("[ORDERCANCELHOOK] has cancelled Order ID: $order->id automatically.");
}

}); //hook

Thank you!

Share this post


Link to post
Share on other sites

Hi Andrea,

The following hook will work, just create a new file in your includes/hooks/ directory and paste the following code:

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

function cancel_overdue_invoices($vars) {

    # How many days after an invoice's due date it should be cancelled.
    $duration = 90;

    # Get all invoices that are unpaid and meet the criteria
    $invoices = Capsule::table('tblinvoices')->where('status', 'Unpaid')->where('duedate', '<=', date('Y-m-d', strtotime("-{$duration} days")))->get();

    # Loop through the invoices
    foreach ($invoices as $invoice) {

        # Update their statuses
        Capsule::table('tblinvoices')->where('id', $invoice->id)->update([
            'status' => 'Cancelled'
        ]);

    }

}

add_hook('DailyCronJob', 1, 'cancel_overdue_invoices');

?>

To change the amount of days from the due date before the invoice should be cancelled, simply change

$duration = 90;

(e.g. 90 days is the default as shown above)

 

The hook is set to run once a day on the daily cron, if you prefer it runs on the more regular cron (however long you've set it up to run, then change the bottom part of the code from

add_hook('DailyCronJob', 1, 'cancel_overdue_invoices');

To the following

add_hook('AfterCronJob', 1, 'cancel_overdue_invoices');

This hook can also be viewed on my GitHub: https://github.com/leemahoney3/whmcs-cancel-overdue-invoices

Share this post


Link to post
Share on other sites

What do you mean by "not working"?  What are the error messages?

Usually hook code has to be installed in a file somewhere.  Where did you put this code?

Share this post


Link to post
Share on other sites
4 hours ago, brianoz said:

What do you mean by "not working"?  What are the error messages?

Usually hook code has to be installed in a file somewhere.  Where did you put this code?

I have added it in /includes/hooks with same permissions as the example file. There are no logs showing on the front end. Where should I look at?

Share this post


Link to post
Share on other sites

I'd start by checking the file for syntax errors. Also worth changing logactivity to logActivity - case does matter in PHP, though WHMCS might have a logactivity function defined to protect you from errors.

Share this post


Link to post
Share on other sites

Also, worth adding a logActivity() call when there's a failure, like:

logActivity("FAILED xyz - results are: " . print_r($results, 1));

At the moment you just have a log entry on success.  Having log errors on failure might help you work out the problem.  If you don't get an activity log error at all, that probably means your file isn't running.  Make sure the file is mode 644 (rw-r--r--) and ends in .php.

I'd also be checking for error log files being created, usually in the admin folder - they're always called "error_log",  The following command can find those files anywhere on your site, if you change directory to be in the root of your WHMCS folder tree:

find . -name error_log -print

 

Share this post


Link to post
Share on other sites
On 7/31/2022 at 7:58 PM, leemahoney3 said:

Hi Andrea,

The following hook will work, just create a new file in your includes/hooks/ directory and paste the following code:


<?php

use Illuminate\Database\Capsule\Manager as Capsule;

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

function cancel_overdue_invoices($vars) {

    # How many days after an invoice's due date it should be cancelled.
    $duration = 90;

    # Get all invoices that are unpaid and meet the criteria
    $invoices = Capsule::table('tblinvoices')->where('status', 'Unpaid')->where('duedate', '<=', date('Y-m-d', strtotime("-{$duration} days")))->get();

    # Loop through the invoices
    foreach ($invoices as $invoice) {

        # Update their statuses
        Capsule::table('tblinvoices')->where('id', $invoice->id)->update([
            'status' => 'Cancelled'
        ]);

    }

}

add_hook('DailyCronJob', 1, 'cancel_overdue_invoices');

?>

To change the amount of days from the due date before the invoice should be cancelled, simply change


$duration = 90;

(e.g. 90 days is the default as shown above)

 

The hook is set to run once a day on the daily cron, if you prefer it runs on the more regular cron (however long you've set it up to run, then change the bottom part of the code from


add_hook('DailyCronJob', 1, 'cancel_overdue_invoices');

To the following


add_hook('AfterCronJob', 1, 'cancel_overdue_invoices');

This hook can also be viewed on my GitHub: https://github.com/leemahoney3/whmcs-cancel-overdue-invoices

working!

Share this post


Link to post
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