IPN脚本不更新MYSQL数据库

时间:2013-04-17 17:15:48

标签: php mysql paypal-ipn

我正在尝试使用ipn脚本,以便将Paypal集成到我的网站中。

一旦购买完成,我的目标是脚本更新3个MYSQL值,溢价值(需要更新为1以显示用户是溢价),然后是注册的开始和结束日期。

但是,当我测试时,数据库不会更新。也没有任何错误消失。

继承数据库更新部分代码:

//update premium to 1    
$query = sprintf("UPDATE `users` SET `premium` = '1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update premium status: ' . mysql_error());

//update startdate to current date
$query = sprintf("UPDATE `users` SET `startdate` = '$startdate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (1): ' . mysql_error());

//update enddate to current date +365 days
$query = sprintf("UPDATE `users` SET `enddate` = '$enddate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (2): ' . mysql_error());

下面是完整的ipn代码:

    <pre class="php"><?php
require 'glob.inc.php';

$posted_data = print_r($_POST,true);
file_put_contents('IPN_data.txt',$posted_data);

// 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);

// assign posted variables to local variables
$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"];
$user_id = $_POST["custom"]; //our users id.

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

        if ($_POST['payment_status'] == 'completed')
{ 

//values for date info

$startdate1 = date("Y-m-d");
$enddate1 = date("Y-m-d", strtotime("+ 365 day"));

//update whatever information you need to               
// update premium to 1

                                            $query = sprintf("UPDATE `users` SET `premium` = '1' WHERE `user_id` = '".$user_id."'");

                                              mysql_query($query)or die('Could not update premium status: ' . mysql_error());

                                              //update startdate to current date

                                                $query = sprintf("UPDATE `users` SET `startdate` = '$startdate1' WHERE `user_id` = '".$user_id."'");

                                              mysql_query($query)or die('Could not update licence date details (1): ' . mysql_error());

                                              //update enddate to current date +365 days

                                               $query = sprintf("UPDATE `users` SET `enddate` = '$enddate1' WHERE `user_id` = '".$user_id."'");

                                              mysql_query($query)or die('Could not update licence date details (2): ' . mysql_error());

}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?></pre>

我知道我正在使用折旧的MYSQL,但我必须参与这个项目。只需要和我一起去那个。我也是一个初学者,所以我正在学习。脚本通过glob.inc.php连接到数据库,这在ipn脚本的开头是必需的。

我查看了Paypal的IPN数据报告,并且正在发送和返回正确的用户ID(自定义字段)。数据库不会更新......

有什么想法吗?

0 个答案:

没有答案