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();
} 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);
writer.close();
} catch (IOException e) {
Debug.logError(e, module);
}
}
// Remove the temporary ship address
try {
GenericValue postalAddress = delegator.findOne("PostalAddress", false, UtilMisc.toMap("contactMechId", contactMechId));
postalAddress.remove();
GenericValue contactMech = delegator.findOne("ContactMech", false, UtilMisc.toMap("contactMechId", contactMechId));
contactMech.remove();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
coh.setCheckOutShippingAddress(oldShipAddress);
return ServiceUtil.returnSuccess();
}