Jump to content

Terminate More then 1 of same product


Recommended Posts

Hi all

 

I offer a 31 day free trial of my product. This is managed with the free trial add-on and works very well. The big problem is people can order more then 1 trial per account.

 

I could never get an action hook to stop the person from fully ordering SO I have restored to using a `clean up` script to help.

 

This will terminate all related products if the user has more then 1

 

So if usera buys 2 trials - both will be terminated.

 

I use mysql to calculate if its duplicated - I know this can be optimized more - I really didnt care.

 

I hope someone else will find it useful.

 

<?php
// include WHMCS and base functions
require("../../dbconnect.php");
require("../../includes/functions.php");
require("functions.php");

$url = "http://..../includes/api.php"; # URL to WHMCS API file
$username = ""; # Admin username goes here
$password = ""; # Admin password goes here
$packageid="";


$allq = "SELECT O.userid FROM tblorders O, tblhosting TH WHERE TH.packageid='$packageid' AND TH.orderid=O.id";
$sq_allq = mysql_query($allq) or die(mysql_error());
$okarray=array();
while($row = mysql_fetch_array($sq_allq)){
if($row['userid']){
	$allqq = "SELECT TH.id, O.userid FROM tblorders O, tblhosting TH WHERE TH.packageid='$packageid' AND TH.orderid=O.id AND O.userid='".$row['userid']."'";
		$sq_allq2 = mysql_query($allqq)or die(mysql_error());
		$cnt=mysql_num_rows($sq_allq2);
		if($cnt > 1){
		while($row2 = mysql_fetch_array($sq_allq2)){
			echo "duplicate found ".$cnt." user #". $row2['userid'].", hosting id#".$row2['id']."\n";
			$postfields=array()
			$postfields["username"] = $username;
			$postfields["password"] = md5($password);

			$postfields["action"] = "moduleterminate";
			$postfields["accountid"] = $row2['id'];

			$postdata = http_build_query($postfields);
			$opts = array('http' =>
				array(
					'method'  => 'POST',
					'header'  => 'Content-type: application/x-www-form-urlencoded',
					'content' => $postdata
				)
			);
			$context  = stream_context_create($opts);
			$data = file_get_contents($url, false, $context);


			$data = explode(";",$data);
			foreach ($data AS $temp) {
			  $temp = explode("=",$temp);
			  $results[$temp[0]] = $temp[1];
			}

			if ($results["result"]=="success") {
			  echo "ok\n";
			} else {
			  # An error occured
			  echo "The following error occured: ".$results["message"]."\n";
			}
		}
	}
}
}



?>

 

I run this as:

modules/freetrial/duplicates.php

Link to comment
Share on other sites

Awesome job.

 

Just so you know on most hosts "file_get_contents" will not allow you to get the contents of a URL for security reasons. I suggest you use cURL to make your API requests to the terminate function :)

 

As well take a look at this quick tip I posted: http://forum.whmcs.com/showthread.php?t=36321

If you run the code on your local server you can save yourself the trouble of connecting to the server (why make a connection when you don't need to?). As well as removing the need for a username/password in the script.

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