这是我解释 PayPal 通知的代码。我不断从贝宝收到“无效”。很抱歉,这个问题本质上只是一个代码片段,但我已经研究了很长时间,似乎没有取得任何进展。
如果有人有任何想法,但需要查看一些调试信息,那么我设法获取它的方式是通过 php mail() 函数 - 所以只要问我就能得到它。
<?php
$debugmessage = "";
require($_SERVER['DOCUMENT_ROOT'] . '/xxxx.php');
mysql_connect("$server", "$user", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
// 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
echo "http error";
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
$debugmessage .= "|".$res;
$debugmessage .= "|".strlen($req);
if (strcmp ($res, "VERIFIED") == 0) {
// PAYMENT VALIDATED & VERIFIED!
$debugmessage .= "|"+"payment VALID";
$email = $_POST['payer_email'];
$password = mt_rand(100000, 999999);
mysql_query("INSERT INTO users (email, password) VALUES('". mysql_escape_string($email) ."', '".md5($password)."' ) ") or die(mysql_error());
$to = $email;
$subject = 'Download Area | Login Credentials';
$message = '
Thank you for your purchase!
Your account information
-------------------------
Email: '.$email.'
Password: '.$password.'
-------------------------
You can now login at xxxxxxxxxxxx';
$headers = 'From:xxxxxxx@xxxxxxx.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
else if (strcmp ($res, "INVALID") == 0) {
$debugmessage .= "|"."payment INvalid";
// PAYMENT INVALID & INVESTIGATE MANUALY!
$to = 'xxxxxx@xxxxxxx.com';
$subject = 'Download Area | Invalid Payment';
$message = '
Dear Administrator,
A payment has been made but is flagged as INVALID.
Please verify the payment manualy and contact the buyer.
Buyer Email: '.$email.'
';
$headers = 'From:xxxxxx@xxxxxxx.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
}
fclose ($fp);
}
mail("xxxxxxx@gmail.com",$debugmessage,"");
?>
以防万一有人理解那可怕$debugmessage
的混乱,一旦脚本运行就包含这个:
|HTTP/1.0 200 OK |840|X-Frame-Options: SAMEORIGIN |840|Strict-Transport-Security: max-age=14400 |840|Strict-Transport-Security: max-age=14400 |840|Content-Type: text/html; charset=UTF-8 |840|Date: Tue, 05 Mar 2013 04:42:09 GMT |840|Content-Length: 7 |840|Connection: close |840| |840|INVALID|840|payment INvalid
我已经读过关于不使用fsockopen
和使用名为“CURL”的东西的东西,但我对这些东西不太自信。
任何帮助将非常感激。