2013-07-19 171 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解析,就會再次成爲原始字符串;對不起,我無法確切地告訴你爲什麼你的腳本不起作用。 –