11

正如以下答案中的评论之一所述,我尝试按照本教程进行操作。所以现在我有以下内容:


ipn.php 文件:

<?php

    $ipn_post_data = $_POST;

    $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

    // Set up request to PayPal
    $request = curl_init();
    curl_setopt_array($request, array
    (
        CURLOPT_URL => $url,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => http_build_query(array('cmd' => '_notify-validate') + $ipn_post_data),
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HEADER => FALSE,
        CURLOPT_SSL_VERIFYPEER => TRUE,
        CURLOPT_CAINFO => 'cacert.pem',
    ));

    // Execute request and get response and status code
    $response = curl_exec($request);
    $status   = curl_getinfo($request, CURLINFO_HTTP_CODE);

    // Close connection
    curl_close($request);

    if($status == 200 && $response == 'VERIFIED')
    {
        $subject = "valid";
        $message = "good";
    }
    else
    {
        $subject = "invalid";
        $message = "bad";
    }

    $to = "oshirowanen@mail.com";
    $from = "me@desktop.com";

    $header  = 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $header .= 'To: Oshirowanen <oshirowanen@mail.com>' . "\r\n";
    $header .= 'From: Me <me@desktop.com>' . "\r\n";

    mail($to,$subject,$message,$header);

?>

收到的邮件:

Subject "invalid"
Message "bad"
4

9 回答 9

14

编辑:

现在我可以看到您输出的数组,尝试替换它以消除 PHP 数组错误:

foreach ($_POST as $key => $value) {
    if (!is_array($value)) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    else if (is_array($value)) {
        $paymentArray = explode(' ', $value[0]);
        $paymentCurrency = urlencode(stripslashes($paymentArray[0]));
        $paymentGross = urlencode(stripslashes($paymentArray[1]));
        $req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
    }
}

这是完整的编辑代码:

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

foreach ($_POST as $key => $value) {
    if (!is_array($value)) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    else if (is_array($value)) {
        $paymentArray = explode(' ', $value[0]);
        $paymentCurrency = urlencode(stripslashes($paymentArray[0]);
        $paymentGross = urlencode(stripslashes($paymentArray[1]);
        $req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
    }
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HEADER, 0);
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_HTTPHEADER, array('Host: www.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);


// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];


if (strcmp ($res, "VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
}
else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}

看看这个

编辑:查看 PayPal 故障排除提示:

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNTesting

于 2012-08-02T18:38:43.523 回答
6

问题是您没有检查 HTTP 响应代码,因此您将“无效主机标头”解释为 PayPal 响应,而它是 Web 服务器响应(对于状态代码 400)。
如果您查看PayPal 文档,有一个与您的代码非常相似的 PHP 示例,因为它使用“fsockopen”、“fputs”和“fgets”函数与 PayPal 服务器进行通信。
但是如果你仔细看“fsockopen”调用后的注释,你可以读到:

// Process validation from PayPal 
// TODO: This sample does not test the HTTP response code. All 
// HTTP response codes must be handled or you should use an HTTP 
// library, such as cUrl

这正是您的问题:在解析响应正文之前,您没有检查 HTTP 响应代码是否为 200(OK)。
此外,使用“strtolower”函数是不正确的,因为来自 PayPal 服务器的真实响应总是大写的,如上面引用的示例所示。
即使 PayPal 示例使用“fsockopen”方法,我认为使用PHP cURL库来实现您的 IPN 侦听器应该会更好。
还请查看以下答案:

但是,如果您真的想使用“fsockopen”函数,则应始终在 POST 请求中指定“Host”标头字段,如以下代码片段所示(摘自PHP 手册):

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

更新

这是递归stripslashes / urlencoding的简单函数:

<html>
<body>
<pre>
<?

$post = Array (
  "transaction" => Array("USD 20.00"),
  "payment_request_date" => "Sun Aug '05 08:49:20 PDT 2012",
  "return_url" => "http://000.000.000.000/success.php"
);

echo "before myUrlencode...\n";
print_r($post);

function myUrlencode($post) {
  foreach ($post as $key => $val) {
    if (is_array($val)) {
      $post[$key] = myUrlencode($val);
    } else {
      $post[$key] = urlencode(stripslashes($val));
    }
  }
  return($post);
}

echo "\nafter myUrlencode...\n";
print_r(myUrlencode($post));

?>
</pre>
</body>
</html>
于 2012-08-03T21:14:35.390 回答
2
  1. 使用基本示例代码4b让它工作,

  2. 从基本示例代码中清除$ipnNotificationUrl = "";,因为我有一个我自己添加的值,

  3. 在沙盒中创建了卖家账户而不是商业专业账户,

  4. 设置卖家账号启用ipn url,

  5. 将以下 PHP 5.2示例代码用于 ipn 侦听器

  6. 将 2 行添加到侦听器中,如此处所述,2 行如下所示:

  7. 从这里cacert.pem将证书下载到我的服务器并将其放在与 ipn 侦听器相同的目录中:

第 6 点中提到的 2 行:

CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_CAINFO => 'cacert.pem',

我不知道为什么沙盒业务专业帐户不允许我设置 ipn url,但卖家帐户可以。

于 2012-08-16T09:55:21.977 回答
1

我不确定你的代码现在到底出了什么问题,但我之前也在苦苦挣扎,我的修复是在标题中添加主机,主机必须是 www.paypal.com。我使用了 fsockopen 方法,现在工作正常。

在 Curl 中,我之前遇到过 ssl 问题。解决方案是把这些行:

curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");

当然文件 cookies.txt 必须存在的地方。而且,我必须运行一个到页面的连接来获取会话数据,然后再发送发布数据。

下面是一个标题,使用 fsockopen 方法对我来说工作正常

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
于 2012-11-15T22:49:47.237 回答
1

这些链接可能会解决您的问题,

Paypal:IPN 无效问题

http://www.webmasterworld.com/ecommerce/4292847.htm

Paypal 沙盒 IPN 返回无效

于 2012-08-03T13:18:22.163 回答
1

这是 + 字符的问题,它经常被错误地获取,所以我做了这个解决方法,它对我有用。

payment_data = 2016 年 6 月 4 日星期六 15:11:16 GMT+0200 (CEST)

foreach ($_POST as $key => $value) {
if($key !== "payment_date"){
    $req .= '&' . $key . '=' . rawurlencode(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
}else{
    $req .= '&' . $key . '=' . rawurlencode(str_replace(array('GMT '),array('GMT+'),$value));
}}
于 2016-06-04T13:32:59.000 回答
0

以下是如何避免这些错误...

foreach ($_POST as $key => $value) {
     if ($key=='transaction')
          foreach ($value as $key2=>$value2) {
               $value['transaction'][$key2] = urlencode(stripslashes($value2));
     }
     else {
          $value = urlencode(stripslashes($value));
     }
     $req .= "&$key=$value";
 }
于 2012-08-09T13:21:34.673 回答
0

我终于找到了这个查询的更新(2016 年 8 月 5 日)工作答案。您可以将此代码用作 Sandbox 或 Live 的最终 IPN。有以下考虑:

  1. 请务必将您的 IPN 监听器放置到 -> 我的销售工具 -> 即时付款通知部分。
  2. 不要在沙箱中使用 IPN 模拟器,它总是会返回 INVALID。
  3. 创建并使用一个实际的沙盒按钮,但不要将您的 IPN 侦听器放到显示“在客户完成结帐时将其带到此 URL”的返回页。

这就是全部。我希望这将有所帮助。

这是工作代码:

<?php
$post_data = file_get_contents('php://input');
$post_array = explode('&', $post_data);
$dataFromPayPal = array();
foreach ($post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $dataFromPayPal[$keyval[0]] = urldecode($keyval[1]);
}

$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($dataFromPayPal 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";
}

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
//use https://www.sandbox.paypal.com/cgi-bin/webscr in case you are testing this on a PayPal Sanbox environment
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_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

if( !($res = curl_exec($ch)) ) {
    curl_close($ch);
    exit;
}
curl_close($ch);



if (strcmp ($res, "INVALID") == 0) {
        echo "INVALID";
}
else if (strcmp ($res, "VERIFIED") == 0) {
        echo "VALID";
}

?>
于 2016-08-05T00:34:13.293 回答
0

拉了几个小时的头发,直到我看到伊祖丁的回答。他是对的..日期中的+没有被转移。只是为了测试,我从模拟器中预先填充的字段中删除了它Verified,最后得到了一个。

于 2016-07-09T12:54:44.697 回答