Excel Posted July 18, 2010 Share Posted July 18, 2010 (edited) I coded this Action Hook which will help in automatically setting the last payment gateway used by a client to pay their invoice as the new default payment method for that product (product: can be a Hosting service or Addon). You may find this useful since clients who choose to pay using a new payment method will not be able to set the payment gateway as the new default on their own without the contribution located at http://forum.whmcs.com/showthread.php?t=28512 This Action Hook extends the above contribution further by automatically setting the last payment method used as the new default for their product when an invoice transaction takes place for their product. Not everyone may want this because if a client just wanted to use a new payment method only once, this script won't know. However, it offers logging capabilities (using the TO DO List) which enables Administrators to verify that the change was appropriate. Installation Instructions: 1) Create a new php file (any file name) and enter the below code exactly as it appears. 2) Upload this php file to your /includes/hooks folder 3) No further customization needed <?php //////////////////////////////////////////////////////////////////////////// //// ********** DO NOT REMOVE THIS COPYRIGHT NOTICE ********** //// //// (C) 2010 CIAN Technologies Inc. d/b/a ExcellentHost Enterprises //// //// Coded by Anand Athi - Public Release //// //// Set Last Payment Method Hook v1.1 - July 2010 //// /////////////////////////////////////////////////////////////////////// ////PURPOSE: This action hook will automatically set the last payment method used by the client as the new default for their product (hosting products OR addons), ////-------- only if they did not already set their profile to use this gateway for future transactions. //// Based on code for Default CC Gateway Hook v1.1 (Private Release) add_hook("afterAddTransaction",1,"check_default_gateway"); function check_default_gateway($vars) { $result = mysql_query("SELECT type, relid FROM tblinvoiceitems WHERE invoiceid='".$vars['invoiceid']."';"); //find packageid from invoice while ($row = mysql_fetch_array($result)) { $relid = $row['relid']; if($row['type'] == "Hosting") { $tabletype = "tblhosting"; } elseif ($row['type'] == "Addon") { $tabletype = "tblhostingaddons"; } else {$tabletype = "none"; } if($tabletype != "none") { $result2 = mysql_query("SELECT paymentmethod FROM $tabletype WHERE id='$relid';"); while ($row2 = mysql_fetch_array($result2)) { if($row2['paymentmethod'] != $vars['gateway']) { //customer has paid using a different gateway, but has not set this as default in profile mysql_query("UPDATE $tabletype SET paymentmethod = '".$vars['gateway']."' WHERE id='$relid'"); $result4 = mysql_query("SELECT paymentmethod FROM $tabletype WHERE id='$relid';"); while ($row4 = mysql_fetch_array($result4)) { $new_paymentmethod = $row4['paymentmethod']; } //log this activity into TO DO LIST for verification purposes. Comment out if you don't want the system to log these events. mysql_query("INSERT INTO tbltodolist SET date='".date("Y-m-d")."', title='Default Gateway Set', description='Client ID ".$vars['userid']. " has had their package ID $relid (type: ".$row['type'].") automatically set to $new_paymentmethod', status='Pending', duedate='".date("Y-m-d")."';"); } } } } } ?> I have another contribution that only changes the payment method for those users who have switched to a CC gateway for automatic billing (e.g. those who previously used PayPal or other payment method). Currently that contribution is a Private Release since it provides additional checks on credit card availability, but if you would like this action hook, you can request it on this thread. I may release it depending on demand. Feedback appreciated. Edited July 18, 2010 by Excel 0 Quote Link to comment Share on other sites More sharing options...
Excel Posted August 20, 2010 Author Share Posted August 20, 2010 If any users have tried this script yet, please leave some feedback. I'm interested to know whether it's working for everyone, as we find it very useful. 0 Quote Link to comment Share on other sites More sharing options...
laszlof Posted August 20, 2010 Share Posted August 20, 2010 Looks like you have a bit of redudent code in there that could be cleaned up. mysql_query("UPDATE $tabletype SET paymentmethod = '".$vars['gateway']."' WHERE id='$relid'"); $result4 = mysql_query("SELECT paymentmethod FROM $tabletype WHERE id='$relid';"); while ($row4 = mysql_fetch_array($result4)) { $new_paymentmethod = $row4['paymentmethod']; This for example, you do not need the result4 output particularly since you just set it with the UPDATE statement above. Also you can use select_query() insert_query(), and update_query() where applicable which should improve performance. 0 Quote Link to comment Share on other sites More sharing options...
Excel Posted August 20, 2010 Author Share Posted August 20, 2010 True, I guess it's just to make sure the update was done properly. But thanks for the tips. Just wondering if anyone had tried this action hook yet. It's useful instead of manually setting the payment gateway. 0 Quote Link to comment Share on other sites More sharing options...
oempire Posted August 21, 2010 Share Posted August 21, 2010 im interested in changing to cc if last paid with cc, for all product types including domains is this possible? 0 Quote Link to comment Share on other sites More sharing options...
Excel Posted August 22, 2010 Author Share Posted August 22, 2010 im interested in changing to cc if last paid with cc, for all product types including domains is this possible? I currently have the code for Hosting Products and Addons only, not domains yet. But you can PM me for the code 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.