String emailAddress = payer.getPayer().trim();
EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
Customer customer = null;
CustomerAddress customerAddress = null;
CustomerAddress billingAddress = null;
CustomerAddress shippingAddress = null;
String sql = "from Customer customer where custEmail = :custEmail";
Query query = em.createQuery(sql);
query.setParameter("custEmail", emailAddress);
List<?> list = query.getResultList();
if (list.size() > 0) {
customer = (Customer) list.iterator().next();
if (!customer.getActive().equals(Constants.VALUE_YES)) {
throw new PaymentCustomerException("Customer suspended");
}
}
if (customer == null) {
customer = new Customer();
CustomerClass customerClass = (CustomerClass) em.find(CustomerClass.class, payPalExpressCheckOut.getPaymentPaypalCustClassId());
customer.setCustomerClass(customerClass);
customer.setActive(Constants.VALUE_YES);
customer.setRecCreateBy(Constants.USERNAME_CUSTOMER);
customer.setRecCreateDatetime(new Date(System.currentTimeMillis()));
}
else {
customerAddress = customer.getCustAddress();
for (CustomerAddress address : customer.getCustAddresses()) {
if (address.getCustAddressType().equals(Constants.CUSTOMER_ADDRESS_BILLING)) {
billingAddress = address;
continue;
}
if (address.getCustAddressType().equals(Constants.CUSTOMER_ADDRESS_SHIPPING)) {
shippingAddress = address;
continue;
}
}
}
if (customerAddress == null) {
customerAddress = new CustomerAddress();
customerAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_OWN);
customerAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_CUST);
customerAddress.setRecCreateBy(Constants.USERNAME_CUSTOMER);
customerAddress.setRecCreateDatetime(new Date(System.currentTimeMillis()));
customer.getCustAddresses().add(customerAddress);
customer.setCustAddress(customerAddress);
}
if (billingAddress == null) {
billingAddress = new CustomerAddress();
billingAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_CUST);
billingAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_BILLING);
billingAddress.setRecCreateBy(Constants.USERNAME_CUSTOMER);
billingAddress.setRecCreateDatetime(new Date(System.currentTimeMillis()));
customer.getCustAddresses().add(billingAddress);
}
if (shippingAddress == null) {
shippingAddress = new CustomerAddress();
shippingAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_SHIPPING);
shippingAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_CUST);
shippingAddress.setRecCreateBy(Constants.USERNAME_CUSTOMER);
shippingAddress.setRecCreateDatetime(new Date(System.currentTimeMillis()));
customer.getCustAddresses().add(shippingAddress);
}
billingAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_CUST);
billingAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_BILLING);
billingAddress.setRecUpdateBy(Constants.USERNAME_CUSTOMER);
billingAddress.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
shoppingCart.setBillingAddress(billingAddress);
if (billingAddress.getCustAddressId() == null) {
em.persist(billingAddress);
}
shippingAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_CUST);
shippingAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_SHIPPING);
shippingAddress.setRecUpdateBy(Constants.USERNAME_CUSTOMER);
shippingAddress.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
shoppingCart.setShippingAddress(shippingAddress);
if (shippingAddress.getCustAddressId() == null) {
em.persist(shippingAddress);
}
String stateName = Format.getString(payer.getAddress().getStateOrProvince());
State state = Utility.getStateByNameOrCode(site.getSiteId(), stateName);
String countryCode = Format.getString(payer.getAddress().getCountry().toString());
Country country = Utility.getCountryByCode(site.getSiteId(), countryCode);
customer.setSite(site);
customer.setCustPublicName("");
customer.setCustEmail(emailAddress);
customer.setCustSource(Constants.CUSTOMER_SOURCE_PAYPAL);
customer.setCustSourceRef(Format.getString(payer.getPayerID()));
customer.setRecUpdateBy(Constants.USERNAME_CUSTOMER);
customer.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
// customer.setCustPublicName(customer.getCustEmail());
char singleCheckout = contentBean.getSiteDomain().getSite().getSingleCheckout();
if (singleCheckout == Constants.VALUE_YES) {
customer.setSiteDomain(contentBean.getSiteDomain().getSite().getSiteDomainDefault());
}
else {
customer.setSiteDomain(contentBean.getSiteDomain());
}
customerAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_OWN);
customerAddress.setCustAddressType(Constants.CUSTOMER_ADDRESS_CUST);
customerAddress.setCustPrefix(Format.getString(payer.getPayerName().getSalutation()));
customerAddress.setCustFirstName(Format.getString(payer.getPayerName().getFirstName()));
customerAddress.setCustMiddleName(Format.getString(payer.getPayerName().getMiddleName()));
customerAddress.setCustLastName(Format.getString(payer.getPayerName().getLastName()));
customerAddress.setCustSuffix(Format.getString(payer.getPayerName().getSuffix()));
customerAddress.setCustAddressLine1(payer.getAddress().getStreet1());
customerAddress.setCustAddressLine2(payer.getAddress().getStreet2());
customerAddress.setCustCityName(payer.getAddress().getCityName());
customerAddress.setCustStateCode(state.getStateCode());
customerAddress.setCustStateName(state.getStateName());
customerAddress.setCustCountryName(country.getCountryName());
customerAddress.setCustCountryCode(country.getCountryCode());
customerAddress.setCustZipCode("");
customerAddress.setCustPhoneNum(Format.getString(payer.getContactPhone()));
customerAddress.setCustFaxNum("");
// TODO what if state and country and null
customerAddress.setState(state);
customerAddress.setCountry(country);
customerAddress.setRecUpdateBy(Constants.USERNAME_CUSTOMER);
customerAddress.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(customerAddress);
em.persist(customer);
shoppingCart.initCustomer(customer, contentBean);
// Automatically sign-in after Pay Pal confirm
ContentAction.setCustId(request, customer.getCustId());
payerId = Format.getString(payer.getPayerID());
return;
}