简体   繁体   English

贝宝php IPN无法更新我的数据库

[英]Paypal php IPN not updating my database

So, I am using Paypal IPN. 因此,我正在使用Paypal IPN。 But it is not working for updating my database (cart and invoice). 但是它不能用于更新我的数据库(购物车和发票)。 Would anyone help me to debug my code, please? 有人能帮我调试我的代码吗? It's sending successful IPN's to paypal, But isn't updating anything. 它向Paypal发送成功的IPN,但未进行任何更新。 Here's the code: 这是代码:

Our code on payment page: 我们在付款页面上的代码:

<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>

Our code on notify_return page (Update page): 我们在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");

Take a look at this article on how to test PayPal IPN for tips on testing within a browser. 查看有关如何测试PayPal IPN的本文, 获取在浏览器中进行测试的提示。 This will allow you to set break points and figure out what is running and what's not, as well as see any errors on screen so you can resolve them accordingly. 这将使您可以设置断点并找出正在运行的和未运行的,以及在屏幕上看到任何错误,以便您可以相应地解决它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM