thebizbuilder Posted December 2, 2010 Share Posted December 2, 2010 Hi I am trying to create action hooks that run when invoice is set to paid or unpaid... I am testing this in the admin panel by simply generating an invoice and then setting it to paid or unpaid to trigger the action log. I have included my code here, if anyone would be so kind as to have a look that would be great. p.s. I have tested all the individual functions and they seem to be working correctly!? Thanks in advance! <?php /* ******************************************** * Hook file that will suspend all services * * connected to an account with an unpaid * * invoice. And will reactivate when invoice* * has been paid. * ******************************************** */ #Function that suspends all modules associated with an account. function suspend_all($vars) { #$vars['invoiceid'] is passed, must first get account number and then get any services/products associated with the account and suspend them using the WHMCS API //write function to get userid $userid = getUserid($vars['invoiceid']); //write function to get array of services/products (API POSSIBLY CAN USE action="getclientproducts") //includes suspention suspendAllservices($userid); } #Function that will unsuspend account when invoice payment has been made and no other outstanding invoices are existing function unsuspend_all($vars) { #$vars['invoiceid'] is passed, must check for any other unpaid outstanding invoices before continuing to unsuspend account //GET USERID $userid = getUserid($vars['invoiceid']); //USE API TO GET INVOICES, USE XML TO SELECT ONLY UNPAID THEN IF 0 CONTINUE TO UNSUSPEND if(getUnpaidInvoices($userid)==0){ //USE GET ALL SERVICES FUNCTION unsuspendAllservices($userid); } else { #function to send out all unpaid invoice emails? } } add_hook("InvoiceUnpaid",1,"suspend_all"); add_hook("InvoicePaid",1,"unsuspend_all"); function getUserid($invoiceid) { #FUNCTION TO RETURN USERID USING THE INVOICE ID mysql_connect('localhost', '*******', '*****'); mysql_select_db('whmcs'); $query = 'select * FROM `tblinvoices` WHERE `id` = "'.$invoiceid.'"'; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_array($result); return $row['userid']; } function suspendAllservices($userid) { #FUNCTION THAT USES THE WHMCS API TO GET ALL THE SERVICES $url = "http://*******/whmcs/includes/api.php"; # URL to WHMCS API file $username = "******"; # Admin username goes here $password = "******"; # Admin password goes here $postfields["username"] = $username; $postfields["password"] = md5($password); $postfields["action"] = "getclientsproducts"; #action performed by the API:Functions $postfields["clientid"] = $userid; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); $dom = new DOMDocument(); $dom->loadHTML($data); $services = $dom->getElementsByTagName('product'); foreach ($services as $elem) { $url = "*****/whmcs/includes/api.php"; # URL to WHMCS API file $username = "*****"; # Admin username goes here $password = "******"; # Admin password goes here $postfields["username"] = $username; $postfields["password"] = md5($password); $postfields["action"] = "modulesuspend"; #action performed by the API:Functions $postfields["accountid"] = $elem->getElementsByTagName('id') ->item(0) ->nodeValue; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); } } function unsuspendAllservices($userid) { #FUNCTION THAT USES THE WHMCS API TO GET ALL THE SERVICES $url = "*****/whmcs/includes/api.php"; # URL to WHMCS API file $username = "*****"; # Admin username goes here $password = "******"; # Admin password goes here $postfields["username"] = $username; $postfields["password"] = md5($password); $postfields["action"] = "getclientsproducts"; #action performed by the API:Functions $postfields["clientid"] = $userid; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); $dom = new DOMDocument(); $dom->loadHTML($data); $services = $dom->getElementsByTagName('product'); foreach ($services as $elem) { $url = "*****/whmcs/includes/api.php"; # URL to WHMCS API file $username = "****"; # Admin username goes here $password = "****"; # Admin password goes here $postfields["username"] = $username; $postfields["password"] = md5($password); $postfields["action"] = "unmodulesuspend"; #action performed by the API:Functions $postfields["accountid"] = $elem->getElementsByTagName('id') ->item(0) ->nodeValue; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); } } function getUnpaidInvoices($userid) { #function that gets all unpaid invoices returns qty of unpaid invoices mysql_connect('localhost', '******', '******'); mysql_select_db('whmcs'); $query = 'select * FROM `tblinvoices` WHERE `status` = "Unpaid"'; $result = mysql_query($query); return mysql_num_rows($result); } ?> 0 Quote Link to comment Share on other sites More sharing options...
thebizbuilder Posted December 4, 2010 Author Share Posted December 4, 2010 I still haven't managed to get the action hooks to work. To the action hooks get called even if u set an invoice to paid/unpaid in the admin panel?? I would assume that they do. Is there anything else that needs to be set to allow the action hooks to work, like in automation setting or something?? Thanks in advance 0 Quote Link to comment Share on other sites More sharing options...
thebizbuilder Posted December 6, 2010 Author Share Posted December 6, 2010 Appeard to be a small error in my api call, i was calling 'suspendunmodule' instead of the real one or something. One issue is with action hooks and api writing is error logging. Although my code was wrong there were no errors and i had to go through very carefully to find the errors! Oh well!.....thanks for all your help 0 Quote Link to comment Share on other sites More sharing options...
IntaHost-Steve Posted December 7, 2010 Share Posted December 7, 2010 Thanks for the feedback, even though the help seems slow in coming. I have struggled with the API in WHMCS quite a bit, it doesnt seem to be the best. 0 Quote Link to comment Share on other sites More sharing options...
thebizbuilder Posted December 7, 2010 Author Share Posted December 7, 2010 Yeh it is difficult, however I don't even get php errors in my plesk logs for the API call. It is the same with the action hooks, any code that errors when the action hooks are called don't seem to produce errors that get logged on the server. It would be great if there could be some form of simple debug log that shows any issues with modules, action hooks and API calls that logs into the admin panel! That would be an amazing feature that would make development much easier. Also I'm not sure if anyone else has found this but when you first create an action hook it seems like it takes a bit of time for the hook to actually start working, I usually find it begins working after the next cron run. This is obviously a problem if this is true because it doesn't seem to be documented anywhere. Has anyone else found this? Its like the hook has to propogate or something!? Generally though I am happy with the results I am getting through this system, and there are a few tweaks that would make life easier as a developer! But oh well, no product or system ever is 100% what you want! 0 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.