2013-07-19 124 views
1

我已经建立了Paypal帐户通知URL去这个脚本:贝宝IPN失败

// Read the notification from PayPal which comes in the form of a POST array 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 
    } 


    // Assign the paypal payment notification values to local variables 
    if($_POST){ 
    $item_name = $_POST['item_name']; 
    $item_number = $_POST['item_number']; 
    $payment_status = $_POST['payment_status']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email'];} 

    //Set up the acknowledgement request headers (this is the updated version for http 1.1) 
    $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 .= "Connection: close\r\n"; 
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 
    //Open a socket for the acknowledgement request 
    $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); 

    if(!$fp){ 
     echo "HTTP ERROR"; 
    } 
    else 
    {//start 1 

    // Post request back to PayPal for validation 
    fputs ($fp, $header . $req); 

    //once paypal receives the acknowledgement response, another message will be send containing the single word VERIFIED or INVALID 

    while (!feof($fp)) 
     { //start 2, while not EndOfFile 
    $res = fgets ($fp, 1024); // Get the acknowledgement response 
    $res = trim($res); 
     if (strcmp ($res, "VERIFIED") == 0) 
      {// start 3, Response is OK 

      if ($payment_status == "Completed") 
       {//start 4 

       //send email announcing success 

       $from = "Rupert Heath Literary Agency"; 
       $to = $payer_email; 
       $subject = "Ebook"; 
       $body = "It works"; 
       mail($to, $subject, $body, $from); 

       }//end 4 
      }//end 3 
      else if(strcmp ($res, "INVALID") == 0) 
       {//start 5 

       //send email announcing failure 
       //$error_log .= 'Line 57' 

       $from = "Guide Test Page"; 
       $to = $payer_email; 
       $subject = "INVALID IPN"; 
       $body = "Doesn't work"; 
       mail($to, $subject, $body, $from); 
       }//end 5 


    } //end 2 
fclose ($fp); //close file pointer 
} //end 1 

这是基于大量的Web托管的例子,迎合升级到HTTP 1.1

该脚本作为测试,根据PayPal的VERIFIED或INVALID响应发送成功或失败电子邮件。问题是我总是收到无效的电子邮件,无法理解原因。我查看了PayPal IPN历史记录,HTTP响应代码是200,似乎表明IPN交换正常工作,因此可能PayPal正在响应验证,但我的脚本有错误。

的IPN历史细节:

即时付款通知(IPN)详情

消息ID69025489S2598613V

日期/时间created18/07/2013 - 23:22 PDT

原始/了Resent原件

最近的投递尝试日期/时间18/07/2013 23:22 PDT

通知URL http://www.rupertheath.com/ipn/ipn_script

HTTP响应代码200

交货状态发送

重试号0

事务ID4D0877596N038120Y

IPN typeTransaction制成

IPN消息 mc_gross = 0.01 & protection_eligibility =合格& address_status =确认& payer_id = C3USV8A4Q2QDW &税= 0.00 & address_street =拉姆齐房屋34福勒路& PAYMENT_DATE = 23:22:44 2013年7月18日PDT & PAYMENT_STATUS =完成&的charset = Windows的1252 & address_zip = SP1 2QU & FIRST_NAME =迈克尔& mc_fee = 0.01 & address_country_code = GB & ADDRESS_NAME =迈克尔·希思& notify_version = 3.7 &定制= & payer_status =验证& [email protected] & = ADDRESS_COUNTRY英国& ADDRESS_CITY =索尔兹伯里&量= 1 & verify_sign = AhKyCHsfiy2frgZNNoQmGHQ3LhKMAboweJqZzYCdqp30Hb7b99tF.04a & [email protected] & txn_id = 4D0877596N038120Y & payment_type =即时&姓氏=希思& ADDRESS_STATE =威尔特& RECEIVER_EMAIL = emailagency @ rupertheath。COM & payment_fee = & receiver_id = BRM2TYMP4ACZ8 & txn_type = web_accept & ITEM_NAME =电子书& mc_currency = GBP & ITEM_NUMBER = & residence_country = GB & handling_amount = 0.00 & transaction_subject =电子书& payment_gross = &运费= 0.00 & ipn_track_id = b0a3b4ae3c51c

任何人都可以帮我调试这个问题吗?

+0

你从哪里得到验证码? –

+0

贝宝有一个IPN历史页面,如果你点击一个特定的IPN事件,你可以获得所有这些数据。从数据中看来,贝宝认为一切正常,所以我怀疑我的剧本,尽管经过了一百遍! – Perkin5

回答

0

这很可能是这一行的问题。如果你的PHP配置有(上帝帮助你)Magic Quotes,你只需要包含stripslashes。值得庆幸的是,魔术引号已被删除的PHP 5.4的,所以你可以放心地可能从该行删除stripslashes,使其:

$value = urlencode($value); 

不管你信不信,贝宝允许用户数据包含反斜线。因此,如果IPN有任何反斜杠,并且您使用stripslashes删除它们,当您将IPN数据发回PayPal进行验证时,Paypal会给您INVALID响应,因为数据不匹配。

1

这将是最好使用原始发布的数据,而不是自己重建它。这是我个人使用的代码,并至今一直运作良好:

file_get_contents(
    'https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate', 
    false, 
    stream_context_create(array(
     'http'=>array(
      'method'=>'POST', 
      'header' => 'Content-Type: application/x-www-form-urlencoded', 
      'content' => file_get_contents('php://input'), 
     ) 
    )) 
); 

它可以很容易地变成卷曲为好。

$value = urlencode(stripslashes($value)); 

具体而言,使用stripslashes有:

+0

感谢您的支持,这可能是一种进步,但我的脚本非常标准,网络上有很多例子,基本上都是一样的,所以为什么它不适合我? – Perkin5

+0

@ Perkin5基本上我不相信任何东西,一旦被PHP解析,就会再次成为原始字符串;对不起,我无法确切地告诉你为什么你的脚本不起作用。 –