2011-08-14 114 views
1

我有用PHP編寫的PayPal IPN腳本,但無論我嘗試什麼,都無法使其工作。PayPal IPN返回無效

PayPal使用PayPal沙盒&返回「INVALID」貝寶IPN模擬器。我還沒有嘗試過,但它可能會返回相同的結果。

<?php 
/// 
// read the post from PayPal system and add 'cmd' 
    $req = 'cmd=_notify-validate'; 
    // Are we using magic quotes? 
    $get_magic_quotes_exists = false; 
    if(function_exists('get_magic_quotes_gpc')){ 
      $get_magic_quotes_exists = true; 
    } 
    foreach($_POST as $key => $value){ 
     // Handle escape characters, which depends on setting of magic quotes 
     if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){ 
      $value = urlencode(stripslashes($value)); 
     }else{ 
      $value = urlencode($value); 
     } 
     if($get_magic_quotes_exists && get_magic_quotes_gpc() == 1){ 
      $req .= '&' . $key . '=' . urlencode(stripslashes(html_entity_decode($value, ENT_COMPAT, 'windows-1252'))); 
      $dc .= '&' . $key . '=' . urlencode(stripslashes(html_entity_decode($value, ENT_COMPAT, 'windows-1252'))); 
      $emailtext .= "\n" . $key . "=" . urlencode(stripslashes(html_entity_decode($value, ENT_COMPAT, 'windows-1252'))); 
     }else{ 
      $req .= '&' . $key . '=' . urlencode(html_entity_decode($value, ENT_COMPAT, 'windows-1252')); 
      $dc .= '&' . $key . '=' . urlencode(html_entity_decode($value, ENT_COMPAT, 'windows-1252')); 
      $emailtext .= "\n" . $key . "=" . urlencode(html_entity_decode($value, ENT_COMPAT, 'windows-1252')); 
     } 
    } 
    /*// post back to PayPal system to validate 
    $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\r\n"; 
    $fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);*/ 
    // CURL 
    $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req))); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $res = curl_exec($ch); 
    if(curl_error($ch)) { 
     $last_error = 'Could not connect to CURL'; 
    } 
    curl_close($ch); 
    // Write to log... 
    $myFile = "log.txt"; 
    $fh = fopen($myFile, 'w'); 
    $stringData = "IPN REQUEST\n\n$req"; 
    fwrite($fh, $stringData); 
    $stringData = "\n\nIPN RESPONSE:\n\n$res"; 
    fwrite($fh, $stringData); 
    fclose($fh); 
    if(strcmp($res, 'VERIFIED') == 0){ 
     // check the payment_status is Completed 
     if($payment_status != 'Completed'){ 
      $last_error = "Payment was not completed."; 
      //exit($last_error); 
    } 

我都試過捲曲的fsockopen。 OpenSSL在服務器上啓用。

這裏是貼回PayPal的數據樣本:

cmd=_notify-validate&test_ipn=1&payment_type=instant&payment_date=12%253A06%253A49%2BAug%2B14%252C%2B2011%2BPDT&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=Peter&last_name=Anderson&payer_email=paypal2.test%2540gmail.com&payer_id=TESTBUYERID01&address_name=John%2BSmith&address_country=United%2BStates&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%2BJose&address_street=123%252C%2Bany%2Bstreet&business=dr.pa%2540custcarecentral.com&receiver_email=paypal.test%2540custcarecentral.com&receiver_id=TESTSELLERID1&residence_country=US&item_name=something&item_number=1&quantity=1&shipping=0&tax=0&mc_currency=USD&mc_fee=0&mc_gross=9.95&mc_gross_1=9.95&txn_type=web_accept&txn_id=49814196&notify_version=2.1&custom=xyz123&charset=windows-1252&verify_sign=A6PXLu.ocjeCd4T66jio.zEQRv65AD4orMH3FTVR7QibqBGYTnYA8ToF 

的Fsock一個我已經嘗試了不同的服務器上的工作,但沒有這一項。

任何人都可以幫助我嗎?我真的開始失去耐心。

在此先感謝

+1

一個已知的工作IPN驗證腳本進行比較:http://www.scriptdevelopers.net/download/paypal_ipn_class.zip –

+0

謝謝,您發佈的腳本工程(儘管貝寶url不正確的沙箱)。我會看看這個做法是正確的,以及爲什麼地雷不工作。 – Peter

+0

第二次看......你打電話給$ value的urlencode()兩次? –

回答

0

你可能想嘗試PHP-PayPal-IPN。這非常簡單,並通過一些非常容易理解的錯誤處理來照顧到PayPal。它允許你選擇SSL/HTTP和fsockopen/cURL。

您的代碼將是這個樣子:

ini_set('log_errors', true); 
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log'); 

include('ipnlistener.php'); 
$listener = new IpnListener(); 
$listener->use_sandbox = true; 

try { 
    $verified = $listener->processIpn(); 
} catch (Exception $e) { 
    error_log($e->getMessage()); 
    exit(0); 
} 

if ($verified) { 
    // TODO: Implement additional fraud checks and MySQL storage 
    mail('YOUR EMAIL ADDRESS', 'Valid IPN', $listener->getTextReport()); 
} else { 
    // manually investigate the invalid IPN 
    mail('YOUR EMAIL ADDRESS', 'Invalid IPN', $listener->getTextReport()); 
} 

你會檢查硬錯誤的錯誤日誌(如來自PayPal無效的HTTP響應代碼)和你的工作互穿網絡的電子郵件。你也可以只看一些想法的來源。

我對你使用html_entity_decode()有點好奇。你運行什麼類型的服務器?