Php PayPal IPN返回无效

Php PayPal IPN返回无效,php,paypal,paypal-ipn,Php,Paypal,Paypal Ipn,我有一个用PHP编写的PayPal IPN脚本,但无论我做什么尝试,都无法让它正常工作 PayPal使用PayPal沙盒和PayPal IPN模拟器返回“无效”。我还没有在现场试用过,但它可能会返回相同的结果 <?php /// // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; // Are we using magic quotes? $get_ma

我有一个用PHP编写的PayPal IPN脚本,但无论我做什么尝试,都无法让它正常工作

PayPal使用PayPal沙盒和PayPal 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);
    }
我试过的Fsock在另一台服务器上工作,但不是这台

有人能帮我吗?我真的开始失去耐心了

提前感谢

您可能想试试。它非常简单,可以通过一些非常容易理解的错误处理将信息发布到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响应代码)和电子邮件中的IPN是否正常工作。你也可以查看一些想法的来源

我对您使用html_entity_decode()有点好奇。您正在运行哪种类型的服务器?

您可能想试试。它非常简单,可以通过一些非常容易理解的错误处理将信息发布到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响应代码)和电子邮件中的IPN是否正常工作。你也可以查看一些想法的来源


我对您使用html_entity_decode()有点好奇。您正在运行哪种类型的服务器?

一个已知的IPN验证脚本可以与之进行比较:谢谢,您发布的脚本可以正常工作(尽管对于沙盒来说,PayPal url不正确)。我会看看这是怎么做的,以及为什么地雷不起作用。第二看。。。您是否在$value上调用urlencode()两次?一个已知的IPN验证脚本可以与之进行比较:谢谢,您发布的脚本可以正常工作(尽管对于沙盒,PayPal url不正确)。我会看看这是怎么做的,以及为什么地雷不起作用。第二看。。。是否对$value调用urlencode()两次?