Paypal IPN Error - Help

Discussion in 'PHP' started by BrettOwnz, Dec 11, 2009.

  1. #1
    Hello,

    I've been working on a PayPal IPN script for the past day, and for some reason I cannot get it to work properly. It appears that the IPNs are not invalid (i'm not being sent an email stating so) and everything seems right.. I'm wondering if it has to do something with the fsockopen and stuff.. Someone who is good with the IPN let me know what you think is wrong.. Thank you ;)

    
    <?php
    
    //Include Required Files
    
    require_once('includes/db_class.php');
    require_once('includes/functions.php');
    
    //Set up Database Variable
    
    $db = new db;
    $db->connect();
    
    // PayPal now provides a variable called test_ipn on sandbox IPN's for simple flagging of sandbox IPN vs. production IPN   
    
    $sandbox = isset($_POST['test_ipn']) ? true : false;
    $ppHost = $sandbox ? 'www.sandbox.paypal.com' : 'www.paypal.com';
    $ssl = $_SERVER['SERVER_PORT'] == '443' ? true : false;
    
    // Read the post from PayPal system and add 'cmd'   
    
    $req = 'cmd=_notify-validate';   
      
    // Store each $_POST value in a NVP string: 1 string encoded and 1 string decoded   
    
    $ipn_email = '';  
    $ipn_data_array = array();
    foreach ($_POST as $key => $value)   
    {   
     $value = urlencode(stripslashes($value));   
     $req .= "&" . $key . "=" . $value;   
     $ipn_email .= $key . " = " . urldecode($value) . '<br />';  
     $ipn_data_array[$key] = urldecode($value);
    }
    
    //Validate Info
    
    	if($ssl)   
    	{   
    	 $header = '';   
    	 $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";   
    	 $header .= "Host: " . $ppHost . ":443\r\n";   
    	 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";   
    	 $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";   
    	 $fp = fsockopen ('ssl://' . $ppHost, 443, $errno, $errstr, 30);   
    	}   
    	else  
    	{   
    	 $header = '';   
    	 $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";   
    	 $header .= "Host: " . $ppHost . ":80\r\n";   
    	 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";   
    	 $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";   
    	 $fp = fsockopen ($ppHost, 80, $errno, $errstr, 30);   
    	}   
    	
    	if (!$fp)   
    		$valid = false;
    	else  
    	{      
    		fputs ($fp, $header . $req);   
    		while(!feof($fp))   
    		{   
    			$res = fgets ($fp, 1024);    
    			if(strcmp ($res, "VERIFIED") == 0)
    				$valid = true;   
    			elseif (strcmp ($res, "INVALID") == 0)   
    				$valid = false;   
    		}   
    		fclose ($fp);   
    	}
    
    //Assign POST variables to Local Variables
    
    $order['payment_status'] = $_POST['payment_status'];
    $order['payment_currency'] = $_POST['payment_currency'];
    $order['item_number'] = $_POST['item_number'];
    $order['item_name'] = $_POST['item_name'];
    $order['payer_name'] = $_POST['address_name'];
    $order['receiver_email'] = $_POST['receiver_email'];
    $order['payer_email'] = $_POST['payer_email'];
    $order['payment_amt'] = $_POST['mc_gross'];
    $order['txn_id'] = $_POST['txn_id'];
    $order['item_price'] = "50.00";
    $order['test_ipn'] = $sandbox ? 1 : 0;
    $order['ipn_status'] = $valide ? 'Verified' : 'Invalid';
    
    if ($valid == true) {
    	
    	if (($order['item_number'] == '0200901') && 
    		($order['payment_status'] == 'Completed') && 
    		($order['payment_amt'] == $order['item_price']) &&
    		($order['receiver_email'] == 'seller@paypalsandbox.com') && 
    		(txn_id_used_before($order['txn_id']) == false)) {
    		
    //Everything Seems Right... Send Data To Database
    	
    	global $db;
    	
    	$sql = "INSERT INTO `licenses` (buyer_name,buyer_email,license_code) VALUES ('$order[payer_name]','$order[payer_email]','$order[txn_id]')";
    	$db->query($sql);
    	
    	$mailTo = $order['payer_email'];
    	$mailFrom = "From: no-reply@minisitescript.com";
    	$mailSub = "Payment Completed - Important Information Enclosed";
    	$mailBody = "Hello, \n\nYour payment for the Mini Site Script has been received. This email contains important details like your License 	code which will be needed later. Be sure to save this email for future reference (you will need your license code while installing the script!). \n\nLicense Code: $txn_id \n\nThank you! We hope you enjoy the script!";
    	
    	mail($mailTo,$mailSub,$mailBody,$mailFrom);
    
    } else {
    
    //Payment Is Invalid.. Send Email To Request Support.
    	
    	$mailTo = $order['payer_email'];
    	$mailFrom = "From: no-reply@minisitescript.com";
    	$mailSub = "Payment Unsuccessful - Please Review";
    	$mailBody = "Hello, \n\nUnfortunately, your payment for the Mini Site Script was not receieved correctly. PayPal returned a 'incomplete' payment status. If your payment was already sent, please contact our support team to help to remedy the issue. If not, please try again. \n\nThank you.";
    	
    	mail($mailTo,$mailSub,$mailBody,$mailFrom);
    
    }} elseif ($valid == false) {
    	
    	//PayPal didn't like what we sent. Check for errors in your code.
    	
    	$mailTo = "sk8trs340@aol.com";
    	$mailFrom = "errors@minisitescript.com";
    	$mailSub = "Error - Invalid IPN";
    	$mailBody = "Invalid IPN receieved. Here's more info: \n\n Txn Id: $order[txn_id]\n\n Payment Status: $order[payment_status]\n\n Fix the error!";
    	
    	mail($mailTo,$mailSub,$mailBody,$mailFrom);
    
        $db->close();
    }
    ?>
    
    PHP:
    Thanks again
     
    BrettOwnz, Dec 11, 2009 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    My suggestion is to use some paypal php class which is already made out there. There are some good one on google :)
     
    xrvel, Dec 18, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    if ($valid == true) {

    should be

    if ($valid) {

    OR

    if ($valid === true) {
     
    Kaizoku, Dec 18, 2009 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i'm using this method and its working perfectly:


    
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST 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 .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    
    $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
    
    if (!$fp) {
    // HTTP ERROR
    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) 
           {
              //completed payment do whatever you want here
               echo "valid payment";
    
           }
    
    else if (strcmp ($res, "INVALID") == 0)
            {
            //invalid payment there must be an error occured
                echo "invalid payment";
    
            }
    }
    fclose ($fp);
    }
    PHP:

    Rep me up if that helps :D
     
    xenon2010, Dec 18, 2009 IP
  5. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #5
    ghprod, Dec 18, 2009 IP
  6. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #6
    I wound up fixing this. It wasn't my paypal class, it was an error in one of my if statements.

    Thanks.
     
    BrettOwnz, Dec 19, 2009 IP
  7. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #7
    glad to hear that brett :)

    then if u fixed all, maybe u can share to community :)

    regards
     
    ghprod, Dec 21, 2009 IP