<?php
$time = time()
?>

<?php
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "no";
$req .= "&tx=$tx_token&at=$auth_token";

$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 ('www.paypal.com', 80, $errno, $errstr, 30);

if (!$fp) {
} else {
fputs ($fp, $header . $req);
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
$headerdone = true;
}
else if ($headerdone)
{
$res .= $line;
}
}

$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}

$con = mysql_connect("localhost:3306","no", "no"); 


if (!$con)
	{
	die("Could not connect: " . mysql_error());
	}
	

	
$db_selected = mysql_select_db("db", $con); 


$firstname = $keyarray['first_name'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['payment_gross'];
$payer = $keyarray['payer_email'];
$itemnumber = $keyarray['item_number'];
$txn = $keyarray['txn_id'];
$quantity = $keyarray['quantity'];
$option1 = $keyarray['option_selection1'];

echo ("<p><h3>Thank you for your purchase! Please visit the store for instructions on how to cash in this code.</h3></p>");

$sql = "SELECT * from gf_dcs WHERE TID='".$txn."'";
$result = mysql_query($sql,$con);
$data = mysql_fetch_array($result,MYSQL_ASSOC);

if($data != false) { 
$dtime = date("Y-m-d", $data["CREATED"]);
$amount = $data["AMOUNT"];
$code = $data["CODE"];
$used = $data["USED"];

echo("<li>You bought $amount  \"$itemname\" at $dtime</li>\n\n");

if ($used == 0) {

echo("<li>Your code is:</li>\n");
echo("<p><h2>" . $code . "</h2></p>");

} else {

echo("<li>Your code has been used.</li>\n");

}



}
else
{

$CODE = "S";

for ($i=1; $i<=25; $i++)
{
$A = rand(0,1);
	if ($A==0)
	{
		$A = rand(0,9);
		
		$CODE .= $A;
	}
	else
	{
		$A = rand(1,26);
		$T = chr(64+$A);
		$CODE .= $T;
	}
	
}

$BONUS = 1.0; 
$AMOUNT = floatval($option1) * floatval($quantity) * $BONUS;


$sql = "INSERT INTO gf_dcs VALUES ('$CODE','p5cash',$AMOUNT,0,$time,'$txn')"; // Inset the data into gforts database



$result = mysql_query($sql,$con);
echo("<li>" . mysql_error() . "</li>");
echo("<li>Your code is:</li>\n");
echo("<p><h2>" . $CODE . "</h2></p>");


$Send = $payer;
$Subject = "Reciept";
$Message = "Your code is: \n" . $CODE . "\n\n You can use your code on any PredatorRealm server to receive your money.";
$Message = wordwrap($Message,80);

}


mysql_close($con);


}
else if (strcmp ($lines[0], "FAIL") == 0) {
}

}

fclose ($fp);

?>



