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 = FastMap.newInstance();
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 = delegator.findOne("UserLogin", true, UtilMisc.toMap("userLoginId", "system"));
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.setShipmentMethodTypeId(shipMethod.getString("shipmentMethodTypeId"));
cart.setCarrierPartyId(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, BigDecimal.ROUND_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, BigDecimal.ROUND_HALF_UP).toPlainString());
line++;
}
String responseMsg = null;
try {
responseMsg = encoder.encode();