<?php
	include("../session.php");
	include("../connect.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/login.php';
$cancel_url = 'http://www.wauhaha.com/smart/login.php';
$notify_url = 'http://www.wauhaha.com/smart/company/updatepaypal.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['company_id'] = $_POST['custom'];
	//$data['amount'] = $_POST['amount_cart'];
    $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'];
 
    // 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) {
 
			echo $data['company_id'];
			echo $data['item_number'];
			echo $data['payment_status'];
			echo $data['payment_amount'];
			echo $data['payment_currency'];
			echo $data['txn_id'];
			echo $data['receiver_email'];
			echo $data['payer_email'];

 
            }else if (strcmp ($res, "INVALID") == 0) {
 
                // PAYMENT INVALID & INVESTIGATE MANUALY!
                // E-mail admin or alert user
            }
        }
    fclose ($fp);
    }
}
?>

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