×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PayPal IPN return POST problem

PayPal IPN return POST problem

PayPal IPN return POST problem

(OP)
I am trying to get paypal IPN script to work.
PayPal sends me a a list of variables an a normal POST.
I then send that back to PayPal.
All is good so far.

If everything is OK, PayPal than sends me back a POST that contains nothing at all except VERIFIED.

This is a page that is text.html and the content and source is VERIFIED, nothing else.

The post URL requested contains no information.

How do I obtain this content?

RE: PayPal IPN return POST problem

Are you using Paypal's validation script or one of your own?

CODE

#!/usr/bin/perl

# 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','https://www.paypal.com/cgi-bin/webscr';
# note: if you have SSL encryption Enabled, use <https://www.paypal.com/cgi-bin/webscr> above
$req->content_type('application/x-www-form-urlencoded');
$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++;
}

# assign posted variables to local variables
# note: additional IPN variables also available -- see IPN documentation
$item_name = $variable{'item_name'};
$receiver_email = $variable{'receiver_email'};
$item_number = $variable{'item_number'};
$invoice = $variable{'invoice'};
$a = $variable{'custom'};
$payment_status = $variable{'payment_status'};
$payment_gross = $variable{'payment_gross'};
$txn_id = $variable{'txn_id'};
$payer_email = $variable{'payer_email'};
$amount = $variable{'amount'};
$on0 = $variable{'option_name1'};
$os0 = $variable{'option_selection1'};
$status = $res->content;
$note = $variable{'memo'};


if ($res->is_error) {
# HTTP error
}
elsif ($res->content eq 'VERIFIED') {
# check the payment_status=Completed
# check that txn_id has not been previously processed
# check that receiver_email is an email address in your PayPal account
# process payment
# print to screen the following if the IPN POST was VERIFIED


print "content-type: text/plain\n\nOK\n";
print "<html><head><title>IPN Screendump</title></head>\n";
print "<body>your email address is <b>$payer_email</b>\n";
print "<br>you paid <b>$payment_gross</b>\n";
print "<br>you paid for <b>$item_number</b>\n";
print "<br>the color of your order was <b>$os0</b>\n";
print "<br>the value of custom was <b>$a</b>\n";
print "<br>the status was<b>$status</b>\n";
print "<br>the note said <b>$note</b>\n";
print "<br>the transaction id was <b>$txn_id</b>\n";
print "<br>the payment status was<b>$payment_status</b>\n";
print "</body></html>\n";


}
elsif ($res->content eq 'INVALID') {
# log for manual investigation
# print to screen the following if the IPN POST was INVALID

print "content-type: text/plain\n\nOK\n";

print "<html><head><title>IPN Screendump</title></head>\n";
print "<br>the status was<b>$status</b>\n";
print "</body></html>\n";


}
else {
# error
}

Keith
www.studiosoft.co.uk

RE: PayPal IPN return POST problem

(OP)
I use mod_perl. Is there a way to get a POSTs content without a list of params?
I  haven't used plain CGI for a long time.
Seems like there must be a mod_perl way, but I can't find anything searching for it.
Other than this one hurdle, everything is working fine.

RE: PayPal IPN return POST problem

Not familiar with mod_perl so I really don't know what to suggest.
To be honest, I don't even know the difference between mod-perl and the perl I use.
I don't know why they insist on using the $ENV string rather than the CGI module but I am sure the decisions are made by minds far superior to mine.
I have used that script, well a far more embelished version, quite a lot and found it to be very reliable.  

Keith
www.studiosoft.co.uk

RE: PayPal IPN return POST problem

(OP)
I have a bunch of mod_perl books, I will look there

RE: PayPal IPN return POST problem

(OP)
I wanted to reply back to make known how I solved this IPN situation.

I had no trouble at all with the mod_perl. Everything went as it should, but PayPal states that the params must be returned in the same order.
That isn't true.
They also say that &cmd=_notify-validate must be added to the end.
That isn't exactly true. Everything works ok at either end or even both ends.

The real problem I had is that PayPal must receive back a 200 reply from my server before the transaction is complete.
There is no mention of this need in any of the documentation.


I admit that the example script shows a page returned to PayPal, but the example page does not look like something necessary. IMHO, it looks like something that would be sent to me during my own local testing!

After days of failure with my script (which always worked fine on my end to get verified response), I saw someone mention that PayPal needed to get a 200 for the process to complete.
I added sending back a blank page header. Voila! All is fine now.

I hope search engines catch this. Even better, that PayPal would add a comment on their sample scripts!
I looked at many forums which lacked this simple answer.

RE: PayPal IPN return POST problem

Is that right, Mod_Perl you have to handroll your own handling of the querystring?

eg.

CODE

getFormData(\%frmFlds);

sub getFormData {
    my($hashRef) = shift;
    my($buffer) = "";

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        $buffer = $ENV{'QUERY_STRING'};
    }
    else {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    }

    foreach (split(/&/, $buffer)) {
        my($key, $value) = split(/=/, $_);
        $key   = decodeURL($key);
        $value = decodeURL($value);
        %{$hashRef}->{$key} = $value;
    }
}

sub decodeURL {
    $_ = shift;
    tr/+/ /;
    s/%(..)/pack('c', hex($1))/eg;
    return($_);
}
 
I used to use this type of processing, but when I joined TT many moons ago, I was told to cease and desist immediately, it is very dangerous and bad practice and I should start using CGI for handling POST/GET data, and have done for many years now.

Why wouldn't you use the same logic when dealing with MOD_PERL ?

 

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta

RE: PayPal IPN return POST problem

(OP)
Yes, you have to roll your own but Apache::Request does the same type of work that CGI.pm does but much faster and adds many options not available in CGI.pm.

Not hard at all.

You can run regular CGI with mod_perl, but after you learn how use mod_perl, you never want to again.
Mod_perl basically requires "perfect" programming styles, but that is always a good thing!
Like comments. I used to skip that but then I had to do maintenance and changes in my old code. Yuck. Now I comment everything wierd in my code.

RE: PayPal IPN return POST problem

Ok, thanks for the info.

I assume though Apache::Request doesn't decode STDIN $ENV{} like CGI does, otherwise you wouldn't need to roll your own.

Maybe one day I'll get into MOD_PERL only thing is it only works on *nix, and my day job is Windows / IIS with ActiveState perl, so no path for gaining MOD_PERL experience or training sad

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close