我一直在试图弄清楚我的 ipn 脚本有什么问题。我尝试了许多不同的方法,也尝试了 fsockopen,但我没有让它工作。现在我正在使用 cURL,但它仍然无法正常工作。我已经安装了 cURL,它应该可以正常工作。
我的脚本(只是一个简单的例子,我删除了一些仅用于测试的检查):
<?php
$ACTIVE_CONNECTION = mysql_connect('127.0.0.1', 'mysqluser', 'mysqlpassword') or die("Could not connect to server.");
mysql_select_db('mysqldb', $ACTIVE_CONNECTION) or die("Could not connect to database.");
$tid = $_GET['tx'];
$auth_token = "aqiopfsawdaisytrgkl";
$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$post_vars = "cmd=_notify-synch&tx=" . $tid . "&at=" . $auth_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'cURL/PHP');
$fetched = curl_exec($ch);
$lines = explode("\n", $fetched);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$payment_amount = $keyarray['mc_gross'];
$payment_status = $keyarray['payment_status'];
$payment_currency = $keyarray['mc_currency'];
$txn_id = $keyarray['txn_id'];
$receiver_email = $keyarray['receiver_email'];
$payer_email = $keyarray['payer_email'];
$username = mysql_real_escape_string($keyarray['custom']);
if ($payment_status == 'Completed'){
mysql_query("UPDATE `users` SET vip = '1' WHERE `username` = '".$username."'");
}
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// manual investigation here
}
?>