我最近为新的 1.1 版本更新了我的 IPN 脚本。它可以工作并且付款通过,但它正在多次运行我的成功代码(插入数据库,发送电子邮件)。例如,我进行了一次测试购买,我的订单经历了 22 次,我收到了 22 封电子邮件?!?
这是我的确切代码(实际的数据库和邮件代码是直截了当的 - 根本没有循环):
//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 (replaces old headers)
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
//
//error connecting to paypal
if (!$fp) {
//
}
//successful connection
if ($fp) {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
$res = trim($res); //NEW & IMPORTANT
if (strcmp($res, "VERIFIED") !== false) {
//insert into db and send an email goes here
}
if (strcmp ($res, "INVALID" ) == 0) { }
}
fclose($fp);
}