KuJoe Posted February 17, 2010 Share Posted February 17, 2010 (edited) I wrote this quick cron job to automatically close accounts that had products/services marked as fraud (currently runs every other minute). I considered doing a hook but since this job only runs for a few seconds and requires no CPU resources I figured it's just as effective. Plus, I am still learning the hooks. With this code I created a new table in the WHMCS database called "fraudclose" to keep track of the UserIDs that are closed by the script. I am no developer by any means so please feel free to point out any flaws or if you know of a better more efficient way to handle this I am all ears. (I keep the file above my public_html directory so it is not web accessible.) <?php $db_host = "localhost"; $db_username = "user"; $db_password = "password"; $db_name = "database"; $con = mysql_connect("$db_host","$db_username","$db_password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("$db_name", $con); $query = "SELECT tblclients.id ". "FROM tblclients, tblorders ". "WHERE tblclients.id = tblorders.userid AND tblclients.status = 'Active' AND tblorders.status = 'Fraud'"; $result = mysql_query($query) or die(mysql_error()); // Print out the contents of each row into a table while($row = mysql_fetch_array($result)){ $userid = $row['id']; $updateq = "UPDATE tblclients SET status='Closed' WHERE id='$userid'"; $insertq = "INSERT fraudclose VALUES ($userid)"; mysql_query($updateq) or die(mysql_error()); mysql_query($insertq) or die(mysql_error()); } mysql_close($con); ?> Edited February 17, 2010 by KuJoe 1 Quote Link to comment Share on other sites More sharing options...
ExsysHost Posted March 4, 2010 Share Posted March 4, 2010 I have re-wrote this to be an action hook and have removed the fraudclose table as most of us keep track of frauds using the already built in whmcs accounts search feature. <?php function close_fraud_accounts_hook($args) { $query = "SELECT tblclients.id FROM tblclients, tblorders WHERE tblclients.id = tblorders.userid AND tblclients.status = 'Active' AND tblorders.status = 'Fraud'"; $result = full_query($query); while($record = mysql_fetch_array($result)) { update_query("tblclients",array("status"=>"Closed"),array("id"=>$record['id'])); } } add_hook("DailyCronJob",1,"close_fraud_accounts_hook",""); ?> 0 Quote Link to comment Share on other sites More sharing options...
BarrySDCA Posted March 1, 2011 Share Posted March 1, 2011 nice script -thank you 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted July 17, 2011 Share Posted July 17, 2011 Nice quick action hook to use 0 Quote Link to comment Share on other sites More sharing options...
laszlof Posted July 18, 2011 Share Posted July 18, 2011 Expanded it a bit more to use the built-in API rather than the raw SQL queries. (This is untested) function close_fraud_accounts_hook($args) { $adminuser = "apiuser"; // Set to a valid admin user with API access $data = select_query("tblorders o, tblclients c", "c.id", array( "c.id"=>"o.userid", "c.status"=>"Active", "o.status"=>"Fraud")); while ($res = mysql_fetch_array($data)) { $val = array("clientid"=>$res[0]); localAPI("closeclient", $val, $adminuser); } } add_hook("DailyCronJob",1,"close_fraud_accounts_hook",""); 0 Quote Link to comment Share on other sites More sharing options...
serverprodigy Posted November 5, 2011 Share Posted November 5, 2011 Can you make another of these that will look for cancelled / terminated products and cancel any add ons for them? We have an ongoing issue with WHMCS leaving add ons active even after the products are terminated or cancelled which means clients who have left are still sent invoices for the add on products. It's become a huge issue to the point where the owner refuses to use add ons and creates additonal products instead which leads to even more confusion. I don't know why WHMCS doesn't do this automatically but we really, really could use a solution for this. Another version that looks for clients with no active products and closes their accounts would be great too. Even if the system automatically terminates a client for non payment the account is left "active" which means they will still get any emails sent out to all active clients. This is yet another source of complaints for us we'd like to solve. Thanks for the fraud closing script, it's helped me save a lot of time on one of our services which gets a ton of fraud sign ups. 0 Quote Link to comment Share on other sites More sharing options...
RFEHosting Posted April 12, 2012 Share Posted April 12, 2012 Is this working on v5.0? 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.