net.authorize.admcApi
Class AimTransaction

java.lang.Object
  extended by net.authorize.admcApi.AimTransaction

public class AimTransaction
extends Object

Authorize.net client. N.b. It would be very easy for a scoundrel to modify this class to steal or use your credit card information. Therefore, only use a copy of this class from a source which you trust, and verify the checksums! N.b. the difference between "transaction key", my password; and "transaction ID", the identifier for a transaction. It seems that Authorize.net is trying to force AIM users into the hands of their own partners by no longer providing easy public access to the AIM API's, like they used to do. You can see the sample code at http://developer.authorize.net/samplecode/ but they leave a lot of questions unanswered, and the code is non-scalable and non-Object Oriented. Modifications by Bruce-Robert Pocock in 2009 to throw exceptions rather than silently "corrupting" data (in my opinion) by truncating values, &c. I've also converted the financial parts to use BigDecimal values, to avoid rounding errors; and removed support for Persistor:s, instead trusting to my own methods to extract the relevant data after processing using the getters.


Field Summary
private  String address
          authorize.net data field
private  String amount
          authorize.net data field
private  String auth_code
          authorize.net data field
private  String card_code
          authorize.net data field
private  String card_num
          authorize.net data field
private  String city
          authorize.net data field
private  String company
          authorize.net data field
private  AimConfig config
          internal configuration
private  String country
          authorize.net data field
private  String cust_id
          authorize.net data field
 String[] data
          The main data stream from authorize.net's online reply
private  String description
          authorize.net data field
private static DecimalFormat df2
          Decimal formatting for US dollars and cents
private  String email
          authorize.net data field
private  String exp_date
          authorize.net data field
private  String fax
          authorize.net data field
private  String first_name
          authorize.net data field
private  String invoice_num
          authorize.net data field
private  String last_name
          authorize.net data field
static String LS
          Local notion of carriage return.
private  MessageDigest md5Digest
          authorize.net data verification field
private  String phone
          authorize.net data field
private  BigDecimal requestedAmount
          authorize.net data field
private  String ship_to_address
          authorize.net data field
private  String ship_to_city
          authorize.net data field
private  String ship_to_company
          authorize.net data field
private  String ship_to_country
          authorize.net data field
private  String ship_to_first_name
          authorize.net data field
private  String ship_to_last_name
          authorize.net data field
private  String ship_to_state
          authorize.net data field
private  String ship_to_zip
          authorize.net data field
private  String state
          authorize.net data field
private  boolean test_request
          is this a test request? if true, it's a test; if false, there's real money involved.
private  String trans_id
          authorize.net data field
private  String type
          authorize.net data field for type of transaction (always AUTH_CAPTURE in my case)
private  String zip
          authorize.net data field
 
Constructor Summary
AimTransaction(AimConfig config)
          Purposefully has only Package visibility.
 
Method Summary
private  void assertConfig()
           
 void assertData()
           
static String assertMaxLength(String inString, int maxLen)
           Public utility method.
 void checkMd5Hash()
           
 BigDecimal getAmount()
           
 String getApproval()
           
 char getAVS()
           
 int getRcode()
           
 int getRRcode()
           
 String getRRtext()
           
 boolean getTestMode()
           
 int getTransId()
           
 String getTransType()
           
 boolean isTestMode()
           
static void main(String[] sa)
          Run with no parameters to see syntax, like:
 void parseAuthNetReply(InputStream in)
           
 void postPurchase()
           Perform post-purchase validation.
private  void postPurchaseValidate()
          Validate that a normal purchase transaction completed successfully.
 void setAmount(BigDecimal inDec)
           
 void setAmount(String inString)
           
 void setAuthCode(String inString)
           
 void setBillAddress(String inString)
           
 void setBillCity(String inString)
           
 void setBillCountry(String inString)
           
 void setBillFax(String inString)
           
 void setBillFirstName(String inString)
           
 void setBillLastName(String inString)
           
 void setBillOrg(String inString)
           
 void setBillPhone(String inString)
           
 void setBillStateName(String inString)
           
 void setBillZip(String inString)
           
 void setCardCode(String inString)
           
 void setCardNum(String inString)
           
 void setCustId(String inString)
           
 void setDescription(String inString)
           
 void setEmail(String inString)
           
 void setExp(String inString)
           
 void setInvoiceNum(int i)
          Convenience data type conversion
 void setInvoiceNum(String inString)
          Misnomer taken from the AIM documentation.
 void setShipAddress(String inString)
           
 void setShipCity(String inString)
           
 void setShipCountry(String inString)
           
 void setShipFirstName(String inString)
           
 void setShipLastName(String inString)
           
 void setShipOrg(String inString)
           
 void setShipStateName(String inString)
           
 void setShipZip(String inString)
           
 void setTestMode(boolean test_request)
          Set the test mode for this one transaction.
 void setTransId(int inInt)
           
 void setType(String inString)
           
 void submit()
          actually beam the data over!
static String toHex(byte[] byteArray)
          Convert a byte array to hex codes
 String toString()
          Dumps entire response data array, except for empty elements, which are skipped.
 void validateInput(String vehicle)
           Verify that input data is good to submit a payment request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

df2

private static final DecimalFormat df2
Decimal formatting for US dollars and cents


LS

public static final String LS
Local notion of carriage return.


address

private String address
authorize.net data field


amount

private String amount
authorize.net data field


auth_code

private String auth_code
authorize.net data field


card_code

private String card_code
authorize.net data field


card_num

private String card_num
authorize.net data field


city

private String city
authorize.net data field


company

private String company
authorize.net data field


config

private AimConfig config
internal configuration


country

private String country
authorize.net data field


cust_id

private String cust_id
authorize.net data field


data

public String[] data
The main data stream from authorize.net's online reply


description

private String description
authorize.net data field


email

private String email
authorize.net data field


exp_date

private String exp_date
authorize.net data field


fax

private String fax
authorize.net data field


first_name

private String first_name
authorize.net data field


invoice_num

private String invoice_num
authorize.net data field


last_name

private String last_name
authorize.net data field


md5Digest

private MessageDigest md5Digest
authorize.net data verification field


phone

private String phone
authorize.net data field


requestedAmount

private BigDecimal requestedAmount
authorize.net data field


ship_to_address

private String ship_to_address
authorize.net data field


ship_to_city

private String ship_to_city
authorize.net data field


ship_to_company

private String ship_to_company
authorize.net data field


ship_to_country

private String ship_to_country
authorize.net data field


ship_to_first_name

private String ship_to_first_name
authorize.net data field


ship_to_last_name

private String ship_to_last_name
authorize.net data field


ship_to_state

private String ship_to_state
authorize.net data field


ship_to_zip

private String ship_to_zip
authorize.net data field


state

private String state
authorize.net data field


test_request

private boolean test_request
is this a test request? if true, it's a test; if false, there's real money involved.


trans_id

private String trans_id
authorize.net data field


type

private String type
authorize.net data field for type of transaction (always AUTH_CAPTURE in my case)


zip

private String zip
authorize.net data field

Constructor Detail

AimTransaction

AimTransaction(AimConfig config)
Purposefully has only Package visibility. It's probably most convenient to get your AimTransaction objects from the factory method AimConfig.newTransaction()

Parameters:
config - configuration
See Also:
AimConfig.newTransaction()
Method Detail

assertMaxLength

public static String assertMaxLength(String inString,
                                     int maxLen)
                              throws StringIndexOutOfBoundsException

Public utility method.

Notes by BRPocock on revisions: In my application, I want to throw an exception back up the ladder if the string is too long, rather than silently truncating it. As such, I took the existing public method named truncateIfLonger and replaced it with this one, assertMaxLength.

Original method: Returns the given String if length is less than or equal to the specified limit. Otherwise returns a newly constructed, truncated version of the given String.

Parameters:
inString - The string to be checked
maxLen - The maximum allowed length
Returns:
the original string, if it's valid
Throws:
StringIndexOutOfBoundsException - if the string is too long

main

public static void main(String[] sa)
                 throws AuthNetException,
                        IOException
Run with no parameters to see syntax, like:
     java com.admc.authnet.AimTransaction
 
N.b. THIS IS FOR NON-SECURE TESTING! Make sure your computer (and your network connection to it, if any) is secure before running this method, because the command-line parameters you give to this command can be observed by others.

Parameters:
sa - Command-line parameters. --v=[visa|mc|amex|disc] (to validate input before and result after processing); then, dollar amount, card number, and card expiry (MM/YY)
Throws:
AuthNetException - if there's a problem with setting up the transaction or validating the data
IOException - if there's a communications error trying to speak to Authorize.Net

toHex

public static String toHex(byte[] byteArray)
Convert a byte array to hex codes

Parameters:
byteArray - bytes
Returns:
a hex string representing those values

assertConfig

private void assertConfig()
                   throws IllegalStateException
Throws:
IllegalStateException - if this object hasn't gotten initialized with a configuration, at least

assertData

public void assertData()
                throws IllegalStateException
Throws:
IllegalStateException - if the transaction hasn't been completed yet

checkMd5Hash

public void checkMd5Hash()
                  throws AuthNetException
Throws:
AuthNetException - if received hash does not match calculated hash.

getAmount

public BigDecimal getAmount()
Returns:
The amount of the transaction as returned by Authorize.Net
Throws:
IllegalStateException - if the transaction hasn't been completed yet

getApproval

public String getApproval()
Returns:
the approval code for the transaction

getAVS

public char getAVS()
Returns:
address verification service return code (single letter code)

getRcode

public int getRcode()
Returns:
response/result code; 1 is success.

getRRcode

public int getRRcode()
Returns:
response reason sub-code

getRRtext

public String getRRtext()
Returns:
response reason text message

getTestMode

public boolean getTestMode()
Returns:
if the request is in test mode, returns true

getTransId

public int getTransId()
Returns:
transaction ID number returned by authorize.net

getTransType

public String getTransType()
Returns:
type of transaction, normally AUTH_CAPTURE; the server seems case-insensitive, but examples generally show the input going in ALL_CAPS and returning all_lower, just for freakiness.

isTestMode

public boolean isTestMode()
Returns:
true if the transaction was executed as a test because of server settings

parseAuthNetReply

public void parseAuthNetReply(InputStream in)
                       throws AuthNetException,
                              IOException
Parameters:
in - input stream from authorize.net
Throws:
AuthNetException - if the data be badness
IOException - if the network be badness

postPurchase

public void postPurchase()
                  throws AuthNetException

Perform post-purchase validation.

User should call either postPurchase() after submitting a purchase transaction.

Throws:
AuthNetException - Your postPurchase call should handle these. Purchase-specific post-processing.

User should call either postPurchase(Persistor) or postPurchase() after submitting a purchase transaction.

Does purchase-specific post-validation, and invokes the user-specified persist method.

AuthNetException It is very important that these critical exceptions be handled appropriately, because the customer has been charged but you may not have a record of it. You just need to set up a SMTP Appender or write these to a critical log file so you know to check the details with the Authorize.net console.

postPurchaseValidate

private void postPurchaseValidate()
                           throws AuthNetException
Validate that a normal purchase transaction completed successfully.

Throws:
AuthNetException - if the transaction failed, or wasn't an AUTH_CAPTURE type transaction

setAmount

public void setAmount(BigDecimal inDec)
Parameters:
inDec - The amount requested for this payment as a decimal

setAmount

public void setAmount(String inString)
Parameters:
inString - The amount requested for this payment as a string

setAuthCode

public void setAuthCode(String inString)
Parameters:
inString - The auth. code. (BRP: I don't know what this means?)

setBillAddress

public void setBillAddress(String inString)
Parameters:
inString - The billing address: street address

setBillCity

public void setBillCity(String inString)
Parameters:
inString - The billing address: city

setBillCountry

public void setBillCountry(String inString)
Parameters:
inString - The billing address: country

setBillFax

public void setBillFax(String inString)
Parameters:
inString - The billing address: optional fax number

setBillFirstName

public void setBillFirstName(String inString)
Parameters:
inString - The billing address: first name

setBillLastName

public void setBillLastName(String inString)
Parameters:
inString - last name to bill to

setBillOrg

public void setBillOrg(String inString)
Parameters:
inString - organization/company to bill

setBillPhone

public void setBillPhone(String inString)
Parameters:
inString - optional phone number for payer

setBillStateName

public void setBillStateName(String inString)
Parameters:
inString - billing address state/province

setBillZip

public void setBillZip(String inString)
Parameters:
inString - billing address ZIP/postal code

setCardCode

public void setCardCode(String inString)
Parameters:
inString - card code for verification (CCV) — thing from the back of the card

setCardNum

public void setCardNum(String inString)
Parameters:
inString - card / account number

setCustId

public void setCustId(String inString)
Parameters:
inString - customer ID ­— our own number

setDescription

public void setDescription(String inString)
Parameters:
inString - description of the transaction (why are we taking their money?)

setEmail

public void setEmail(String inString)
Parameters:
inString - buyer's email, depending on server settings, authorize.net might send them a note or something??

setExp

public void setExp(String inString)
Parameters:
inString - expiry date: must be MM/DD and yes, the month must be zero-padded

setInvoiceNum

public void setInvoiceNum(int i)
Convenience data type conversion

Parameters:
i - invoice number as an actual number

setInvoiceNum

public void setInvoiceNum(String inString)
Misnomer taken from the AIM documentation. This is an invoice identifier, not necessary a number.

Parameters:
inString - the invoice ID "number"

setShipAddress

public void setShipAddress(String inString)
Parameters:
inString - shipping address — street part, property number (123 Sesame St Apt 2C)

setShipCity

public void setShipCity(String inString)
Parameters:
inString - shipping address city

setShipCountry

public void setShipCountry(String inString)
Parameters:
inString - shipping address country

setShipFirstName

public void setShipFirstName(String inString)
Parameters:
inString - shipping address first (given) name

setShipLastName

public void setShipLastName(String inString)
Parameters:
inString - shipping address last (family) name

setShipOrg

public void setShipOrg(String inString)
Parameters:
inString - shipping address organization/company

setShipStateName

public void setShipStateName(String inString)
Parameters:
inString - shippping address state/province

setShipZip

public void setShipZip(String inString)
Parameters:
inString - shipping address ZIP/postal code

setTestMode

public void setTestMode(boolean test_request)
Set the test mode for this one transaction.

Parameters:
test_request - true if this is a test transaction

setTransId

public void setTransId(int inInt)
Parameters:
inInt - set the transaction ID, only do this for follow-up transaction types

setType

public void setType(String inString)
Parameters:
inString - set the transaction type, I think?

submit

public void submit()
            throws AuthNetException,
                   IOException
actually beam the data over!

Throws:
IOException - if the network is having a bad day
AuthNetException - if the data is having a bad day

toString

public String toString()
Dumps entire response data array, except for empty elements, which are skipped.

Overrides:
toString in class Object

validateInput

public void validateInput(String vehicle)
                   throws AuthNetException

Verify that input data is good to submit a payment request.

We assume that the addresses have already been validated.

Parameters:
vehicle - credit-card type: one of the literal strings, "visa" "mc" "amex" "disc" (Discover/Novus)
Throws:
AuthNetException - if anything is bad