Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with Perl CGI script

Posted on 2008-10-09
4
Medium Priority
?
438 Views
Last Modified: 2013-12-25
Greetings,

I am working on a Paypal ipn script. Code attached.

Using strict, I get ta syntax error. What am I overlooking?
Is there an easier way?

Any help or input greatly appreciated.
#!/usr/bin/perl
use strict;
 
 
        # read post from PayPal system and add 'cmd'
        read (STDIN, my $query, $ENV{'CONTENT_LENGTH'});
        $query .= '&cmd=_notify-validate';
 
        # post back to PayPal system to validate
        use LWP::UserAgent;
        my $ua = new LWP::UserAgent;
        my $req = new HTTP::Request 'POST','https://www.paypal.com/cgi-bin/webscr';
        $req->content_type('application/x-www-variable-urlencoded');
        $req->content($query);
        my $res = $ua->request($req);
 
        # split posted variables into pairs
       my @pairs = split(/&/, $query);
       my $count = 0;
        foreach my $pair (@pairs) {
       my ($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
        my $payment_status = $variable{'payment_status'};
        my $txn_id = $variable{'txn_id'};
        my $custom = $variable{'custom'};
        my $option_name0= $variable{'option_name0'};
        my $option_name1= $variable{'option_name1'};
        my $payment_date = $variable{'payment_date'};
        my $receiver_email = $variable{'receiver_email'};
        my $payment_gross = $variable{'payment_gross'};
        my $payment_fee = $variable{'payment_fee'};
        my $payment_type = $variable{'payment_type'};
        my $first_name = $variable{'first_name'};
        my $last_name = $variable{'last_name'};
        my $item_number = $variable{'item_number'};
        my $item_name = $variable{'item_name'};
        my $payer_email = $variable{'payer_email'};
        my $payer_status = $variable{'payer_status'};
 
 
 
        if ($res->is_error) {&http_error; }
        elsif ($res->content eq 'VERIFIED') {
        }}
        elsif ($res->content eq 'INVALID') {
        open(LOG,">>$paylog");
         }
 
        else { }

Open in new window

0
Comment
Question by:capper5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 200 total points
ID: 22678985
Your line 49 has two } when it should have only 1.

You don't define %variable before it's used.
    add this line before line 20:
    my %variable;

You don't define or set $paylog before it's used.
    add this line before line 51
    my $paylog = '/the/path/to/the/log.file';


You are processing the input data manually, you should be using the CGI module:
http://search.cpan.org/author/LDS/CGI.pm-3.42/CGI.pm
0
 

Author Comment

by:capper5
ID: 22679085
What code do I use to make the CGI module parse the post and assign the values to local?
I have never worked with this module?

Thanks
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22679802
0
 
LVL 48

Accepted Solution

by:
Tintin earned 300 total points
ID: 22682401
Putting it all together, it would look something like:
#!/usr/bin/perl
use strict;
use CGI::Carp 'fatalsToBrowser';
use CGI;
use LWP::UserAgent;
 
read (STDIN, my $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';
 
my $q = new CGI;
my %variable = $q->Vars;
 
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request 'POST','https://www.paypal.com/cgi-bin/webscr';
$req->content_type('application/x-www-variable-urlencoded');
$req->content($query);
my $res = $ua->request($req);
 
my $paylog='somelog';
 
my $payment_status = $variable{'payment_status'};
my $txn_id = $variable{'txn_id'};
my $custom = $variable{'custom'};
my $option_name0= $variable{'option_name0'};
my $option_name1= $variable{'option_name1'};
my $payment_date = $variable{'payment_date'};
my $receiver_email = $variable{'receiver_email'};
my $payment_gross = $variable{'payment_gross'};
my $payment_fee = $variable{'payment_fee'};
my $payment_type = $variable{'payment_type'};
my $first_name = $variable{'first_name'};
my $last_name = $variable{'last_name'};
my $item_number = $variable{'item_number'};
my $item_name = $variable{'item_name'};
my $payer_email = $variable{'payer_email'};
my $payer_status = $variable{'payer_status'};
 
 
 
http_error() if ($res->is_error);
 
if ($res->content eq 'VERIFIED') {
   # Do something
}
elsif ($res->content eq 'INVALID') {
   open(LOG,">>$paylog") or die "Can not open $paylog $!\n";
 }
else {
   # do something
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question