Php 贝宝IPN问题

Php 贝宝IPN问题,php,paypal,paypal-ipn,Php,Paypal,Paypal Ipn,我让我的自动支付系统使用贝宝IPN。只有一个问题。问题是,贝宝付款后发生的事情发生在这之前,这意味着人们可以利用这一点,而不是支付,但得到的项目 <?php include("../config/config.php"); include("../config/functions.php"); // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; foreach ($_P

我让我的自动支付系统使用贝宝IPN。只有一个问题。问题是,贝宝付款后发生的事情发生在这之前,这意味着人们可以利用这一点,而不是支付,但得到的项目

    <?php
include("../config/config.php");
include("../config/functions.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.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) {


// PAYMENT VALIDATED & VERIFIED!


}

else if (strcmp ($res, "INVALID") == 0) {

// PAYMENT INVALID & INVESTIGATE MANUALY!

}
}
fclose ($fp);
}
?>

这就是我正在使用的paypal ipn侦听器代码


为什么会发生这种情况?

我认为您的代码中没有安全问题。您的代码看起来像是基于官方PayPal示例。顺序如下:

  • PayPal联系您配置的地址
  • 您收到一条消息
  • 你把信息发送给贝宝
  • 他们确认他们确实发出了这一信息
  • 如果PayPal未确认,您将丢弃该消息
如果您想使其更加安全,请将安全令牌(如随机加密值)作为自定义参数的一部分,作为初始事务的一部分传递给PayPal。这些参数将在名为
custom
的查询字符串参数中重新发送给IPN处理程序。然后,您可以验证请求是否遵循了预期的生命周期

样品 如果我误读了你的代码,我会发布一个直接从PayPal SDK移植的.Net版本。逻辑很简单

byte[] inputBytes = Request.BinaryRead( HttpContext.Current.Request.ContentLength );
string incomingParams = Encoding.ASCII.GetString( inputBytes );
string outgoingParams = incomingParams + "&cmd=_notify-validate";

//
// Create request to send back to PayPal
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( AppSettings.PayPalUrl );
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = outgoingParams.Length;

//
// send the request back to PayPal
StreamWriter streamOut = new StreamWriter( request.GetRequestStream(), System.Text.Encoding.ASCII );
streamOut.Write( outgoingParams );
streamOut.Close();

//
// receive a response from PayPal
StreamReader streamIn = new StreamReader( request.GetResponse().GetResponseStream() );
string verificationStatus = streamIn.ReadToEnd();
streamIn.Close();

if( verificationStatus == "VERIFIED" )
{
    // if the request/response relationship is valid, we can now
    // use the initial parameters received from PayPal.

}
else if( verificationStatus == "INVALID" )
{
    //log for manual investigation
}
else
{
    //log response/ipn data for manual investigation
}

我用这个教程来构建我的IPN处理程序,它工作得很好,尽管它看起来和你的类似:那是因为它与你的完全相同:)。。这是因为它们都是基于PayPal IPN示例代码的——只是删除了版权声明,呵呵