package aim;

/* ********************************************************

***********************************************************
*                                                         *
*  D O C U M E N T A T I O N                              *
*                                                         *
*  This code sample 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         *
*                                                         *
***********************************************************

***********************************************************
*                                                         *
*  D I S C L A I M E R                                    *
*                                                         *
*  WARNING: ANY USE BY YOU OF THE SAMPLE CODE PROVIDED    *
*  IS AT YOUR OWN RISK.                                   *
*                                                         *
*  Authorize.Net provides this code "as is" without       *
*  warranty of any kind, either express or implied,       *
*  including but not limited to the implied warranties    *
*  of merchantability and/or fitness for a particular     *
*  purpose.                                               *
*                                                         *
*                                                         *
***********************************************************

***********************************************************
*                                                         *
*  J A V A     D E V E L O P E R S                        *
*                                                         *
*  The provided sample code is merely a blue print,       *
*  demonstrating one possible approach to making AIM      *
*  work, by way of performing the required HTTPS POST     *
*  operation.                                             *
*                                                         *
*  1. This sample code is not a tutorial. If you are      *
*  unfamiliar with specific programming functions and     *
*  concepts, please consult the necessary reference       *
*  materials.                                             *
*                                                         *
*  2. This sample code is provided "as is," meaning that  *
*  we will not be able to assist individual e-commerce    *
*  developers with specific programming issues, relating  *
*  to the availability or non-availability of specific    *
*  modules, code libraries or other requirements to make  *
*  this code work on your specific web server             *
*  configuration.                                         *
*                                                         *
*  3. If you cannot get this sample code to work, please  *
*  do not contact Authorize.Net to complain. However, if  *
*  you encounter specific issues and would like to find   *
*  out what you can do to resolve a specific problem, we  *
*  would be happy to help you find a suitable solution    *
*  if time allows and if resources are available. We do   *
*  not promise, however, that we will be able to solve    *
*  your programming problems nor do we make any           *
*  guarantees or promises -- either express or            *
*  implied -- that we will even attempt to address any    *
*  programming issues that anyone encounters using our    *
*  sample code.                                           *
*                                                         *
*  Again, this sample code merely serves as blue print    *
*  for e-commerce developers who either are inexperienced *
*  performing HTTPS POST operations or simply want an     *
*  example of how other developers have dealt with this   *
*  challenge in the past.                                 *
*                                                         *
*                                                         *
***********************************************************

***********************************************************
*                                                         *
*  P R E R E Q U I S I T E S                              *
*                                                         *
*  To submit any kind of transaction (even test           *
*  transactions) to Authorize.Net, you need to provide    *
*  valid Authorize.Net account information (a merchant    *
*  log-in ID and a valid merchant transaction key).       *
*                                                         *
*                                                         *
*  If you cannot get the sample code to work due to an    *
*  unknown server configuration, and if you cannot        *
*  figure out how to test for the availability of those   *
*  modules, consider consulting a professional server     *
*  administrator.                                         *
*                                                         *
*  Authorize.Net is unable to assist you with trouble-    *
*  shooting and other issues relating to server           *
*  configuration.                                         *
*                                                         *
*                                                         *
***********************************************************

***********************************************************
*                                                         *
*  C O N T A C T    I N F O R M A T I O N                 *
*                                                         *
*  For specific questions,                                *
*  please contact Authorize.Net's Integration Services:   *
*                                                         *
*  integration at authorize dot net                       *
*                                                         *
*  Please remember that we cannot support individual      *
*  e-commerce developers with programming problems and    *
*  other issues that could be easily solved by referring  *
*  to the available reference materials.                  *
*                                                         *
***********************************************************

***********************************************************
*                                                         *
*  A I M   I N   A   N U T S H E L L                      *
*                                                         *
***********************************************************
*                                                         *
*  1. You gather all the required transaction data on     *
*  your secure web site.                                  *
*                                                         *
*  2. The transaction data gets submitted (via HTTPS      *
*  POST) to Authorize.Net as one long string, consisting  *
*  of specific name/value pairs.                          *
*                                                         *
*  3. When performing the HTTPS POST operation, you       *
*  remain on the same web page from which you’ve          *
*  performed the operation.                               *
*                                                         *
*  4. Authorize.Net immediately returns a transaction     *
*  response string to the same web page from which you    *
*  have performed the HTTPS POST operation.               *
*                                                         *
*  5. You may then parse the response string and act      *
*  upon certain response criteria, according to your      *
*  business needs.                                        *
*                                                         *
*                                                         *
***********************************************************

*/


/**
 * <p>Title: AIM Java Version 1.4.1_02-b06</p>
 * <p>Description: Advanced Integration Method</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Authorize.Net</p>
 * @author Authorize.Net
 * @version 3.1
 */

/**
 *  Based on sample code and snipptes provided by:
 *  Patrick Phelan, phelan@choicelogic.com
 *  Roedy Green, Canadian Mind Products
 */


import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.security.*;


public class aim_basic {

  public static void main(String[] Args){

    try{
      // standard variables for basic Java AIM test
      // use your own values where appropriate

      StringBuffer sb = new StringBuffer();

      // mandatory name/value pairs for all AIM CC transactions
      // as well as some "good to have" values
      sb.append("x_login=yourloginid&");             // replace with your own
      sb.append("x_tran_key=eoXaDm2LUnz2OiyQ&");     // replace with your own
      sb.append("x_version=3.1&");
      sb.append("x_test_request=TRUE&");             // for testing
      sb.append("x_method=CC&");
      sb.append("x_type=AUTH_CAPTURE&");
      sb.append("x_amount=1.00&");
      sb.append("x_delim_data=TRUE&");
      sb.append("x_delim_char=|&");
      sb.append("x_relay_response=FALSE&");

      // CC information
      sb.append("x_card_num=4007000000027&");
      sb.append("x_exp_date=0509&");

      // not required...but my test account is set up to require it
      sb.append("x_description=Java Transaction&");


      // open secure connection
      URL url = new URL(
          "https://test.authorize.net/gateway/transact.dll");
     //  Uncomment the line ABOVE for test accounts or BELOW for live merchant accounts
     //   https://secure.authorize.net/gateway/transact.dll

      /* NOTE: If you want to use SSL-specific features,change to:
          HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      */

     URLConnection connection = url.openConnection();
     connection.setDoOutput(true);
     connection.setUseCaches(false);

     // not necessarily required but fixes a bug with some servers
     connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

     // POST the data in the string buffer
     DataOutputStream out = new DataOutputStream( connection.getOutputStream() );
     out.write(sb.toString().getBytes());
     out.flush();
     out.close();

     // process and read the gateway response
     BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     String line;
     line = in.readLine();
     in.close();	                     // no more data
     System.err.println(line);



     // ONLY FOR THOSE WHO WANT TO CAPTURE GATEWAY RESPONSE INFORMATION
     // make the reply readable (be sure to use the x_delim_char for the split operation)
     Vector ccrep = split("|", line);

     System.out.print("Response Code: ");
     System.out.println(ccrep.elementAt(0));
     System.out.print("Human Readable Response Code: ");
     System.out.println(ccrep.elementAt(3));
     System.out.print("Approval Code: ");
     System.out.println(ccrep.elementAt(4));
     System.out.print("Trans ID: ");
     System.out.println(ccrep.elementAt(6));
     System.out.print("MD5 Hash Server: ");
     System.out.println(ccrep.elementAt(37));


    }catch(Exception e){
      e.printStackTrace();
    }
}


  // utility functions
  public static Vector split(String pattern, String in){
          int s1=0, s2=-1;
          Vector out = new Vector(30);
          while(true){
                  s2 = in.indexOf(pattern, s1);
                  if(s2 != -1){
                          out.addElement(in.substring(s1, s2));
                  }else{
                          //the end part of the string (string not pattern terminated)
                          String _ = in.substring(s1);
                          if(_ != null && !_.equals("")){
                                  out.addElement(_);
                          }
                          break;
                  }
                  s1 = s2;
                  s1 += pattern.length();
          }
          return out;
  }

  // by Roedy Green (c)1996-2003 Canadian Mind Products
  public static String toHexString ( byte[] b ){
          StringBuffer sb = new StringBuffer( b.length * 2 );
          for ( int i=0 ; i<b.length ; i++ )
          {
                  // look up high nibble char
                  sb.append( hexChar [ ( b[ i] & 0xf0 ) >>> 4 ] ) ;

                  // look up low nibble char
                  sb.append( hexChar [ b[ i] & 0x0f ] ) ;
          }
          return sb.toString() ;
 }

// table to convert a nibble to a hex character
static char[] hexChar = {
'0' , '1' , '2' , '3' ,
'4' , '5' , '6' , '7' ,
'8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' }
;
}

