0

我正在使用 Paypal IPN。我的应用程序设法收到了来自 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&notify_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

提前致谢!

4

3 回答 3

0

谢谢!今晚我尝试更改 HTTP 1.1,直到我更改了“已验证”的检查以匹配您的版本,才识别出任何从 PayPal 返回的响应

    if (stripos($res, "VERIFIED") !== false) :

它现在可以与实时 PayPal 网站一起使用

于 2013-08-04T20:39:23.637 回答
0

我设法通过另一个参考解决了这个问题,或者至少得到了 IPN 的回复 [无效回复] - 毕竟这是一个进步:

参考:PayPal IPN Bad Request 400 错误

将标题格式更改为:

$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)

于 2013-07-07T04:13:10.297 回答
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)) {

这对我有用。

于 2013-08-26T07:26:48.847 回答