-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaxCalculator.cs
57 lines (53 loc) · 1.66 KB
/
TaxCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using RestSharp;
using Newtonsoft.Json.Linq;
using System;
namespace Tax_Service
{
public class TaxCalculator : ITaxCalculator
{
private const string _apiUrl = "https://api.taxjar.com/v2/";
private const string _apiKey = "5da2f821eee4035db4771edab942a4cc";
private RestClient _client;
public TaxCalculator()
{
_client = new RestClient(_apiUrl);
}
public Rates GetRates(string zipcode)
{
Rates rtn = null;
var path = "rates/" + zipcode;
try
{
var request = new RestRequest(path, Method.GET);
request.AddHeader("Authorization", "Bearer " + _apiKey);
var rtnJson = _client.Get(request).Content;
var jo = JObject.Parse(rtnJson);
rtn = new Rates(jo);
}
catch (Exception ex)
{
// log the exception
}
return rtn;
}
public string CalculateTaxesForOrder(object order)
{
var rtn = "";
var path = "taxes";
try
{
var request = new RestRequest(path, Method.POST);
request.AddHeader("Authorization", "Bearer " + _apiKey);
request.AddJsonBody(order);
var rtnJson = _client.Post(request).Content;
var jo = JObject.Parse(rtnJson);
rtn = jo["tax"]["amount_to_collect"].ToString();
}
catch (Exception ex)
{
// log the exception
}
return rtn;
}
}
}