Exemplo n.º 1
0
        public USPSShipmentResponse SendShipmentConfirmation(USPSShipment shipDetails, int providerId, int storeId, int portalId)
        {
            try
            {


                string api = "";
                _isShipment = true;
                XmlDocument shipment = new XmlDocument();
                XmlNode root = null;
                var domestic = true;
                LoadConfig(providerId, storeId, portalId);
                var wareHouseAddress = GetWareHouseAddress(storeId, portalId);
                if (wareHouseAddress.Country == "US" || wareHouseAddress.Country == "UNITED STATES")
                {
                    #region INTERNATIONAL
                    if (shipDetails.ToAddress.ToCountry.ToUpper().Trim() != "US" && shipDetails.ToAddress.ToCountry.ToUpper().Trim() != "UNITED STATES")
                    {
                        if (shipDetails.ServiceType.ToString().ToUpper().Contains("EXPRESS"))
                        {
                            root = shipment.CreateNode(XmlNodeType.Element, "ExpressMailIntlCertifyRequest", "");
                            api = "ExpressMailIntlCertify"; //
                        }
                        if (shipDetails.ServiceType.ToString().ToUpper().Contains("PRIORITY"))
                        {
                            root = shipment.CreateNode(XmlNodeType.Element, "PriorityMailIntlRequest", "");
                            api = "PriorityMailIntlCertify"; //
                        }
                        if (shipDetails.ServiceType.ToString().ToUpper().Contains("FIRSTCLASS"))
                        {
                            root = shipment.CreateNode(XmlNodeType.Element, "FirstClassMailIntlCertifyRequest", "");
                            api = "FirstClassMailIntlCertify"; //
                        }

                        XmlNode ids = shipment.CreateNode(XmlNodeType.Attribute, "USERID", "");
                        ids.Value = this._userId;
                        root.Attributes.SetNamedItem(ids); //save attribute value



                        domestic = false;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ImageParameters", ""));
                        // root.SelectSingleNode("Option").InnerXml = shipDetails.Option.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromFirstName", ""));
                        root.SelectSingleNode("FromFirstName").InnerXml = shipDetails.FromAddress.FirstName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromLastName", ""));
                        root.SelectSingleNode("FromLastName").InnerXml = shipDetails.FromAddress.LastName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromFirm", ""));
                        root.SelectSingleNode("FromFirm").InnerXml = wareHouseAddress.Name;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromAddress1", ""));
                        root.SelectSingleNode("FromAddress1").InnerXml = wareHouseAddress.StreetAddress1;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromAddress2", ""));
                        root.SelectSingleNode("FromAddress2").InnerXml = wareHouseAddress.StreetAddress2;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromCity", ""));
                        root.SelectSingleNode("FromCity").InnerXml = wareHouseAddress.City;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromState", ""));
                        root.SelectSingleNode("FromState").InnerXml = wareHouseAddress.State;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromZip5", ""));
                        root.SelectSingleNode("FromZip5").InnerXml = wareHouseAddress.PostalCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromZip4", ""));
                        root.SelectSingleNode("FromZip4").InnerXml = shipDetails.FromAddress.ZipPlus4;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromPhone", ""));
                        root.SelectSingleNode("FromPhone").InnerXml = wareHouseAddress.Phone;


                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToName", ""));
                        root.SelectSingleNode("ToName").InnerXml = shipDetails.ToAddress.FirstName + " " +
                                                                   shipDetails.ToAddress.LastName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToFirm", ""));
                        root.SelectSingleNode("ToFirm").InnerXml = shipDetails.ToAddress.Company;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToAddress1", ""));
                        root.SelectSingleNode("ToAddress1").InnerXml = shipDetails.ToAddress.ToAddress;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToAddress2", ""));
                        root.SelectSingleNode("ToAddress2").InnerXml = shipDetails.ToAddress.ToAddress2;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToAddress3", ""));
                        root.SelectSingleNode("ToAddress3").InnerXml = "";

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToCity", ""));
                        root.SelectSingleNode("ToCity").InnerXml = shipDetails.ToAddress.ToCity;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToProvince", ""));
                        root.SelectSingleNode("ToProvince").InnerXml = shipDetails.ToAddress.ToState;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToCountry", ""));
                        root.SelectSingleNode("ToCountry").InnerXml = shipDetails.ToAddress.ToCountry;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToPostalCode", ""));
                        root.SelectSingleNode("ToPostalCode").InnerXml = shipDetails.ToAddress.ToPostalCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToPOBoxFlag", ""));
                        root.SelectSingleNode("ToPOBoxFlag").InnerXml = "N";//Y OR NO

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToPhone", ""));
                        root.SelectSingleNode("ToPhone").InnerXml = shipDetails.ToAddress.Phone;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToFax", ""));
                        root.SelectSingleNode("ToFax").InnerXml = shipDetails.ToAddress.ToFax;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToEmail", ""));
                        root.SelectSingleNode("ToEmail").InnerXml = shipDetails.ToAddress.Email;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "NonDeliveryOption", ""));
                        root.SelectSingleNode("NonDeliveryOption").InnerXml = shipDetails.NonDeliveryOption.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Container", ""));
                        root.SelectSingleNode("Container").InnerXml = shipDetails.Container.ToString();



                        XmlNode
                            shippingContents = shipment.CreateNode(XmlNodeType.Element, "ShippingContents", "");

                        foreach (var item in shipDetails.Items)
                        {
                            XmlNode itemDetail = shipment.CreateNode(XmlNodeType.Element, "ItemDetail", "");

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Description", ""));
                            itemDetail.SelectSingleNode("Description").InnerXml = item.Description;

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Quantity", ""));
                            itemDetail.SelectSingleNode("Quantity").InnerXml = item.Quantity.ToString();

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Value", ""));
                            itemDetail.SelectSingleNode("Value").InnerXml = item.Price.ToString();

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "NetPounds", ""));
                            itemDetail.SelectSingleNode("NetPounds").InnerXml = item.Weight.ToString();

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "NetOunces", ""));
                            itemDetail.SelectSingleNode("NetOunces").InnerXml = (item.Weight * 16).ToString();

                            itemDetail.AppendChild(shipment.CreateNode(XmlNodeType.Element, "CountryOfOrigin", ""));
                            itemDetail.SelectSingleNode("CountryOfOrigin").InnerXml = wareHouseAddress.Country;
                            shippingContents.AppendChild(itemDetail);
                        }

                        root.AppendChild(shippingContents);

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "GrossPounds", ""));
                        root.SelectSingleNode("GrossPounds").InnerXml = shipDetails.GrossPound.ToString();
                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "GrossOunces", ""));
                        root.SelectSingleNode("GrossOunces").InnerXml = shipDetails.GrossOunce.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ContentType", ""));
                        root.SelectSingleNode("ContentType").InnerXml = shipDetails.ContentType.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Agreement", ""));
                        root.SelectSingleNode("Agreement").InnerXml = "Y"; //Y OR N

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Comments", ""));
                        root.SelectSingleNode("Comments").InnerXml = "";//additional comments

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ImageType", ""));
                        root.SelectSingleNode("ImageType").InnerXml = shipDetails.ImageType.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ImageLayout", ""));
                        root.SelectSingleNode("ImageLayout").InnerXml = shipDetails.ImageLayout.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "POZipCode", ""));
                        root.SelectSingleNode("POZipCode").InnerXml = shipDetails.POZipCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "LabelDate", ""));
                        // root.SelectSingleNode("LabelDate").InnerXml = DateTime.Now.ToString("dd/mm/yy");

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "HoldForManifest", ""));
                        root.SelectSingleNode("HoldForManifest").InnerXml = "N"; //Y OR N

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Length", ""));
                        root.SelectSingleNode("Length").InnerXml = shipDetails.Length.ToString("0.00");

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Width", ""));
                        root.SelectSingleNode("Width").InnerXml = shipDetails.Width.ToString("0.00");

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Height", ""));
                        root.SelectSingleNode("Height").InnerXml = shipDetails.Height.ToString("0.00");

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Girth", ""));
                        root.SelectSingleNode("Girth").InnerXml = shipDetails.Grith.ToString("0.00");




                    }
                    #endregion
                    else
                    {
                        #region For DOMESTIC
                        if (_useTestMode)
                        {
                            if (shipDetails.Api.ToLower() == "signatureconfirmation")
                            {
                                root = shipment.CreateNode(XmlNodeType.Element, "SigConfirmCertifyV3.0Request", "");
                                api = "SignatureConfirmationCertifyV3";
                            }
                            else
                            {
                                root = shipment.CreateNode(XmlNodeType.Element, "DelivConfirmCertifyV3.0Request", "");
                                api = "DelivConfirmCertifyV3";
                            }

                        }
                        else
                        {
                            if (shipDetails.Api.ToLower() == "signatureconfirmation")
                            {
                                root = shipment.CreateNode(XmlNodeType.Element, "SignatureConfirmationV3.0Request", "");
                                api = "SignatureConfirmation";
                            }
                            else
                            {
                                root = shipment.CreateNode(XmlNodeType.Element, "DeliveryConfirmationV3.0Request", "");
                                api = "DeliveryConfirmationV3";
                            }

                        }
                        XmlNode ids = shipment.CreateNode(XmlNodeType.Attribute, "USERID", "");
                        ids.Value = this._userId;
                        root.Attributes.SetNamedItem(ids); //save attribute value



                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "Option", ""));
                        root.SelectSingleNode("Option").InnerXml = "1";
                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ImageParameters", ""));
                        //root.SelectSingleNode("ImageParameters").InnerXml = "";

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromName", ""));
                        root.SelectSingleNode("FromName").InnerXml = shipDetails.FromAddress.FirstName + " " + shipDetails.FromAddress.LastName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromFirm", "")); //company
                        root.SelectSingleNode("FromFirm").InnerXml = wareHouseAddress.Name;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromAddress1", ""));
                        root.SelectSingleNode("FromAddress1").InnerXml = wareHouseAddress.StreetAddress1;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromAddress2", ""));
                        root.SelectSingleNode("FromAddress2").InnerXml = wareHouseAddress.StreetAddress2;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromCity", ""));
                        root.SelectSingleNode("FromCity").InnerXml = wareHouseAddress.City;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromState", ""));
                        root.SelectSingleNode("FromState").InnerXml = wareHouseAddress.State;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromZip5", ""));
                        root.SelectSingleNode("FromZip5").InnerXml = wareHouseAddress.PostalCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "FromZip4", ""));
                        root.SelectSingleNode("FromZip4").InnerXml = "";

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToName", ""));
                        root.SelectSingleNode("ToName").InnerXml = shipDetails.ToAddress.FirstName + " " + shipDetails.ToAddress.LastName;


                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToFirm", ""));
                        root.SelectSingleNode("ToFirm").InnerXml = shipDetails.ToAddress.Company;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToAddress1", ""));
                        root.SelectSingleNode("ToAddress1").InnerXml = shipDetails.ToAddress.ToAddress;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToAddress2", ""));
                        root.SelectSingleNode("ToAddress2").InnerXml = shipDetails.ToAddress.ToAddress2;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToCity", ""));
                        root.SelectSingleNode("ToCity").InnerXml = shipDetails.ToAddress.ToCity;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToState", ""));
                        root.SelectSingleNode("ToState").InnerXml = shipDetails.ToAddress.ToState;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToZip5", ""));
                        root.SelectSingleNode("ToZip5").InnerXml = shipDetails.ToAddress.ToPostalCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ToZip4", ""));
                        root.SelectSingleNode("ToZip4").InnerXml = shipDetails.ToAddress.ZipPlus4;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "WeightInOunces", ""));
                        root.SelectSingleNode("WeightInOunces").InnerXml = shipDetails.GrossOunce.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ServiceType", ""));
                        root.SelectSingleNode("ServiceType").InnerXml = shipDetails.ServiceType.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "SeparateReceiptPage", ""));
                        root.SelectSingleNode("SeparateReceiptPage").InnerXml = shipDetails.SeparateReceiptPage.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "POZipCode", ""));
                        root.SelectSingleNode("POZipCode").InnerXml = shipDetails.POZipCode;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "ImageType", ""));
                        root.SelectSingleNode("ImageType").InnerXml = shipDetails.ImageType.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "LabelDate", ""));
                        //root.SelectSingleNode("LabelDate").InnerXml = DateTime.Now.ToString("dd/mm/yy");

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "AddressServiceRequested", ""));
                        root.SelectSingleNode("AddressServiceRequested").InnerXml =
                            shipDetails.AddressServiceRequested.ToString();

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "SenderName", ""));
                        root.SelectSingleNode("SenderName").InnerXml = shipDetails.FromAddress.FirstName + " " + shipDetails.FromAddress.LastName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "SenderEMail", ""));
                        root.SelectSingleNode("SenderEMail").InnerXml = shipDetails.FromAddress.Email;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "RecipientName", ""));
                        root.SelectSingleNode("RecipientName").InnerXml = shipDetails.ToAddress.FirstName + " " + shipDetails.ToAddress.LastName;

                        root.AppendChild(shipment.CreateNode(XmlNodeType.Element, "RecipientEMail", ""));
                        root.SelectSingleNode("RecipientEMail").InnerXml = shipDetails.ToAddress.Email;

                        #endregion
                    }

                    shipment.AppendChild(root);
                    labelExtention = shipDetails.ImageType.ToString().Trim();

                }

                XmlDocument shipmentResponse = SendRequestToProvider(shipment, api);
                USPSShipmentResponse shipmentinfo
                    = domestic ? ParseShipmentResponse(shipmentResponse) : ParseShipmentResponseIntl(shipmentResponse);
                // SaveLabel(shipmentinfo.DeliveryConfirmationLabel, shipmentinfo.Label);
                return shipmentinfo;

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
    private void CreateLabel()
    {
        try
        {


            string api = rblLabelTypeList.SelectedValue;
            var items = (List<ItemDetail>)Session["sl_items"];
            var orderInfo = (OrderLabel)Session["labelOrderInfo"];
            if (items == null)
            {
                BindPackageDetails(orderInfo);
                items = (List<ItemDetail>)Session["sl_items"];
            }
            var totalWt = items.Sum(itemDetail => itemDetail.Weight);
            if (totalWt == 0)
            {
                throw new Exception("Minimum weight of package must be 0.1");
            }
            decimal totalWtInOunce = 0;
            if (_weightUnit.ToLower().Trim() == "lbs" || _weightUnit.ToLower().Trim() == "lb")
            {
                               totalWtInOunce = (decimal)((totalWt) * 16);
            }
            else
            {
                               totalWt = totalWt * (decimal)2.2;
            }

            var package = new USPSShipment();
            package.Width = decimal.Parse(txtPackageWidth.Text.Trim());
            package.Height = decimal.Parse(txtPackageHeight.Text.Trim());
            package.FromAddress = (OriginAddress)Session["sl_frAddress"];
            package.GrossOunce = totalWtInOunce;
            package.GrossPound = totalWt;
            var total = CalculateGirth();
            package.Container = Container.RECTANGULAR;
            package.ContentType = ContentType.OTHER;
            package.ImageLayout = ImageLayout.ALLINONEFILE;
            package.ImageType = (ImageType)Enum.Parse(typeof(ImageType), ddlImageType.SelectedValue);
            package.Items = items;
            package.NonDeliveryOption = NonDeliveryOption.RETURN;
            package.Option = "1";
            package.POZipCode = "";
            package.ServiceType = (ServiceType)Enum.Parse(typeof(ServiceType), ddlServiceType.SelectedValue);
            package.ToAddress = (DestinationAddress)Session["sl_toAddress"];
            package.Length = decimal.Parse(txtPackageLength.Text.Trim());
            package.Grith = decimal.Parse(txtPackageGirth.Text.Trim());
            package.Api = api;
            var providerId = int.Parse(Session["sl_pid"].ToString());

            var labelCreater = new USPS();

            var response = labelCreater.SendShipmentConfirmation(package, providerId, GetStoreID, GetPortalID);

            if (!response.IsFailed)
            {


                if (response.IsDomestic)
                {
                    string trackingNo = response.DeliveryConfirmationNumber;
                                                          ShippingLabelInfo info = new ShippingLabelInfo();
                    info.TrackingNo = trackingNo;
                    info.ShippingLabelImage = response.LabelString;
                    info.OrderID = orderInfo.OrderId;
                    info.Extension = package.ImageType.ToString().ToLower();
                    info.IsRealTime = true;
                    SaveLabelInfo(info);
                    DisplayFile(response.TempLabelPath);
                    lblErrorMessage.Text = "";
                }
                else
                {
                    string barcodeNo = response.IntlResponse.BarcodeNumber;
                                      
                    ShippingLabelInfo info = new ShippingLabelInfo();
                    info.ShippingLabelImage = response.LabelString;
                    info.OrderID = orderInfo.OrderId;
                    info.Extension = package.ImageType.ToString().ToLower();
                    info.IsRealTime = true;
                    info.TrackingNo = "";
                    info.BarcodeNo = barcodeNo;
                    SaveLabelInfo(info);
                }
                ClearForm();

            }
            else
            {
                lblErrorMessage.Text = response.Error;
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }