dongshungai4857 2015-03-14 10:02
浏览 35
已采纳

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

So, I am using 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. 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):

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
";
$header .= "Content-Type:application/x-www-form-urlencoded
";
$header .= "Content-Length:" . strlen($req) ."

";


// 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
 $req");
  • 写回答

1条回答 默认 最新

  • dtewnsdf47253 2015-03-15 01:39
    关注

    Take a look at this article on how to test PayPal IPN for tips on testing within a browser. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题