Paypal php IPN没有更新我的数据库

时间:2015-03-14 10:02:59

标签: php paypal paypal-ipn

所以,我使用的是Paypal IPN。但它不适用于更新我的数据库(购物车和发票)。有人会帮我调试我的代码吗?它将成功的IPN发送到PayPal,但是没有更新任何东西。这是代码:

付款页面上的代码:

<form action="https://www.paypal.com/cgi-bin/webscr" name="frm" id="frm" method="post">

<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="fin@mycoupon.com.hk" />
<?

$result = mysql_query("SELECT * FROM cart, member, product
where cart.member_id = member.member_id
And cart.product_id = product.product_id
And member.email = '$_SESSION[member]'
And cart.status = 'cart'
And cart.quantity > (SELECT quantity FROM quantity where quantity.quantity_id = cart.quantity_id)
"); 

while($row = mysql_fetch_array($result))
{
$del_cart_id = $row['cart_id'];
$del_product_id = $row['product_id'];
$del_member_id = $row['member_id'];
mysql_query("UPDATE cart SET status = 'delete', last_modified = now()
where member_id = '$del_member_id'
And product_id = '$del_product_id'
And cart_id = '$del_cart_id'
"); 
}

$i =1;
$cartresult = mysql_query("SELECT * FROM cart, product
where cart.member_id = '$member_id'
And cart.product_id = product.product_id
And cart.invoice_id = '$invoice'
And cart.status = 'cart'
And cart.quantity <= (SELECT quantity FROM quantity where quantity.quantity_id = cart.quantity_id)

"); 

while($row = mysql_fetch_array($cartresult))
{
echo'<input type="hidden" name="item_number_'.$i.'" value="'.$row['cart_id'].'">';
echo'<input type="hidden" name="item_name_'.$i.'" value="'.$row['productname'].'">';
echo'<input type="hidden" name="amount_'.$i.'" value="'.$row['dprice'].'">';
echo'<input type="hidden" name="quantity_'.$i.'" value="'.$row['quantity'].'">';
mysql_query("UPDATE cart SET final_price = '$row[dprice]', last_modified = now()
where member_id = '$row[member_id]'
And product_id = '$row[product_id]'
And cart_id = '$row[cart_id]'
"); 
$i++;
}

$cartresult_rows = mysql_num_rows($cartresult); 

?>

<input type="hidden" name="currency_code" value="HKD">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="lc" value="HK">
<input type="hidden" name="return" id="return" value="http://www.mycoupon.com.hk/finish.php">   
<input type="hidden" name="cancel_return" value="http://www.mycoupon.com.hk/checkout_step3.php">    
<input type="hidden" name="notify_url" value="http://www.mycoupon.com.hk/notify_return.php">

<? if ($cartresult_rows > 0) {?>    
<script language="javascript">
setTimeout("frm.submit();",1000); 
</script>
<? } else {
echo '<META HTTP-EQUIV=REFRESH CONTENT="0; index.php">';
}
?>
</form>

我们在notify_return页面上的代码(更新页面):

header("content-type:text/html; charset=utf-8");
mb_internal_encoding('UTF-8');


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


// If testing on Sandbox use:
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);


// assign posted variables to local variables

$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payment_date = $_POST['payment_date'];
$payment_amount= $_POST['payment_amount'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payment_status = $_POST['payment_status'];
$payer_email = $_POST['payer_email'];
$num_cart_items = $_POST['num_cart_items'];

//DB connect creds and email 
$notify_email = "fin@mycoupon.com.hk"; 


//email address to which debug emails are sent to
function db_connect(){
$link_id=mysql_connect('xxx','xxx','xxxx');
mysql_query("SET NAMES 'UTF8'");
if(!mysql_select_db('mycouponshop', $link_id)){
echo mysql_errno($link_id).": " . mysql_error($link_id);
}
return $link_id;
if (!$link_id)
{
die('Could not connect: ' . mysql_error());
}   
}

$link = db_connect();



if (!$fp) {
//HTTP error
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($payment_status == "Refunded") {
mysql_query("UPDATE invoice SET cmd='$payment_status' WHERE txn_id='$txn_id'");
exit();
}

for($i = 1; $i <= $num_cart_items; $i++){
$cart_id = $_POST['item_number_'.$i];   
mysql_query("UPDATE cart SET status = 'finish' WHERE cart_id = '$cart_id'");
}   


$result = mysql_query("SELECT * FROM cart WHERE cart_id = '$cart_id'"); 
while($row = mysql_fetch_array($result))
{   
$invoice_id = $row['invoice_id'];
$memebr_id = $row['memebr_id'];
}


mysql_query("UPDATE invoice SET txn_id = '$txn_id',cmd = '$payment_status', pay_user='$payer_email', payment='paypal' , last_modified = now() where invoice_id = '$invoice_id'");   




}

// if the IPN POST was 'INVALID'...do this
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation

}
}
fclose ($fp);
}

mail($notify_email, "INVALID IPN", "$res - $payment_status\n $req");

1 个答案:

答案 0 :(得分:0)

how to test PayPal IPN上查看有关在浏览器中进行测试的提示的文章。这将允许您设置断点并找出正在运行的内容和不正常的内容,以及在屏幕上查看任何错误,以便您可以相应地解决它们。