Example #1
0
        public ITaxStructured DoCalc(
            TaxJarSession session,
            IAddress fromAddress,
            IAddress toAddress,
            Amount amount,
            Amount shipping)
        {
            if (fromAddress == null)
            {
                throw new TaxException(StringConsts.TAX_TAXJAR_TOADDRESS_ISNT_SUPPLIED_ERROR + this.GetType() + ".DoCalc");
            }

            if (toAddress == null)
            {
                throw new TaxException(StringConsts.TAX_TAXJAR_TOADDRESS_ISNT_SUPPLIED_ERROR + this.GetType() + ".DoCalc");
            }

            try
            {
                var bodyPrms = new Dictionary <string, string>()
                {
                    { PRM_AMOUNT, amount.Value.ToString() },
                    { PRM_SHIPPING, 0.ToString() }
                };

                bodyPrms.Add(PRM_FROM_COUNTRY, fromAddress.Country);
                bodyPrms.Add(PRM_FROM_STATE, fromAddress.Region);
                bodyPrms.Add(PRM_FROM_ZIP, fromAddress.PostalCode);

                bodyPrms.Add(PRM_TO_COUNTRY, toAddress.Country);
                bodyPrms.Add(PRM_TO_STATE, toAddress.Region);
                bodyPrms.Add(PRM_TO_ZIP, toAddress.PostalCode);

                var prms = new WebClient.RequestParams()
                {
                    Uri     = new Uri(CALC_URI),
                    Caller  = this,
                    Headers = new Dictionary <string, string>()
                    {
                        { AUTH_HEADER_KEY, AUTH_HEADER_VALUE_PATTERN.Args(session.ApiKey) }
                    },
                    Method         = HTTPRequestMethod.POST,
                    BodyParameters = bodyPrms
                };

                dynamic obj = WebClient.GetJsonAsDynamic(prms);

                var result = new TaxStructured();

                result.Total = (decimal)obj.Data["tax"]["amount_to_collect"];

                return(result);
            }
            catch (Exception ex)
            {
                var wex = ex as System.Net.WebException;

                if (wex != null)
                {
                    var response = wex.Response as System.Net.HttpWebResponse;
                    if (response != null)
                    {
                        string errorMessage = this.GetType().Name +
                                              ".DoCalc(fromAddress: {0}, toAddress: {1}, amount: '{2}', shipping: '{3}')".Args(fromAddress, toAddress, amount, shipping);
                        var taxEx = TaxException.Compose(response, errorMessage, wex);
                        throw taxEx;
                    }
                }

                throw new TaxException(StringConsts.TAX_CALC_ERROR + this.GetType()
                                       + " .Calc(session='{0}', fromAddress='{1}', toAddress='{2}', amount='{3}', shipping='{4}')"
                                       .Args(session, fromAddress, toAddress, amount, shipping), ex);
            }
        }
Example #2
0
    public ITaxStructured DoCalc(
        TaxJarSession session,
        IAddress fromAddress, 
        IAddress toAddress, 
        Amount amount, 
        Amount shipping)
    {
      if (fromAddress == null)
        throw new TaxException(StringConsts.TAX_TAXJAR_TOADDRESS_ISNT_SUPPLIED_ERROR + this.GetType() + ".DoCalc");

      if (toAddress == null)
        throw new TaxException(StringConsts.TAX_TAXJAR_TOADDRESS_ISNT_SUPPLIED_ERROR + this.GetType() + ".DoCalc");

      try
      {
        var bodyPrms = new Dictionary<string, string>() { 
          {PRM_AMOUNT, amount.Value.ToString()},
          {PRM_SHIPPING, 0.ToString()}
        };

        bodyPrms.Add(PRM_FROM_COUNTRY, fromAddress.Country);
        bodyPrms.Add(PRM_FROM_STATE, fromAddress.Region);
        bodyPrms.Add(PRM_FROM_ZIP, fromAddress.PostalCode);

        bodyPrms.Add(PRM_TO_COUNTRY, toAddress.Country);
        bodyPrms.Add(PRM_TO_STATE, toAddress.Region);
        bodyPrms.Add(PRM_TO_ZIP, toAddress.PostalCode);

        var prms = new WebClient.RequestParams() {
          Uri = new Uri(CALC_URI),
          Caller = this,
          Headers = new Dictionary<string, string>() { { AUTH_HEADER_KEY, AUTH_HEADER_VALUE_PATTERN.Args(session.ApiKey) } },
          Method = HTTPRequestMethod.POST,
          BodyParameters = bodyPrms
        };

        dynamic obj = WebClient.GetJsonAsDynamic(prms);

        var result = new TaxStructured();

        result.Total = (decimal)obj.Data["tax"]["amount_to_collect"];

        return result;
      }
      catch (Exception ex)
      {
        var wex = ex as System.Net.WebException;

        if (wex != null)
        {
          var response = wex.Response as System.Net.HttpWebResponse;
          if (response != null)
          {
            string errorMessage = this.GetType().Name +
              ".DoCalc(fromAddress: {0}, toAddress: {1}, amount: '{2}', shipping: '{3}')".Args(fromAddress, toAddress, amount, shipping);
            var taxEx = TaxException.Compose(response, errorMessage, wex);
            throw taxEx;  
          }
        }

        throw new TaxException(StringConsts.TAX_CALC_ERROR + this.GetType()
          + " .Calc(session='{0}', fromAddress='{1}', toAddress='{2}', amount='{3}', shipping='{4}')"
            .Args(session, fromAddress, toAddress, amount, shipping), ex);
      }
    }