<?php
	

// Database variables
$host = "localhost"; //database location
$user = "wauhaha_smart"; //database username
$pass = "asd123"; //database password
$db_name = "smart"; //database name
 
// PayPal settings
$paypal_email = 'paypal@example.com';
$return_url = 'http://www.wauhaha.com/smart/company/company.php';
$cancel_url = 'http://www.wauhaha.com/smart/company/company.php';
$notify_url = 'http://www.wauhaha.com/smart/company/updatepaypal.php';

include("../connect.php");
	
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
    // Request from step 3
}else{
 
    // Response from Paypal
 
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
        $req .= "&$key=$value";
    }
 
    // assign posted variables to local variables

    $data['item_number']        = $_POST['item_number'];
    $data['payment_status']     = $_POST['payment_status'];
    $data['payment_amount']     = $_POST['mc_gross'];
    $data['payment_currency']   = $_POST['mc_currency'];
    $data['txn_id']             = $_POST['txn_id'];
    $data['receiver_email']     = $_POST['receiver_email'];
    $data['payer_email']        = $_POST['payer_email'];
    $data['custom']             = $_POST['custom'];
	$data['item_name'] = $_POST['option_selection1'];
	$data['receiver_id']   = $_POST['receiver_id'];
    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0rn";
    $header .= "Content-Type: application/x-www-form-urlencodedrn";
    $header .= "Content-Length: " . strlen($req) . "rnrn";
 
    $fp = fsockopen ('ssl://sandbox.paypal.com', 443, $errno, $errstr, 30);
 
    if (!$fp) {
        // HTTP ERROR
    } else {
                mail('ash@evoluted.net', '0', '0');
        fputs ($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp ($res, "VERIFIED") == 0) {
			/////////////////asd123//////////////////////////
 
            }else if (strcmp ($res, "INVALID") == 0) {
 
                // PAYMENT INVALID & INVESTIGATE MANUALY!
                // E-mail admin or alert user
            }
        }
    fclose ($fp);
    }
}

			date_default_timezone_set ("Singapore"); 
			$date=date("d-m-Y", time());
			$time=date("h:i:sa", time());
			$data1 = $data['custom'] ;
			$data2 = $data['item_name'] ;
			$data7 = $data['txn_id'] ;
			$data3 = $data['payment_amount']." ".$data['payment_currency'];
			$data4 = $date;
			$data5 = $time;
			$data6 = $data['payment_status'];
			$data8 = $data['receiver_id'];
			
			mysql_query("INSERT INTO license (CompanyID,payment_choice,payment_amount,payment_date,payment_times,payment_status,txd_id,receiver_id) VALUES ('$data1','$data2','$data3','$date4','$data5','$data6','$data7','$data8')");
			
//---------------------id increment-----------------------//
	$str = "SELECT Max(id) FROM licensereport";
	
		$result2 = mysql_query ($str);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "L0000001";
		}
	
	date_default_timezone_set ("Singapore"); 
		$ldate=date("d-m-Y", time());
		$ltime=date("h:i:sa", time());

//--------------sub date to be day, month, year-----------//
$day=substr($ldate,0,2);
$month=substr($ldate,3,2);
$years=substr($ldate,6,4);

$query=mysql_query("select * from company where id='$data1'");
$display=mysql_fetch_array($query);
$old=$display['expireDate'];


list($aaa1, $bbb1, $ccc1) = split('[/.-]', $old);
list($aaa2, $bbb2, $ccc2) = split('[/.-]', $ldate);
if ((strtotime($ccc1."-".$bbb1."-".$aaa1)) > (strtotime($ccc2."-".$bbb2."-".$aaa2))){
	$expire = $old;	
}else{
	$expire = $ldate;
}

//----------------------------------Validate the update year---------------------------------------//
if($data2=="Update License(1 YRS)"){
	$dateS = strtotime(date("d-m-Y", strtotime($expire)) . " +365 days");
	$true=date("d-m-Y", $dateS);
	$true2=date("Y-m-d", $dateS);
	$yearA = 1;
}
if($data2=="Update License(2 YRS)"){
	$dateS = strtotime(date("d-m-Y", strtotime($expire)) . " +730 days");
	$true=date("d-m-Y", $dateS);
	$true2=date("Y-m-d", $dateS);
	$yearA = 2;
}
if($data2=="Update License(3 YRS)"){
	$dateS = strtotime(date("d-m-Y", strtotime($expire)) . " +1095 days");
	$true=date("d-m-Y", $dateS);
	$true2=date("Y-m-d", $dateS);
	$yearA = 3;
}
$money1 = $data['payment_amount'];
$money2 = $data['payment_currency'];
$time2=date("H:i:sa", time());
$sorttime= $true2." ".$time2;

$Gquery=mysql_query("select * from company where id='$data1'");
$asd = mysql_fetch_array($Gquery);
$agent = $asd['agentID'];

mysql_query("UPDATE company SET expireDate='$true',sorttime='$sorttime' WHERE id = '$data1'");	
mysql_query("INSERT INTO licensereport (id,companyID,day,month,year,updateDate,updateTime,oldExpire,newExpire,updateYear,payment,type,dollarsign,paymentType,agent,sorttime)
	VALUES('$number','$data1','$day','$month','$years','$ldate','$ltime','$old','$true','$yearA','$money1','','$money2','paypal','$agent','$sorttime')"); 
?>

<?php 
include("../closeconnection.php");
?>