<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";

$page=$_SERVER["QUERY_STRING"];
$paypalipn=false;


$orderid					=$_GET['orderid'];

$userid                                         = $_GET['userid'];

$digital_flag                                   = $_GET['digitalflag'];

$gift_flag                                      = $_GET['giftflag'];

$usertype                                       = $_GET['usertype'];

$_SESSION["sess_guest_password"]                = $_GET['guestpassword'];


$sql = "SELECT *  FROM ".$tableprefix."settings ";
$res = mysql_query($sql);

if(mysql_num_rows($res)>0)
{
$row = mysql_fetch_array($res);

$chkPaypal = $row["enablepaypal"];
$siteurl=$row["site_url"];

}
	 $raw_post_data = file_get_contents('php://input');
 	 $raw_post_array = explode('&', $raw_post_data);

	$post_array = decodePayPalIPN( $raw_post_data); //THIS WORKS

	 $myPost = array();
   foreach ($raw_post_array as $keyval)
      {
      $keyval = explode ('=', $keyval);
      if (count($keyval) == 2)
         $myPost[$keyval[0]] = urldecode($keyval[1]);
      }


   //Adds the necessary variable and builds the string back up to send to PayPal
   $_req = 'cmd=_notify-validate';
   foreach ($myPost as $key => $value)
         {
			 $value = urlencode(stripslashes($value));
			 $_req .= "&$key=$value";

         }

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
//$header .= "Host: www.sandbox.paypal.com:443\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "Content-Length: " . strlen($_req) . "\r\n\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if ($fp)

{
	fputs ($fp, $header . $_req);
	while (!feof($fp))
		{
			$res = fgets ($fp, 1024);

			if (strcmp ($res, "VERIFIED") == 0)
				{
						$paypalipn=ture;
				}
	}

	fclose ($fp);

}


	
		if(strtoupper($post_array['status'])=='COMPLETED')
		{

                    if($orderid!='')
                        {

                      // Update Order Table
                        $sql_update="UPDATE " .$tableprefix. "orders SET vpayment_status='C' where order_id=".$orderid." ";

                        $sql_qr=mysql_query($sql_update) or die(mysql_error());

                      // Update Artists Payemt Table

                        $sql_update_payment="UPDATE " .$tableprefix. "artist_payments SET payment_status='C' where order_id=".$orderid." ";

                        $re_up=mysql_query($sql_update_payment) or die(mysql_error());
                        
                         // Settle seller , Admin Amounts

                        // End

                        $sql_select_pay = "SELECT payment_completed_status  FROM  " .$tableprefix. "orders where order_id=".$orderid." ";

                        $rs_select_pay  =mysql_query($sql_select_pay) or die(mysql_error());

                        $rowPay         =mysql_fetch_array($rs_select_pay);

                        $paymentComplete_Status =$rowPay['payment_completed_status'];

                     if($paymentComplete_Status=='N'){

                        if($gift_flag==1)
                            {
                            sendgiftCertificateMail_Buyer($orderid,$userid );
			    sendgiftCertificateMail_Seller($orderid,$userid );
			    sendgiftCertificateMail_Admin($orderid,$userid );
                        }
                         else{
                            sendBuyerEmail($orderid,$userid ,$usertype);
                            sendSellerEmail($orderid,$userid );
                            sendAdminEmail($orderid,$userid );
                        }
                        /*else if($digital_flag==1)
                            {
                            sendDigitalCertificateMail_Buyer($orderid,$userid ,$usertype);
                            sendDigitalCertificateMail_Seller($orderid,$userid );
			    sendDigitalCertificateMail_Admin($orderid,$userid );
                        }*/
                       

                        $sql_update_order_payment="UPDATE " .$tableprefix. "orders SET payment_completed_status='Y' where order_id=".$orderid." ";

                        $res_update=mysql_query($sql_update_order_payment) or die(mysql_error());

               }
                        adjustStock($userid ,"-" );
                        emptyCart($userid);
   }
                            
// End Payapal Success check

		}
  

   // End




?>