PHP - Paypal IPN始终返回Invalid

时间:2016-02-22 03:23:53

标签: php paypal paypal-ipn

这是我第一次处理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); 
    } 
    ?>

1 个答案:

答案 0 :(得分:0)

您可以访问以下链接获取PayPal最新的IPN示例代码。

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

以下PayPal IPN文档供您参考。

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