Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal IPN error with embedded newline in address

Tags:

php

paypal-ipn

So, this is a new one on me. My Paypal IPN has been working for sometime, and started getting an error today.

During the postback to verify with PayPal (adding cmd=_notify-validate), the PayPal responder says "no that wasn't from me". The only thing bizarre about this particular entry is (I believe) the way the user specified their address:

123 Address Street
#789

Everything else seems normal and the IPN-handler is handling other notifications quite happily.

Anyone seen anything like this?

like image 632
Shadow Radiance Avatar asked Nov 22 '10 18:11

Shadow Radiance


2 Answers

Okay, so I've found my error, and yes, it is related to the newline in the address.

Basically I was doing this:

foreach ($post_array as $name => $value) {
  $value = urlencode($value);
  $post_string .= $name . '=' . $value . '&';
}
$post_string .= "cmd=_notify-validate";

When I needed to also convert the /n to a /r/n, like so:

foreach ($post_array as $name => $value) {
  $value = urlencode(str_replace("\n", "\r\n", $value));
  $post_string .= $name . '=' . $value . '&';
}
$post_string .= "cmd=_notify-validate";

Now PayPal is happy.

Le sigh.

like image 58
Shadow Radiance Avatar answered Oct 31 '22 15:10

Shadow Radiance


Update 2018, since I just hit my head against this bug AGAIN after updating a site from CI v2 to CI v3...

Starting in CodeIgniter v3, newline standardization is now off by default (was previously ON by default). And as of CI v3.1.3, standardize_newlines is considered deprecated, so time to wean off it if you haven't already.

The result of this was that the first time my IPN script had to process an address with a line break in it, Paypal's response was that it was invalid.

Took a while to figure out, since I didn't realize that the newline standardization default was different. But the solution is to simplify the code that's concatenating that response string to send back to Paypal, by just taking out the (str_replace("\n", "\r\n", $value) part and replacing it with just $value (still doing the urlencode though).

So it's back to how it should have worked in the first place, essentially... and now PayPal is happy again.

like image 24
Dr Marble Avatar answered Oct 31 '22 15:10

Dr Marble