Jump to content

InvoiceCreated Hook for auto invoices


servetas

Recommended Posts

Hello,

 

I have created a module which runs something when "InvoiceCreated" hook runs. I have noticed that the hooked function does not run when invoices are getting created by my system's cronjob. The same function runs hooked on the "InvoiceChangeGateway" without issues. Could you please let me know if the InvoiceCreated runs when invoices are getting created automatically from the cronjob?

 

For testing reasons, I have added a "mail()" inside this function to troubleshoot the issue. The email is sent when I change the invoice's payment gateway or when an invoice is getting created during the initial order but it is not sent when invoices are getting created by the cron job.

 

If there anyone else who might has come up with this issue?

Link to comment
Share on other sites

  • 1 month later...
Hello,

 

I have created a module which runs something when "InvoiceCreated" hook runs. I have noticed that the hooked function does not run when invoices are getting created by my system's cronjob. The same function runs hooked on the "InvoiceChangeGateway" without issues. Could you please let me know if the InvoiceCreated runs when invoices are getting created automatically from the cronjob?

 

For testing reasons, I have added a "mail()" inside this function to troubleshoot the issue. The email is sent when I change the invoice's payment gateway or when an invoice is getting created during the initial order but it is not sent when invoices are getting created by the cron job.

 

If there anyone else who might has come up with this issue?

 

Yes, this affects a custom module we have created as well. Our module is never notified of invoices being created from the cron task because the InvoiceCreated hook is never notified from the cron.

 

Whether this is by design, or an oversight, remains to be determined. Since WHMCS developers usually monitor the forums, perhaps they will enlighten us on this subject?

Link to comment
Share on other sites

from the v6 docs...

 

This hook is run when a new invoice has been generated by the cron, order process, API or when converting a quote to an invoice. The invoice has already been emailed to the client.

whether something's changed in v7, or if there is a bug, I don't know - you might find it quicker to contact support directly...

Link to comment
Share on other sites

Hi Brian,

 

Thanks for the input. That description you quoted would pertain to us as we are actually still on v6.2.0. However, I am definitely seeing our custom module fail at being notified of invoice creation during the cron. I have inserted quite a bit of debug logging code lines into our own module code base to trap what was being called and when. All of the code inside the InvoiceCreated hook point is NOT being executed when the cron runs. All other code in our module and WHMCS hook points executes as expected.

 

One would assume that there is a coding issue with our hook point, but if invoices are created via the API, or from within the web GUI, the InvoiceCreated hook point is called as documentation states and executes perfectly.

 

Weird to say the least. I will keep researching this and continue to hope WHMCS chimes in also. Thank you again! :0)

Link to comment
Share on other sites

  • 2 weeks later...

InvoiceCreated has been dodgy for me in the past. I think we need a WHMCS developer to come to a peek.

 

How does InvoiceCreation work for you? InvoiceCreation is before the invoice is sent. It has worked for me in the past (I'll do some testing later)

Link to comment
Share on other sites

  • 2 weeks later...
  • 5 months later...

We have confirmed that the hook point InvoiceCreated and InvoiceCreationPreEmail are NOT invoked from within the cron.  We implemented our own logging function inside of our addon to monitor every call into WHMCS--whether from the web client area, web admin area, api, or cron.  Each and every invocation of WHMCS is logged with our addon.  Then throughout our addon code, we output log lines to track what our code is or is not doing.  One of its very useful features is that it allows us to know for certain if WHMCS is notifying our code of events or not. 

We have hook points defined for all major invoice events: AddInvoiceLateFee, InvoiceCancelled, InvoiceCreated, InvoiceCreation, InvoiceCreationPreEmail, UpdateInvoiceTotal, etc.  Inside each of these hook points the code starts as follows (we are not showing all hook points):

add_hook("AddInvoiceLateFee", 1, $vars)  {

  OURLOG::writeLine("hook_AddInvoiceLateFee()->vars[invoiceid]=>" . $vars['invoiceid']);

  ..code to handle event omitted..

});

add_hook("InvoiceCreated", 1, $vars)  {

  OURLOG::writeLine("hook_InvoiceCreated()->vars[invoiceid]=>" . $vars['invoiceid']);

  ..code to handle event omitted..

});

add_hook("InvoiceCreation", 1, $vars)  {

  OURLOG::writeLine("hook_InvoiceCreation()->vars[invoiceid]=>" . $vars['invoiceid']);

  ..code to handle event omitted..

});

add_hook("UpdateInvoiceTotal", 1, $vars)  {

  OURLOG::writeLine("hook_UpdateInvoiceTotal()->vars[invoiceid]=>" . $vars['invoiceid']);

  ..code to handle event omitted..

});

Below is a sample output from our addon during a standard scheduled cron run.  An invoice was created during this particular cron and notice the log does include the hook points for InvoiceCreation and UpdateInvoiceTotal, but NOT for InvoiceCreated.
------------------------------------------------------------------------------
DATE: 10/08/2017 16:00
BACKTRACE:
/home/cpanel_account_removed/whmcs_secure_folder_removed/crons/cron.php
/home/cpanel_account_removed/whmcs_secure_folder_removed/crons/bootstrap.php
/home/cpanel_account_removed/whmcs_secure_folder_removed/init.php
/home/cpanel_account_removed/whmcs_secure_folder_removed/functions.php
/home/cpanel_account_removed/whmcs_secure_folder_removed/hookfunctions.php
/home/cpanel_account_removed/public_html/modules/addons/addon_module_name_removed/hooks.php
PARAMS: None
LOG:
hook_InvoiceCreation()->vars[invoiceid]=>24970
..other log lines from our addon omittted..
hook_updateInvoiceTotal()->vars[invoiceid]=>24970
..other log lines from our addon omittted..

------------------------------------------------------------------------------

We have learned you can use these reliably and maybe find a way to accomplish your goals without relying on InvoiceCreated or InvoiceCreationPreEmail.  Also, note our version of WHMCS is still 6.2 so we have not yet confirmed if this same scenario holds true for the latest version 7.3.  We have clients that have the latest WHMCS so we will run this test on their install and update with our findings...

Edited by Chance
Spelling and grammar
Link to comment
Share on other sites

  • 3 months later...

I just wanted to share my tests, hopefully they will be of help for others to verify the behaviour of hooks in cron executions.

I've tested the cron execution of the InvoiceCreationPreEmail hook and it is working on WHMCS 7.2.3.

Bear in mind that the cron.php script doesn't always process all the tasks, in particular the InvoiceCreation task is only executed once a day. The best way to test cron tasks individually is to run the cron script from terminal and make use of its different options: the docs for the cron script describe all of them (https://docs.whmcs.com/Crons#System_Cron_2).

In my case I tried hooking to the InvoiceCreationPreEmail hook, with a simple script to output the hook's variables.

add_hook('InvoiceCreationPreEmail', 1, function($vars) {
	print_r($vars);
	die();
});

To then test the execution from cron, I ran the cron.php script with these options:

php -q ./crons/cron.php do --CreateInvoices -v

The do option skips all the cron tasks except --CreateInvoices, while the -v option is obviously for a verbose output.

This is the result in my case:

# php -q ./crons/cron.php do --CreateInvoices -v
ea-php-cli Copyright 2017 cPanel, Inc.

WHMCS Automation Task Utility: do
=================================

Queuing Tasks
-------------

 Task queues ready

Executing Application Queue
---------------------------

 0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0% < 1 secArray
(
    [source] => autogen
    [user] => system
    [invoiceid] => 95
    [status] => Unpaid
)

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • 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