📄 upsservices.java
字号:
// send ShipmentConfirmRequest String // get ShipmentConfirmResponse String back StringBuffer xmlString = new StringBuffer(); // TODO: note that we may have to append <?xml version="1.0"?> before each string xmlString.append(accessRequestString); xmlString.append(shipmentConfirmRequestString); if (shipmentUpsSaveCertificationInfo) { String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentConfirmRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; try { FileOutputStream fileOut = new FileOutputStream(outFileName); fileOut.write(xmlString.toString().getBytes()); fileOut.flush(); fileOut.close(); } catch (IOException e) { Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); } } try { shipmentConfirmResponseString = sendUpsRequest("ShipConfirm", xmlString.toString()); } catch (UpsConnectException e) { String uceErrMsg = "Error sending UPS request for UPS Service ShipConfirm: " + e.toString(); Debug.logError(e, uceErrMsg, module); return ServiceUtil.returnError(uceErrMsg); } if (shipmentUpsSaveCertificationInfo) { String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentConfirmResponse" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; try { FileOutputStream fileOut = new FileOutputStream(outFileName); fileOut.write(shipmentConfirmResponseString.getBytes()); fileOut.flush(); fileOut.close(); } catch (IOException e) { Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); } } Document shipmentConfirmResponseDocument = null; try { shipmentConfirmResponseDocument = UtilXml.readXmlDocument(shipmentConfirmResponseString, false); } catch (SAXException e2) { String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (ParserConfigurationException e2) { String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (IOException e2) { String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } return handleUpsShipmentConfirmResponse(shipmentConfirmResponseDocument, shipmentRouteSegment); } catch (GenericEntityException e) { Debug.logError(e, module); if (shipmentConfirmResponseString != null) { Debug.logError("Got XML ShipmentConfirmRespose: " + shipmentConfirmResponseString, module); return ServiceUtil.returnError(UtilMisc.toList( "Error reading or writing Shipment data for UPS Shipment Confirm: " + e.toString(), "A ShipmentConfirmRespose was received: " + shipmentConfirmResponseString)); } else { return ServiceUtil.returnError("Error reading or writing Shipment data for UPS Shipment Confirm: " + e.toString()); } } } public static Map handleUpsShipmentConfirmResponse(Document shipmentConfirmResponseDocument, GenericValue shipmentRouteSegment) throws GenericEntityException { // process ShipmentConfirmResponse, update data as needed Element shipmentConfirmResponseElement = shipmentConfirmResponseDocument.getDocumentElement(); // handle Response element info Element responseElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "Response"); Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); List errorList = new LinkedList(); UpsServices.handleErrors(responseElement, errorList); if ("1".equals(responseStatusCode)) { // handle ShipmentCharges element info Element shipmentChargesElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "ShipmentCharges"); Element transportationChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TransportationCharges"); String transportationCurrencyCode = UtilXml.childElementValue(transportationChargesElement, "CurrencyCode"); String transportationMonetaryValue = UtilXml.childElementValue(transportationChargesElement, "MonetaryValue"); Element serviceOptionsChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "ServiceOptionsCharges"); String serviceOptionsCurrencyCode = UtilXml.childElementValue(serviceOptionsChargesElement, "CurrencyCode"); String serviceOptionsMonetaryValue = UtilXml.childElementValue(serviceOptionsChargesElement, "MonetaryValue"); Element totalChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TotalCharges"); String totalCurrencyCode = UtilXml.childElementValue(totalChargesElement, "CurrencyCode"); String totalMonetaryValue = UtilXml.childElementValue(totalChargesElement, "MonetaryValue"); if (UtilValidate.isNotEmpty(totalCurrencyCode)) { if (UtilValidate.isEmpty(shipmentRouteSegment.getString("currencyUomId"))) { shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); } else if(!totalCurrencyCode.equals(shipmentRouteSegment.getString("currencyUomId"))) { errorList.add("The Currency Unit of Measure returned [" + totalCurrencyCode + "] is not the same as the original [" + shipmentRouteSegment.getString("currencyUomId") + "], setting to the new one."); shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); } } try { shipmentRouteSegment.set("actualTransportCost", Double.valueOf(transportationMonetaryValue)); } catch (NumberFormatException e) { String excErrMsg = "Error parsing the transportationMonetaryValue [" + transportationMonetaryValue + "]: " + e.toString(); Debug.logError(e, excErrMsg, module); errorList.add(excErrMsg); } try { shipmentRouteSegment.set("actualServiceCost", Double.valueOf(serviceOptionsMonetaryValue)); } catch (NumberFormatException e) { String excErrMsg = "Error parsing the serviceOptionsMonetaryValue [" + serviceOptionsMonetaryValue + "]: " + e.toString(); Debug.logError(e, excErrMsg, module); errorList.add(excErrMsg); } try { shipmentRouteSegment.set("actualCost", Double.valueOf(totalMonetaryValue)); } catch (NumberFormatException e) { String excErrMsg = "Error parsing the totalMonetaryValue [" + totalMonetaryValue + "]: " + e.toString(); Debug.logError(e, excErrMsg, module); errorList.add(excErrMsg); } // handle BillingWeight element info Element billingWeightElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "BillingWeight"); Element billingWeightUnitOfMeasurementElement = UtilXml.firstChildElement(billingWeightElement, "UnitOfMeasurement"); String billingWeightUnitOfMeasurement = UtilXml.childElementValue(billingWeightUnitOfMeasurementElement, "Code"); String billingWeight = UtilXml.childElementValue(billingWeightElement, "Weight"); try { shipmentRouteSegment.set("billingWeight", Double.valueOf(billingWeight)); } catch (NumberFormatException e) { String excErrMsg = "Error parsing the billingWeight [" + billingWeight + "]: " + e.toString(); Debug.logError(e, excErrMsg, module); errorList.add(excErrMsg); } shipmentRouteSegment.set("billingWeightUomId", unitsUpsToOfbiz.get(billingWeightUnitOfMeasurement)); // store the ShipmentIdentificationNumber and ShipmentDigest String shipmentIdentificationNumber = UtilXml.childElementValue(shipmentConfirmResponseElement, "ShipmentIdentificationNumber"); String shipmentDigest = UtilXml.childElementValue(shipmentConfirmResponseElement, "ShipmentDigest"); shipmentRouteSegment.set("trackingIdNumber", shipmentIdentificationNumber); shipmentRouteSegment.set("trackingDigest", shipmentDigest); // set ShipmentRouteSegment carrierServiceStatusId after each UPS service applicable shipmentRouteSegment.put("carrierServiceStatusId", "SHRSCS_CONFIRMED"); // write/store all modified value objects shipmentRouteSegment.store(); // -=-=-=- Okay, now done with that, just return any extra info... StringBuffer successString = new StringBuffer("The UPS ShipmentConfirm succeeded"); if (errorList.size() > 0) { // this shouldn't happen much, but handle it anyway successString.append(", but the following occurred: "); Iterator errorListIter = errorList.iterator(); while (errorListIter.hasNext()) { String errorMsg = (String) errorListIter.next(); successString.append(errorMsg); if (errorListIter.hasNext()) { successString.append(", "); } } } return ServiceUtil.returnSuccess(successString.toString()); } else { errorList.add(0, "The UPS ShipmentConfirm failed"); return ServiceUtil.returnError(errorList); } } public static Map upsShipmentAccept(DispatchContext dctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); String shipmentId = (String) context.get("shipmentId"); String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId"); boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); File shipmentUpsSaveCertificationFile = null; if (shipmentUpsSaveCertificationInfo) { shipmentUpsSaveCertificationFile = new File(shipmentUpsSaveCertificationPath); if (!shipmentUpsSaveCertificationFile.exists()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -