Jump to content

Set Last Payment Method As Default


Excel

Recommended Posts

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 by Excel
Link to comment
Share on other sites

  • 1 month later...

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.

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