in ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStoreOrder.java [97:308]
public static Map<String, Object> EbayStoreCreateTransactionShoppingCart(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Map<String, Object> result = new HashMap<>();
String productStoreId = context.get("productStoreId").toString();
String defaultCurrencyUomId = "";
String payToPartyId = "";
String facilityId = "";
try {
if (UtilValidate.isEmpty(productStoreId)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "ordersImportFromEbay.productStoreIdIsMandatory", locale));
} else {
GenericValue productStore = EntityQuery.use(delegator).from("ProductStore").where("productStoreId", productStoreId).queryOne();
if (productStore != null) {
defaultCurrencyUomId = productStore.getString("defaultCurrencyUomId");
payToPartyId = productStore.getString("payToPartyId");
facilityId = productStore.getString("inventoryFacilityId");
} else {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "ordersImportFromEbay.productStoreIdIsMandatory", locale));
}
}
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, defaultCurrencyUomId);
String externalId = context.get("externalId").toString();
if (UtilValidate.isNotEmpty(externalId)) {
cart.setExternalId(externalId);
} else {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "EbayStoreOrder.externalIdNotAvailable", locale));
}
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 = context.get("amountPaid").toString();
BigDecimal amountPaid = new BigDecimal(amountStr);
if (UtilValidate.isNotEmpty(amountPaid)) {
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);
String productId = context.get("productId").toString();
if (UtilValidate.isEmpty(productId)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "ordersImportFromEbay.productIdNotAvailable", locale));
} else {
GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
if (UtilValidate.isEmpty(product)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "ordersImportFromEbay.productIdDoesNotExist", locale));
}
}
if (UtilValidate.isEmpty(context.get("paidTime"))) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "ordersImportFromEbay.paymentIsStillNotReceived", locale));
}
BigDecimal unitPrice = new BigDecimal(context.get("transactionPrice").toString());
BigDecimal quantity = new BigDecimal(context.get("quantityPurchased").toString());
cart.addItemToEnd(productId, null, quantity, unitPrice, null, null, null, "PRODUCT_ORDER_ITEM", dispatcher, Boolean.FALSE, Boolean.FALSE);
if (UtilValidate.isNotEmpty(payToPartyId)) {
cart.setBillFromVendorPartyId(payToPartyId);
}
String shippingCost = context.get("shippingServiceCost").toString();
if (UtilValidate.isNotEmpty(shippingCost)) {
BigDecimal shippingAmount = new BigDecimal(shippingCost);
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);
}
}
}
String shippingTotalAdditionalCost = context.get("shippingTotalAdditionalCost").toString();
if (UtilValidate.isNotEmpty(shippingTotalAdditionalCost)) {
double shippingAdditionalCost = Double.parseDouble(shippingTotalAdditionalCost);
if (shippingAdditionalCost > 0) {
GenericValue shippingAdjustment = EbayHelper.makeOrderAdjustment(delegator, "MISCELLANEOUS_CHARGE", cart.getOrderId(), null, null, shippingAdditionalCost, 0.0);
if (shippingAdjustment != null) {
cart.addAdjustment(shippingAdjustment);
}
}
}
String salesTaxAmount = context.get("salesTaxAmount").toString();
String salesTaxPercent = context.get("salesTaxPercent").toString();
if (UtilValidate.isNotEmpty(salesTaxAmount)) {
double salesTaxAmountTotal = Double.parseDouble(salesTaxAmount);
if (salesTaxAmountTotal > 0) {
double salesPercent = 0.0;
if (UtilValidate.isNotEmpty(salesTaxPercent)) {
salesPercent = Double.parseDouble(salesTaxPercent);
}
GenericValue salesTaxAdjustment = EbayHelper.makeOrderAdjustment(delegator, "SALES_TAX", cart.getOrderId(), null, null, salesTaxAmountTotal, salesPercent);
if (salesTaxAdjustment != null) {
cart.addAdjustment(salesTaxAdjustment);
}
}
}
Debug.logInfo("Importing new order from eBay", MODULE);
// set partyId to
String partyId = null;
String contactMechId = "";
GenericValue partyAttribute = null;
if (UtilValidate.isNotEmpty(context.get("eiasTokenBuyer").toString())) {
partyAttribute = EntityQuery.use(delegator).from("PartyAttribute").where("attrValue", context.get("eiasTokenBuyer").toString()).queryFirst();
}
// if we get a party, check its contact information.
if (partyAttribute != null) {
partyId = (String) partyAttribute.get("partyId");
Debug.logInfo("Found existing party associated to the eBay buyer: " + partyId, MODULE);
GenericValue party = EntityQuery.use(delegator).from("Party").where("partyId", partyId).queryOne();
contactMechId = EbayHelper.setShippingAddressContactMech(dispatcher, delegator, party, userLogin, context);
String emailBuyer = context.get("emailBuyer").toString();
if (!("".equals(emailBuyer) || "Invalid Request".equalsIgnoreCase(emailBuyer))) {
EbayHelper.setEmailContactMech(dispatcher, delegator, party, userLogin, context);
}
EbayHelper.setPhoneContactMech(dispatcher, delegator, party, userLogin, context);
}
// create party if none exists already
if (UtilValidate.isEmpty(partyId)) {
Debug.logInfo("Creating new party for the eBay buyer.", MODULE);
partyId = EbayHelper.createCustomerParty(dispatcher, context.get("buyerName").toString(), 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", context);
if (UtilValidate.isEmpty(contactMechId)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(RESOURCE, "EbayStoreUnableToCreatePostalAddress", locale) + context);
}
Debug.logInfo("Created postal address: " + contactMechId, MODULE);
Debug.logInfo("Creating new phone number for party: " + partyId, MODULE);
EbayHelper.createPartyPhone(dispatcher, partyId, context.get("shippingAddressPhone").toString(), userLogin);
Debug.logInfo("Creating association to eBay buyer for party: " + partyId, MODULE);
EbayHelper.createEbayCustomer(dispatcher, partyId, context.get("ebayUserIdBuyer").toString(), context.get("eiasTokenBuyer").toString(), userLogin);
String emailBuyer = context.get("emailBuyer").toString();
if (UtilValidate.isNotEmpty(emailBuyer) && !"Invalid Request".equalsIgnoreCase(emailBuyer)) {
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.setAllShippingContactMechId(contactMechId);
cart.setAllMaySplit(Boolean.FALSE);
Debug.logInfo("Setting shipment method: " + context.get("shippingService").toString(), MODULE);
EbayHelper.setShipmentMethodType(cart, context.get("shippingService").toString(), productStoreId, delegator);
cart.makeAllShipGroupInfos(dispatcher);
// create the order
Debug.logInfo("Creating CheckOutHelper.", MODULE);
CheckOutHelper checkout = new CheckOutHelper(dispatcher, delegator, cart);
Debug.logInfo("Creating order.", MODULE);
Map<String, Object> orderCreate = checkout.createOrder(userLogin);
String orderId = orderCreate.get("orderId").toString();
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);
}
}
} catch (Exception e) {
result = ServiceUtil.returnFailure(e.getMessage());
}
return result;
}