-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionBuilder.java
More file actions
117 lines (98 loc) · 4.36 KB
/
TransactionBuilder.java
File metadata and controls
117 lines (98 loc) · 4.36 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package spring;
import JSON.JSONParser;
import com.google.gson.Gson;
import net.avalara.avatax.rest.client.enums.DocumentType;
import net.avalara.avatax.rest.client.models.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@SessionAttributes({"taxcode","zip","username","password"})
public class TransactionBuilder {
@RequestMapping(value="/transaction", method=RequestMethod.GET)
@ResponseBody
public String transactionBuilder(HttpServletRequest post, @RequestParam String taxcode,
@RequestParam String zip, @RequestParam String username, @RequestParam String password) {
String rate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = new Date();
CreateTransactionModel CTModel = new CreateTransactionModel();
LineItemModel line = new LineItemModel();
line.setTaxCode(taxcode);
line.setDescription("doesn't matter");
BigDecimal decimal = BigDecimal.valueOf(100);
line.setAmount(decimal);
line.setQuantity(decimal);
ArrayList<LineItemModel> lines = new ArrayList<>();
lines.add(line);
AddressesModel addModel = new AddressesModel();
AddressLocationInfo addInfo = new AddressLocationInfo();
addInfo.setPostalCode(zip);
addModel.setSingleLocation(addInfo);
CTModel.setDate(date);
CTModel.setCompanyCode("DEFAULT");
CTModel.setCustomerCode("custom");
CTModel.setType(DocumentType.SalesOrder);
CTModel.setCurrencyCode("USD");
CTModel.setCommit(false);
CTModel.setAddresses(addModel);
CTModel.setLines(lines);
Gson gson = new Gson();
try {
String userPass = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
String requestUrl = "https://rest.avatax.com/api/v2/transactions/create";
StringEntity requestEntity = new StringEntity(gson.toJson(CTModel), ContentType.APPLICATION_JSON);
/*
RESOURCES: https://hc.apache.org/httpcomponents-client-ga/quickstart.html
info for using the HttpClient
*/
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(requestUrl);
httpGet.addHeader("accept", "application/json");
httpGet.addHeader("authorization", "Basic " + userPass);
// httpGet.setEntity(requestEntity);
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200 && (response.getStatusLine().getStatusCode() != 201)) {
throw new RuntimeException("Error : " + response.getStatusLine().getStatusCode());
}
BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity()
.getContent())));
String scan;
while ((scan = reader.readLine()) != null) {
rate = scan;
}
//parses out the tax rate and sums the tax's then
// converts to string after formatting it to 3 decimals places.
DecimalFormat d3 = new DecimalFormat("#.000");
double rateDouble = JSONParser.parseTaxRate(rate);
rateDouble *= 100;
rate = d3.format(rateDouble);
rate = rate + "%";
System.out.println(rate);
return rate;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(rate);
return rate;
}
}