<?php
ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log');
// instantiate the IpnListener class
include('../ipnlistener.php');
include('../../../connect.php');
$listener = new IpnListener();
$listener->use_sandbox = false;
try {
    $listener->requirePostMethod();
    $verified = $listener->processIpn();
} catch (Exception $e) {
    error_log($e->getMessage());
    exit(0);
}

if ($verified) {
    /*
    Once you have a verified IPN you need to do a few more checks on the POST
    fields--typically against data you stored in your database during when the
    end user made a purchase (such as in the "success" page on a web payments
    standard button). The fields PayPal recommends checking are:
    
        1. Check the $_POST['payment_status'] is "Completed"
	    2. Check that $_POST['txn_id'] has not been previously processed 
	    3. Check that $_POST['receiver_email'] is your Primary PayPal email 
	    4. Check that $_POST['payment_amount'] and $_POST['payment_currency'] 
	       are correct
    
    Since implementations on this varies, I will leave these checks out of this
    example and just send an email using the getTextReport() method to get all
    of the details about the IPN.  
    */
		if($_POST['payment_status'] =="Completed"){
			include('../../../connect.php');
			$custom=$_POST['custom'];
			$custom=explode("-",$custom);
			$idu=$custom[0];
			$idp=$custom[1];
			$fetchp=@mysql_fetch_array(mysql_Query("SELECT * FROM `products` WHERE `id`='$idp'"));
			$price=$_POST['payment_gross'];
			$details=json_encode($_POST);
			$datefix=date('d-m-Y');
			$date=time();
			$website=@mysql_result(mysql_query("SELECT `value` FROM `settings` WHERE `type`='site_url'"),0,0);
			$customlogodb=@mysql_result(mysql_query("SELECT `value` FROM `settings` WHERE `type`='custom_logo'"),0,0);
			$customlogo=$website."/uploads/$customlogodb";
			@mysql_query("INSERT INTO `sales`(`idu`,`idp`,`payment`,`price`,`details`,`datefix`,`date`) VALUES('$idu','$idp','paypal','$price','$details','$datefix','$date')");
			$ids= mysql_insert_id();
			@mysql_query("INSERT INTO `download`(`idu`,`idp`,`ids`,`active`) VALUES('$idu','$idp','$ids','1')");
			$idd= mysql_insert_id();
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
			$headers .= "From: $fetchp[contact_email]\r\n"; 
			$message="<!DOCTYPE html>
<html>
<head>
	<title>Email template</title>
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-9\" />

	<style type=\"text/css\">
		body {
			padding:0;
			margin:0;
			-webkit-text-size-adjust: none;
		}
	</style>

</head>
<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" background=\"$website/images/paypal/background.png\">

<table width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
	<tr>
		<td background=\"$website/images/paypal/background.png\">
			
			<!-- Header -->

				<table width=\"600\" align=\"center\" cellpadding=\"10\" cellspacing=\"0\">
					<tr>
						<td bgcolor=\"white\" background=\"$website/images/paypal/second_background.png\" height=\"180\" align=\"center\">
							<br/><br/><br/><a href=\"#website\"><img src=\"$customlogo\" /></a>
						</td>
					</tr>
				</table>

			<!-- End header -->
			<!-- Content -->

				<table width=\"600\" align=\"center\" cellspacing=\"0\" cellpadding=\"10\">
					<tr>
						<td bgcolor=\"#f8f8f9\">
							
							<p style=\"margin: 0; padding: 0;\">
								<h2 style=\"margin: 0; padding: 0;\"><strong>Hello ".$_POST['first_name']." ".$_POST['last_name'].",</strong></h2>
								 you get download link avaible 24 hours !<br/><br/>

								<div style=\"font-size: 14px;\">
								 <strong>Name:</strong> $fetchp[name]<br/>
								 <strong>Description:</strong> $fetchp[description]<br/>
								 <strong>Price:</strong> $fetchp[price] $fetchp[price_value]<br/>
								</div>

							</p>
							<center><img src=\"$website/images/paypal/hr.png\" /></center>
							<p>
								<strong>Here are your link:</strong><br/><Br/>
								Download : <a href=\"$website/download/$idd\">$website/download/$idd</a><br/>
							</p>

						</td>
					</tr>
				</table>

			<!-- end content -->

		</td>
	</tr>
</table>

</body>
</html>";
		
			mail($_POST['payer_email'], 'Download Link', $message,$headers);
		
		}
    

} else {
    mail('tehnicsmart@gmail.com', 'Invalid IPN', $listener->getTextReport());
}

?>
