コード例 #1
0
ファイル: XmlTools.cs プロジェクト: appliedi/MerchantTribe
        private static string BuildShipConfirmRequest(ref ShipRequest req)
        {

            StringWriter strWriter = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(strWriter);

            xw.Formatting = Formatting.Indented;
            xw.Indentation = 3;

            xw.WriteStartDocument();

            //--------------------------------------------            
            // Start Shipment Confirm Request
            xw.WriteStartElement("ShipmentConfirmRequest");

            //--------------------------------------------
            // Request
            xw.WriteStartElement("Request");
            //--------------------------------------------
            // TransactionReference
            xw.WriteStartElement("TransactionReference");
            xw.WriteElementString("CustomerContext", "Shipment Confirm Request");
            xw.WriteElementString("XpciVersion", "1.0001");
            xw.WriteEndElement();
            // End TransactionReference
            //--------------------------------------------
            xw.WriteElementString("RequestAction", "ShipConfirm");

            if (req.AddressVerification == true)
            {
                xw.WriteElementString("RequestOption", "validate");
            }
            else
            {
                xw.WriteElementString("RequestOption", "nonvalidate");
            }

            xw.WriteEndElement();
            // End Request
            //--------------------------------------------

            //--------------------------------------------
            // Start Label Specification
            xw.WriteStartElement("LabelSpecification");

            // Label Print Method
            xw.WriteStartElement("LabelPrintMethod");
            switch (req.LabelFormat)
            {
                case ShipLabelFormat.Epl2:
                    xw.WriteElementString("Code", "EPL");
                    break;
                case ShipLabelFormat.Gif:
                    xw.WriteElementString("Code", "GIF");
                    break;
                default:
                    xw.WriteElementString("Code", "GIF");
                    break;
            }
            xw.WriteEndElement();

            // Image Label Format
            xw.WriteStartElement("LabelImageFormat");
            xw.WriteElementString("Code", "GIF");
            xw.WriteEndElement();

            // Http User Agent
            if (req.BrowserHttpUserAgentString != string.Empty)
            {
                xw.WriteElementString("HTTPUserAgent", req.BrowserHttpUserAgentString);
            }

            // Stock Size
            if ((req.LabelStockSizeHeight != 0) & (req.LabelStockSizeWidth != 0))
            {
                xw.WriteStartElement("LabelStockSize");
                if (req.LabelFormat == ShipLabelFormat.Epl2)
                {
                    // Only 4 is valid for Epl
                    xw.WriteElementString("Height", "4");
                    // Only 6 and 8 are valid
                    if ((req.LabelStockSizeWidth != 6) & (req.LabelStockSizeWidth != 8))
                    {
                        xw.WriteElementString("Width", "6");
                        // Pick a default
                    }
                    else
                    {
                        xw.WriteElementString("Width", req.LabelStockSizeWidth.ToString());
                    }
                }
                else
                {
                    xw.WriteElementString("Height", req.LabelStockSizeHeight.ToString());
                    xw.WriteElementString("Width", req.LabelStockSizeWidth.ToString());
                }
                xw.WriteEndElement();
            }

            xw.WriteEndElement();
            // End Label Specification
            //--------------------------------------------

            //--------------------------------------------
            // Shipment
            xw.WriteStartElement("Shipment");

            if (req.ShipmentDescription != string.Empty)
            {
                xw.WriteElementString("Description", XmlTools.TrimToLength(req.ShipmentDescription, 35));
            }

            if (req.NonValuedDocumentsOnly == true)
            {
                xw.WriteElementString("DocumentsOnly", "");
            }

            // Shipper
            Entity tempShipper = req.Shipper;
            XmlTools.WriteShipperXml(ref xw,ref tempShipper);
            // ShipTo
            Entity tempShipTo = req.ShipTo;
            XmlTools.WriteShipToXml(ref xw,ref tempShipTo);
            // ShipFrom
            Entity tempShipFrom = req.ShipFrom;
            XmlTools.WriteShipFromXml(ref xw,ref tempShipFrom);


            // Start Payment Information
            xw.WriteStartElement("PaymentInformation");
            switch (req.BillTo)
            {
                case PaymentType.ReceiverUpsAccount:
                    xw.WriteStartElement("FreightCollect");
                    xw.WriteStartElement("BillReceiver");
                    xw.WriteElementString("AccountNumber", req.BillToAccountNumber);
                    if (req.BillToPostalCode != string.Empty)
                    {
                        xw.WriteStartElement("Address");
                        xw.WriteElementString("PostalCode", req.BillToPostalCode);
                        xw.WriteEndElement();
                    }

                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    break;
                case PaymentType.ShipperCreditCard:
                    xw.WriteStartElement("Prepaid");
                    xw.WriteStartElement("BillShipper");
                    xw.WriteStartElement("CreditCard");
                    xw.WriteElementString("Type", "0" + Convert.ToInt32(req.BillToCreditCardType).ToString());
                    xw.WriteElementString("Number", req.BillToCreditCardNumber);
                    string expDate = "";
                    if (req.BillToCreditCardExpirationMonth < 10)
                    {
                        expDate = "0" + req.BillToCreditCardExpirationMonth.ToString();
                    }
                    else
                    {
                        expDate = req.BillToCreditCardExpirationMonth.ToString();
                    }

                    expDate = expDate + req.BillToCreditCardExpirationYear.ToString();
                    xw.WriteElementString("ExpirationDate", expDate);
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    break;
                case PaymentType.ShipperUpsAccount:
                    xw.WriteStartElement("Prepaid");
                    xw.WriteStartElement("BillShipper");
                    xw.WriteElementString("AccountNumber", req.BillToAccountNumber);
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    break;
                case PaymentType.ThirdPartyUpsAccount:
                    xw.WriteStartElement("BillThirdParty");
                    xw.WriteStartElement("BillThirdPartyShipper");
                    xw.WriteElementString("AccountNumber", req.BillToAccountNumber);
                    xw.WriteStartElement("ThirdParty");
                    xw.WriteStartElement("Address");
                    if (req.BillToPostalCode != string.Empty)
                    {
                        xw.WriteElementString("PostalCode", req.BillToPostalCode);
                    }

                    xw.WriteElementString("CountryCode", req.BillToCountryCode);
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                    break;
            }
            xw.WriteEndElement();
            // End Payment Information

            // Reference Number
            if (req.ReferenceNumber != string.Empty)
            {
                xw.WriteStartElement("ReferenceNumber");
                xw.WriteElementString("Value", XmlTools.TrimToLength(req.ReferenceNumber, 35));
                string codeType = ConvertReferenceNumberCodeToString(req.ReferenceNumberType);
                xw.WriteElementString("Code", codeType);
                xw.WriteEndElement();
            }

            // Start Service
            xw.WriteStartElement("Service");
            string stringServiceCode = Convert.ToInt32(req.Service).ToString();
            if (stringServiceCode.Length < 2)
            {
                stringServiceCode = "0" + stringServiceCode;
            }
            xw.WriteElementString("Code", stringServiceCode);
            xw.WriteEndElement();
            // End Service

            // Invoice Line Total
            if (req.InvoiceLineTotal == true)
            {
                xw.WriteStartElement("InvoiceLineTotal");
                xw.WriteElementString("MonetaryValue", req.InvoiceLineTotalAmount.ToString());
                xw.WriteElementString("CurrencyCode", XmlTools.ConvertCurrencyCodeToString(req.InvoiceLineTotalCurrency));
                xw.WriteEndElement();
            }



            for (int i = 0; i <= req.Packages.Count - 1; i++)
            {
                Package tempPackage = (Package)req.Packages[i];
                XmlTools.WriteSingleUpsPackage(ref xw,ref tempPackage);
            }

            //-------------------------------------------
            // Start Shipment Service Options
            xw.WriteStartElement("ShipmentServiceOptions");

            // SaturdayDelivery
            if (req.SaturdayDelivery == true)
            {
                xw.WriteElementString("SaturdayDelivery", "");
            }

            // COD
            if (req.COD == true)
            {
                xw.WriteStartElement("COD");
                xw.WriteElementString("CODCode", "3");
                xw.WriteElementString("CODFundsCode", Convert.ToInt32(req.CODPaymentType).ToString());
                xw.WriteStartElement("CODAmount");
                xw.WriteElementString("CurrencyCode", XmlTools.ConvertCurrencyCodeToString(req.CODCurrencyCode));
                xw.WriteElementString("MonetaryValue", req.CODAmount.ToString());
                xw.WriteEndElement();
                xw.WriteEndElement();
            }

            // Notification
            if (req.Notification == true)
            {
                xw.WriteStartElement("Notification");
                xw.WriteElementString("NotificationCode", Convert.ToInt32(req.NotificationType).ToString());
                xw.WriteStartElement("EMailMessage");
                xw.WriteElementString("EMailAddress", req.NotificationEmailAddress);
                if (req.NotificationUndeliverableEmailAddress != string.Empty)
                {
                    xw.WriteElementString("UndeliverableEMailAddress", req.NotificationUndeliverableEmailAddress);
                }
                if (req.NotificationFromName != string.Empty)
                {
                    xw.WriteElementString("FromName", req.NotificationFromName);
                }
                if (req.NotificationMemo != string.Empty)
                {
                    xw.WriteElementString("Memo", req.NotificationMemo);
                }
                if (req.NotificationSubjectType != NotificationSubjectCode.DefaultCode)
                {
                    xw.WriteElementString("SubjectCode", "0" + Convert.ToInt32(req.NotificationSubjectType).ToString());
                }
                xw.WriteEndElement();
                xw.WriteEndElement();
            }

            xw.WriteEndElement();
            // End Shipment Service Options
            //-------------------------------------------

            xw.WriteEndElement();
            // End Shipment
            //--------------------------------------------

            xw.WriteEndElement();
            // End Shipment Confirm Request
            //--------------------------------------------

            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();


            // Output Xml As String with Access Key
            string sXML = "";
            sXML = XmlTools.BuildAccessKey(req.Settings);
            sXML += "\n";
            sXML += strWriter.GetStringBuilder().ToString();

            return sXML;
        }
コード例 #2
0
ファイル: XmlTools.cs プロジェクト: appliedi/MerchantTribe
        public static ShipResponse SendConfirmRequest(ref ShipRequest req)
        {
            ShipResponse result = new ShipResponse();

            // Build Request and save output
            string requestXml = BuildShipConfirmRequest(ref req);
            req.XmlConfirmRequest = requestXml;

            // Build Url for UPS Service
            string actionUrl = req.Settings.ServerUrl;
            actionUrl += "ShipConfirm";

            // Send Request and Store Response
            string responseXml = string.Empty;
            responseXml = XmlTools.ReadHtmlPage_POST(actionUrl, requestXml);
            req.XmlConfirmResponse = responseXml;

            // Parse Response
            result = ParseShipConfirmResponse(responseXml);

            return result;
        }