我有一个paypal ipn监听器的脚本,运行时会验证付款并执行除应该更新数据库之外的所有其他内容。
我已经包含了以下代码。正在运行该脚本,因为INSERT正在运行,所以我也知道SQL数据库连接是成功的。有人可以确定为什么当我运行脚本时,行链接结束空白。
顺便说一下,我知道user_id是正确的。
<?php
session_start(); //start sessions
$user_id = $_SESSION['user_id']; //set user_id to session user_id
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
} //get the raw post data from paypal
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
} //get the IPN message sent from PayPal and set 'cmd=_notify-validate'
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
curl_close($ch); //post data back to paypal for validation
if (strcmp ($res, "VERIFIED") == 0) { //check if the data is valied
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
//create local variables from post data
foreach($_POST as $key => $value) {
echo $key." = ". $value."<br>";
}
include '../includes/connect_mysql.php'; //connect to mysql database with external file
mysqli_query($connect,"INSERT INTO tbl_payments (txnid, payment_amount, payment_status, itemid, createdtime)
VALUES ('".$txn_id."', '".$payment_amount."', '".$payment_status."', '".$item_number."', '".date("Y-m-d H:i:s")."')"); //insert transaction details into database
$result = mysqli_query($connect,"SELECT * FROM tbl_users WHERE user_id='$user_id' LIMIT 1");
while($row = mysqli_fetch_array($result)) {
$links = $row['links']; //find how many links the user has
}
if ($item_name == 0.10) {
$value = 10;
} elseif ($item_name == 0.25) {
$value = 25;
} elseif ($item_name == 0.50) {
$value = 50;
} else {
$value = 0;
} //work out how many links were bought
$new_value = $links + $value; //work out the users new amount of links
mysqli_query($connect, "UPDATE tbl_users SET links='$new_value' WHERE user_id='$user_id'"); //update the user's link total
$mail_From = "********";
$mail_To = $payer_email;
$mail_Subject = "Sucessful Purchase";
$mail_Body = "Thank you for your recent purchase at our store";
mail($mail_To, $mail_Subject, $mail_Body, $mail_From); //setup and send confirmation email
mysqli_close($connect); //close mysql database connection
} else if (strcmp ($res, "INVALID") == 0) {
echo "The response from IPN was: <b>" .$res ."</b>";
} //output error message if transaction failed
?>
非常感谢任何帮助。
提前致谢。