2012-07-28 117 views
2

使用paypal沙箱ipn模擬器工具時,如何調試ipn.php文件?使用paypal沙盒ipn模擬器時調試ipn.php

的代碼看起來是這樣的:

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 

foreach($_POST as $key = > $value) { 
    $value = urlencode(stripslashes($value)); 
    $req. = "&$key=$value"; 
} 

// 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('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

if (!$fp) { 
    // HTTP ERROR 
} else { 
    fputs($fp, $header.$req); 

    while (!feof($fp)) { 
     $res = fgets($fp, 1024); 

     if (strcmp($res, "VERIFIED") == 0) { 

      $DBH = new PDO("mysql:host=localhost;dbname=db", "user", "pass"); 
      $DBH - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $STH = $DBH - > prepare("update table2 set status = :status where tracking_id = :tracking_id"); 

      $status = 1; 
      parse_str($req, $data); 

      $STH - > bindParam(':status', $status, PDO::PARAM_INT, 1); 
      $STH - > bindParam(':tracking_id', 'id_goes_here', PDO::PARAM_STR, 50); 

      $STH - > execute(); 

      $DBH = null; 

     } else if (strcmp($res, "INVALID") == 0) { 
      // do something else 
     } 
    } 

    fclose($fp); 
} 

我通常使用NetBeans調試工具來調試,但如何調試使用沙盒模擬器?當我從沙箱ipn模擬器中點擊send ipn時,我在沙箱中收到一條消息IPN successfully sent.,但是當我進入我的數據庫檢查status時,它仍然是0

回答

1

也許問題出在參數綁定

$STH - > bindParam(':tracking_id', 'id_goes_here', PDO::PARAM_STR, 50); 

如果表「表2」不含有TRACKING_ID =「id_goes_here」更新操作將失敗行。

當我想調試腳本不會在瀏覽器中直接提供輸出試試這個

<?php 

$testMode = false; 
$url = 'https://www.paypal.com/cgi-bin/webscr'; 
if ($testMode === true) 
    $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 

$ipnResponse = ''; // holds the IPN response from paypal 
$ipnData = array(); // array will contain the POST values for IPN 

$urlParsed = parse_url($url); 

$req = 'cmd=_notify-validate'; // Add 'cmd' to req (ipn command) 

// Read the post from PayPal system and add them to req 
foreach ($_POST as $key => $value) { 
    $ipnData["$key"] = $value; 
    $value = urlencode(stripslashes($value)); 
    $req .= "&" . $key . "=" . $value; 
} 

// Open the connection to paypal 
$fp = fsockopen($urlParsed['host'], "80", $errno, $errstr, 30); 

// If could open the connection and check response 
if ($fp) { 

    fputs($fp, "POST " . $urlParsed['path'] . " HTTP/1.1\r\n"); 
    fputs($fp, "Host: " . $urlParsed['host'] . "\r\n"); 
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
    fputs($fp, "Content-length: " . strlen($req) . "\r\n"); 
    fputs($fp, "Connection: close\r\n\r\n"); 
    fputs($fp, $req . "\r\n\r\n"); 

    // Loop through the response from the server and append to variable 
    while (!feof($fp)) { 
     $ipnResponse .= fgets($fp, 1024); 
    } 
    fclose($fp); 

    // Valid IPN transaction. 
    if (preg_match('/^VERIFIED/', $ipnResponse)) { 
     // Some action on IPN validation - update payment status etc 
     die("OK. IPN Validation: Success");  
    } 
    // Invalid IPN transaction 
    else { 
     // Some action on IPN validation - update payment status etc 
     die("ERROR. IPN Validation: Failed"); 
    } 
} 
// Else no connection, so maybe wrong url or other reasons, you can do another call later 
else { 
    die("ERROR. IPN Connection: fsockopen error"); 
} 


?> 
+0

欲瞭解更多信息,請查看官方代碼示例https://www.x.com/developers/paypal/documentation-tools/paypal-code-samples – DTukans 2012-10-31 22:17:08

1

一般情況下,我所說的簡單的日誌功能,在整個腳本輸出變量的值或者幫助確定錯誤正在發生。

當我不知道腳本在哪裏死的時候,我會在每行重要的代碼後面寫一行到日誌文件。運行腳本並檢查日誌文件後,我可以看到哪一行導致腳本死亡,下面是一個非常簡單的示例。當然,在一切運行平穩後,所有的日誌都應該被刪除。

public function logToFile($msg){ 
    $file = 'log.txt'; 
    $current = file_get_contents($file); 
    $current .= $msg . "\n"; 
    file_put_contents($file, $current); 
} 

public function doOtherStuff(){ 

    foreach ($_POST as $key => $value) { 
     $ipnData["$key"] = $value; 
     $value = urlencode(stripslashes($value)); 
     $req .= "&" . $key . "=" . $value; 
    } 

    logToFile("1"); 

    $fp = fsockopen($urlParsed['host'], "80", $errno, $errstr, 30); 

    logToFile("2"); 

    if ($fp) { 
     logToFile("3"); 
     fputs($fp, "POST " . $urlParsed['path'] . " HTTP/1.1\r\n"); 
     logToFile("4"); 
     fputs($fp, "Host: " . $urlParsed['host'] . "\r\n"); 
     logToFile("5"); 
     fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
     logToFile("6"); 
     fputs($fp, "Content-length: " . strlen($req) . "\r\n"); 
     logToFile("7"); 
     fputs($fp, "Connection: close\r\n\r\n"); 
     logToFile("8"); 
     fputs($fp, $req . "\r\n\r\n"); 
     logToFile("9"); 

     while (!feof($fp)) { 
     logToFile("10"); 
     $ipnResponse .= fgets($fp, 1024); 
     } 
     logToFile("11"); 
     fclose($fp); 
    } 
}