예제 #1
0
    public string ValidateUSAddress(string AddressLine1, string AddressLine2, string City, string State, string ZipCode, string Country, string Type, string CrewName, string ClientName, string Mode, string StaffCode, string DOB, string AppliedRank)
    {
        StringBuilder Return = new StringBuilder();

        BLL_Crew_Admin objBLL_Crew_Admin = new BLL_Crew_Admin();
        DataTable      dt = objBLL_Crew_Admin.CRW_LIB_AddressValidationSetting();
        string         Url = "", Username = "", Password = "", ToEmailId = "", FromEmailId = "", EmailBody = "";

        try
        {
            var           requestXml     = new XmlDocument();
            StringBuilder strURLParamter = new StringBuilder();

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    dt.DefaultView.RowFilter = "key='USPSUrl'";
                    Url = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    dt.DefaultView.RowFilter = "";
                    dt.DefaultView.RowFilter = "key='Username'";
                    Username = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    dt.DefaultView.RowFilter = "";
                    dt.DefaultView.RowFilter = "key='Password'";
                    Password = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    dt.DefaultView.RowFilter = "";
                    dt.DefaultView.RowFilter = "key='ToEmailId'";
                    ToEmailId = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    dt.DefaultView.RowFilter = "";
                    dt.DefaultView.RowFilter = "key='FromEmailId'";
                    FromEmailId = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    dt.DefaultView.RowFilter = "";
                    dt.DefaultView.RowFilter = "key='EmailBody'";
                    EmailBody = Convert.ToString(dt.DefaultView[0]["Value"]).Trim();

                    if (Url != "" && Username != "" && Password != "")
                    {
                        strURLParamter.Append("<AddressValidateRequest USERID=\"" + Username + "\">");

                        strURLParamter.Append("<Address ID=\"0\">");
                        strURLParamter.Append("<FirmName />");
                        strURLParamter.Append("<Address1>" + AddressLine1.Trim() + "</Address1>");
                        strURLParamter.Append("<Address2>" + AddressLine2.Trim() + "</Address2>");
                        strURLParamter.Append("<City>" + City.Trim() + "</City>");
                        strURLParamter.Append("<State>" + State.Trim() + "</State>");
                        strURLParamter.Append("<Zip5>" + ZipCode.Trim() + "</Zip5>");
                        strURLParamter.Append("<Zip4></Zip4>");
                        strURLParamter.Append("</Address>");
                        strURLParamter.Append("</AddressValidateRequest>");

                        // build XML request

                        var httpRequest = HttpWebRequest.Create(Url + "?API=Verify&XML=" + strURLParamter.ToString());
                        httpRequest.Method      = "POST";
                        httpRequest.ContentType = "text/xml";

                        // set appropriate headers

                        using (var requestStream = httpRequest.GetRequestStream())
                        {
                            requestXml.Save(requestStream);
                        }

                        using (var response = (HttpWebResponse)httpRequest.GetResponse())
                            using (var responseStream = response.GetResponseStream())
                            {
                                // may want to check response.StatusCode to
                                // see if the request was successful

                                var responseXml = new XmlDocument();
                                responseXml.Load(responseStream);
                                if (responseXml.SelectSingleNode("//Error") != null)
                                {
                                    XmlNodeList objXmlNode = responseXml.SelectNodes("AddressValidateResponse/Address/Error");
                                    if (objXmlNode != null)
                                    {
                                        foreach (XmlNode item in objXmlNode)
                                        {
                                            Return.Append("Result:Invalid,");
                                            Return.Append("ErrorMessage: " + item["Description"].InnerText);
                                        }
                                    }
                                }
                                else
                                {
                                    XmlNodeList objXmlNode = responseXml.SelectNodes("AddressValidateResponse/Address");
                                    if (objXmlNode != null)
                                    {
                                        foreach (XmlNode item in objXmlNode)
                                        {
                                            string Address1 = "", Address2 = "";
                                            if (item["Address1"] != null)
                                            {
                                                Address1 = item["Address1"].InnerText;
                                                if (item["Address1"].InnerText.Contains("#"))
                                                {
                                                    AddressLine1 = "# " + AddressLine1;
                                                }
                                            }
                                            if (item["Address2"] != null)
                                            {
                                                Address2 = item["Address2"].InnerText;
                                                if (item["Address2"].InnerText.Contains("#"))
                                                {
                                                    AddressLine2 = "# " + AddressLine2;
                                                }
                                            }

                                            if (AddressLine1.ToLower() != Address1.ToLower() || AddressLine2.ToLower() != Address2.ToLower() || City.ToLower() != item["City"].InnerText.ToLower() || State.ToLower() != item["State"].InnerText.ToLower() || ZipCode.ToLower() != item["Zip5"].InnerText.ToLower())
                                            {
                                                Return.Append("Result:ValidAddress,");
                                                Return.Append("Address Line 1: " + Address2 + ",");
                                                Return.Append("Address Line 2: " + Address1 + ",");
                                                Return.Append("City: " + item["City"].InnerText + ",");
                                                Return.Append("State: " + item["State"].InnerText + ",");
                                                Return.Append("ZipCode: " + item["Zip5"].InnerText);
                                            }
                                            else
                                            {
                                                Return.Append("Result:Valid");
                                            }
                                        }
                                    }
                                }
                            }
                    }
                    else
                    {
                        Return.Append("Result:No Settings");//No setting exists in DB
                    }
                }
                else
                {
                    Return.Append("Result:No Settings");
                }
            }
            else
            {
                Return.Append("Result:No Settings");
            }
        }
        catch (Exception ex)
        {
            if (Type == "Crew")
            {
                if (!ex.ToString().ToLower().Contains(":line"))
                {
                    BLL_Crew_CrewDetails objCrew = new BLL_Crew_CrewDetails();
                    EmailBody = EmailBody.Replace("$$Client$$", ClientName);
                    EmailBody = EmailBody.Replace("$$CrewName$$", CrewName);
                    EmailBody = EmailBody.Replace("$$Date$$", DateTime.Now.ToString());
                    EmailBody = EmailBody.Replace("$$Status$$", ex.ToString());
                    EmailBody = StaffCode == "" ? EmailBody.Replace("$$StaffCode$$", "-") : EmailBody.Replace("$$StaffCode$$", StaffCode);
                    EmailBody = DOB == "" ? EmailBody.Replace("$$DOB$$", "-") : EmailBody.Replace("$$DOB$$", DOB);
                    EmailBody = AppliedRank == "" ? EmailBody.Replace("$$AppliedRank$$", "-") : EmailBody.Replace("$$AppliedRank$$", AppliedRank);

                    if (Mode == "Add")
                    {
                        EmailBody = EmailBody.Replace("$$Remark$$", "USPS service is down/not responding while adding the crew.");
                    }
                    else if (Mode == "Edit")
                    {
                        EmailBody = EmailBody.Replace("$$Remark$$", "USPS service is down/not responding while updating the crew.");
                    }

                    objCrew.Send_CrewNotification(0, 0, 0, 0, ToEmailId, "", "", ClientName + "- USPS Service Error", EmailBody, "", "MAIL", "", 1, "READY");
                }
            }
            Return.Append("Result:Error"); //-- Error
        }
        return(Return.ToString());
    }