In the previous post I’ve told how to bill a customer with a 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 customer’s billing and shipping profiles. Each of these profiles you can update, delete or create a new one. Now let’s see that actions in examples.
First of all, to issue request 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:
1
2
3
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 Merchant’s postTransaction(Transaction transaction) method.
You can create solely customer profile (it will store customer’s billing
and shipping information in the future):
1
2
3
4
5
6
7
8
9
10
11
12
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
1
2
3
4
5
6
7
8
9
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 payment profile, shipping profile or
customer profile with all attached information profiles:
1
2
3
Transaction transaction = merchant.createCIMTransaction(TransactionType.DELETE_CUSTOMER_PROFILE);
transaction.setCustomerProfileId(customerProfileId);
postTransaction(transaction);
With these example you’ll be able to issue create, update and delete requests to all three types of profiles (customer profile, payment profile and shipping profile) as they are very similar. To have some reference to rely on, check Authorize.Net SOAP API Guide.