From e017e769ccfe822a3e614032ac00c3344a5c341a Mon Sep 17 00:00:00 2001 From: au Date: Tue, 5 Jul 2016 20:24:23 +0300 Subject: [PATCH] added missing elements for creating ARB transaction based on CIM profile --- .../java/net/authorize/arb/Transaction.java | 21 ++++- .../java/net/authorize/data/arb/Profile.java | 81 +++++++++++++++++++ .../net/authorize/data/arb/Subscription.java | 20 +++++ .../arb/functional_test/ARBTest.java | 50 ++++++++++-- 4 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/authorize/data/arb/Profile.java diff --git a/src/main/java/net/authorize/arb/Transaction.java b/src/main/java/net/authorize/arb/Transaction.java index d80d96e1..3757eb89 100644 --- a/src/main/java/net/authorize/arb/Transaction.java +++ b/src/main/java/net/authorize/arb/Transaction.java @@ -12,6 +12,7 @@ import net.authorize.data.xml.BankAccount; import net.authorize.data.xml.Payment; import net.authorize.util.BasicXmlDocument; +import net.authorize.util.StringUtils; import net.authorize.util.XmlUtility; import org.w3c.dom.Element; @@ -172,8 +173,24 @@ private void addSubscriptionToRequest(BasicXmlDocument document, Subscription su subscr_el.appendChild(trial_el); } - addPaymentToSubscription(document, subscription, subscr_el); - addBillingInfoToSubscription(document, subscription, subscr_el); + if (subscription.getProfile() != null && StringUtils.isNotEmpty(subscription.getProfile().getCustomerProfileId()) && StringUtils.isNotEmpty(subscription.getProfile().getCustomerPaymentProfileId())) { + Element profile = document.createElement(AuthNetField.ELEMENT_PROFILE.getFieldName()); + Element customerProfileId = document.createElement(AuthNetField.ELEMENT_CUSTOMER_PROFILE_ID.getFieldName()); + customerProfileId.appendChild(document.getDocument().createTextNode(subscription.getProfile().getCustomerProfileId())); + profile.appendChild(customerProfileId); + Element customerPaymentProfileId = document.createElement(AuthNetField.ELEMENT_CUSTOMER_PAYMENT_PROFILE_ID.getFieldName()); + customerPaymentProfileId.appendChild(document.getDocument().createTextNode(subscription.getProfile().getCustomerPaymentProfileId())); + profile.appendChild(customerPaymentProfileId); + if (StringUtils.isNotEmpty(subscription.getProfile().getCustomerAddressId())) { + Element customerAddressId = document.createElement(AuthNetField.ELEMENT_CUSTOMER_ADDRESS_ID.getFieldName()); + customerAddressId.appendChild(document.getDocument().createTextNode(subscription.getProfile().getCustomerAddressId())); + profile.appendChild(customerAddressId); + } + subscr_el.appendChild(profile); + } else { + addPaymentToSubscription(document, subscription, subscr_el); + addBillingInfoToSubscription(document, subscription, subscr_el); + } document.getDocumentElement().appendChild(subscr_el); } diff --git a/src/main/java/net/authorize/data/arb/Profile.java b/src/main/java/net/authorize/data/arb/Profile.java new file mode 100644 index 00000000..d62c71d5 --- /dev/null +++ b/src/main/java/net/authorize/data/arb/Profile.java @@ -0,0 +1,81 @@ +package net.authorize.data.arb; + +import net.authorize.data.xml.Address; + +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; +import java.util.ArrayList; + +@XmlRootElement +public class Profile implements Serializable { + private static final long serialVersionUID = 1L; + + protected String customerProfileId; + protected String customerPaymentProfileId; + protected String customerAddressId; + + private Profile() { } + + /** + * Create a new CustomerProfile object + * + * @return Profile + */ + public static Profile createProfile() { + return new Profile(); + } + + /** + * Get the customer profile id. + * + * @return the id + */ + public String getCustomerProfileId() { + return customerProfileId; + } + + /** + * Set the customer profile id. + * + * @param id the id to set + */ + public void setCustomerProfileId(String id) { + this.customerProfileId = id; + } + + /** + * Get the customer payment profile id. + * + * @return the id + */ + public String getCustomerPaymentProfileId() { + return customerPaymentProfileId; + } + + /** + * Set the customer payment profile id. + * + * @param id the id to set + */ + public void setCustomerPaymentProfileId(String id) { + this.customerPaymentProfileId = id; + } + + /** + * Get the customer payment profile id. + * + * @return the id + */ + public String getCustomerAddressId() { + return customerAddressId; + } + + /** + * Set the customer payment profile id. + * + * @param id the id to set + */ + public void setCustomerAddressId(String id) { + this.customerAddressId = id; + } +} diff --git a/src/main/java/net/authorize/data/arb/Subscription.java b/src/main/java/net/authorize/data/arb/Subscription.java index 3d475d21..4afb89c6 100644 --- a/src/main/java/net/authorize/data/arb/Subscription.java +++ b/src/main/java/net/authorize/data/arb/Subscription.java @@ -7,6 +7,7 @@ import net.authorize.arb.Transaction; import net.authorize.data.Order; +import net.authorize.data.cim.CustomerProfile; import net.authorize.data.xml.Customer; import net.authorize.data.xml.Payment; @@ -25,6 +26,7 @@ public class Subscription implements Serializable { private BigDecimal trial_amount = Transaction.ZERO_AMOUNT; private Payment payment = null; private Customer customer; + private Profile profile; private String refId = null; private Order order = null; @@ -80,6 +82,24 @@ public void setCustomer(Customer customer) { this.customer = customer; } + /** + * Get the profile container. + * + * @return Profile + */ + public Profile getProfile() { + return profile; + } + + /** + * Set the profile container. + * + * @param profile + */ + public void setProfile(Profile profile) { + this.profile = profile; + } + /** * Get the subscription amount. * diff --git a/src/test/java/net/authorize/arb/functional_test/ARBTest.java b/src/test/java/net/authorize/arb/functional_test/ARBTest.java index d1414164..0cd57b5f 100644 --- a/src/test/java/net/authorize/arb/functional_test/ARBTest.java +++ b/src/test/java/net/authorize/arb/functional_test/ARBTest.java @@ -1,23 +1,22 @@ package net.authorize.arb.functional_test; import java.math.BigDecimal; +import java.util.Arrays; +import java.util.Random; import net.authorize.Transaction; import net.authorize.UnitTestData; import net.authorize.arb.Result; import net.authorize.arb.TransactionType; +import net.authorize.cim.ValidationModeType; import net.authorize.data.Order; -import net.authorize.data.arb.PaymentSchedule; -import net.authorize.data.arb.Subscription; -import net.authorize.data.arb.SubscriptionStatusType; -import net.authorize.data.arb.SubscriptionUnitType; +import net.authorize.data.arb.*; +import net.authorize.data.cim.CustomerProfile; +import net.authorize.data.cim.PaymentProfile; import net.authorize.data.creditcard.CreditCard; import net.authorize.data.echeck.BankAccountType; import net.authorize.data.echeck.ECheckType; -import net.authorize.data.xml.Address; -import net.authorize.data.xml.BankAccount; -import net.authorize.data.xml.Customer; -import net.authorize.data.xml.Payment; +import net.authorize.data.xml.*; import net.authorize.util.XmlUtility; import org.junit.Assert; @@ -204,4 +203,39 @@ private net.authorize.arb.Result createCreditCardSubscription() { return result; } + + @Test + public void createCimSubscription() throws InterruptedException { + // Create a new subscription request from the subscription object + // + net.authorize.cim.Transaction transactionCim = merchant.createCIMTransaction(net.authorize.cim.TransactionType.CREATE_CUSTOMER_PROFILE); + transactionCim.setRefId("REF:" + System.currentTimeMillis()); + + CustomerProfile customerProfile = CustomerProfile.createCustomerProfile(); + customerProfile.setMerchantCustomerId(Integer.toString(new Random().nextInt(10000))); + transactionCim.setCustomerProfile(customerProfile); + + PaymentProfile paymentProfile = PaymentProfile.createPaymentProfile(); + paymentProfile.setBillTo(billing_info); + paymentProfile.setCustomerType(CustomerType.INDIVIDUAL); + paymentProfile.addPayment(Payment.createPayment(credit_card)); + transactionCim.addPaymentProfile(paymentProfile); + transactionCim.setValidationMode(ValidationModeType.TEST_MODE); + net.authorize.cim.Result resultCim = (net.authorize.cim.Result) merchant.postTransaction(transactionCim); + Profile profile = Profile.createProfile(); + profile.setCustomerProfileId(resultCim.getCustomerProfileId()); + profile.setCustomerPaymentProfileId(resultCim.getCustomerPaymentProfileIdList().get(0)); + + subscription.setProfile(profile); + subscription.setPayment(null); + subscription.setCustomer(null); + Thread.sleep(30000); //wait for authorize to create CIM profile + net.authorize.arb.Transaction transaction = merchant.createARBTransaction(TransactionType.CREATE_SUBSCRIPTION, subscription); + net.authorize.arb.Result result = (net.authorize.arb.Result)merchant.postTransaction(transaction); + Assert.assertNotNull(result); + result.printMessages(); + Assert.assertTrue(result.isOk()); + Assert.assertNotNull(result.getResultSubscriptionId()); + } + }