use of org.apache.ofbiz.order.shoppingcart.shipping.ShippingEstimateWrapper in project ofbiz-framework by apache.
the class PayPalServices method payPalCheckoutUpdate.
public static Map<String, Object> payPalCheckoutUpdate(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String token = (String) paramMap.get("TOKEN");
WeakReference<ShoppingCart> weakCart = tokenCartMap.get(new TokenWrapper(token));
ShoppingCart cart = null;
if (weakCart != null) {
cart = weakCart.get();
}
if (cart == null) {
Debug.logError("Could locate the ShoppingCart for token " + token, module);
return ServiceUtil.returnSuccess();
}
// Since most if not all of the shipping estimate codes requires a persisted contactMechId we'll create one and
// then delete once we're done, now is not the time to worry about updating everything
String contactMechId = null;
Map<String, Object> inMap = new HashMap<String, Object>();
inMap.put("address1", paramMap.get("SHIPTOSTREET"));
inMap.put("address2", paramMap.get("SHIPTOSTREET2"));
inMap.put("city", paramMap.get("SHIPTOCITY"));
String countryGeoCode = (String) paramMap.get("SHIPTOCOUNTRY");
String countryGeoId = PayPalServices.getCountryGeoIdFromGeoCode(countryGeoCode, delegator);
if (countryGeoId == null) {
return ServiceUtil.returnSuccess();
}
inMap.put("countryGeoId", countryGeoId);
inMap.put("stateProvinceGeoId", parseStateProvinceGeoId((String) paramMap.get("SHIPTOSTATE"), countryGeoId, delegator));
inMap.put("postalCode", paramMap.get("SHIPTOZIP"));
try {
GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").cache().queryOne();
inMap.put("userLogin", userLogin);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
boolean beganTransaction = false;
Transaction parentTransaction = null;
try {
parentTransaction = TransactionUtil.suspend();
beganTransaction = TransactionUtil.begin();
} catch (GenericTransactionException e1) {
Debug.logError(e1, module);
}
try {
Map<String, Object> outMap = dispatcher.runSync("createPostalAddress", inMap);
contactMechId = (String) outMap.get("contactMechId");
} catch (GenericServiceException e) {
Debug.logError(e.getMessage(), module);
return ServiceUtil.returnSuccess();
}
try {
TransactionUtil.commit(beganTransaction);
if (parentTransaction != null)
TransactionUtil.resume(parentTransaction);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
// clone the cart so we can modify it temporarily
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
String oldShipAddress = cart.getShippingContactMechId();
coh.setCheckOutShippingAddress(contactMechId);
ShippingEstimateWrapper estWrapper = new ShippingEstimateWrapper(dispatcher, cart, 0);
int line = 0;
NVPEncoder encoder = new NVPEncoder();
encoder.add("METHOD", "CallbackResponse");
for (GenericValue shipMethod : estWrapper.getShippingMethods()) {
BigDecimal estimate = estWrapper.getShippingEstimate(shipMethod);
// Check that we have a valid estimate (allowing zero value estimates for now)
if (estimate == null || estimate.compareTo(BigDecimal.ZERO) < 0) {
continue;
}
cart.setAllShipmentMethodTypeId(shipMethod.getString("shipmentMethodTypeId"));
cart.setAllCarrierPartyId(shipMethod.getString("partyId"));
try {
coh.calcAndAddTax();
} catch (GeneralException e) {
Debug.logError(e, module);
continue;
}
String estimateLabel = shipMethod.getString("partyId") + " - " + shipMethod.getString("description");
encoder.add("L_SHIPINGPOPTIONLABEL" + line, estimateLabel);
encoder.add("L_SHIPPINGOPTIONAMOUNT" + line, estimate.setScale(2, RoundingMode.HALF_UP).toPlainString());
// Just make this first one default for now
encoder.add("L_SHIPPINGOPTIONISDEFAULT" + line, line == 0 ? "true" : "false");
encoder.add("L_TAXAMT" + line, cart.getTotalSalesTax().setScale(2, RoundingMode.HALF_UP).toPlainString());
line++;
}
String responseMsg = null;
try {
responseMsg = encoder.encode();
} catch (PayPalException e) {
Debug.logError(e, module);
}
if (responseMsg != null) {
try {
response.setContentLength(responseMsg.getBytes("UTF-8").length);
} catch (UnsupportedEncodingException e) {
Debug.logError(e, module);
}
try (Writer writer = response.getWriter()) {
writer.write(responseMsg);
} catch (IOException e) {
Debug.logError(e, module);
}
}
// Remove the temporary ship address
try {
GenericValue postalAddress = EntityQuery.use(delegator).from("PostalAddress").where("contactMechId", contactMechId).queryOne();
postalAddress.remove();
GenericValue contactMech = EntityQuery.use(delegator).from("ContactMech").where("contactMechId", contactMechId).queryOne();
contactMech.remove();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
coh.setCheckOutShippingAddress(oldShipAddress);
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.order.shoppingcart.shipping.ShippingEstimateWrapper in project ofbiz-framework by apache.
the class ShoppingCart method setDefaultCheckoutOptions.
// Preset with default values some of the checkout options to get a quicker checkout process.
public void setDefaultCheckoutOptions(LocalDispatcher dispatcher) {
// skip the add party screen
this.setAttribute("addpty", "Y");
if ("SALES_ORDER".equals(getOrderType())) {
// set as the default shipping location the first from the list of available shipping locations
if (this.getPartyId() != null && !"_NA_".equals(this.getPartyId())) {
try {
GenericValue orderParty = this.getDelegator().findOne("Party", UtilMisc.toMap("partyId", this.getPartyId()), false);
Collection<GenericValue> shippingContactMechList = ContactHelper.getContactMech(orderParty, "SHIPPING_LOCATION", "POSTAL_ADDRESS", false);
if (UtilValidate.isNotEmpty(shippingContactMechList)) {
GenericValue shippingContactMech = (shippingContactMechList.iterator()).next();
this.setAllShippingContactMechId(shippingContactMech.getString("contactMechId"));
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error setting shippingContactMechId in setDefaultCheckoutOptions() method.", module);
}
}
// set the default shipment method
ShippingEstimateWrapper shipEstimateWrapper = org.apache.ofbiz.order.shoppingcart.shipping.ShippingEstimateWrapper.getWrapper(dispatcher, this, 0);
GenericValue carrierShipmentMethod = EntityUtil.getFirst(shipEstimateWrapper.getShippingMethods());
if (carrierShipmentMethod != null) {
this.setAllShipmentMethodTypeId(carrierShipmentMethod.getString("shipmentMethodTypeId"));
this.setAllCarrierPartyId(carrierShipmentMethod.getString("partyId"));
}
} else {
// checkout options for purchase orders
// TODO: should we select a default agreement? For now we don't do this.
// skip the order terms selection step
this.setOrderTermSet(true);
// set as the default shipping location the first from the list of available shipping locations
String companyId = this.getBillToCustomerPartyId();
if (companyId != null) {
// the facilityId should be set prior to triggering default options, otherwise we do not set up facility information
String defaultFacilityId = getFacilityId();
if (defaultFacilityId != null) {
GenericValue facilityContactMech = ContactMechWorker.getFacilityContactMechByPurpose(this.getDelegator(), facilityId, UtilMisc.toList("SHIPPING_LOCATION", "PRIMARY_LOCATION"));
if (facilityContactMech != null) {
this.setShippingContactMechId(0, facilityContactMech.getString("contactMechId"));
}
}
}
// shipping options
this.setAllShipmentMethodTypeId("NO_SHIPPING");
this.setAllCarrierPartyId("_NA_");
this.setAllShippingInstructions("");
this.setAllGiftMessage("");
this.setAllMaySplit(Boolean.TRUE);
this.setAllIsGift(Boolean.FALSE);
}
}
Aggregations