×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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!

*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

CGI - Hangs Script
3

CGI - Hangs Script

CGI - Hangs Script

(OP)
I have a script, which I changed from a self baked STDIN routine and chenged it to use the CGI.pm via my $cgi = new CGI; , however now when data is posted to the script it hangs.

What is wrong , it worked fine before I decided to make the change to CGI.

thanks

1DMF.

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

RE: CGI - Hangs Script

Couldn't say without seeing the script.

 - George

RE: CGI - Hangs Script

(OP)
#!/usr/bin/perl -w

# PayPal IPN (Instant Payment Notification) CGI

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:all unescape);

my $cgi = new CGI;

# These modules are required to make the secure HTTP request to PayPal.

use LWP::UserAgent;
use Crypt::SSLeay;

# Set path to user modules
use lib qw(/mypath/Modules);

######################
# Use globals module #
######################
use ssplglobal;

##################
# Use SQL module #
##################
use sql;

#################
# Get POST Data #
#################
my $query;

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= "&cmd=_notify-validate";

# Post Back to PayPal to Authenticate Request

my $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";

my $user_agent = new LWP::UserAgent;

my $request = new HTTP::Request("POST", $paypal_url);

$request->content_type("application/x-www-form-urlencoded");

$request->content($query);

# Make the request
my $result = $user_agent->request($request);

# HTTPS error
if (!$result->is_success) {
  &send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","paypal communication error","paypal error");
  die "Error communicating with PayPal server.";
  exit();
}

if($result->content eq 'INVALID'){
    &send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","Invalid transaction","paypal test");
    die "Invalid Transaction.";
    exit();
}

if($result->content eq 'VERIFIED') {
    # check the $payment_status=Completed
    # check that $txn_id has not been previously processed
    # check that $receiver_email is your Primary PayPal email
    # check that $payment_amount/$payment_currency are correct
    # process payment

    my $tx = $cgi->param('txn_id');

    &send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","Transaction OK","TX = $tx");
    die "Transaction Valid.";
    exit();
}



die "Script Error!";

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

RE: CGI - Hangs Script

(OP)
I've found if i use this code it works fine

CODE

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
    # split posted variables into pairs
    my @pairs = split(/&/, $query);

    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;
    }

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

RE: CGI - Hangs Script

Quote:


I have a script, which I changed from a self baked STDIN routine and chenged it to use the CGI.pm via my $cgi = new CGI;

It appears all you are doing with the CGI module is fetching this one form field:

my $tx = $cgi->param('txn_id');

Why do you have all those 'die' functions in the script? Is that just for debugging?

RE: CGI - Hangs Script

(OP)
The script isn't finished, i'm testing, but it was just hanging - which is my point, I'm hardly doing anything with CGI at the moment and it just hangs.

Yes the die statements are for debugging

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

RE: CGI - Hangs Script

just by looking at the code you have posted I don't know why it's hanging.

RE: CGI - Hangs Script

(OP)
me neither, I've had this problem using CGI on another script that just gets a simple form with a few hidden inputs and a couple of select lists, when submitted the script hangs - and I got a warning from my host for resource abuse.

Is there a know issue with a certain version of the CGI.pm module, or have I found one?

Cheers,
1DMF

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

RE: CGI - Hangs Script

(OP)
Ok I've found the conflict, can some explain why it is happening.

CODE

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= "&cmd=_notify-validate";

my $cgi = new CGI;

basicaly I've narrowed it down to if I use the PayPal code with the line to read STDIN and also try to use CGI, it hangs.

It does the same if I create the cgi object first and then also try to read STDIN.

So I am assuming that you can only read STDIN once!

once STDIN is read does it become empty? is that the problem? or does it leave it in some sort of 'open' state causing some kind of 'file in use' buffer error?

Any help understanding the mechanism of reading STDIN and why you can't read it twice is much appreciated.

Regards,
1DMF

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

RE: CGI - Hangs Script

The first read of STDIN consumes all the bytes of "content_length"

A subsequent read of stdin has nothing in the stream.  All the bytes have been previously consumed. so it is waiting for additional input to arrive.  There is none. It is used up by the first read.

STDIN is normally the keyboard.  Picture reading bytes from the keyboard.  Once a program has captures a keystroke, the keystroke is no longer in the stream.

Same with a cgi script.  STDIN is redirected to read the posted data. STDOUT is redirected back to the browser instead of the console.

RE: CGI - Hangs Script

(OP)
Thanks, that's what I love about TT, learn something new everyday!

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

RE: CGI - Hangs Script

To change that code to use CGI all the way down:

First, alter your use CGI line to use the oldstyle URLs. I don't know if PayPal will accept the newer style, but I'm suggesting this to keep the script behaving in the same way as it has been:

CODE

use CGI qw(:all unescape -oldstyle_urls);

Then, instead of using read(), do the following:

CODE

my $cgi = new CGI;
$cgi->param( cmd => 'notify_validate' );
my $query = $cgi->query_string;
Even when using POST data, the query_string() method will return the query string you're looking for.

HTH.

RE: CGI - Hangs Script

(OP)
wow - once again Ishnid, your a gent, you've answered my next question without me even asking it.

Thanks,
1DMF

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

RE: CGI - Hangs Script

I guessed that a natural progression from "why doesn't my code work?" would be "how can I fix it?" winky smile

RE: CGI - Hangs Script

(OP)
We I'd already fixed it, but using the self baked PayPal way, but now I can use the CGI method, which was my original intention.

What confused me was I thought STDIN was where the post/mime data was stored and could be read again and again, like a holding variable, but obviously it doesn't work like that.

It must be a store for the data, as you don't have to read STDIN at the top of a script before doing anything else, but it doesn't like an attempted read on it if it's empty, why is that?

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

RE: CGI - Hangs Script

With "normal" filehandles (i.e. ones that relate to opened files), if you read from them, you have to use the seek() function to get back to the start before you read them again. STDIN is consumed by each read, so it can't be read twice.

RE: CGI - Hangs Script

(OP)
I see - thanks for that Ishnid.

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

RE: CGI - Hangs Script

(OP)
doesn't seem to work ishnid? i've had to drop using CGI as I cannot get it to work.

using CGI

CODE

my $cgi = new CGI;

$cgi->param( cmd => 'notify_validate' );

my $query = $cgi->query_string;

my $user_agent = new LWP::UserAgent;

my $request = new HTTP::Request("POST", $paypal_url);

$request->content_type("application/x-www-form-urlencoded");

$request->content($query);

# Make the request
my $result = $user_agent->request($request);

die "result=" . $result->content;

I get

Quote:

result=<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--
       Script info: script: webscr, cmd: _home, template: p/wel/sandbox-outside, date: Aug. 4, 2006 08:53:42 PDT; country: US, language: en_US, xslt server:
       web version: 41.0-236808 branch: live-410_int
       content version: 41.0-229712
       pexml version: 41.0-236729
       page XSL: /default/en_US/homepage/SandBox-outside.xsl
-->
<title>Welcome - PayPal</title>
<meta http-equiv="keywords" content="">
<meta http-equiv="description" content="">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/xpt.css">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/xptInvoice.css">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/xptObsolete.css">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/xptdev.css">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/sandbox.css">
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/default.css">
<!--[if IE 6]><link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/ie60win.css"><![endif]-->

<!--[if IE 7]><link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/ie70win.css"><![endif]-->
<link rel="stylesheet" type="text/css" href="https://www.sandbox.paypal.com/css/en_US/lang.css">
<style type="text/css"></style>
<link rel="shortcut icon" href="https://www.sandbox.paypal.com/en_US/i/icon/pp_favicon_x.ico">
<script type="text/javascript" src="https://www.sandbox.paypal.com/js/pp_main.js"></script>
</head>
<body id="xptSandbox">
<div id="xptContentMain"><table align="center" border="0" cellpadding="0" cellspacing="0" width="760">
<tr><td></td></tr>
<tr><td valign="top"><div>
<img src="https://www.sandbox.paypal.com/en_US/i/logo/logo_paypalSandbox_w_100x50.gif" border="0" alt=""><br><br><div class="textCenter" id="sandbox_redirect">
<hr>
<br><br><span class="emphasis">To access the PayPal Sandbox, please log in to <a href="https://developer.paypal.com/">PayPal Developer Central</a>.</span><br><br><br><hr>
</div>
</div></td></tr>
</table></div>
<script type="text/javascript" src="https://www.sandbox.paypal.com/js/pp_naturalsearch.js"></script><script type="text/javascript">
<!--
var ppns = new PayPalNaturalSearch('https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=p/wel/sandbox-outside','3484-30830-12422-0',this.document);
            ppns.addEngines(new Array(
                                        "A9.com",
                                        ".altavista.com",
                                        "clusty.com",
                                        "google.co.jp",
                                        "google.co.kr",
                                        "google.ru",
                                        "www.google.com",
                                        "icerocket.com",
                                        "infospace.com",
                                        "mooter.com",
                                        "search.msn.",
                                        "snap.com",
                                        "s[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: result=
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:   [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:  [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: --------------------------------------------------------------------------------[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: To access the PayPal Sandbox, please log in to PayPal Developer Central.--------------------------------------------------------------------------------[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:  [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:  
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
earch.yahoo.com",
                                        "search.yahoo.co.jp"
                                    ,"www.overture.com/d/search/p/altavista/",
                                    "aolsearch.aol.com",
                                    "search.aol.com",
                                    "web.ask.com",
                                    "pictures.ask.com",
                                    "images.google.com",
                                    "groups.google.com",
                                    "www.google.com/search",
                                    "www.hotbot.com",
                                    "search.netscape.com",
                                    "s.teoma.com/",
                                    "www.wisenut.com"
                                )); // End of aEngines array.
            ppns.init();
            -->
</script>
</body>
</html>

For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

i've removed the oldstlye urls switch and still no joy, not sure what i'm sending or receiving using the CGI method but I cannot get it to work, oh well nice thought, but it's a lot easier just to use

CODE

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

# split posted variables into pairs
    my @pairs = split(/&/, $query);

    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;
        $data{$name} = $value;
    }

$query .= "&cmd=_notify-validate";

the die "result=" . $result->content; using this method gives me

Quote:

result=VERIFIED

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

RE: CGI - Hangs Script

(OP)
that'll learn me, copying and pasting from TT supplied code - lol

it should be

CODE

$cgi->param( cmd => '_notify-validate' );
but it still doesn't work.

OK i'm getting a response from PayPal now, but every transaction is responding as 'Invalid', including valid test ones that work with the other code.

so what ever is being posted to paypal's server is not what is received, I am guessing CGI is altering something in the query string that paypal doesn't like or can't cope with and so is denying the transaction.

any ideas what this is and how i might fix it?

note: I had to put the oldstlye_urls switch back in!

thanks 1DMF

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

RE: CGI - Hangs Script

(OP)
Forget my dumb ass! - my bad, i was not running the test in TEST MODE on PayPal's server - d'oh.


The test form they provide has a toggle on it 'live -> test' - everything working fine!

have a star Chris!

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

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