cancel
Showing results for 
Search instead for 
Did you mean: 

Perl example based on Accept.JS

I spent some time putting together some Perl code for anyone interested. It’s based on Accept.JS.  I’ve included my example sandbox code below. It’s not complete but should provide the process flow and technique.  I’m sure there’s room for improvement. If anyone see’s any issues or concerns on this approach please comment.

 

HTML

 

<!DOCTYPE html>
<html>
<head>
 <title>sandbox test</title>
</head>
<body>

<script type="text/javascript" src="https://jstest.authorize.net/v1/Accept.js" charset="utf-8"></script>

<input  type="text" name="cardNumber"    id="cardNumber" value="5424000000000015" /> <br><br>
<input  type="text" name="expMonth"      id="expMonth"   value="12" /> <br><br>
<input  type="text" name="expYear"       id="expYear"    value="2020" /> <br><br>
<input  type="text" name="cardCode"      id="cardCode"   value="999" /> <br><br>

<button type="button" onclick="sendPaymentDataToAnet()">Pay</button>

<div id="results"></div>

<script type="text/javascript">

 function sendPaymentDataToAnet() {

  var authData = {};
      authData.clientKey  = "<your sandbox key>";
      authData.apiLoginID = "<your sandbox id>";
  var cardData = {};
      cardData.cardNumber = document.getElementById("cardNumber").value;
      cardData.month      = document.getElementById("expMonth").value;
      cardData.year       = document.getElementById("expYear").value;
      cardData.cardCode   = document.getElementById("cardCode").value;
  var secureData = {};
      secureData.authData = authData;
      secureData.cardData = cardData;

  Accept.dispatchData(secureData, responseHandler);

 }

 function responseHandler(response) {

  var F = new FormData();
  var R = JSON.stringify(response);
  var T = new Date().getTime();
  F.append("response",R);
  F.append("timestamp",T);

  if (window.XMLHttpRequest) {
      var XHR = new XMLHttpRequest();
  } else {
      var XHR = new ActiveXObject("Microsoft.XMLHTTP");
  }
  XHR.open("POST","<perl script location>",true);
  XHR.onreadystatechange = function () {
    if (XHR.readyState == 4 && XHR.status == 200) {
        document.getElementById("results").innerHTML = XHR.responseText;
    }
  }
  XHR.send(F);

 }

</script>

</body>
</html>

PERL

 

#!/usr/bin/perl

MAIN:
{
 use CGI;
 $CGI=new CGI;
 use LWP::UserAgent; # yum install perl-LWP-Protocol-https perl-libwww-perl perl-JSON
 use JSON;

 $API_URL             = 'https://apitest.authorize.net/xml/v1/request.api';
 $API_LOGIN_ID        = "<your sanfbox id>";
 $API_TRANSACTION_KEY = "<your sandbox key>";
 $AMOUNT              = "10";  # test amount

 $response = $CGI->param('response');
 $response = decode_json($response);

 my $log = "<br>";
 $log.="resultCode   = $$response{messages}{resultCode}<br>";
 $log.="message code = $$response{messages}{message}[i]{code}<br>";
 $log.="message text = $$response{messages}{message}[i]{text}<br>";

 if ($$response{messages}{resultCode} ne "Error") {

     &post_transactionRequest;
     $response = decode_json($postresponse);

     $log.="responseCode         = $$response{transactionResponse}{responseCode}<br>";
     $log.="messages code        = $$response{transactionResponse}{messages}[0]{code}<br>";
     $log.="messages description = $$response{transactionResponse}{messages}[0]{description}<br>";

 }
 print qq~Content-type: text/html\n\n$log~;
 exit;

 #############################
 
 sub post_transactionRequest {
     #
     my $json=qq~{ "createTransactionRequest": {
  "merchantAuthentication": {
   "name": "$API_LOGIN_ID",
   "transactionKey": "$API_TRANSACTION_KEY"
  },
  "transactionRequest": {
   "transactionType": "authCaptureTransaction",
   "amount": "$AMOUNT",
   "payment": {
    "opaqueData": {
     "dataDescriptor": "$$response{opaqueData}{dataDescriptor}",
     "dataValue": "$$response{opaqueData}{dataValue}" }}}}}~;
     #
     my $ua        = new LWP::UserAgent();
     my $response  = $ua->post($API_URL, Content => $json);
     $postresponse = $response->decoded_content;
     $postresponse =~ s/[^{]+//;
     return;
 }

}
airman81
Contributor
0 REPLIES 0