Paypal IPN无法正常工作,无法验证

时间:2013-11-17 12:37:23

标签: php paypal paypal-ipn paypal-sandbox

我正在使用沙盒测试Paypal IPN,但它无法运行。结果总是显示INVALID,我不明白为什么。请帮助我检查编码中是否有任何错误。

(每次我发送IPN时,IPN历史记录中都没有记录数据。)

    <?php

class Paypal_IPN
{
    /** @var string $_url The paypal url to go to through cURL;

/**
* @param string $mode 'live' or 'sandbox'
*/

public function __construct($mode = 'live')
{
    if ($mode == 'live')
    $this->_url = 'https://www.paypal.com/cgi-bin/webscr';
    else
    $this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

}

public function run()
{
    $postFields = 'cmd=_notify-validate';

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

    $ch = curl_init();

    curl_setopt_array($ch, array(
    CURLOPT_URL => $this->_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postFields
    ));

    $result = curl_exec($ch);echo curl_error($ch);
    curl_close($ch);

    $fh = fopen('result.txt', 'w');
    fwrite($fh, $result . ' -- ' . $postFields);
    fclose($fh);
    echo $result;
    }
}
?>




  <?php
require 'Paypal_IPN.php';
$paypal = new Paypal_IPN('sandbox');
$paypal->run();
?>

1 个答案:

答案 0 :(得分:0)

这是我重新填充字符串的循环。多年来我一直在调整它以处理奇怪的问题。另外,请记住最后一个字符串必须编码为UTF8

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    //Fixes some special characters Paypal sends
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}', $value);
    $req .= '&' . $key . '=' . $value;
}