Dvds Den

info@dvdsden.com

 


                                                
 

AuthorizeNet API Transactions

 
 
February 11, 2013

For some mysterious reason once in a while a transaction authorized by AuthorizeNet didn't get into the orders database. SilentPost is used to trigger the database entry. A method was needed to pull down a list of transactions from AuthorizeNet and check each one for being in the database. Doing it manually was just too tedious.

Initially it was assummed that the API could be used to pull down settled transactions. It turns out that it is a lot more complicated than a simple API call. It isnecessary to first download a list of batched settlements, and then make another call to get the transactions from each batch. A date range can be put in to limit the number of batched settlements.

The AuthorizeNet API in Java was downloaded as source and compiled. It is available at

http://dvdsdirect.us/notes/anet-1.4.6.jar. If you need them, the dependencies are at

http://dvdsdirect.us/notes/dependencies/anetlib.jar

Here is the Java source for the project. Notice that there are two references in the second listing, GetSettledTransactions:

        GetAuthorizeNetProfile getprofile = new GetAuthorizeNetProfile();
        AuthorizeNetContext context = getprofile.apiContext();

These simply provide a property for the developer's login id and transaction key that are provided by AuthorizeNet. A new transaction key is easily obtained in the developer's AuthorizeNet administration webpage.
package authorizenet;

import dvdsupport.DvdOrderRecord;
import dvdsupport.GetOrderByRef;
import java.math.BigDecimal;
import java.util.ArrayList;
import net.authorize.ResponseCode;
import net.authorize.data.xml.reporting.TransactionDetails;
import net.authorize.data.xml.reporting.TransactionStatusType;
import net.authorize.reporting.TransactionType;

/**
 *
 * @author Gary L. Harris
 */
public class VerifySettledTransactions {

    public void verify() {
        GetSettledTransactions gst = new GetSettledTransactions();
        ArrayList settledtransactions = gst.runGetList();
        int nritems = settledtransactions.size();
        GetOrderByRef gobr = new GetOrderByRef();
        for (Object obj : settledtransactions) {
            TransactionDetails transdetails = (TransactionDetails) obj;
            String tid = transdetails.getTransId();
            String invoicenr = transdetails.getInvoiceNumber();
            String lastname = transdetails.getLastName();
            String firstname = transdetails.getFirstName();
            BigDecimal amount = transdetails.getSettleAmount();
            ResponseCode responsecode=transdetails.getResponseCode();
            TransactionStatusType transtype=transdetails.getTransactionStatus();
            String status=transtype.value();
            if (status==null) status="UNKNOWN";
            System.out.println();
            System.out.println(tid + "\t" + invoicenr+"\t"+lastname + " " + firstname+"\t"+status);
            DvdOrderRecord orderrec = gobr.getOrderDetails(tid);
            if (orderrec==null) {
                System.out.println("reforder missing: "+tid);
                continue;
            }
            String ordernr = orderrec.ordernr;
            String customernr = orderrec.custnr;
            String orderdate = orderrec.orderdate;
            String valid = orderrec.valid;
            if (valid == null | valid.equals(" ")) {
                valid = "Y";
            }
            System.out.println(ordernr + "\t" + customernr + "\t" + tid + "\t" + orderdate + "\t" + valid);
        }
    }

    public static void main(String[] args) {
        VerifySettledTransactions main = new VerifySettledTransactions();
        main.verify();
        System.out.println("End of Program");
    }
}
                                                		
                                                		
    
package authorizenet;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import net.authorize.Environment;
import net.authorize.Merchant;
import net.authorize.Transaction;
import net.authorize.data.creditcard.CardType;
import net.authorize.data.xml.reporting.BatchDetails;
import net.authorize.data.xml.reporting.BatchStatistics;
import net.authorize.data.xml.reporting.ReportingDetails;
import net.authorize.data.xml.reporting.TransactionDetails;
import net.authorize.reporting.Result;
import net.authorize.xml.Message;

/**
 *
 * @author Gary L. Harris
 */
public class GetSettledTransactions {
    static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GetSettledTransactions.class);
    boolean debug = false;

    public ArrayList runGetList() {
        log.debug("");
        log.debug("calling authorizenet");
        ArrayList settledtransactions=new ArrayList();
        GetAuthorizeNetProfile getprofile = new GetAuthorizeNetProfile();
        AuthorizeNetContext context = getprofile.apiContext();
        String apiLoginID = context.apiloginid;
        String transactionKey = context.transactionid;

        Merchant merchant = Merchant.createMerchant(Environment.PRODUCTION,
                apiLoginID, transactionKey);
        net.authorize.reporting.Transaction transaction =
                merchant.createReportingTransaction(
                net.authorize.reporting.TransactionType.GET_SETTLED_BATCH_LIST);
        net.authorize.data.xml.reporting.ReportingDetails reportingDetails =
                net.authorize.data.xml.reporting.ReportingDetails.createReportingDetails();

        Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();

        cal.add(Calendar.DAY_OF_YEAR, -10);     // set date range 10 days back until today
        Date begdate = cal.getTime();
        reportingDetails.setBatchFirstSettlementDate(begdate);
        reportingDetails.setBatchLastSettlementDate(today);

        reportingDetails.setBatchIncludeStatistics(true);
        long longnum = today.getTime();
        reportingDetails.setBatchId("1");
        transaction.setReportingDetails(reportingDetails);

        Result<Transaction> result =
                (Result<Transaction>) merchant.postTransaction(transaction);
        ArrayList messages = result.getMessages();
        for (Object obj : messages) {
            Message msg = (Message) obj;
            log.debug("Message: " + msg.getText());
        }

        ReportingDetails rd = result.getReportingDetails();

        try {
            Transaction traction = result.getTarget();
            ReportingDetails repdetails = result.getReportingDetails();
            ArrayList batchdetailslist = repdetails.getBatchDetailsList();

            Iterator iter = batchdetailslist.listIterator();
            System.out.println();
            while (iter.hasNext()) {
                BatchDetails batchdetails = (BatchDetails) iter.next();
                String thisbatchid = batchdetails.getBatchId();
                Date datesettled = batchdetails.getSettlementTimeLocal();
                log.debug(datesettled + "\t" + thisbatchid);
                
                // get transactions in the batch
                ArrayList transactionlist=getTransactionList(merchant, reportingDetails, thisbatchid);
                int nrtransactions=transactionlist.size();
                for (int i=0; i<nrtransactions; i++) {
                    TransactionDetails transdetails=(TransactionDetails)transactionlist.get(i);
                    String tid=transdetails.getTransId();
                    String lastname=transdetails.getLastName();
                    String firstname=transdetails.getFirstName();
                    BigDecimal amount=transdetails.getSettleAmount();
                    System.out.println(tid+"\t"+amount+"\t"+lastname+"\t"+firstname);
                    settledtransactions.add(transdetails);
                }
                
                ArrayList batchstatslist = batchdetails.getBatchStatisticsList();
                Iterator statsiter = batchstatslist.listIterator();
                while (statsiter.hasNext()) {
                    BatchStatistics batchstats = (BatchStatistics) statsiter.next();

                    CardType cardtype = batchstats.getAccountType();
                    BigDecimal chargeamt = batchstats.getChargeAmount();

                    // for debugging
                    System.out.println("CardType: " + cardtype.getValue());
                    System.out.println("ChargeAmount: " + chargeamt);
                    System.out.println();
                }
            }


            //transdetailslist.
            String xmlstring = traction.toString();
            System.out.println(xmlstring);
            String resultstring = traction.toXMLString();
            System.out.println(resultstring);

            if (debug) {
            }

        } catch (Exception ex) {
            log.error("Exception: " + ex.getMessage());
        }
        return settledtransactions;
    }

    public ArrayList getTransactionList(Merchant merchant,  
                ReportingDetails reportingdetails, String batchid) {
        ArrayList transactionlist=new ArrayList();        
        net.authorize.reporting.Transaction transaction =
                merchant.createReportingTransaction(
                net.authorize.reporting.TransactionType.GET_TRANSACTION_LIST);
        net.authorize.data.xml.reporting.ReportingDetails reportingDetails =
                net.authorize.data.xml.reporting.ReportingDetails.createReportingDetails();        

        reportingdetails.setBatchId(batchid);
        transaction.setReportingDetails(reportingdetails);

        Result<Transaction> result =
                (Result<Transaction>) merchant.postTransaction(transaction);
        ArrayList messages = result.getMessages();
        for (Object obj : messages) {
            Message msg = (Message) obj;
            log.debug("Message: " + msg.getText());
        }
        ReportingDetails rdetails=result.getReportingDetails();
        ArrayList translist=rdetails.getTransactionDetailList();
        int nrtrans=translist.size();
        for (int i=0; i<nrtrans; i++) {
            TransactionDetails transdetails=(TransactionDetails)translist.get(i);
            transactionlist.add(transdetails);
        }
        
        return transactionlist;
    }

    public static void main(String[] args) {
        GetSettledTransactions main = new GetSettledTransactions();
        ArrayList settledtransactions=main.runGetList();
        System.out.println("End of Program");
    }
}
                                        	   
     --Gary