我试图让这个PayPal的IPN脚本工作,但似乎我没有得到Paypal的回应.我不知道什么是问题,是在形式或脚本。
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input name="return" type="hidden" value="http://localhost/home/menu/index.php" />
<input name="cancel_return" type="hidden" value="http://localhost/home/menu/index.php" />
<input name="notify_url" type="hidden" value="http://localhost/home/menu/php/inni.php" />
这是脚本inni.php。
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$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.sandbox.paypal.com', 443, $errno, $errstr, 30);
$item_name = $_POST['item_name'];
if (!$fp) {
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// vérifier que payment_status a la valeur Completed
if ( $payment_status == "Completed") {
if ( $email_account == $receiver_email) {
//insert in db
}
}
else {
// Statut de paiement: Echec
}
exit();
}
else if (strcmp ($res, "INVALID") == 0) {
// Transaction invalide
}
}
fclose ($fp);
}
问题是你把PayPal发到了localhost。
把链接(notify_url)改成一个可以公开访问的URL或IP地址。
PayPal从他们的服务器上发送通知,所以发送至localhost将无法到达你,因为在这种情况下localhost将是PayPal服务器本身。
你需要将PayPal的IPN请求转发到你的本地电脑上,这样才能正常工作。有几个项目在处理这个问题,包括免费和商业项目。
免费和开源的变体是。https:/github.comantoniomikasish。
一个商业变种。https:/ngrok.com。
由于不需要配置服务器,Ngrok的实现速度更快。
使用Ngrok转发http请求到本地主机
./ngrok http -host-header=localhost 80
然后在浏览器中打开
http://localhost:4040/inspect/http
从状态页中复制你的ngrok网址。
https://d1c7361aebf0.ngrok.io
假设你的IPN通知网址是
https://www.example.com/api/paypal-ipn
更新为并开始测试即时付款通知。
https://d1c7361aebf0.ngrok.io/api/paypal-ipn
注意:确保你禁用所有与域名相关的重定向,它才会工作。