<?php 
/*
##################################################################
#                       PHP 4&5 MySQL 4&5                        #
##################################################################
#           Copyright (c) 2004-2008 Armia Systems Inc            #
##################################################################
#               This file is part of Cybermatch site             #
##################################################################
#    Developed By: simi (simi@armia.com)                         #

##################################################################
#               Developed On:   03-04-2008                       #
##################################################################
#               Modified By:                                     #
##################################################################
#               Modified On:                                     #
##################################################################
# This script may not be distributed, sold, given away for free  #
# to third party, or used as a part of any internet services 	 #
# such as webdesign etc.                                         #
##################################################################
*/

require_once ("config.inc.php");
require_once ("includes/mysql.inc.php");
require_once ("includes/arrays.inc.php");
require_once ("classes/settings.class.php");
require_once ("classes/userside.class.php");

if (!$objDB) 
{
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
	$objU			= new userside($objDB);
}//end else

// Retrieve the XML sent in the HTTP POST request to the ResponseHandler
$xml_response = $HTTP_RAW_POST_DATA; 
if (@get_magic_quotes_gpc())
{ 
	$xml_response = stripslashes($xml_response); 
	// Capture the Return Response XML from the Google Checkout. 
}
//checking response ends here

if(trim($xml_response)!='')
{

	$gc_flag=true;
	$gc_status='success';
	$_SESSION['sess_flag_failure']='';
	
	$merchant_data=preg_match_all("/<merchant-private-data>(.*?)<\/merchant-private-data>/",$xml_response, $matchkeyword); 
	$merchant_data=$matchkeyword[1][0];
	
	$merchant_data=explode('-',$merchant_data);
	
	switch($merchant_data[0])
	{
		case "buynow":
			
			$product=$merchant_data[1];
			$amount=$merchant_data[2];
			$_SESSION['sess_register_stype']=$merchant_data[3];
			$_SESSION['user_id']=$merchant_data[4];
			
       		$gc_tran=time();
			$gc_flag = true;
			$_SESSION['sess_TransId']=$gc_tran;

           if($gc_flag == true)
		   {
            	// Start of the transaction  for adding a user since payment is successfull
				$txnid = $gc_tran;
				// ////////////////////////////////////////////////////////////////////////////////////
				// check if txnid alredy there to prevent refresh
				$sql="select * from ".TABLEPREFIX."payments where vTxn_Id='".addslashes($txnid)."' and vMethod='GC'";
				$result = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($result)==0)
				{
					$_SESSION['sess_Paymode']='GC';
					$message	= $objU->buy_real_products($_SESSION['user_id'], $product);
					if($message=='')
					{
						$_SESSION['txtGoogleId']='';
						$_SESSION['txtGoogleKey']='';
						$_SESSION['chkGoogleSandbox']='';
						$_SESSION['sess_site_name']='';
						$_SESSION['sess_pass_msg']='';
						$_SESSION['sess_site_secure_url']='';
					}//end if
				}//end if
		   }//end if

		break;
		
		case "paynow":
			
			$product=$merchant_data[1];
			$amount=$merchant_data[2];
			$_SESSION['sess_register_stype']=$merchant_data[3];
			$_SESSION['user_id']=$merchant_data[4];
			
       		$gc_tran=time();
			$gc_flag = true;
			$_SESSION['sess_TransId']=$gc_tran;

           if($gc_flag == true)
		   {
            	// Start of the transaction  for adding a user since payment is successfull
				$txnid = $gc_tran;
				// ////////////////////////////////////////////////////////////////////////////////////
				// check if txnid alredy there to prevent refresh
				$sql="select * from ".TABLEPREFIX."payments where vTxn_Id='".addslashes($txnid)."' and vMethod='GC'";
				$result = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($result)==0)
				{
					$_SESSION['sess_Paymode']='GC';
					$message	= $objU->buy_product($_SESSION['user_id'], $product);
					if($message=='')
					{
						$_SESSION['txtGoogleId']='';
						$_SESSION['txtGoogleKey']='';
						$_SESSION['chkGoogleSandbox']='';
						$_SESSION['sess_site_name']='';
						$_SESSION['sess_pass_msg']='';
						$_SESSION['sess_site_secure_url']='';
					}//end if
				}//end if
		   }//end if
		break;
			
		case "googleregister":
			
			$_SESSION['register_id']=$merchant_data[1];
			$_SESSION['sess_register_stype']='GOOGLE';
			
       		$gc_tran=time();
			$gc_flag = true;
			$_SESSION['sess_TransId']=$gc_tran;

           if($gc_flag == true)
		   {
            	// Start of the transaction  for adding a user since payment is successfull
				$txnid = $gc_tran;
				// ////////////////////////////////////////////////////////////////////////////////////
				// check if txnid alredy there to prevent refresh
				$sql="select * from ".TABLEPREFIX."payments where vTxn_Id='".addslashes($txnid)."' and vMethod='GC'";
				$result = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($result)==0)
				{
					$_SESSION['sess_Paymode']='GC';
					$message	= $objU->register_authorize($_SESSION['register_id']);
					if($message=='')
					{
						$_SESSION['txtGoogleId']='';
						$_SESSION['txtGoogleKey']='';
						$_SESSION['chkGoogleSandbox']='';
						$_SESSION['sess_site_name']='';
						$_SESSION['register_id']='';
						$_SESSION['sess_site_secure_url']='';
					}//end if
				}//end if
		   }//end if
		
	   break;
	}//end switch
}//end if
?>
