#!/usr/local/bin/perl ########################################################### # Please make sure that your using the correct perl # # location (SHEBANG) for this script to execute # # properly. Check with your web host. # ########################################################### ########################################################### # # # D O C U M E N T A T I O N # # # # This CGI/PERL code has been successfully tested on # # third-party web servers and performed according to # # documented Advanced Integration Method (AIM) # # standards. # # # # Last updated September 2004. # # # # For complete and freely available documentation, # # please visit the Authorize.Net web site at: # # # # http://www.authorizenet.com/support/guides.php # # # ########################################################### ########################################################### # # # When you upload (FTP) this CGI/PERL script to your web # # server, please make sure that the CHMOD has been set # # 755. # # # # If you are using a command-line tool to assign # # permissions to this file, please make sure it has read # # and execute privileges for everyone -- otherwise, the # # code cannot be excuted. # # # ########################################################### ########################################################### # # # Make sure you have the LWP module installed and verify # # which version you are using, since you need to # # reference that version number in your code. # # # ########################################################### use LWP 5.69; use HTTP::Request::Common qw( POST ); ########################################################### # # # For an easy way to find out which PERL modules are # # available on your web server, consider the freely # # available ENV.PL script: # # # # ####################################### # # ## Server Info CGI v1.08 © 2000 ## # # ## http://www.widexl.com ## # # ## Made by Henk Boonstra ## # # ####################################### # # # ########################################################### ############################################### # ENTER YOUR LOGIN ID AND PASSWORD HERE # ############################################### my $auth_net_login_id = "ENTER LOGIN ID HERE"; my $auth_net_tran_key = "ENTER TRAN KEY HERE"; my $request_values = { x_test_request => "TRUE", x_login => $auth_net_login_id, x_tran_key => $auth_net_tran_key, ########################################################### # # # Please, pay special attention to the version number. # # Unless you have a specific reason to make a change, # # leave it at 3.1. # # # # Ideally, you would already have set it to 3.1 in the # # Merchant Interface to make sure all of your trans- # # actions will by default follow the 3.1 protocol # # # ########################################################### x_version => "3.1", ########################################################### # # # Make absolutely sure that you use a delimiter that # # will not show up in any legitimate name, billing # # address, product descriptions, etc. # # # # For this reason, we prefer using the pipe character: # # # # | # # # # # # If necessary, make sure you search for and replace # # the delimiter character before you accept and process # # user input -- so that parsing and evaluating the # # returned string from Authorize.Net will not be # # negatively affected. # # # ########################################################### x_delim_char => "|", x_delim_data => "TRUE", x_adc_url => "FALSE", x_type => "AUTH_CAPTURE", x_card_num => "4242424242424242", x_exp_date => "1209", x_description => "Recycled Toner Cartridges", x_amount => "12.23", x_first_name => "Charles D.", x_last_name => "Gaulle", x_address => "342 N. Main Street #150", x_city => "Ft. Worth", x_state => "TX", x_zip => "12345", x_country => "USA", x_company => "ACME USA", x_cust_id => "dpb-11937-84065", x_invoice_num => "0722031200-PM", x_phone => "1-888-555-1234", x_fax => "1-888-555-2345", ########################################################### # # # NOTE: The @ symbol in the e-mail address must be # # encoded or else you will have to escape it when you # # parse the return string in CGI/Perl # # # # Instead of @ use @ # # # # You don't have to use extensive encoding as shown in # # this example. # # # ########################################################### x_email => "customer@customer.com", x_email_customer => "FALSE", x_ship_to_first_name => "Donovan", x_ship_to_last_name => "Leitch", x_ship_to_company => "ACME Explosives & Anvils", x_ship_to_address => "4567 Wilshire Blvd.", x_ship_to_city => "Los Angeles", x_ship_to_state => "California", x_ship_to_zip => "98765-0987", x_ship_to_country => "USA", x_po_num => "1837350987", x_tax => "6.25", x_tax_exempt => "TRUE", x_freight => "11.25", x_duty => "0.00", ########################################################### # # # M E R C H A N T D E F I N E D V A L U E S # # # # You can define any number of special, custom fields # # to have them accompany a transaction. # # # ########################################################### CustomerBirthMonth => "12", CustomerBirthDay => "1", CustomerBirthYear => "1959", }; ########################################################### # # # For the purpose of this demonstration, we have hard- # # coded a number of values that will get posted to the # # Authorize.Net payment processor. # # # # Obviously, in a real-world e-commerce situation, you # # would have securely collected most of the data through # # a shopping cart or by some other means. # # # # Moreover, you could have obviously supplied many more # # values (see the AIM API Guide for specific # # information), but it varies from merchant to merchant, # # dependent on their business models, accounting needs, # # etc. as to which information gets collected. # # # ########################################################### ########################################################### # # # The following four lines demonstrate how to post the # # transaction data securely to Authorize.Net. # # You may of course adjust this code to your liking. # # We simply wanted to provide a working approach to # # those who don't want to spend a lot of time learning # # CGI/PERL (or don't have the resources to do custom CGI # # programming. # # # #---------------------------------------------------------# # NOTE: # #---------------------------------------------------------# # For testing purposes, we are submitting all # # transactions to the certification server. If you have # # a valid merchant account with Authorize.Net, and if # # you are done testing, you must post to the actual # # transaction server, as outlined in your merchant # # information that you should have received when you # # opened the Authorize.Net account. # # # #---------------------------------------------------------# # For assistance with your Authorize.Net merchant # # account, please contact Authorize.Net Customer Support # # at: # # 1-877-447-3938 # #---------------------------------------------------------# # # ########################################################### my $useragent = LWP::UserAgent->new( protocols_allowed => ["https"] ); my $request = POST( "https://test.authorize.net/gateway/transact.dll", $request_values ); # Uncomment the line ABOVE for test accounts or BELOW for live merchant accounts # my $request = POST( "https://secure.authorize.net/gateway/transact.dll", $request_values ); my $response = $useragent->request( $request ); my @responses = split( /$request_values->{x_delim_char}/, $response->content ); ########################################################### # # # The remaining code simply demonstrates how to # # # # 1. get to the response string from Authorize.Net # # 2. how to iterate through the response string to get # # specific information and display it in a web # # browser # # # # Obviously, you are welcome to add on to the existing # # code, build your own custom functions to process the # # information, and so forth. # # # # Please keep in mind that we are following the 3.1 AIM # # standards, as documented. In particular, this means # # that we are processing the response string from # # Authorize.Net with regards to the 39th field (Card # # Code Response) to address CVV2, CVC2 and CID # # responses. # # # ########################################################### print "Content-type: text/html\n\n"; $header = qq| Authorize.Net AIM Sample in CGI/Perl |; $output .= "$header\n\n"; eval {require LWP}; if ($@) { $output .= "The library www-perl (LWP) is not installed
\n"; }else{ $output .= "Library www-perl (LWP) v$LWP::VERSION installed
\n"; } $apiresponse .= $response->content . "

\n"; $endoutput .= "\n"; # Error handling (very basic) if( $response->is_success ){ # Communication was successful $fback .= "Payment processing successful!
"; }else{ $fback .= "We were unable to communicate with the authorizing site. Please try again.
"; } print "$output"; print "
\n"; print "$apiresponse"; print "
\n"; print "$fback"; print "
\n"; ########################################################### # # # We need to split the returned string, using a regular # # expression to find the pipe character (|). # # # # To escape any other meaning of the pipe character in a # # regular expression, PERL requires the following # # syntax: # # # # /\Q|/ # # # # PS: If you prefer another approach to accomplish the # # same thing, you are of course, welcome to do so. # # # # Your feedback would be welcome. Please send us your # # code improvements by e-mail to: # # # # certify@authorize.net # # integration@authorize.net # # # # Thank you. # # # ########################################################### $a = 0; $b = 0; foreach $pair(@responses){ # The next statement splits based on the pipe character: | ($name,$val) = split(/\Q|/,$pair); $a++; } print "Total number of characters in the Authorize.Net gateway response string: $a"; print "
\n"; # split the words using the pipe character (|) as the delimiter. @words = split /\Q|/, $apiresponse; # get the count of the words $count = $#words; print "Total number of separators in the Authorize.Net gateway response string: $count"; print "
\n"; # print the parsed words on the console, one in each line print ""; for ($i=0; $i <=$count; $i++) { $ctr = $i+1; if($ctr==1){ print ""; }elsif($ctr==2){ print ""; }elsif($ctr==3){ print ""; }elsif($ctr==4){ print ""; }elsif($ctr==5){ print ""; }elsif($ctr==6){ print ""; }elsif($ctr==7){ print ""; }elsif($ctr==8){ print ""; }elsif($ctr==9){ print ""; }elsif($ctr==10){ print ""; }elsif($ctr==11){ print ""; }elsif($ctr==12){ print ""; }elsif($ctr==13){ print ""; }elsif($ctr==14){ print ""; }elsif($ctr==15){ print ""; }elsif($ctr==16){ print ""; }elsif($ctr==17){ print ""; }elsif($ctr==18){ print ""; }elsif($ctr==19){ print ""; }elsif($ctr==20){ print ""; }elsif($ctr==21){ print ""; }elsif($ctr==22){ print ""; }elsif($ctr==23){ print ""; }elsif($ctr==24){ print ""; }elsif($ctr==25){ print ""; }elsif($ctr==26){ print ""; }elsif($ctr==27){ print ""; }elsif($ctr==28){ print ""; }elsif($ctr==29){ print ""; }elsif($ctr==30){ print ""; }elsif($ctr==31){ print ""; }elsif($ctr==32){ print ""; }elsif($ctr==33){ print ""; }elsif($ctr==34){ print ""; }elsif($ctr==35){ print ""; }elsif($ctr==36){ print ""; }elsif($ctr==37){ print ""; }elsif($ctr==38){ print ""; }elsif($ctr==39){ print ""; }elsif($ctr==40 || $ctr==41 || $ctr==42 || $ctr==43 || $ctr==44 || $ctr==45 || $ctr==46 || $ctr==47 || $ctr==48 || $ctr==49 || $ctr==50 || $ctr==51 || $ctr==52 || $ctr==53 || $ctr==54 || $ctr==55 || $ctr==56 || $ctr==57 || $ctr==58 || $ctr==59 || $ctr==60 || $ctr==61 || $ctr==62 || $ctr==63 || $ctr==64 || $ctr==65 || $ctr==66 || $ctr==67 || $ctr==68){ print ""; }elsif($ctr>=69){ print ""; }else{ print ""; } } print ""; print "
"; print "Response Code:"; print " "; print "@words[$i]\n"; print "
"; print "Response Sub-Code:"; print " "; print "@words[$i]\n"; print "
"; print "Response Reason Code:"; print " "; print "@words[$i]\n"; print "
"; print "Response Reason Text:"; print " "; print "@words[$i]\n"; print "
"; print "Approval Code:"; print " "; print "@words[$i]\n"; print "
"; print "AVS Result Code:"; print " "; print "@words[$i]\n"; print "
"; print "Transaction ID:"; print " "; print "@words[$i]\n"; print "
"; print "Invoice Number:"; print " "; print "@words[$i]\n"; print "
"; print "Description:"; print " "; print "@words[$i]\n"; print "
"; print "Amount:"; print " "; print "@words[$i]\n"; print "
"; print "Method:"; print " "; print "@words[$i]\n"; print "
"; print "Transaction Type:"; print " "; print "@words[$i]\n"; print "
"; print "Customer ID:"; print " "; print "@words[$i]\n"; print "
"; print "Cardholder First Name:"; print " "; print "@words[$i]\n"; print "
"; print "Cardholder Last Name:"; print " "; print "@words[$i]\n"; print "
"; print "Company:"; print " "; print "@words[$i]\n"; print "
"; print "Billing Address:"; print " "; print "@words[$i]\n"; print "
"; print "City:"; print " "; print "@words[$i]\n"; print "
"; print "State:"; print " "; print "@words[$i]\n"; print "
"; print "ZIP/Postal Code:"; print " "; print "@words[$i]\n"; print "
"; print "Country:"; print " "; print "@words[$i]\n"; print "
"; print "Phone:"; print " "; print "@words[$i]\n"; print "
"; print "Fax:"; print " "; print "@words[$i]\n"; print "
"; print "E-Mail Address:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to First Name:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to Last Name:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to Company:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to Address:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to City:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to State:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to ZIP/Postal Code:"; print " "; print "@words[$i]\n"; print "
"; print "Ship-to Country:"; print " "; print "@words[$i]\n"; print "
"; print "Tax Amount:"; print " "; print "@words[$i]\n"; print "
"; print "Duty Amount:"; print " "; print "@words[$i]\n"; print "
"; print "Freight Amount:"; print " "; print "@words[$i]\n"; print "
"; print "Tax Exempt Flag:"; print " "; print "@words[$i]\n"; print "
"; print "PO Number:"; print " "; print "@words[$i]\n"; print "
"; print "MD5 Hash:"; print " "; print "@words[$i]\n"; print "
"; print "Card Code Verification Result:"; print " "; print "@words[$i]\n"; print "
"; print "Reserved for future use:"; print " "; print "@words[$i]\n"; print "
"; print "Merchant-defined:"; print " "; print "@words[$i]\n"; print "
"; print "$ctr: @words[$i]\n"; print "
 
"; print "
\n"; print "$endoutput";