in reply to Interfacing to Authorize.Net

THRAK - don't know if you came up with the code to interface with authorize.net yet - but here is some code that I worked out recently for version 3.0 of the authorize.net interface. Hope it helps...
#!/usr/bin/perl -wT ###################################################################### +################# # Copyright 2001. General Public License # # You can use, modify and redistribute this software under the terms o +f the GNU # General Public License as published by the Free Software Foundation. + And provided # that this header appear on all copies of the software. This program +is distributed # without warranty. # # References: http://www.perldoc.com/cpan/Net/SSLeay.html # http://remus.prakinf.tu-ilmenau.de/ssl-users/arc +hive29/0042.html # ###################################################################### +################# use strict; use CGI qw(:standard); use Text::CSV; use Net::SSLeay qw(post_https make_headers make_form); my $DEBUG = 1; ############################### Credit Card Data ############ +################### # only address number and zip code required for AVS my $first_name = 'Firstname'; my $last_name = 'Lastname'; my $address = 'Address'; my $city = 'City'; my $state = 'State'; my $zip = 'Zip'; my $email = 'Email'; my $cc_type = 'VISA'; my $cc_number = '4222222222222'; my $exp_date = '11/2002'; my $cc_amount = '1.00'; my $description = 'Description'; ############################### Authorize.Net Configuration # +############################## my $login = 'login'; # Authorize.Net login name my $test_mode; if ($DEBUG) { $test_mode = 'TRUE'; # TRUE=test } else { $test_mode = 'FALSE'; # FALSE=live } # for the most part these should remain the same my $adc_delim_data = 'TRUE'; # return results in machine read +able format my $adc_url = 'FALSE'; # no return url my $auth_version = '3.0'; # version of authorize.net gatew +ay code # version 3.0 defaults my $server = 'secure.authorize.net'; my $path = '/gateway/transact.dll'; my $port = '443'; # if other authorization types will be accepted, may need to make +it an argument # password is not needed for auth_capture type my $auth_type = 'AUTH_CAPTURE'; my $password = ''; ### check for required data for this auth type such as credit card, et +c. # setup ADC form fields my %submit_data = (x_Login => "$login", x_Password => "$password", x_Version => "$auth_version", x_ADC_Delim_Data => "$adc_delim_data", x_ADC_URL => "$adc_url", x_Test_Request => "$test_mode", x_Type => "$auth_type", x_Description => "$description", x_First_Name => "$first_name", x_Last_Name => "$last_name", x_Address => "$address", x_City => "$city", x_State => "$state", x_Zip => "$zip", x_Email => "$email", x_Card_Num => "$cc_number", x_Exp_Date => "$exp_date", x_Amount => "$cc_amount"); ############################### Process the Transaction ##### +########################## # make the form data my $post_data = &make_form(%submit_data); # Send data via SSL (encrypted channel) and wait for reply # $page is the content as defined by http (usually your HTML page) # $response is the first line sent back on hte SSL connection. # %headers is a hash of HTTP headers/response codes sent back my($page,$response,%headers) = &post_https($server,$port,$path,'',$p +ost_data); ############################### See What Was Returned ######## +####################### print header(), start_html(); # if $page is defined, success! if (defined $page) { if ($DEBUG) { print h3('Sent the following string:'), "https://${server}${path}?$post_data<BR><BR>\n"; # Display the reply headers. print h4('Headers received from v3.0 transact.dll:'); foreach my $key (keys %headers) { print "$key: $headers{$key}<BR>\n"; } print h4('Server response received from v3.0 transact.dll:'), "$response<br>\n"; #parse the page my $csv = new Text::CSV(); $csv->parse($page); my @col = $csv->fields(); print h4("see Developer's Guide - Appendix C - Response Codes"); print "Response Code (0): $col[0].<br>"; print "Response Subcode (1): $col[1]<br>\n"; print "Response Reason Code (2): $col[2]<br>\n"; print "Response Reason Text (3): $col[3]<br>\n"; print "Authorization Code (4): $col[4]<br>\n"; print "AVS Code (5): $col[5]<br>\n"; print "Trans ID (6): $col[6]<br>\n", p; my $elements = scalar(@col); my $fields = $elements - 7; print "<b>Total Number of Fields: $fields</b><br>"; my ($i, $item); for ($i=7; $i<$elements; $i++) { $item = $i - 6; print "$item. ($i) $col[$i]<BR>\n"; } } } else { # return empty list (or was it undef) if they fail to connection +(or any internal errors) print "Failed to open tcp connection, SSL connection negotiation f +ailed or there was an internal error." } print end_html;

Replies are listed 'Best First'.
Re: Re: Interfacing to Authorize.Net
by gmorris (Initiate) on Dec 29, 2001 at 00:43 UTC
    I am having a problem interfacing with Authorize.net as well. I am creating a perl module using Net::SSLeay in a similay way as listed in this thread and other sources I have found.

    The problem comes when I perform a post_https operation. Every time I attempt to post, I receive the error message "Invalid Merchant Login or Account Inactive". However, I turn right around and perform a get_https operation using the same information and achieve desired results (i.e. a delimited response string).

    Am I doing something wrong? Will using the get_https method produce the desired results? I can provide more info on what I am doing, if needed. Please someone help!!!

    Below is my module code:

    package Authnet; my($VERSION) = '1.00'; use strict; no strict 'refs'; use Net::SSLeay qw(get_https post_https sslcat make_headers make_form) +; #--------------------------------------------------------------------- +--------------------------- ## BEGIN SUBS HERE #--------------------------------------------------------------------- +--------------------------- sub new { my $self = bless({},shift); my $parm = shift; $self->{_prog_name} = $parm->{prog_name} or croak("No prog_name pa +rm"); $self->{_conf_path} = $parm->{conf_path} or croak("No conf_path pa +rm"); $self->initialize(); return($self); } #--------------------------------------------------------------------- +-------------- sub AUTOLOAD { # Handle the case where an undefined subroutine is called return(''); } #--------------------------------------------------------------------- +-------------- sub DESTROY { my($self) = shift; $self->finish(); } #--------------------------------------------------------------------- +-------------- sub finish { my($self) = shift; } #--------------------------------------------------------------------- +-------------- sub initialize { my($self) = shift; require $self->{_conf_path}; if (defined(%conf::data)) { $self->{_conf} = \%conf::data; } $self->{_error} = undef; $self->{_err_txt} = undef; } #--------------------------------------------------------------------- +-------------- #this sub processes the payment transaction via authorize.net #--------------------------------------------------------------------- +-------------- sub auth_trans{ my ($self) = shift; my ($ref_url) = shift; my ($sub_id) = shift; my ($f_name) = shift; my ($l_name) = shift; my ($addr) = shift; my ($city) = shift; my ($state) = shift; my ($zip) = shift; my ($card_no) = shift; my ($cvv2) = shift; my ($exp_date) = shift; my ($amt) = shift; my ($DEBUG) = 1; if (!$sub_id){ $self->{_error} = 97; $self->system_notify(); return(0); } if (!$f_name || !$l_name || !$card_no || !$exp_date || !$amt){ $self->{_error} = 33; $self->{_err_txt} = 'First name, Last Name, Card Number, Exp D +ate, or Amount'; return(0); } my (@auth_array) = (); my($host) = $self->{_conf}{'Authnet_host'}; my($script) = $self->{_conf}{'Authen_script'}; my ($url) = $self->{_conf}{'Authnet_URL'}; my($headers) = make_headers('Connection' => 'close', 'Referer' => $ref_url, 'Accept-Language' => 'en-us', 'User-Agent' => 'Mozilla/4.0 (compatib +le; AIT 1.1)'); my ($form) = make_form('x_Login' => $self->{_conf}{'x_Login'}, 'x_Password' => $self->{_conf}{'x_Password' +}, 'x_ADC_Delim_Character' => $self->{_conf}{' +x_ADC_Delim_Character'}, 'x_ADC_Delim_Data' => $self->{_conf}{'x_ADC +_Delim_Data'}, 'x_Encapsulate_Character' => $self->{_conf} +{'x_Encapsulte_Character'}, 'x_ADC_URL' => $self->{_conf}{'x_ADC_URL'}, 'x_Method' => $self->{_conf}{'x_Method'}, 'x_Type' => $self->{_conf}{'x_Type'}, 'x_Version' => $self->{_conf}{'x_Version'}, 'x_Description' => $self->{_conf}{'x_Descri +ption'}, 'x_Cust_id' => $sub_id, 'x_First_Name' => $f_name, 'x_Last_Name' => $l_name, 'x_Address' => $addr, 'x_City' => $city, 'x_State' => $state, 'x_Zip' => $zip, 'x_Card_Num' => $card_no, 'x_Card_Code' => $cvv2, 'x_Exp_Date' => $exp_date, 'x_Amount' => $amt); if ($DEBUG){ print "\nFORM> $form\n"; print "\nHEADERS> $headers\n"; } my($page1,$response,%reply_headers1) = post_https($host,'443',$scr +ipt,'',$form); print "\nPAGE1> $page1...$response\n"; if ($DEBUG){ my($key) = undef; foreach $key (sort keys %reply_headers1){ print "$key -> $reply_headers1{$key}\n"; } } my($x) = undef; ############attempt to contact transact.dll at least 5 times, exi +t loop if sucessfull, else generate error####### for($x=0;$x<5;$x++){ my ($page2,$result,%reply_headers2) = get_https($host,'443',"$ +script?$form"); print "\nPAGE2> $page2...$result\n"; if($result =~ m/OK/){ @auth_array = split(/,/,$page2); $self->{_error} = $auth_array[2]; last; }else{ $self->{_error} = 99; } if ($DEBUG){ $key = undef; foreach $key (sort keys %reply_headers2){ print "$key -> $reply_headers2{$key}\n"; } } } if($self->{_error} > 1){ #-- if error --# ###### if the error is a system error, then notify system +contact ###### if ($self->{_conf}{'x_response_reason_code'}{$self->{_erro +r}}{'System'} == 1){ $self->system_notify($sub_id); } ###### if omitted field error, then get field name for cus +tom message ###### if ($self->{_error} == 33){ $auth_array[3] =~ /^(.+)\scannot/; $self->{_err_txt} = $1; } return(0); }else{ ##### if success, return x_trans_id ##### return(1,$auth_array[6]); } } #--------------------------------------------------------------------- +------------- #this sub returns error text #--------------------------------------------------------------------- +-------------- sub error{ my($self) = shift; my($t) = $self->{_conf}{'x_response_reason_code'}{$self->{_error}} +{'Text'}; if ($self->{_err_txt}){ $t .= " $self->{_err_txt}"; } undef($self->{_error}); return($t); } #--------------------------------------------------------------------- +-------------- #this sub emails system contact in the event of a system error #--------------------------------------------------------------------- +-------------- sub system_notify{ my($self) = shift; my($sub_id) = shift; my($error) = $self->{_conf}{'x_response_reason_code'}{$self->{_err +or}}{'Text'}; if ($DEBUG){print "SUB_ID => $sub_id\nERROR => $error\n";} my($mailto) = 'ccgateway@cinergycom.com'; my($smail) = '/usr/lib/sendmail'; open (MAIL, "|$smail -f $mailto gmorris\@evansville.net") or die " +Error sending email:$!"; print MAIL <<EOM; From: Authorize.net Processing Gateway <$mailto> To: Authorize.net Processing Gateway <$mailto> Subject: A system error has occurred The following error has occurred while processing a credit card transaction for customer $sub_id: Error $self->{_error}: $error Calling Program: $self->{_prog_name} EOM close MAIL; $self->{_error} = 98; } 1;
      Hi All,

      Apologies for bringing up an old thread. I would like to use 'lil sisers' code snippet to process cc transactions on my site. But when reading the cc numbers into the program and storing them in a variable the number would go into the system memory, is this something I should be concerned about? or anyway to prevent it?

      Thanks a lot