【问题标题】:Paypal IPN listener not hearingPaypal IPN 听众听不到
【发布时间】:2014-08-23 16:20:24
【问题描述】:

我在 Paypal 中有一些示例 IPN,我正在尝试设置侦听器,但无论我尝试什么,它都只是不与它交谈,尽管它说是。

这是监听器代码:

$debug=true;

//Put together postback info
$postback = 'cmd=_notify-validate';

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

// build the header string to post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postback) . "\r\n\r\n";

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);//open the connection

if(!$fp){ //no conn
    die();
}

//post data back
fputs($fp, $header . $postback);

$res=stream_get_contents($fp, 1024);

if((strcmp($res, "VERIFIED")) == 0){ //verified!
    if($debug){         
        $filename = 'debug/verify.txt'; //create a file telling me we're verified
        $filehandle=fopen($filename, 'w');
        fwrite($filehandle,'VERIFIED!');
        fclose($filehandle);
    }
} else {
    if($debug){       
        $filename = 'debug/verify.txt'; //create a file telling me we're NOT verified
        $filehandle=fopen($filename, 'w');
        fwrite($filehandle,'NOT VERIFIED!');
        fclose($filehandle);
    }
}

if($debug){       
    $filename = 'debug/alive.txt'; //create a file telling me we're alive at least
    $filehandle=fopen($filename, 'w');
    fwrite($filehandle,'NOT VERIFIED!');
    fclose($filehandle);
}

非常简单,这是我发送的 IPN 的屏幕截图,它什么都不做:

我在侦听器文件的末尾添加了代码,至少让我知道它已被击中,但它什么也没做。

有什么明显的我错过了吗?用这些东西扯掉我的头发。我还能做些调试吗?

【问题讨论】:

    标签: php paypal paypal-ipn paypal-sandbox


    【解决方案1】:

    我不知道你的代码,但试试我目前在 IPN 监听器中使用的代码,它工作得很好:

    $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_data);
    $myPost = array();
    foreach ($raw_post_array as $keyval) {
        $keyval = explode ('=', $keyval);
        if (count($keyval) == 2)
            $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    if(function_exists('get_magic_quotes_gpc')) {
        $get_magic_quotes_exists = true;
    }
    foreach ($myPost as $key => $value) {
        if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
            $value = urlencode(stripslashes($value));
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
    }
    
    // Post IPN data back to PayPal to validate the IPN data is genuine
    // Without this step anyone can fake IPN data
    
    $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
    if ($ch == FALSE) {
        return FALSE;
    }
    
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    
    // CONFIG: Optional proxy configuration
    //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    
    // Set TCP timeout to 30 seconds
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
    
    // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
    // of the certificate as shown below. Ensure the file is readable by the webserver.
    // This is mandatory for some environments.
    // $cert = __DIR__ . "./cacert.pem";
    // curl_setopt($ch, CURLOPT_CAINFO, $cert);
    $res = curl_exec($ch);
    if (curl_errno($ch) != 0) {
        error_log(date('[Y-m-d H:i:s] '). "Couldn't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
        curl_close($ch);
        exit;
    
    } else {
            curl_close($ch);
    }
    
    // Inspect IPN validation result and act accordingly
    if (strcmp($res, 'VERIFIED') == 0) {
        // do something...
    } elseif (strcmp($res, 'INVALID') == 0) {
        // do something...
    }
    

    【讨论】:

    • 感谢这个但同样的结果,什么都没有...我确实收到了错误日志,所以更改为 www.sandbox.paypal.com 解决了这个问题,但我什么也没得到 - 贝宝帐户中有什么我可能没有激活?我已经设置了 IPN 监听路径
    • 根据您的截图,通知发送状态为“已发送”,如果 URL 正确,则通知已发送,因此应该没有其他更改。在你的情况下,我真的不知道可能是什么问题。上面的代码确实适用于我的 IPN。
    猜你喜欢
    • 2021-10-21
    • 2022-01-07
    • 2016-06-29
    • 2013-08-07
    • 2012-10-09
    • 2012-05-20
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多