I know this has been asked some times, but none of the solutions worked for me. I am using the Instant Payment Notification (IPN) simulator from paypal (https://developer.paypal.com/webapps/developer/applications/ipn_simulator) but I always receive: Unexpected response from PayPal: HTTP/1.1 400 Bad Request
<?php
//Open a socket for the acknowledgement request
$fp = fsockopen (ssl://www.sandbox.paypal.com, 443, $errno, $errstr, 30);
if (!$fp) {
// fsockopen error
throw new Exception("An error occured while using fsockopen(): [$errno] $errstr");
}
//Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: ssl://www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
// Post request back to PayPal for validation
fputs ($fp, $header . $data);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$response = 'verified';
}
else if (strcmp ($res, "INVALID") == 0) { // Response is INVALID
$response = 'invalid';
}
else {
throw new Exception("Unexpected response from PayPal: $res");
}
}
Edit: After changing
$header .= "Host: ssl://www.sandbox.paypal.com\r\n";
to
$header .= "Host: www.sandbox.paypal.com\r\n";
I now receive a HTTP/1.1 200 OK response. Another problem is, that my if() part is not working, it always jumps into the last
else {
throw new Exception("Unexpected response from PayPal: $res");
}
Is there anything wrong with if (strcmp ($res, "VERIFIED") == 0)
?
UPDATE 2: Here is my full code. Maybe someone can find the mistakes:
class Paypal {
protected $sandbox = false;
protected $data = null;
const SANDBOX_URL = 'www.sandbox.paypal.com';
const PAYPAL_URL = 'www.paypal.com';
public function __construct($sandbox = false) {
$this->sandbox = $sandbox;
}
public function receiveData() {
if (empty($_POST)) {
throw new Exception('No $_POST data found');
}
$this->data = $_POST;
// Read the notification from PayPal and create the acknowledgement response
$req = 'cmd=_notify-validate'; // add 'cmd' to beginning of the acknowledgement you send back to PayPal
foreach ($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode the values
$req .= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
if ($this->fsock($req))
return true;
else
return false;
}
public function getData() {
return $this->data;
}
private function fsock($data) {
//Open a socket for the acknowledgement request
$fp = fsockopen ('ssl://'.$this->getURL(), 443, $errno, $errstr, 30);
if (!$fp) {
// fsockopen error
throw new Exception("An error occured while using fsockopen(): [$errno] $errstr");
}
//Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: ".$this->getURL()."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
// Post request back to PayPal for validation
fputs ($fp, $header . $data);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$response = 'verified';
// Notification protocol is complete, OK to process notification contents
// Possible processing steps for a payment might include the following:
// Check that the payment_status is Completed
// Check that receiver_email is your Primary PayPal email
// Check that payment_amount/payment_currency are correct
// Process payment
}
else if (strcmp ($res, "INVALID") == 0) { // Response is INVALID
$response = 'invalid';
}
else {
throw new Exception("Unexpected response from PayPal: $res");
}
}
fclose ($fp); //close file pointer
if ($response == 'verified')
return true;
else
return false;
}
private function getURL() {
if ($this->sandbox)
return self::SANDBOX_URL;
else
return self::PAYPAL_URL;
}
}
$paypal = new Paypal(true);
try {
if ($paypal->receiveData()) {
// success
}
else {
// fail
}
}
catch (Exception $e) {
// exception
echo 'Exception: ', $e->getMessage(), "\n";
}