📄 uspsservices.java
字号:
} double actualTransportCost = 0; String carrierDeliveryZone = null; String carrierRestrictionCodes = null; String carrierRestrictionDesc = null; // send a new request for each package for (Iterator i = shipmentPackageRouteSegList.iterator(); i.hasNext();) { GenericValue shipmentPackageRouteSeg = (GenericValue) i.next(); String sprsKeyString = "[" + shipmentPackageRouteSeg.getString("shipmentId") + "," + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + "," + shipmentPackageRouteSeg.getString("shipmentRouteSegmentId") + "]"; Document requestDocument = createUspsRequestDocument("RateRequest"); Element packageElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "Package", requestDocument); packageElement.setAttribute("ID", "0"); UtilXml.addChildElementValue(packageElement, "Service", serviceType, requestDocument); UtilXml.addChildElementValue(packageElement, "ZipOrigination", originZip, requestDocument); UtilXml.addChildElementValue(packageElement, "ZipDestination", destinationZip, requestDocument); GenericValue shipmentPackage = null; shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage"); String spKeyString = "[" + shipmentPackage.getString("shipmentId") + "," + shipmentPackage.getString("shipmentPackageSeqId") + "]"; // weight elements - Pounds, Ounces String weightStr = shipmentPackage.getString("weight"); if (UtilValidate.isEmpty(weightStr)) { return ServiceUtil.returnError("weight not found for ShipmentPackage " + spKeyString); } double weight = 0; try { weight = Double.parseDouble(weightStr); } catch (NumberFormatException nfe) { nfe.printStackTrace(); // TODO: handle exception } String weightUomId = shipmentPackage.getString("weightUomId"); if (UtilValidate.isEmpty(weightUomId)) { weightUomId = "WT_lb"; // assume weight is in pounds } if (!"WT_lb".equals(weightUomId)) { // attempt a conversion to pounds Map result = new HashMap(); try { result = dispatcher.runSync("convertUom", UtilMisc.toMap("uomId", weightUomId, "uomIdTo", "WT_lb", "originalValue", new Double(weight))); } catch (GenericServiceException ex) { return ServiceUtil.returnError(ex.getMessage()); } if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) { weight *= ((Double) result.get("convertedValue")).doubleValue(); } else { return ServiceUtil.returnError("Unsupported weightUom [" + weightUomId + "] for ShipmentPackage " + spKeyString + ", could not find a conversion factor for WT_lb"); } } double weightPounds = Math.floor(weight); double weightOunces = Math.ceil(weight % weightPounds * 16); DecimalFormat df = new DecimalFormat("#"); UtilXml.addChildElementValue(packageElement, "Pounds", df.format(weightPounds), requestDocument); UtilXml.addChildElementValue(packageElement, "Ounces", df.format(weightOunces), requestDocument); // Container element GenericValue carrierShipmentBoxType = null; List carrierShipmentBoxTypes = null; carrierShipmentBoxTypes = shipmentPackage.getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "USPS"), null); if (carrierShipmentBoxTypes.size() > 0) { carrierShipmentBoxType = (GenericValue) carrierShipmentBoxTypes.get(0); } if (carrierShipmentBoxType != null && UtilValidate.isNotEmpty(carrierShipmentBoxType.getString("packagingTypeCode"))) { UtilXml.addChildElementValue(packageElement, "Container", carrierShipmentBoxType.getString("packagingTypeCode"), requestDocument); } else { // default to "None", for customers using their own package UtilXml.addChildElementValue(packageElement, "Container", "None", requestDocument); } // Size element if (carrierShipmentBoxType != null && UtilValidate.isNotEmpty("oversizeCode")) { UtilXml.addChildElementValue(packageElement, "Size", carrierShipmentBoxType.getString("oversizeCode"), requestDocument); } else { // default to "Regular", length + girth measurement <= 84 inches UtilXml.addChildElementValue(packageElement, "Size", "Regular", requestDocument); } // Although only applicable for Parcel Post, this tag is required for all requests UtilXml.addChildElementValue(packageElement, "Machinable", "False", requestDocument); Document responseDocument = null; try { responseDocument = sendUspsRequest("Rate", requestDocument); } catch (UspsRequestException e) { Debug.log(e, module); return ServiceUtil.returnError("Error sending request for USPS Domestic Rate Calculation service: " + e.getMessage()); } Element respPackageElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "Package"); if (respPackageElement == null) { return ServiceUtil.returnError("Incomplete response from USPS Domestic Rate Calculation service: " + "no Package element found"); } Element respErrorElement = UtilXml.firstChildElement(respPackageElement, "Error"); if (respErrorElement != null) { return ServiceUtil.returnError("The following error was returned by the USPS Domestic Rate Calculation " + "service for ShipmentPackage " + spKeyString + ": " + UtilXml.childElementValue(respErrorElement, "Description")); } // update the ShipmentPackageRouteSeg String postageString = UtilXml.childElementValue(respPackageElement, "Postage"); if (UtilValidate.isEmpty(postageString)) { return ServiceUtil.returnError("Incomplete response from USPS Domestic Rate Calculation service: " + "missing or empty Postage element"); } double postage = 0; try { postage = Double.parseDouble(postageString); } catch (NumberFormatException nfe) { nfe.printStackTrace(); // TODO: handle exception } actualTransportCost += postage; shipmentPackageRouteSeg.setString("packageTransportCost", postageString); shipmentPackageRouteSeg.store(); // if this is the last package, get the zone and APO/FPO restrictions for the ShipmentRouteSegment if (!i.hasNext()) { carrierDeliveryZone = UtilXml.childElementValue(respPackageElement, "Zone"); carrierRestrictionCodes = UtilXml.childElementValue(respPackageElement, "RestrictionCodes"); carrierRestrictionDesc = UtilXml.childElementValue(respPackageElement, "RestrictionDescription"); } } // update the ShipmentRouteSegment shipmentRouteSegment.set("carrierDeliveryZone", carrierDeliveryZone); shipmentRouteSegment.set("carrierRestrictionCodes", carrierRestrictionCodes); shipmentRouteSegment.set("carrierRestrictionDesc", carrierRestrictionDesc); shipmentRouteSegment.setString("actualTransportCost", String.valueOf(actualTransportCost)); shipmentRouteSegment.store(); } catch (GenericEntityException gee) { Debug.log(gee, module); return ServiceUtil.returnError("Error reading or writing shipment data for the USPS " + "Domestic Rate Calculation service: " + gee.getMessage()); } return ServiceUtil.returnSuccess(); } /* Delivery Confirmation Samples: <DeliveryConfirmationV2.0Request USERID="xxxxxxx" PASSWORD="xxxxxxxx"> <Option>3</Option> <ImageParameters></ImageParameters> <FromName>John Smith</FromName> <FromFirm>ABC Corp.</FromFirm> <FromAddress1>Ste 4</FromAddress1> <FromAddress2>6406 Ivy Lane</FromAddress2> <FromCity>Greenbelt</FromCity> <FromState>MD</FromState> <FromZip5>20770</FromZip5> <FromZip4>4354</FromZip4> <ToName>Jane Smith</ToName> <ToFirm>XYZ Corp.</ToFirm> <ToAddress1>Apt 303</ToAddress1> <ToAddress2>4411 Romlon Street</ToAddress2> <ToCity>Beltsville</ToCity> <ToState>MD</ToState> <ToZip5>20705</ToZip5> <ToZip4>5656</ToZip4> <WeightInOunces>22</WeightInOunces> <ServiceType>Parcel Post</ServiceType> <ImageType>TIF</ImageType> </DeliveryConfirmationV2.0Request> <DeliveryConfirmationV2.0Response> <DeliveryConfirmationNumber>02805213907052510758</DeliveryConfirmationNumber> <DeliveryConfirmationLabel>(Base64 encoded data)</DeliveryConfirmationLabel> </DeliveryConfirmationV2.0Response> */ public static Map uspsDeliveryConfirmation(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String shipmentId = (String) context.get("shipmentId"); String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId"); // ShipmentRouteSegment identifier - used in error messages String srsKeyString = "[" + shipmentId + "," + shipmentRouteSegmentId + "]"; try { GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); if (shipment == null) { return ServiceUtil.returnError("Shipment not found with ID " + shipmentId); } GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); if (shipmentRouteSegment == null) { return ServiceUtil.returnError("ShipmentRouteSegment not found with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } // ensure the carrier is USPS if (!"USPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) { return ServiceUtil.returnError("The Carrier for ShipmentRouteSegment " + srsKeyString + ", is not USPS"); } // get the origin address GenericValue originAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress"); if (originAddress == null) { return ServiceUtil.returnError("OriginPostalAddress not found for ShipmentRouteSegment [" + shipmentId + ":" + shipmentRouteSegmentId + "]"); } if (!"USA".equals(originAddress.getString("countryGeoId"))) { return ServiceUtil.returnError("ShipmentRouteSeqment " + srsKeyString + " does not originate from a US address"); } // get the destination address GenericValue destinationAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress"); if (destinationAddress == null) { return ServiceUtil.returnError("DestPostalAddress not found for ShipmentRouteSegment " + srsKeyString); } if (!"USA".equals(destinationAddress.getString("countryGeoId"))) { return ServiceUtil.returnError("ShipmentRouteSeqment " + srsKeyString + " is not destined for a US address"); } // get the service type from the CarrierShipmentMethod String shipmentMethodTypeId = shipmentRouteSegment.getString("shipmentMethodTypeId"); String partyId = shipmentRouteSegment.getString("carrierPartyId"); String csmKeystring = "[" + shipmentMethodTypeId + "," + partyId + ",CARRIER]"; GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", UtilMisc.toMap("partyId", partyId, "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentMethodTypeId)); if (carrierShipmentMethod == null) { return ServiceUtil.returnError("CarrierShipmentMethod " + csmKeystring + " not found for ShipmentRouteSegment " + srsKeyString); } String serviceType = carrierShipmentMethod.getString("carrierServiceCode"); if (UtilValidate.isEmpty(serviceType)) { return ServiceUtil.returnError("carrierServiceCode not found for CarrierShipmentMethod" + csmKeystring); } // get the packages for this shipment route segment List shipmentPackageRouteSegList = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId")); if (UtilValidate.isEmpty(shipmentPackageRouteSegList)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -