Paypal IPN

Discussion in 'PHP' started by Kalyse, Apr 27, 2007.

  1. #1
    I have written this but for some reason $fp returns HTTP/1.1 400 Bad Request

    I have no idea why this would happen. Is there something wrong?

    
    
    $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 ('www.paypal.com', 80, $errno, $errstr, 30);
    
    
    
    
    $message = $req;
    
    if (!$fp) {
    	// HTTP ERROR
    
    	
    } else {
    	
    	fputs ($fp, $header . $req);
    
    
    	
    	while (!feof($fp)) {
    
    
    		$res = fgets ($fp, 1024);
    
    		if (strcmp ($res, "VERIFIED") == 0) {
    
    			
    			$match = array();
    			$result = $req;
    
    
    			$invoice = $_POST['invoice'];
    
    			preg_match('/txn_id=([^&]*)/', $result, $transmatch);
    			$transid = $transmatch[1];
    			preg_match('/mc_gross=([^&]*)/', $result, $pricematch);
    			$price = $pricematch[1];
    
    
    			
    
    			order_process($invoice,$transid,'Paypal');
    
    
    		} else if (strcmp ($res, "INVALID") == 0) {
    
    			
    
    		}
    	}
    	fclose ($fp);
    }
    
    PHP:

     
    Kalyse, Apr 27, 2007 IP
  2. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyone able to see ?
     
    Kalyse, Apr 27, 2007 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    It looks fine to me.

    The only difference mine has is that I have an error handling function:

    
    if(!$fp) {
    
    $last_error = "fsockopen error no. $errnum: $errstr";
    return false;
    }
    
    PHP:
     
    jestep, Apr 27, 2007 IP
  4. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    HTTP/1.1 400 Bad Request

    Makes no sense. Ah. I dont see why it is there.
     
    Kalyse, Apr 27, 2007 IP
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    Just a shot in the dark here. Shouldn't:

    be https://www.paypal.com. For the secure server?
     
    Colbyt, Apr 27, 2007 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    That's what I thought as well, but the script I use passes only the www version and it works fine. It sounds like the server isn't allowing incomming data or something similar. Might check the firewall if there is one.
     
    jestep, Apr 27, 2007 IP
  7. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #7
    You might try testing with another service just to identify the problem.

    This one has worked well for me in the past. Belahost IPN Testing Environment

    Some scripts have problems with PayPal in test mode.
     
    Colbyt, Apr 27, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Try adding this under the POST header
    
    $header .= "Host: www.paypal.com\r\n";
    
    PHP:
    Try echoing the complete header and see if it contains the expected content.
     
    nico_swd, Apr 27, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    krakjoe, Apr 28, 2007 IP
  10. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #10
    My code is good... it just doesnt work. Man I wasted my 1000th post on that. pfff I wanted to say something meaningful and wise...


    *think think*

    The crow only flies down the wicker path, yet the antellope will plunder through the grove.

    Think about it ;)
     
    Kalyse, Apr 28, 2007 IP