tartou2 Posted May 7, 2010 Share Posted May 7, 2010 Hello Can anybody help me with this paypal ipn code? The ipn simulator says it is working but i can't update the data from the databse. It is not working for some reason and i am trying to work on it from months but can't figure out the problem this is the code from the purchase.php <body onLoad=\"document.paypal_form.submit();\"> <form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" name=\"paypal_form\"> <input type=\"hidden\" name=\"cmd\" value=\"_xclick\"> <input type=\"hidden\" name=\"business\" value=\"[my email]\"> <input type=\"hidden\" name=\"item_name\" value=\"SMS Credit Purchase\"> <input type=\"hidden\" name=\"item_number\" value=\"$no[1]\"> <input type=\"hidden\" name=\"no_note\" value=\"1\"> <input type=\"hidden\" name=\"currency_code\" value=\"USD\"> <input type=\"hidden\" name=\"return\" value=\"$full_url\"> <input type=\"hidden\" name=\"amount\" value=\"$price\"> <input type=\"hidden\" name=\"notify_url\" value=\"[my website]/paypal/ipn.php\"> </form> And this is the code of the ipn.php <?php $server = 'localhost'; $database = '[hidden]'; $username = '[hidden]'; $password = '[hidden]'; $connection = mysql_connect($server,$username,$password); if (!$connection) { die('Could not connect to MySQL database, the server return the error: '.mysql_error()); } // $db2 = mysql_select_db($database2); $db = @mysql_select_db($database); if(isset($_COOKIE['username'])) { $username = $_COOKIE['username']; } $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; //Remove this line after you have debugged //print $req; } // 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 $payment_status = $_POST['payment_status']; $receiver_email = $_POST['receiver_email']; $payer_email = $_POST['payer_email']; // email header $em_headers = "From: $receiver_email\n"; $em_headers .= "Reply-To: $receiver_email\n"; $em_headers .= "Return-Path: $receiver_email\n"; if (!$fp) { // HTTP ERROR } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { print "VERIFIED"; // process payment if ($receiver_email == "[my paypal email]" AND $payment_status == "Completed") { mysql_query("UPDATE approvepaypal SET approved = 'Yes' WHERE username = '".$username."'"); } } else if (strcmp ($res, "INVALID") == 0) { // log for manual investigation print "NOT VERIFIED"; mysql_query("UPDATE approvepaypal SET approved = 'Failed' WHERE username = '".$username."'") or die(mysql_error()); } } fclose ($fp); } mysql_close(); ?> The user is submitted to the paypal payment page and pay the price but the database is not updated. I know the code still need more work to be improved and i will work on it but i need first some help to make it work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/ Share on other sites More sharing options...
tartou2 Posted May 7, 2010 Author Share Posted May 7, 2010 No one knows how to fix it??? Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054766 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2010 Share Posted May 7, 2010 Give people time to look -- you only waited 45 minutes. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054780 Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2010 Share Posted May 7, 2010 The most obvious problem is that the HTTP request that that IPN makes to your web server does not contain any cookie values, so your code does not have any $username and the queries won't ever match any row. The HTTP request from your visitor (which would contain his cookie values or his session id) is completely separate from the HTTP request you receive from the IPN. The only information you get from the IPN is the $_POST data that you have configured the IPN to send to you. Edit: Perhaps you are thinking of the AUTO RETURN URL that paypal returns the visitor to at the end of a payment? That HTTP request would contain any cookie/session id that is set for that visitor because it would be the visitor's browser that is making that request on your site. Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054802 Share on other sites More sharing options...
tartou2 Posted May 7, 2010 Author Share Posted May 7, 2010 The most obvious problem is that the HTTP request that that IPN makes to your web server does not contain any cookie values, so your code does not have any $username and the queries won't ever match any row. The HTTP request from your visitor (which would contain his cookie values or his session id) is completely separate from the HTTP request you receive from the IPN. The only information you get from the IPN is the $_POST data that you have configured the IPN to send to you. Edit: Perhaps you are thinking of the AUTO RETURN URL that paypal returns the visitor to at the end of a payment? That HTTP request would contain any cookie/session id that is set for that visitor because it would be the visitor's browser that is making that request on your site. I tried to replace the $username with a direct username which is "test1" but still no updates are done to the database. And the return link is just so the user will return to my website after he made the payment. I don't want to use it to update the database because it might be used to bypass the purchased process So the problem remain and the code is still not fixed Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054819 Share on other sites More sharing options...
tartou2 Posted May 7, 2010 Author Share Posted May 7, 2010 ok i have removed the if statment from the verified part and the database got updated. so what is the problem with this if statment? if ($receiver_email == "[my paypal email]" AND $payment_status == "Completed") { mysql_query("UPDATE approvepaypal SET approved = 'Yes' WHERE username = 'test1'"); } And this the used code: <?php $server = 'localhost'; $database = '[hidden]'; $username = '[hidden]'; $password = '[hidden]'; $connection = mysql_connect($server,$username,$password); if (!$connection) { die('Could not connect to MySQL database, the server return the error: '.mysql_error()); } // $db2 = mysql_select_db($database2); $db = @mysql_select_db($database); if(isset($_COOKIE['username'])) { $username = $_COOKIE['username']; } $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; //Remove this line after you have debugged //print $req; } // 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 $payment_status = $_POST['payment_status']; $receiver_email = $_POST['receiver_email']; $payer_email = $_POST['payer_email']; // email header $em_headers = "From: $receiver_email\n"; $em_headers .= "Reply-To: $receiver_email\n"; $em_headers .= "Return-Path: $receiver_email\n"; if (!$fp) { // HTTP ERROR } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { print "VERIFIED"; mysql_query("UPDATE approvepaypal SET payed = 'yes' WHERE username = 'test1'"); } else if (strcmp ($res, "INVALID") == 0) { // log for manual investigation print "NOT VERIFIED"; mysql_query("UPDATE approvepaypal SET approved = 'Failed' WHERE username = '".$username."'") or die(mysql_error()); } } fclose ($fp); } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054825 Share on other sites More sharing options...
tartou2 Posted May 7, 2010 Author Share Posted May 7, 2010 oh i have found the solution thx PFMaBiSmAd for your help. Your idea about the cookies was correct. The ipn works fine now. Cheeers Quote Link to comment https://forums.phpfreaks.com/topic/201035-need-help/#findComment-1054831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.