2012-07-28 2 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); 
} 

내가 일반적으로 디버깅 넷빈즈 디버그 기능을 사용하지만, 어떻게 샌드 박스 시뮬레이터를 사용하여 디버깅 할 수 있습니까? 내가 샌드 박스 ipn 시뮬레이터에서 send ipn을 클릭하면 모래 상자에 IPN successfully sent.을 말하는 메시지가 나타납니다. 그런 다음 데이터베이스에 들어가서 status을 확인하면 여전히 0입니다. 테이블 '표 2'가 실패합니다 TRACKING_ID = 'id_goes_here'업데이트 작업으로 행을 포함하지 않는 경우

답변

1

은 어쩌면 문제는

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

바인딩 매개 변수입니다. 내가 브라우저에서 직접 출력을 제공하지 않는 스크립트를 디버깅 할 때

, 나는 위치를 결정하는 출력 변수 값에 스크립트를 통해 간단한 로깅 함수를 호출하거나 도움이 일반적으로이

<?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

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); 
    } 
}