我知道有人问过这个问题,但没有一个解决方案对我有用。我正在使用来自 paypal ( https://developer.paypal.com/webapps/developer/applications/ipn_simulator ) 的即时付款通知 (IPN) 模拟器,但我总是收到: 来自 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");
}
}
编辑:更改后
$header .= "Host: ssl://www.sandbox.paypal.com\r\n";
至
$header .= "Host: www.sandbox.paypal.com\r\n";
我现在收到HTTP/1.1 200 OK响应。另一个问题是,我的 if() 部分不起作用,它总是跳到最后
else {
throw new Exception("Unexpected response from PayPal: $res");
}
有什么问题if (strcmp ($res, "VERIFIED") == 0)
吗?
更新 2: 这是我的完整代码。也许有人可以找到错误:
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";
}