'Authorize.Net CIM With Java SDK: How To Bill Your Customers' post illustration

Authorize.Net CIM With Java SDK: How To Bill Your Customers

avatar

Authorize.Net is a popular payment gateway service provider allowing merchants to accept credit card and electronic check payments.
Using such a gateway to issue a one-time transactions doesn't raise any great troubles, but serving returning customers or managing complex subscriptions requires you either to be compliant with the Payment Card Industry Data Security Standard (PCI DSS) or to ask your customer to re-enter his or her billing info every time. Neither is always what you want. Well, among other Authorize's features there is a Customer Information Manager (CIM), which targets that particular problem.

CIM makes it possible to charge returning customers and process recurring transactions without the need to store sensitive information in your database. This is achieved by storing customers' payment information on Authorize’s secure servers and accessing it later by IDs. For each customer you create a separate customer profile, which can include up to 10 payment profiles and up to 100 shipping profiles.

All the communication with Authorize.Net occurs through XML calls or SOAP. Guides devoted to this topic can be downloaded from Authorize's website. Thankfully you do not have to worry about constructing correct requests and parsing responses. This routine is done by Java SDK provided by Authorize. But to the big surprise there is very scant information on how to work with this SDK. So, I'll try to fill this gap and begin with an overview of the least effort required to bill a customer.

To start working with Authorize CIM first sign up for a test account to obtain an API Login ID and Transaction Key. These keys will authenticate requests to the payment gateway.
Secondly, manually download net.authorize:anet-java-sdk artifact, as it is not available from any public maven repository, and place it in your local or corporate maven repository.
As a code example let’s create a customer and make a test transaction.

Classes we are working with are:

1
2
3
4
5
6
7
8
9
10
11
12
import net.authorize.Environment;
import net.authorize.Merchant;
import net.authorize.cim.Result;
import net.authorize.cim.Transaction;
import net.authorize.cim.TransactionType;
import net.authorize.data.Order;
import net.authorize.data.creditcard.CreditCard;
import net.authorize.data.cim.CustomerProfile;
import net.authorize.data.cim.PaymentTransaction;
import net.authorize.data.cim.PaymentProfile;
import net.authorize.data.xml.CustomerType;
import net.authorize.data.xml.Payment;

First we must create customer profile with an appropriate billing information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Create a merchant object:
String apiLoginID = "YOUR_API_LOGIN_ID";
String transactionKey = "YOUR_TRANSACTION_KEY";
Merchant merchant = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey);

// Construct a transaction to create a customer profile along with a customer payment profile. Also a shipping profile if you want
Transaction transaction = merchant.createCIMTransaction(TransactionType.CREATE_CUSTOMER_PROFILE);

// Add customer profile information
CustomerProfile customerProfile = CustomerProfile.createCustomerProfile();
customerProfile.setMerchantCustomerId("CustomerID");
customerProfile.setEmail("customer[at]email.test");
customerProfile.setDescription("Test account");
transaction.setCustomerProfile(customerProfile);

// Add customer payment profile information
CreditCard creditCard = CreditCard.createCreditCard();
creditCard.setCreditCardNumber("4111 1111 1111 1111");
creditCard.setExpirationMonth("12");
creditCard.setExpirationYear("2020");
Payment payment = Payment.createPayment(creditCard);
PaymentProfile paymentProfile = PaymentProfile.createPaymentProfile();
paymentProfile.setCustomerType(CustomerType.INDIVIDUAL);
paymentProfile.addPayment(payment);
transaction.addPaymentProfile(paymentProfile);

// Send request
Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);

// Check if the response is positive and signal if it is not
if (result.isOk()) {
    System.out.println("Transaction has succeeded!\n");
    System.out.println("Customer profile ID: " + result.getCustomerProfileId() + "\n");
    System.out.println("Customer payment profile ID: " + result.getCustomerPaymentProfileIdList().get(0) + "\n");
} else {
    System.out.println("Transaction has failed. Reason: " + result.getMessages().get(0).getText());
}

You should store these retrieved IDs for the future use. Then, when the need arises, you can charge that customer with his IDs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Create payment transaction
Transaction transaction = merchant.createCIMTransaction(TransactionType.CREATE_CUSTOMER_PROFILE_TRANSACTION);

// Set customer ID and customer's payment profile ID that you want to be billed
transaction.setCustomerProfileId("customerProfileId");
transaction.setCustomerPaymentProfileId("paymentProfileId");

// Add information about the transaction
PaymentTransaction paymentTransaction = PaymentTransaction.createPaymentTransaction();
Order order = Order.createOrder();
order.setTotalAmount(new BigDecimal(9.99));
order.setDescription("Test charge");
paymentTransaction.setOrder(order);
paymentTransaction.setTransactionType(net.authorize.TransactionType.AUTH_CAPTURE);
transaction.setPaymentTransaction(paymentTransaction);

// Send request and check the response
Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
if (result.isOk()) {
    System.out.println("Payment has succeeded!\n");
    System.out.println("Transaction ID: " + result.getDirectResponseList().get(0).getDirectResponseMap().get(ResponseField.TRANSACTION_ID));
} else {
    System.out.println("Payment has failed. Reason: " + result.getMessages().get(0).getText());
}

After running your code, you can either inspect the response code result or go to your transaction report to verify that the payment transaction succeeded.

If you're looking for a developer or considering starting a new project,
we are always ready to help!