Paypal IPN问题

时间:2012-03-11 07:19:40

标签: php paypal paypal-ipn

我的自动付款系统使用Paypal IPN工作。只有一个问题。问题是,在发送PayPal支付之后发生的事情发生在此之前,这意味着人们可以利用这个而不是支付而是获得该项目。

    <?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-listener代码。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

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

  • 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
}