<?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-23-2009                       #
##################################################################
#               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");
require_once ("classes/misc.class.php");

$message	= "";
$script		= "";
$product	= isset($_REQUEST['product'])	?	trim($_REQUEST['product'])	:	"0";
$amount		= 0;

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

include_once ("title.inc.php");
include_once ("tophtml.inc.php");

$txtPaypalEmail = $siteSettings["paypalemail"];
$txtPaypalSandbox = $siteSettings["enablepaypalsandbox"];
$txtPaypalAuthtoken = $siteSettings["paypalauthtokenid"];

if ($txtPaypalSandbox == "Y") 
{
	$paypalurl = "www.sandbox.paypal.com";
}//end if
else 
{
    $paypalurl = "www.paypal.com";
}//end if

	$flag_to_continue=false;

	//Data check for PDT and proceeded further
	// read the post from PayPal system and add 'cmd'
	$req = 'cmd=_notify-synch';

	$tx_token = $_GET['tx'];
	$auth_token=$txtPaypalAuthtoken;
	$req .= "&tx=$tx_token&at=$auth_token";

	// post back to PayPal system to validate
	$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
	$fp = fsockopen ($paypalurl, 443, $errno, $errstr, 30);
	if($txtPaypalAuthtoken != '')	{
	if (!$fp) 
	{
			// HTTP ERROR
	}//end if
	else 
	{
			fputs ($fp, $header . $req);
			// read the body data
			$res = '';
			$headerdone = false;
			while (!feof($fp)) 
			{
				$line = fgets ($fp, 1024);
				if (strcmp($line, "\r\n") == 0) 
				{
						// read the header
						$headerdone = true;
				}//end if
				else if ($headerdone)
				{
						// header has been read. now read the contents
						$res .= $line;
				}//end else if
			}//end while

			// parse the data
			$lines = explode("\n", $res);
			$keyarray = array();
			//echo($lines[0] . " here");
			if (strcmp ($lines[0], "SUCCESS") == 0) 
			{
				for ($i=1; $i<count($lines);$i++)
				{
						list($key,$val) = explode("=", $lines[$i]);
						//echo "$key::$val <br>";
						$keyarray[urldecode($key)] = urldecode($val);
				}//end for loop
				
				// check the payment_status is Completed
				// check that txn_id has not been previously processed
				// check that receiver_email is your Primary PayPal email
				// check that payment_amount/payment_currency are correct
				// process payment
				$flag_to_continue=true;
				
			}//end if
				
			else if (strcmp ($lines[0], "FAIL") == 0) 
			{
					// log for manual investigation
					//		echo("Fail in result");
					$flag_to_continue=false;
			}//end else if
                        //Test mode checking for paypal without ssl
                        if ($txtPaypalSandbox == "Y"){
                            $flag_to_continue=true; //hard coding flag
                        }

		}//end else

		//echo "<br>-------------subscribersid==".$keyarray["subscr_id"];
		//echo "<br>-------------transactionID==".$keyarray["txn_id"];

		fclose ($fp);
		}
else
		$flag_to_continue = true;
//exit;
		$var_id="";
		$var_new_id="";
		$message="";
		$flag=false;
		
		$sql="select * from ".TABLEPREFIX."payments where vTxn_Id='".addslashes($tx_token)."' and vMethod='PP'";
		if($flag_to_continue == true and mysql_num_rows(mysql_query($sql))>0)
		{
 				$message=" Transaction Error .Please contact <a href='mailto:".$config['adminMail']."'>".$config['adminMail']."</a>";
		}//end if
		else if($flag_to_continue == true)
		{
         		$today = date("Y-m-d");
        		$paymentMethod="PP";
         		$transactionid=$tx_token;
         		//$transactionid=$keyarray["txn_id"];
         		$susbscriptionid=$keyarray["subscr_id"];

         		$Amount=$_GET['amount'];
				
				$_SESSION['sess_Paymode']='PP';
				$_SESSION['sess_TransId']=$transactionid;

				$message=$objU->register_authorize($_SESSION['register_id']);

				if ($message == "") 
				{
					header("Location:register_success.php");
					exit();
				}//end if
        }//end else if
?>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="72%" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="5">
          <tr>
            <td>              <table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td class="box111 headings">Payment Options - Success</td>
              </tr>
            </table>
				<table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td class="headings2"><?php echo $ShowMsg;?></td>
                    </tr>
                  </table>
                    <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><img src="images/spacer.gif" width="1" height="10"></td>
                      </tr>
                    </table>
                    <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                    <tr align="center">
                      <td width="4259875%" colspan="3" class="maintext1 warning"><?php 
													if($flag_to_continue==true)
													{
				                  						echo '<b>'.$message.'</b>';
													}//end if
													else
													{
														echo '<b>There was an error in your payment process.Please contact <a href="mailto:'.$siteSettings['adminmail'].'">'.$siteSettings['adminmail'].'</a> for details.</b>';
													}//end else
							 				?></td>
                      </tr>
                  </table>
                    </td>
                </tr>
              </table>
              <div id="hide"><?php echo $script?></div>
			</td>
          </tr>
        </table></td>
        <td width="28%" valign="top"><?php include_once ("left.inc.php");?></td>
      </tr>
    </table>      
      </td>
  </tr>
</table>
<?php include_once('bottomhtml.inc.php');?>
