princeofpersia Posted November 21, 2010 Share Posted November 21, 2010 Hi Guys I have created a registration page where when users login they can pay to become premium, however everything looks fine but paypal wont update the field. The field is 0 and needs to be 1 once payment has been made. Below is my code, <?php //connect to database $error = "Problem connecting"; mysql_connect('localhost','_prince','getmeout') or die($error); mysql_select_db('hmt_cms') or die($error); // 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 ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // assign posted variables to local variables $item_name = $_POST['item_name']; $item_number = $_POST['item_number']; $payment_status = $_POST['payment_status']; $payment_amount = $_POST['mc_gross']; $payment_currency = $_POST['mc_currency']; $txn_id = $_POST['txn_id']; $receiver_email = $_POST['receiver_email']; $payer_email = $_POST['payer_email']; if (!$fp) { // HTTP ERROR } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { if ($payment_status=="completed") { if ($payment_amount==1.00&&$payment_currency=="GBP") { $update = mysql_query("UPDATE cms SET premium='1' WHERE email='$payer_email'"); } } // check the payment_status is Completed // check that txn_id has not been previously processed // check that receiver_email is your Primary PayPal email // check that payment_amount/payment_currency are correct // process payment } else if (strcmp ($res, "INVALID") == 0) { // log for manual investigation } } fclose ($fp); } ?> im testing it from sanbox and these are my settings. IPN handler URL is correct Transaction type : cart checkout payment_status : completed payment type: instant email is the same email registered mc_fee = 1 the rest of mcs are set to 0 currency is GBP any help would be appreciated, thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/ Share on other sites More sharing options...
PaulRyan Posted November 21, 2010 Share Posted November 21, 2010 This seems to be a common problem wiht PayPal IPN. I always make sure I send myself an e-mail using the PHP mail function, this allows me to see what variables PayPal sends back and whther or not it is working. Add one mail after each section of code, this allows you to find out where the script is stopping, if it is stopping at all. Try adding some IF ELSE statements around the MySQL querys to check that they are functioning correctly and that indeed you are getting back the data and using it how you want too. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137575 Share on other sites More sharing options...
princeofpersia Posted November 21, 2010 Author Share Posted November 21, 2010 hi, thanks, im quit new to php can you please tell me what do u mean by adding email to each part of the code? thanks Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137578 Share on other sites More sharing options...
PaulRyan Posted November 21, 2010 Share Posted November 21, 2010 <?php //connect to database $error = "Problem connecting"; mysql_connect('localhost','_prince','getmeout') or die($error); mysql_select_db('hmt_cms') or die($error); mail('you@example.com', 'IPN Progress', 'Section 1 Completed'); // 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"; } mail('you@example.com', 'IPN Progress', 'Section 2 Completed'); // 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 ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); mail('you@example.com', 'IPN Progress', 'Section 3 Completed'); // assign posted variables to local variables $item_name = $_POST['item_name']; $item_number = $_POST['item_number']; $payment_status = $_POST['payment_status']; $payment_amount = $_POST['mc_gross']; $payment_currency = $_POST['mc_currency']; $txn_id = $_POST['txn_id']; $receiver_email = $_POST['receiver_email']; $payer_email = $_POST['payer_email']; if (!$fp) { mail('you@example.com', 'IPN Progress', 'IPN Error 1'); } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { if ($payment_status=="completed") { mail('you@example.com', 'IPN Progress', 'Section 4 Completed'); if ($payment_amount==1.00&&$payment_currency=="GBP") { mail('you@example.com', 'IPN Progress', 'Section 5 Completed'); $update = mysql_query("UPDATE cms SET premium='1' WHERE email='$payer_email'"); } } // check the payment_status is Completed // check that txn_id has not been previously processed // check that receiver_email is your Primary PayPal email // check that payment_amount/payment_currency are correct // process payment } else if (strcmp ($res, "INVALID") == 0) { // log for manual investigation mail('you@example.com', 'IPN Progress', 'IPN Error 2'); } } fclose ($fp); } ?> Try using the code above in place of your own, and change the email address to your own address. Then see how many emails you get, if you don't get 1 of the completed ones theres an error before that section, if you get an error email you know you aren't getting the correct details back from PayPal. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137587 Share on other sites More sharing options...
princeofpersia Posted November 21, 2010 Author Share Posted November 21, 2010 thank you very much for ur time Paul I'll give it a try and let you know. thanks Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137598 Share on other sites More sharing options...
princeofpersia Posted November 21, 2010 Author Share Posted November 21, 2010 checked it, I have recieved 7 emails, do u know what it could be? Section 1 Completed Section 3 Completed Section 2 Completed IPN Error 2 Section 2 Completed Section 3 Completed Section 1 Completed Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137605 Share on other sites More sharing options...
princeofpersia Posted November 21, 2010 Author Share Posted November 21, 2010 i foorgot to change the tax to 0 and now tried it again and only have 3 emails for section 1 - 2 - 3 completed Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137608 Share on other sites More sharing options...
PaulRyan Posted November 21, 2010 Share Posted November 21, 2010 Replace the following if ($payment_status=="completed") and replace it with if (strtolower($payment_status)=="completed") Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137614 Share on other sites More sharing options...
Pikachu2000 Posted November 21, 2010 Share Posted November 21, 2010 Please, when posting code on the forum, make use of the . . . BBCode tags. It will only help us help you. Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137618 Share on other sites More sharing options...
princeofpersia Posted November 21, 2010 Author Share Posted November 21, 2010 sure Pikachu2000 PAUL: you are a genius mate, it works perfectly. thank you very very much Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137619 Share on other sites More sharing options...
PaulRyan Posted November 21, 2010 Share Posted November 21, 2010 Glad it worked for you Could you please mark the thread as solved for us. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/#findComment-1137627 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.