<?php

$dir = '../';

require_once $dir.'includes/main.php';

$req = 'cmd=_notify-validate';

if(function_exists('get_magic_quotes_gpc')){
   $get_magic_quotes_exists = true;
}

foreach ($_POST as $key => $value){

   $value = urlencode($value);

   $req .= "&$key=$value";

}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://www.paypal.com/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);

$result = curl_exec($ch);

curl_close($ch);

if($result == 'VERIFIED'){

   $payer_email = $_POST['payer_email'];
   $receiver_email = $_POST['receiver_email'];
   $payment_status = $_POST['payment_status'];
   $gross = $_POST['mc_gross'];
   $handling = $_POST['mc_fee'];
   $total = $gross - $handling;
   $txn_id = $_POST['txn_id'];

   $custom = $_POST['custom'];

   if($payment_status == 'Completed'){

      query("UPDATE _stats SET earnings = earnings + $total ORDER BY id DESC LIMIT 1");
      query("UPDATE orders SET paypal = '{$payer_email}', status = '{$payment_status}', amount = '{$gross}', fee = '{$handling}' WHERE id = '{$custom}' ORDER BY id DESC LIMIT 1");
      
      $x = fetch_records("SELECT * FROM orders WHERE id = '{$custom}' LIMIT 1");

      query("UPDATE users SET points = points + '{$x['points']}', orders = orders + 1 WHERE id = '{$x['user_id']}' LIMIT 1");

   }

}

?>
