-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
296 lines (223 loc) · 6.47 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
var https = require('https');
var url = require('url');
var latestAPIVersion = "1.0";
/*
===========
Constructor
===========
*/
function AvaTax(username, password, options) {
if (typeof username !== "string" || typeof password !== "string") {
throw new Error("Credentials not supplied for AvaTax");
}
this.username = username;
this.password = password;
options = options || {};
//Development or Production
options.development = options.development || false;
options.version = options.version || latestAPIVersion;
options.client = options.client !== undefined ? options.client : "node-avatax";
options.client = options.client === false ? undefined : options.client;
if (options.version > latestAPIVersion) {
console.warn("This library does not support an API version greater than `" + latestAPIVersion + "`. Contact this module's author to allow for newer versions.");
}
var authenticationHeader = "Basic " + new Buffer([this.username, this.password].join(":"), 'utf8').toString('base64');
this.requestOptions = function() {
var requestOptions = {
port: 443,//https
method: 'GET',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": authenticationHeader
}
};
requestOptions.host = options.development ? 'development.avalara.net' : 'avatax.avalara.net';
//allow overriding of host completely
requestOptions.host = options.host || requestOptions.host;
return requestOptions;
};
this.options = options;
return this;
}
/*
=========
Utilities
=========
*/
AvaTax.prototype._makeRequest = function(requestOptions, requestBody, next) {
var req = https.request(requestOptions, function(res) {
var responseBody = "";
res.setEncoding('utf-8');
res.on('data', function(chunk) {
responseBody += chunk;
});
res.on('end', function() {
var errorText = "AvaTax server error";
var err;
var json = null;
try {
json = JSON.parse(responseBody);
} catch (e) {}
if (res.statusCode >= 400) {
if (
json &&
json.Messages &&
json.Messages.length &&
json.Messages[0].Summary
) {
errorText = json.Messages[0].Summary;
}
err = new Error(errorText);
err.code = res.statusCode;
}
next(err, json);
});
});
req.on('error', next);
req.end(requestBody);
};
/*
========
Validate Address
========
*/
//returns true if all required fields are present
function validateAddressFields(address) {
return address && (
(
address.Line1 !== undefined &&
address.PostalCode !== undefined
) || (
address.Line1 !== undefined &&
address.City !== undefined &&
address.Region !== undefined
)
);
}
AvaTax.prototype._validateAddress = function(options, next) {
var requestOptions = this.requestOptions();
requestOptions.path = url.format({
pathname: "/" + this.options.version + '/address/validate',
query: options
});
this._makeRequest(requestOptions, null, function(err, json) {
if (err) {
return next(err);
}
if (json.ResultCode === "Error") {
return next(new Error(json.Messages[0].Summary));
}
if (json.ResultCode === "Success") {
return next(null, json.Address);
}
return next(null, json);
});
};
AvaTax.prototype.validateAddress = function(address, next) {
if (!validateAddressFields(address)) {
//return an error if both Line+Postal OR Line+City+Region aren't satisfied
return next(new Error("You must specify at least Line & Postal Code, or Line & City & Region"));
}
return AvaTax.prototype._validateAddress.call(this, address, next);
};
/*
========
Tax
========
*/
AvaTax.prototype._estimateTax = function(options, next) {
options = options || {};
var requestOptions = this.requestOptions();
requestOptions.path = url.format({
pathname: "/" + this.options.version + '/tax/' + options.latitude + "," + options.longitude + "/get",
query: {
saleamount: options.amount
}
});
this._makeRequest(requestOptions, null, next);
};
AvaTax.prototype.estimateTax = function(latitude, longitude, amount, next) {
if (!isFinite(latitude) || !isFinite(longitude) || !isFinite(amount)) {
throw new Error("Invalid arguments for `estimateTax`");
}
var options = {
latitude: latitude,
longitude: longitude,
amount: amount
};
return AvaTax.prototype._estimateTax.call(this, options, next);
};
AvaTax.prototype._getTax = function(options, next) {
options = options || {};
var requestBody = JSON.stringify(options, null, '\t');
var requestOptions = this.requestOptions();
requestOptions.path = url.format({
pathname: "/" + this.options.version + '/tax/get'
});
requestOptions.method = "POST";
this._makeRequest(requestOptions, requestBody, next);
};
AvaTax.prototype.getTax = function(options, next) {
if (!options) {
throw new Error("Options are missing for `getTax`");
}
options.DocDate = options.DocDate || new Date();
options.Commit = options.Commit !== undefined ? options.Commit : false;
options.CurrencyCode = options.CurrencyCode || "USD";
options.Client = options.Client !== undefined ? options.Client : this.options.client;
if (options.DocDate instanceof Date) {
var docDateYear = options.DocDate.getFullYear();
var docDateMonth = options.DocDate.getMonth() + 1;
var docDateDate = options.DocDate.getDate();
options.DocDate = [
docDateYear,
docDateMonth < 10 ? "0" + docDateMonth : docDateMonth,
docDateDate < 10 ? "0" + docDateDate : docDateDate
].join("-");
}
if (
options.CustomerCode === undefined ||
!Array.isArray(options.Lines) ||
!options.Lines.length ||
!Array.isArray(options.Addresses) ||
!options.Addresses.length
) {
return next(new Error("Missing required fields"));
}
var err = null;
options.Lines.some(function(line, index) {
if (
line.LineNo === undefined ||
line.DestinationCode === undefined ||
line.OriginCode === undefined ||
!isFinite(line.Qty) ||
!isFinite(line.Amount)
) {
err = new Error("Line indexOf#" + index + " is missing required fields");
return err;
}
});
options.Addresses.some(function(address, index) {
if (
address.AddressCode === undefined ||
!(
validateAddressFields(address) ||
address.TaxRegionId !== undefined ||
(
address.Latitude !== undefined &&
address.Longitude !== undefined
)
)
) {
err = next(new Error("Address indexOf#" + index + " is missing required fields"));
return err;
}
});
if (err) {
return next(err);
}
return AvaTax.prototype._getTax.call(this, options, next);
};
module.exports = AvaTax;