PHP - PayPal IPN 始终返回无效


PHP - Paypal IPN always returns Invalid

这是我第一次处理PayPal IPN。我已经浏览了网络以获得一些帮助,并遇到了以下代码。我花了一段时间,它总是返回无效。不过,我想知道,这是否也与我在PayPal上选择的设置有关?我启用的只是 IPN 和重定向。

<?php 
    // read the post from PayPal system and add 'cmd' 
    $req = 'cmd=_notify-validate'; 
    foreach ($_POST as $key => $value) { 
    $value = urlencode(stripslashes($value)); 
    $req .= "&$key=$value"; 
    } 
    // 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);

    //handle 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 (!$fp) { 
      echo $errstr.' ('.$errno.').<br />'; 
    } else { 
      fputs ($fp, $header . $req); 
      while (!feof($fp)) { 
        $res = fgets($fp, 1024); 
        if (strcmp ($res, "VERIFIED") == 0) { 
          echo "Verified";
          if(strcmp($payment_status, 'Completed') == 0) { 
                if(strcmp($payment_currency, 'USD') == 0) { 
                        //process data 
                } 
          } 
        } elseif(strcmp ($res, "INVALID") == 0) { 
             echo "Invalid";
        } 
      } 
      fclose ($fp); 
    } 
    ?>
您可以

转到下面的链接以获取PayPal最新的IPN示例代码。

https://github.com/paypal/ipn-code-samples

PayPal下面的 IPN 文档供您参考。

https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNIntro/