javaauthorize.net

Authorize.net Java example


I am looking to develop simple Java program which will be sending a request and get response from "Authorize.net" say for Authorization, Capture, Void, Refund Transactions etc. I have created my test account in authorized.net - https://sandbox.authorize.net/. I did goggled lots of week and didn't find any sample example for reference.

I am using AIM for my development, let's say I've code for AuthTransaction

GenericValue cc = (GenericValue) params.get("creditCard");
        String currency = (String) params.get("currency");
        String amount = ((BigDecimal)params.get("processAmount")).toString();
        String number = UtilFormatOut.checkNull(cc.getString("cardNumber"));
        String expDate = UtilFormatOut.checkNull(cc.getString("expireDate"));
        String cardSecurityCode = (String) params.get("cardSecurityCode");
        AIMRequest.put("x_Amount", amount);
        AIMRequest.put("x_Currency_Code", currency);        
        AIMRequest.put("x_Method", props.getProperty("method"));
        AIMRequest.put("x_Type", props.getProperty("transType"));
        AIMRequest.put("x_Card_Num", number);
        AIMRequest.put("x_Exp_Date", expDate);
        if (UtilValidate.isNotEmpty(cardSecurityCode)) {
            AIMRequest.put("x_card_code", cardSecurityCode);
        }
        if (AIMRequest.get("x_market_type") != null) {
            AIMRequest.put("x_card_type", getCardType(UtilFormatOut.checkNull(cc.getString("cardType"))));
        }

But I don't know how to make a request call and get a response back. Any one please guide me / provide me sample code ?


Solution

  • Yes, Authorize.net does provide Java SDK and maven dependency from http://mvnrepository.com/artifact/net.authorize/anet-java-sdk.

    Authorize.Net SDK includes standard payments, recurring billing, and customer profiles.

    I've perform the simple AuthCapture Transaction through following Java code

    package com.auth.net;
    
    import java.math.BigDecimal;
    
    import net.authorize.Environment;
    import net.authorize.Merchant;
    import net.authorize.TransactionType;
    import net.authorize.aim.Result;
    import net.authorize.aim.Transaction;
    import net.authorize.data.creditcard.CreditCard;
    
    public class AIMauthCaptureTransactionDemo {
        public static final String apiLoginID= "Your Sandbox API Login ID";
        public static final String transactionKey= "Your Sandbox API Transaction Key";
    
        public static void main(String[] args) {
            Merchant merchant = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey);
            // create credit card
            CreditCard creditCard = CreditCard.createCreditCard();
            creditCard.setCreditCardNumber("4111 1111 1111 1111");
            creditCard.setExpirationMonth("12");
            creditCard.setExpirationYear("2018");
    
            // create transaction
            Transaction authCaptureTransaction = merchant.createAIMTransaction
                    (TransactionType.AUTH_CAPTURE, new BigDecimal("5.00"));
    
            authCaptureTransaction.setCreditCard(creditCard);
    
            @SuppressWarnings("unchecked")
            Result<Transaction> result = (Result<Transaction>)merchant.postTransaction(authCaptureTransaction);
    
            if(result.isApproved()) {
                System.out.println("Response Code : "+ result.getReasonResponseCode());
                System.out.println("Response Text : " + result.getResponseText());
                System.out.println("Transaction Id: " + result.getTarget().getTransactionId());
                System.out.println("AuthorizationCode : "+result.getTarget().getAuthorizationCode());
            } 
            else if (result.isDeclined()) {
                System.out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
            }
            else {
                System.out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
            }
        }
    
    }
    

    Here, Please refer first transaction which I performed some time before in below screen shot

    enter image description here