Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
laziness, impatience, and hubris
 
PerlMonks  

Responding to Authorize.Net

by PopeFelix (Sexton)
on Mar 17, 2010 at 18:38 UTC ( #829241=perlquestion: print w/ replies, xml ) Need Help??
PopeFelix has asked for the wisdom of the Perl Monks concerning the following question:

SOLVED! I had forgotten to redirect STDIN and STDOUT to /dev/null

I have a client whose web store software will interface with Authorize.Net, but waits until it has built a job before responding. This often takes longer than Authorize.Net's timeout. So what I want to do is put a CGI script up to accept the POST response from Authorize.Net, immediately respond positively to Authorize.Net, then send the POST data from Authorize.Net on to the web store. I've got the CGI script together. Authorize.Net successfully communicates with the CGI script, and that CGI script is capable of passing the POST data on. However, no matter what I do, it seems, when I pass the POST data on, i'm waiting on the web store to respond before Authorize.Net gets my response.

Code:

#!/usr/bin/perl # A script to accept a POST response from Authorize.Net and pass the d +ata to Xerox use strict; use CGI; use Digest::MD5 qw/md5_hex/; use LWP; use Config::YAML; use CGI::Carp qw(fatalsToBrowser); use POSIX qw/strftime/; use Time::HiRes qw/time/; use Data::Dumper; my $configFile = 'adn_helper.yaml'; for (0..$#ARGV) { $configFile = $ARGV[$_ + 1] and last if /--config/; } my $config = Config::YAML->new( config => $configFile, timeout => 60, db_host => q/localhost/, db_name => q/adn_passthrough_log/, ); for (qw/salt loginID postURL/) { die "Missing required key \"$_\" in configuration file" unless $co +nfig->{$_}; } my $query = new CGI; $query->import_names('Q'); my $t0 = time; my $old_handle = select(STDOUT); local $| = 1; select($old_handle); syswrite STDOUT, "Content-type: text/html\n\n"; my $md5 = md5_hex($config->{salt}, $config->{loginID}, $Q::x_trans_id, + $Q::x_amount); if (lc($md5) eq lc($Q::x_MD5_Hash)) { my $browser = getBrowser($Q::sid); my %hash; $hash{$_} = $query->param($_) for $query->param; logline(" params: ", Dumper [$query->param]); logline(" ", Dumper \%hash); syswrite STDOUT, "Your card was successfully charged for \$$Q::x_a +mount<br>\n" if $Q::x_response_reason_code == 1; syswrite STDOUT, "Your card could not be charged. $Q::x_response_ +reason_text<br>\n" if $Q::x_response_reason_code == 2; syswrite STDOUT, "There was an error processing your card: $Q::x_r +esponse_reason_text<br>\n" if $Q::x_response_reason_code > 2; my $pid = fork; my $elapsed = time - $t0; if ($pid) { # parent logline(" time elapsed in responding to Authorize.Net and fork +ing: $elapsed"); logline(" forked, child pid $pid"); close STDOUT; exit; } open STDIN, "</dev/null"; open STDOUT, ">/dev/null"; $t0 = time; my $response = $browser->post($config->{postURL}, [%hash]); $elapsed = time - $t0; logline(" time elapsed in waiting for response from Xerox: $elapse +d"); logline(" query: ", Dumper $query); logline(" response: ", Dumper $response); } else { logger($query, undef); print "This transaction could not be processed due to a checksum m +ismatch.<br>"; # possible spoofing attack } sub logger { use DBI; my ($query, $response, $elapsed) = @_; my $dbh = DBI->connect("DBI:mysql:$config->{db_name};host=$config- +>{db_host}", $config->{db_username}, $config->{db_password}, { RaiseE +rror => 1 }) || die "Could not connect to logging database: $DBI::errstr"; open my $out, ">>/tmp/passthrough.log"; print $out strftime("%Y-%m-%d %T\n", localtime(time)); print $out "Elapsed: $elapsed\n"; print $out "Query\n"; print $out Dumper $query, "\n"; print $out "Response\n"; print $out Dumper $response, "\n"; close $out; } sub logline { open my $out, ">>/tmp/passthrough.log"; print $out strftime("%Y-%m-%d %T", localtime(time)), @_, "\n"; } sub getBrowser { my $sid = shift; open my $out, ">>/tmp/passthrough.log"; print $out strftime("%Y-%m-%d %T: ", localtime(time)), "Creating L +WP::UserAgent with Session ID $sid, user-agent $ENV{HTTP_USER_AGENT}\ +n"; close $out; my $browser = LWP::UserAgent->new(); $browser->timeout($config->{timeout}); $browser->cookie_jar({}); $browser->agent($ENV{HTTP_USER_AGENT}); # $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port +, $path_spec, $secure, $maxage, $discard, \%rest ) $browser->cookie_jar->set_cookie(1, 'SessionID', "SessionID=$sid", + '/'); return $browser; }

Comment on Responding to Authorize.Net
Download Code
Re: Responding to Authorize.Net
by Khen1950fx (Canon) on Mar 17, 2010 at 21:16 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829241]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-04-19 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls