我的自动付款系统使用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代码。
为什么会这样?
答案 0 :(得分:0)
我认为您的代码中没有安全问题。您的代码看起来像是基于官方的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
}