【问题标题】:Paypal Ipn Not Working Code StopsPaypal Ipn 不工作代码停止
【发布时间】:2013-12-18 22:22:04
【问题描述】:

我的 paypal ipn 代码在一段时间后停止工作,或者似乎没有从 while 中收集正确的信息,或者信息未设置。是否有人对这行代码的正确输入有任何想法。代码如下:

 while (!feof($fp)) {
 $res = fgets ($fp, 1024);

上面的代码是否与下面的代码一起正常运行。代码如下:

  $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);   

大部分代码如下:

// Response from Paypal

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
    $req .= "&$key=$value";
}

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

// 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) {

有人知道为什么这部分paypal ipn代码没有通过吗?

【问题讨论】:

  • 应该sslhttps 吗?另外,这在什么情况下不起作用?它停在哪里?
  • $res = fgets ($fp, 1024);

标签: php paypal


【解决方案1】:

我找到了这个脚本: https://github.com/paypal/ipn-code-samples/blob/master/paypal_ipn.php

我遇到了同样的问题,没有得到回应。决定改用 cURL 进行研究。该脚本完美运行。我确实需要安装 cURL 并下载脚本 cmets 中引用的证书。

【讨论】:

    【解决方案2】:

    您应该使用 curl 而不是 ssl 套接字。您需要构建您的请求(在我的示例中为 $req),作为对值字符串,即:$req="key1=value1&key2=value2";

      $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
      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);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
    
      if( !($res = curl_exec($ch)) ) {
          curl_close($ch);
          exit;
      }
      curl_close($ch);
    

    您将在 $res var 中获得 Paypal 结果。

    【讨论】:

      【解决方案3】:

      改变这一行:

      $value = urlencode(stripslashes($value));
      

      到这里:

      $value = urlencode($value);
      

      如果您正在运行带有魔术引号的 PHP,您只需要在此处使用 stripslashes。从 PHP 5.4 开始,这(谢天谢地)不再可能。

      Paypal 允许用户数据包含反斜杠(我在 IPN 数据的 address_street 变量中看到了它)。如果您从包含它们的 IPN 数据中去掉反斜杠,Paypal 将返回一个 INVALID 响应。

      【讨论】: