為什麼我的PHP PAYPAL IPN不能Update database

為什麼我的PHP PAYPAL IPN不能Update database? 有沒有高手大大可幫忙debug?謝謝!


payment前的頁面:
<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="[email protected]" />

<?
$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++;
}

?>


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





update database的頁面:
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";



$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 = "[email protected]"; 


//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");
標籤:
Sign In or Register to comment.