Authorize.Net CIM with Java SDK: Managing Your Customers’ Info

In the previous post, I told how to bill a customer with minimal fuss. But oftentimes, returning customers wish to add or change their billing information, or maybe you want to delete the old ones. Let’s dive deeper and see what’s possible to do with Authorize.Net Customer Information Manager by remote procedure calls using, as previously, Java SDK.

To be able to bill a customer, you have to create a customer profile. In the customer profile, you can store customers’ billing and shipping profiles. For each of these profiles, you can update, delete, or create a new one. Now let’s see those actions in examples.

First of all, to issue requests and transactions to Authorize.Net, you need to authenticate yourself by creating a merchant object with the API Login ID and Transaction Key you’ve got from Authorize.Net:

String apiLoginID = "YOUR_API_LOGIN_ID";
String transactionKey = "YOUR_TRANSACTION_KEY";
Merchant merchant = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey);

Now you’ll issue all requests by invoking the Merchant’s postTransaction(Transaction transaction) method.

You can create a customer profile solely (it will store the customer’s billing and shipping information in the future):

Transaction transaction = merchant.createCIMTransaction(TransactionType.CREATE_CUSTOMER_PROFILE);

CustomerProfile customerProfile = CustomerProfile.createCustomerProfile();
customerProfile.setMerchantCustomerId("Your_Customer_Id");
customerProfile.setEmail("Your_Customer_Email");
customerProfile.setDescription("Your_Customer_Description”");
transaction.setCustomerProfile(customerProfile);

Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
if (result.isOk()) {
   String customerId = result.getCustomerProfileId();
}

Add shipping profile to the existing customer profile:

Transaction transaction = merchant.createCIMTransaction(TransactionType.CREATE_CUSTOMER_SHIPPING_ADDRESS);
transaction.setCustomerProfileId(customerProfileId);
transaction.setShipTo(Address.createAddress());
Address address = Address.createAddress();
address.setFirstName("FirstName");
address.setLastName("LastName");
address.setCompany("Company");
address.setAddress("Street");
address.setCity("City");
address.setState("State");
address.setZipPostalCode("12345");
address.setCountry("Country");
Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
if (result.isOk()) {
   String shippingId = result.getCustomerShippingAddressIdList().get(0);
}

Add payment profile:

Transaction transaction = merchant.createCIMTransaction(TransactionType.CREATE_CUSTOMER_PAYMENT_PROFILE);
transaction.setCustomerProfileId(customerProfileId);

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);

Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
if (result.isOk()) {
   String paymentProfileId = result.getCustomerPaymentProfileIdList().get(0);
}

Edit payment profile:

Transaction transaction = merchant.createCIMTransaction(TransactionType.UPDATE_CUSTOMER_PAYMENT_PROFILE);
transaction.setCustomerProfileId(customerProfileId);

CreditCard creditCard = CreditCard.createCreditCard();
creditCard.setCreditCardNumber("4111 1111 1111 2222");
creditCard.setExpirationMonth("12");
creditCard.setExpirationYear("2022");
Payment payment = Payment.createPayment(creditCard);
PaymentProfile paymentProfile = PaymentProfile.createPaymentProfile();
paymentProfile.setCustomerType(CustomerType.INDIVIDUAL);
paymentProfile.addPayment(payment);
transaction.addPaymentProfile(paymentProfile);
// Add Payment Profile ID you want to update with the new info
paymentProfile.setCustomerPaymentProfileId(paymentProfileId);

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

Now, when you’ve set a payment profile, you can post real transactions, or you can verify it beforehand by generating a test transaction:

Transaction transaction = merchant.createCIMTransaction(TransactionType.VALIDATE_CUSTOMER_PAYMENT_PROFILE);
transaction.setCustomerProfileId(customerProfileId);
transaction.setCustomerPaymentProfileId(paymentProfileId);
transaction.setValidationMode(ValidationModeType.TEST_MODE);

Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
if (result.isOk()) {
   String response = result.getDirectResponseList().get(0);
}

After you are done, you can delete the payment profile, shipping profile, or customer profile with all attached information profiles:

Transaction transaction = merchant.createCIMTransaction(TransactionType.DELETE_CUSTOMER_PROFILE);
transaction.setCustomerProfileId(customerProfileId);
postTransaction(transaction);

With these examples, you’ll be able to issue create, update, and delete requests for all three profile types (customer profile, payment profile, and shipping profile), as they are very similar.

Software Developer