Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding problem? 1

Status
Not open for further replies.

audiopro

Programmer
Joined
Apr 1, 2004
Messages
3,165
Location
GB
I am having a problem getting a check string to validate.
I am interfacing with PayPal's Instant Payment Notification (IPN) system where the IPN sends a string to me, I append a var to the end and send it back. The original and the new string are compared and the only difference should be the appended bit at the end.
The transaction is being rejected as the two strings are different in some way.
Could the problem lie with the type of encoding?
Is there a better test I could carry out to determine the problem?


Code:
The following code receives and returns the string to IPN. The in and out strings are written to a log file here and then compared later in the script and they are identical apart from the appended cmd string.

#Receive form PP

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});


# Log in string

open (LOG, ">quer.txt") || &errormess;
print LOG "$query\n";
close (LOG);

# Append cmd item
$query .= '&cmd=_notify-validate';

#write out string to log
open (LOG, ">>quer.txt") || &errormess;
print LOG "$query\n";
close (LOG);


# post back to PayPal system to validate
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','[URL unfurl="true"]http://www.paypal.com/cgi-bin/webscr';[/URL]
$req->content_type('application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
$req->content($query);
$res = $ua->request($req);



Keith
 
Are you supposed to have the space in front of &cmd?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Hi Steve
The space before the single speech mark wouldn't affect the string. I tried it any way but still the same result.
The two log entries are identical but the validation says they are different.

Keith
 
Can you use binmode on STDIN to prevent any encoding issues?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
What does $query contain when they send it to you?
 
Thanks for your input guys.

I am sure this is one of those things which will have a simple answer. I had a similar problem with a bank interface a few years but that turned out to be an added line feed on the appended text, no such luck in this case.

I tried to paste the contents of quer.txt into here but on preview, a lot of it disappears off the end of the page.
The file contains text but chars '&' '@' etc have been subbed for %?? chars. Both files contain the same substitutions so are still the same. Could they be changed when I receive them so I see them as the same but they don't.

As for BINMODE - this script was supplied by PayPal themselves so would expect it to work although I may be putting too much faith in the ability of 'experts'.

The blurb does say it should be URL encoded - are there different types of that?

Keith
 
I think there is..

it looks like you are sending this to the production url. Should you be sending it to a sandbox (developer) url for testing?
 
Here is what I saw posted on paypal's site.. and from what I can tell your code looks identical

print "Content-Type: text/html\n\n";
# read post from PayPal system and add 'cmd'
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';

# post back to PayPal system to validate
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','$req->content_type('application/x-$req->content($query);
$res = $ua->request($req);

# split posted variables into pairs
@pairs = split(/&/, $query);
$count = 0;
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$variable{$name} = $value;
$count++;
}
 
If it's a URL, then the extra space in front of the &cmd will definitely break it. But you say that you've tried that.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
travs69 - You are spot on!!!!
I was foolishly sending it to the production URL although when I changed it, it still didn't work. I put it back in its cage and when I tried it later - it worked!!!! I didn't change anything else but at least it is working now.
Have a purple thing for your wisdom.

Steve - The space is before the speech mark so has no effect on the string itself.

Keith
 
Thanks.. glad I could help.

Stevexff, I'm not sure where you are seeing he is adding a space before the &cmd. All I see when I look at his code is a single tick then a ampersand. No space between the two of them.
 
Now I look at it again, neither am I. Must be senility...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top