^ Scroll to Top
Contact us | Login

Paypal IPN INVALID on LIVE server but VALID on TEST server FIXED


This paylap IPN INVALID problem has been the bane of my life for the past few days.  I have been building a invoicing system to invoice my clients, this system uses the paypal IPN to update my invoices in my system as paid and send a thank you email to the customer.

I started running my test transactions on paypals sandbox server and everything worked fine, every transaction processed properly but as soon as i tried a few small transactions on the LIVE paypal site they were coming back as INVALID by the IPN script, they were being processed and completing successfully but just not being validated by the IPN script.

Naturally you start to weed out what the difference’s can be and the only things changing between the TEST & LIVE server was the server address, the buyer email and seller email addresses (you have to use test email accounts on the test server).  Eventually after trying PHP IPN scripts direct from the paypal site and stil getting the same result i thought it was maybe a problem with my personal account so i got my mother to try a live transaction and it failed too.

After trying everything i could think of, wasting a lot of time and making sure it was not my fault i decided to call paypal support, the tech-guy i eventually spoke to was very good and ran me through everything and didn’t make me feel rushed at all. I sent him the scripts i had been using and he checked them and they seemed to look ok to him so he was a bit puzzled as well. After eliminating my scripts as the problem he was happy to try a few test live transactions from his live test account and hay presto they worked!!! We then tried to find out why his worked and mine and my mothers failed, there was no obvious answer so i got a friend to  try a live transaction and it worked too. It was very confusing to me and the tech but i decided to go live and i sent out a few invoices from my new invoicing system and the first two that were paid came back as INVALID grrr…

I now had a long list of  VALID & INVALID IPN responses, and i lined them up in a table and went through every detail, i firstly noticed that the ones that actually VALIDATED had unconfirmed addresses and the INVALID ones had confirmed addresses.   I then ran a few tests and captured the IPN response before it was sent to paypal with the one paypal had in it’s IPN history, i then noticed that address line one and address line two(optional) were put on the same line in the IPN response but there was a carriage return between the first and second line.

I noticed that paypal sends this carriage as %0D%0A but carriage returns in php 5.2 is %0A which is what my server was sending back causing the INVALID IPN response. The reason this never happened with paypal’s sandbox test server is that the test accounts you make in the sandbox don’t have the second address line which is optional and which was causing the problem, also the IPN tester in the sandbox account doesn’t have the second address line, so it is almost imposable to recreate this problem on the test server, and in case you were wondering the tech and my friend’s liver test’s that worked both didn’t have the second optional address line filed out.

Solution

The one line of code that could have saved me hundreds in wasted time.

$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);

This line replaces the %0A with %0D%0A in your IPN repose string thus solving the INVALID warning and making it VALID where it should be.

I am not sure what is causing the difference in the way the server are encoding the url but if you are having this same problem just add the line to your IPN script like i did below.

// read the post from PayPal system and add 'cmd'
		$req = 'cmd=_notify-validate';

		foreach ($_POST as $key => $value)
		{
			$value = urlencode(stripslashes($value));
			$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
			$req .= "&$key=$value";

Thanks for reading and if this helps you please feel free to leave me a comment.

27 Responses to “Paypal IPN INVALID on LIVE server but VALID on TEST server FIXED”

  1. Paul Arnold says:

    I’ve just been testing a new site we’ve built and some transactions were working and some weren’t.
    The only difference was that I was using two different postal addresses.

    This has just saved me a hell of a lot of work. Thank you very much.

  2. Your instructions helped me very much and i saved my time smartly. thanks a lot.

  3. David says:

    Thanks for this post. It gave me guidelines to solve the problem

  4. Martin Dodd says:

    This was the missing link and I appreciate the help. Many, many thanks!

  5. Majika says:

    Thank you

  6. Nathan says:

    I just solved my problem with this solution. I was able to send a test IPN through the IPN simulator and got back a VERIFIED response each time. But, when I tried to do a real purchase through the sandbox I would get an INVALID response each time. It’s annoying the the different paypal environments have different results and different features and I’m interested to see what happens when I push this through to prod. Thanks again.

  7. [...] found the solution on this site. The long and short is there is a carriage return between address line 1 and line 2 that paypal [...]

  8. Damir says:

    Just encountered the same problem. I would never (and not event then) realize that problem is in new line on address_street field.

    But your regexp didn’t work for me. I am using this one:

    $v = preg_replace(‘/(.*?)(%0D%0A|%0A)(.*)/i’,'${1}%0D%0A${3}’,$v);

    Thanks and best regards.

  9. Evana Yasmin says:

    It is really helpful. Thank you so much.

  10. James says:

    After many days of banging my head against a wall with intermittent IPN INVALID responses, this has solved the problem…

    Thanks!

  11. tiz says:

    thanks very much, bravo!

  12. anonimo says:

    vayo pavo nene!!!! que corrida en tu cara!!

    Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!Gracias!!!

  13. Atanu Dey says:

    Life saver! Man I wasted a day… finally its ur blog saved me…

    thanks a lot.

  14. Eugene says:

    Just wanted to say thank you so very much! I was getting extremely frustrated, but this post saved the day!!

  15. Kyle Noland says:

    Excellent debugging. This was exactly my problem as well. Do you know if there is a way to detect the version of PHP PayPal is using in order to handle this issue dynamically?

  16. Michael Soriano says:

    Wow! that line of regex worked for me! although I’m still having random invalid responses. But this sure helps. Thank you.

  17. Many thanks for this, spent far too many hours trying to figure this out!

  18. Iain H says:

    Thanks for your post! Your post helps me solve my problem!!

    Thank you very much!!! :D

  19. Sergei says:

    Thanks!!!

  20. Danil says:

    helped, ty, wondering what kind of retards work in paypal if they cant even document this

  21. Paul says:

    Thanks, I’d figured out the address issue, your fix worked flawlessly once I found it. Thank you for posting it!

  22. Adam Phillips says:

    You sir have saved thy bacon!! My clients were getting quite frustrated at the fact that it wasn’t working and to be honest, so was I.

    This worked a treat, so thank you so much!

  23. Jepoy says:

    tnx a lot! finally it worked!

  24. Anita says:

    Thanks a lot! I was tearing my hair out over this. Great help.

  25. Luke says:

    Many thanks – this fixed a problem before it became a problem – I am sure I could have spent days on this!

  26. Peter says:

    I’m having sporadic INVALIDs returned – often the exact same information will pass as VERIFIED – which is frustrating…. so I’m hoping your regex is the solution. Even if not – thanks for taking the time to publish the information.

  27. Holy crap man, I literally cannot believe this is the solution. Thank you!!

Leave a Reply