facilityId = productStore.getString("inventoryFacilityId");
} else {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ordersImportFromEbay.productStoreIdIsMandatory", locale));
}
}
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, defaultCurrencyUomId);
// set the external id with the eBay Item Id
String externalId = (String) context.get("externalId");
cart.setOrderType("SALES_ORDER");
cart.setChannelType("EBAY_SALES_CHANNEL");
cart.setUserLogin(userLogin, dispatcher);
cart.setProductStoreId(productStoreId);
if (UtilValidate.isNotEmpty(facilityId)) {
cart.setFacilityId(facilityId);
}
String amountStr = (String) context.get("amountPaid");
BigDecimal amountPaid = BigDecimal.ZERO;
if (UtilValidate.isNotEmpty(amountStr)) {
amountPaid = new BigDecimal(amountStr);
}
cart.addPaymentAmount("EXT_EBAY", amountPaid, externalId, null, true, false, false);
Timestamp orderDate = UtilDateTime.nowTimestamp();
if (UtilValidate.isNotEmpty(context.get("createdDate"))) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date createdDate = dateFormat.parse((String) context.get("createdDate"));
orderDate = new Timestamp(createdDate.getTime());
}
cart.setOrderDate(orderDate);
// Before import the order from eBay to OFBiz is mandatory that the payment has be received
String paidTime = (String) context.get("paidTime");
if (UtilValidate.isEmpty(paidTime)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ordersImportFromEbay.paymentIsStillNotReceived", locale));
}
List<Map<String, Object>> orderItemList = UtilGenerics.checkList(context.get("orderItemList"));
Iterator<Map<String, Object>> orderItemIter = orderItemList.iterator();
while (orderItemIter.hasNext()) {
Map<String, Object> orderItem = orderItemIter.next();
addItem(cart, orderItem, dispatcher, delegator, 0);
}
// set partyId from
if (UtilValidate.isNotEmpty(payToPartyId)) {
cart.setBillFromVendorPartyId(payToPartyId);
}
Map<String, Object> shippingServiceSelectedCtx = UtilGenerics.checkMap(context.get("shippingServiceSelectedCtx"));
if (UtilValidate.isNotEmpty(shippingServiceSelectedCtx.get("shippingServiceCost"))) {
BigDecimal shippingAmount = new BigDecimal(shippingServiceSelectedCtx.get("shippingServiceCost").toString());
if (shippingAmount.doubleValue() > 0) {
GenericValue shippingAdjustment = EbayHelper.makeOrderAdjustment(delegator, "SHIPPING_CHARGES", cart.getOrderId(), null, null, shippingAmount.doubleValue(), 0.0);
if (shippingAdjustment != null) {
cart.addAdjustment(shippingAdjustment);
}
}
}
// Apply additional shipping costs as order adjustment
if (UtilValidate.isNotEmpty(shippingServiceSelectedCtx.get("shippingTotalAdditionalCost"))) {
BigDecimal shippingAdditionalCost = new BigDecimal(shippingServiceSelectedCtx.get("shippingTotalAdditionalCost").toString());
if (shippingAdditionalCost.doubleValue() > 0) {
GenericValue shippingAdjustment = EbayHelper.makeOrderAdjustment(delegator, "MISCELLANEOUS_CHARGE", cart.getOrderId(), null, null, shippingAdditionalCost.doubleValue(), 0.0);
if (shippingAdjustment != null) {
cart.addAdjustment(shippingAdjustment);
}
}
}
// Apply sales tax as order adjustment
Map<String, Object> shippingDetailsCtx = UtilGenerics.checkMap(context.get("shippingDetailsCtx"));
if (UtilValidate.isNotEmpty(shippingDetailsCtx.get("salesTaxAmount"))) {
BigDecimal salesTaxAmount = new BigDecimal(shippingDetailsCtx.get("salesTaxAmount").toString());
if (salesTaxAmount.doubleValue() > 0) {
double salesPercent = 0.0;
if (UtilValidate.isNotEmpty(shippingDetailsCtx.get("salesTaxPercent"))) {
salesPercent = new Double(shippingDetailsCtx.get("salesTaxPercent").toString()).doubleValue();
}
GenericValue salesTaxAdjustment = EbayHelper.makeOrderAdjustment(delegator, "SALES_TAX", cart.getOrderId(), null, null, salesTaxAmount.doubleValue(), salesPercent);
if (salesTaxAdjustment != null) {
cart.addAdjustment(salesTaxAdjustment);
}
}
}
Debug.logInfo("Importing new order from eBay", module);
// set partyId to
String partyId = null;
String contactMechId = null;
Map<String, Object> shippingAddressCtx = UtilGenerics.checkMap(context.get("shippingAddressCtx"));
if (UtilValidate.isNotEmpty(shippingAddressCtx)) {
String buyerName = (String) shippingAddressCtx.get("buyerName");
String firstName = buyerName.substring(0, buyerName.indexOf(" "));
String lastName = buyerName.substring(buyerName.indexOf(" ")+1);
String country = (String) shippingAddressCtx.get("shippingAddressCountry");
String state = (String) shippingAddressCtx.get("shippingAddressStateOrProvince");
String city = (String) shippingAddressCtx.get("shippingAddressCityName");
EbayHelper.correctCityStateCountry(dispatcher, shippingAddressCtx, city, state, country);
String shippingAddressStreet = null;
if (UtilValidate.isEmpty(shippingAddressCtx.get("shippingAddressStreet1"))) {
shippingAddressStreet = shippingAddressCtx.get("shippingAddressStreet").toString();
shippingAddressCtx.put("shippingAddressStreet1", shippingAddressStreet);
} else {
shippingAddressStreet = shippingAddressCtx.get("shippingAddressStreet1").toString();
}
List<GenericValue> shipInfo = PartyWorker.findMatchingPersonPostalAddresses(delegator, shippingAddressStreet,
(UtilValidate.isEmpty(shippingAddressCtx.get("shippingAddressStreet2")) ? null : shippingAddressCtx.get("shippingAddressStreet2").toString()), shippingAddressCtx.get("city").toString(), shippingAddressCtx.get("stateProvinceGeoId").toString(),
shippingAddressCtx.get("shippingAddressPostalCode").toString(), null, shippingAddressCtx.get("countryGeoId").toString(), firstName, null, lastName);
if (UtilValidate.isNotEmpty(shipInfo)) {
GenericValue first = EntityUtil.getFirst(shipInfo);
partyId = first.getString("partyId");
Debug.logInfo("Existing shipping address found for : (party: " + partyId + ")", module);
}
}
// If matching party not found then try to find partyId from PartyAttribute entity.
GenericValue partyAttribute = null;
if (UtilValidate.isNotEmpty(context.get("eiasTokenBuyer"))) {
partyAttribute = EntityUtil.getFirst(delegator.findByAnd("PartyAttribute", UtilMisc.toMap("attrValue", (String) context.get("eiasTokenBuyer"))));
if (UtilValidate.isNotEmpty(partyAttribute)) {
partyId = (String) partyAttribute.get("partyId");
}
}
// if we get a party, check its contact information.
if (UtilValidate.isNotEmpty(partyId)) {
Debug.logInfo("Found existing party associated to the eBay buyer: " + partyId, module);
GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
contactMechId = EbayHelper.setShippingAddressContactMech(dispatcher, delegator, party, userLogin, shippingAddressCtx);
String emailBuyer = (String) context.get("emailBuyer");
if (!(emailBuyer.equals("") || emailBuyer.equalsIgnoreCase("Invalid Request"))) {
EbayHelper.setEmailContactMech(dispatcher, delegator, party, userLogin, context);
}
EbayHelper.setPhoneContactMech(dispatcher, delegator, party, userLogin, shippingAddressCtx);
}
// create party if none exists already
if (UtilValidate.isEmpty(partyId)) {
Debug.logInfo("Creating new party for the eBay buyer.", module);
partyId = EbayHelper.createCustomerParty(dispatcher, (String) shippingAddressCtx.get("buyerName"), userLogin);
if (UtilValidate.isEmpty(partyId)) {
Debug.logWarning("Using admin party for the eBay buyer.", module);
partyId = "admin";
}
}
// create new party's contact information
if (UtilValidate.isEmpty(contactMechId)) {
Debug.logInfo("Creating new postal address for party: " + partyId, module);
contactMechId = EbayHelper.createAddress(dispatcher, partyId, userLogin, "SHIPPING_LOCATION", shippingAddressCtx);
if (UtilValidate.isEmpty(contactMechId)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "EbayStoreUnableToCreatePostalAddress", locale) + shippingAddressCtx);
}
Debug.logInfo("Created postal address: " + contactMechId, module);
Debug.logInfo("Creating new phone number for party: " + partyId, module);
EbayHelper.createPartyPhone(dispatcher, partyId, (String) shippingAddressCtx.get("shippingAddressPhone"), userLogin);
Debug.logInfo("Creating association to eBay buyer for party: " + partyId, module);
EbayHelper.createEbayCustomer(dispatcher, partyId, (String) context.get("ebayUserIdBuyer"), null, userLogin);
String emailBuyer = (String) context.get("emailBuyer");
if (UtilValidate.isNotEmpty(emailBuyer) && !emailBuyer.equalsIgnoreCase("Invalid Request")) {
Debug.logInfo("Creating new email for party: " + partyId, module);
EbayHelper.createPartyEmail(dispatcher, partyId, emailBuyer, userLogin);
}
}
Debug.logInfo("Setting cart roles for party: " + partyId, module);
cart.setBillToCustomerPartyId(partyId);
cart.setPlacingCustomerPartyId(partyId);
cart.setShipToCustomerPartyId(partyId);
cart.setEndUserCustomerPartyId(partyId);
Debug.logInfo("Setting contact mech in cart: " + contactMechId, module);
cart.setShippingContactMechId(contactMechId);
cart.setMaySplit(Boolean.FALSE);
Debug.logInfo("Setting shipment method: " + (String) shippingServiceSelectedCtx.get("shippingService"), module);
EbayHelper.setShipmentMethodType(cart, (String) shippingServiceSelectedCtx.get("shippingService"), productStoreId, delegator);
cart.makeAllShipGroupInfos();
// create the order
Debug.logInfo("Creating CheckOutHelper.", module);
CheckOutHelper checkout = new CheckOutHelper(dispatcher, delegator, cart);
Debug.logInfo("Creating order.", module);
Map<?, ?> orderCreate = checkout.createOrder(userLogin);
if ("error".equals(orderCreate.get("responseMessage"))) {
List<String> errorMessageList = UtilGenerics.checkList(orderCreate.get("errorMessageList"), String.class);
return ServiceUtil.returnError(errorMessageList);
}
String orderId = (String) orderCreate.get("orderId");
Debug.logInfo("Created order with id: " + orderId, module);
// approve the order
if (UtilValidate.isNotEmpty(orderId)) {
Debug.logInfo("Approving order with id: " + orderId, module);
boolean approved = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
Debug.logInfo("Order approved with result: " + approved, module);
// create the payment from the preference
if (approved) {
Debug.logInfo("Creating payment for approved order.", module);
EbayHelper.createPaymentFromPaymentPreferences(delegator, dispatcher, userLogin, orderId, externalId, cart.getOrderDate(), amountPaid, partyId);
Debug.logInfo("Payment created.", module);
}
result = ServiceUtil.returnFailure("Order created successfully with ID (" + orderId + ") & eBay Order ID associated with this order is (" + externalId + ").");
}
} catch (Exception e) {