Example #1
0
        public static decimal GetTax(int orderId, bool UpdateOrderTax, bool CartTax, ClientCartContext clientData)
        {
            decimal taxAmount = 0M;

            try
            {
                XmlNode config = null;
                config = GetTax_AvalaraConfig();
                string accountNumber = config.Attributes["accountNumber"].Value;
                string licenseKey    = config.Attributes["licenseKey"].Value;
                string serviceURL    = config.Attributes["serviceURL"].Value;

                TaxSvc taxSvc = new TaxSvc(accountNumber, licenseKey, serviceURL);

                GetTaxRequest           getTaxRequest = GetTax_Request(orderId, UpdateOrderTax, CartTax, clientData);
                XmlSerializerNamespaces namesp        = new XmlSerializerNamespaces();
                namesp.Add(string.Empty, string.Empty);
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                XmlSerializer x1 = new XmlSerializer(getTaxRequest.GetType());
                StringBuilder sb = new StringBuilder();
                x1.Serialize(XmlTextWriter.Create(sb, settings), getTaxRequest, namesp);
                string       req          = sb.ToString();
                int          timeout      = getTimeOutTax(); // This is for Request TimeOut
                GetTaxResult getTaxResult = taxSvc.GetTax(getTaxRequest, timeout);

                XmlSerializer x2  = new XmlSerializer(getTaxResult.GetType());
                StringBuilder sb2 = new StringBuilder();
                x2.Serialize(XmlTextWriter.Create(sb2, settings), getTaxResult, namesp);
                string res = sb2.ToString();

                if (getTaxResult.ResultCode.Equals(SeverityLevel.Success))
                {
                    taxAmount = getTaxResult.TotalTax;
                }

                if (UpdateOrderTax && orderId > 0)
                {
                    CSResolve.Resolve <IOrderService>().UpdateOrderTax(orderId, taxAmount);
                    Dictionary <string, AttributeValue> orderAttributes = new Dictionary <string, AttributeValue>();
                    orderAttributes.Add("TaxRequest", new CSBusiness.Attributes.AttributeValue(req));
                    orderAttributes.Add("TaxResponse", new CSBusiness.Attributes.AttributeValue(res));
                    CSResolve.Resolve <IOrderService>().UpdateOrderAttributes(orderId, orderAttributes, null);
                }
            }
            catch
            {
            }
            return(taxAmount);
        }
Example #2
0
        // This actually calls the service to perform the tax calculation, and returns the calculation result.
        public async Task <GetTaxResult> GetTax(GetTaxRequest req)
        {
            // Convert the request to XML
            XmlSerializerNamespaces namesp = new XmlSerializerNamespaces();

            namesp.Add(string.Empty, string.Empty);
            XmlWriterSettings settings = new XmlWriterSettings {
                OmitXmlDeclaration = true
            };
            XmlSerializer x  = new XmlSerializer(req.GetType());
            StringBuilder sb = new StringBuilder();

            x.Serialize(XmlWriter.Create(sb, settings), req, namesp);
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(sb.ToString());

            // Call the service
            Uri address = new Uri(_svcUrl + "tax/get");
            var request = HttpHelper.CreateRequest(address, _accountNumber, _license);

            request.Method      = "POST";
            request.ContentType = "text/xml";
            //request.ContentLength = sb.Length;
            Stream newStream = await request.GetRequestStreamAsync();

            newStream.Write(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
            GetTaxResult result = new GetTaxResult();

            try
            {
                WebResponse response = await request.GetResponseAsync();

                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (GetTaxResult)r.Deserialize(response.GetResponseStream());
            }
            catch (WebException ex)
            {
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (GetTaxResult)r.Deserialize(((HttpWebResponse)ex.Response).GetResponseStream());
            }

            return(result);
        }