简体   繁体   English

贝宝IPN问题

[英]Paypal IPN issue

I got my automated payment system to work using Paypal IPN. 我使用Paypal IPN使我的自动付款系统可以工作。 There is just one problem. 只有一个问题。 The problem is that the stuff that happens after the paypal payment has been sent happens before that, meaning people could exploit this and not pay but get the item. 问题在于,在发送贝宝付款之后发生的事情发生在那之前,这意味着人们可以利用此东西,而不是付款却得到了物品。

    <?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);
}
?>

That is the paypal ipn-listener code I am using. 那就是我正在使用的贝宝ipn-listener代码。

Why is this happening? 为什么会这样呢?

I don't think there is a security issue in your code. 我认为您的代码中没有安全问题。 Your code looks like it is based on the official PayPal samples. 您的代码看起来像是基于官方的PayPal示例。 The sequence is this: 顺序是这样的:

  • PayPal contacts an address you configure 贝宝联系您配置的地址
  • You receive a message 您收到一条消息
  • You send the message to PayPal 您将消息发送到PayPal
  • They confirm that they indeed sent the message 他们确认他们确实发送了邮件
  • If PayPal doesn't confirm, you discard the message 如果贝宝(PayPal)无法确认,则您丢弃该消息

If you want to make it even more secure, include a security token (such as a random encrypted value) as part of the custom parameters which are passed to PayPal as part of the initial transaction. 如果您想使其更加安全,请将安全令牌(例如随机加密值)作为自定义参数的一部分,并将其作为初始交易的一部分传递给PayPal。 These parameter(s) will be resent to the IPN handler in a query string parameter named custom . 这些参数将在名为custom的查询字符串参数中重新发送到IPN处理程序。 You can then validate that the request followed the lifecycle you intended. 然后,您可以验证请求是否遵循了预期的生命周期。

Sample 样品

In case I am misreading your code, I'm posting a .Net version which I ported straight from the PayPal SDK. 万一我误读了您的代码,我会发布一个.Net版本,我直接从PayPal SDK移植了该版本。 The logic is pretty simple to follow. 逻辑很容易遵循。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM