我正在开发Paypal IPN。我的应用程序设法从Paypal收到第一个通知,然而,在发送回发后,似乎没有来自PayPal的回复是否有效或无效。
我从paypal开发者网站的示例代码中获得了大部分代码:
public function process(){
// 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
//$raw = file_get_contents("php://input");
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
}
//Set up the acknowledgement request headers
$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";
$header .= "Connection: close\r\n\r\n";
//Open a socket for the acknowledgement request
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Post request back to PayPal for validation
fputs ($fp, $header . $req);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
//$res=stream_get_contents($fp, 1024);
$this->emailtest(print_r($_POST,true) .'<br /><br />' . $header.$req);
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$this->emailtest('VERIFIED');
// Send an email announcing the IPN message is VERIFIED
//$mail_From = "IPN@example.com";
//$mail_To = "Your-eMail-Address";
//$mail_Subject = "VERIFIED IPN";
//$mail_Body = $req;
//mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
// 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 txn_id has not been previously processed
// 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
$this->emailtest('INVALID');
// Notification protocol is NOT complete, begin error handling
// Send an email announcing the IPN message is INVALID
$mail_From = "IPN@example.com";
$mail_To = "Your-eMail-Address";
$mail_Subject = "INVALID IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
}
fclose ($fp);
}
我的标题看起来像这样:[来自连接$ header。$ req]:
Content-Type: application/x-www-form-urlencoded
Content-Length: 960
Connection: close
cmd=_notify-validate&mc_gross=30.00&protection_eligibility=Eligible&address_status=confirmed&payer_id=V6AHFXYY8YVFU&tax=0.00&address_street=1+Main+St&payment_date=09%3A20%3A02+Jul+06%2C+2013+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=LY&mc_fee=1.37&address_country_code=US&address_name=LY+Y¬ify_version=3.7&custom=&payer_status=verified&business=test-facilitator%40hotmail.com&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=ALty0ZLnfvwh23G8AkAyx34uB78KAtFMUzdUnz11sJNJNFF4NI8JnFNu&payer_email=test%40hotmail.com&txn_id=2VB92036BK537324F&payment_type=instant&last_name=Y&address_state=CA&receiver_email=test-facilitator%40hotmail.com&payment_fee=&receiver_id=82EXQSKTSV6ZN&txn_type=web_accept&item_name=Chef&mc_currency=SGD&item_number=1&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=Chef&payment_gross=&shipping=0.00&ipn_track_id=1dc5a2e7b3934
提前致谢!
答案 0 :(得分:0)
我设法通过另一个参考解决了这个问题,或者至少得到了IPN的回复[无效回复] - 毕竟这是一个进步:
提到:PayPal IPN Bad Request 400 Error
将标题格式更改为:
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n"; // www.paypal.com for a live site
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
改变:
if(strcmp($ res,“VERIFIED”)== 0)
到此:
if(stripos($ res,“VERIFIED”)!== false)
答案 1 :(得分:0)
谢谢!我今天晚上尝试对HTTP 1.1进行更改,并且在我更改了“已验证”的检查以匹配您的版本之前,我没有发现任何从PayPal返回的响应
if (stripos($res, "VERIFIED") !== false) :
它现在可以与现场PayPal网站一起使用
答案 2 :(得分:0)
对我来说有用的是删除连接关闭标头,并为PP的响应添加修剪。以下是标题:
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
这是fsockopen:
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
以下是来自PP的回复修剪:
if (!$fp) {
// HTTP ERROR
error_mail("Could not open socket");
//
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = trim(fgets ($fp, 1024));
}
//
// check the payment_status is Completed
// check that receiver_email is your Primary PayPal email
//
if ((strcmp ($res, "VERIFIED") == 0) && ($payment_status == "Completed") && ($receiver_email == $valid_receiver_email)) {
这对我有用。