-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusps.js
More file actions
60 lines (52 loc) · 1.47 KB
/
Copy pathusps.js
File metadata and controls
60 lines (52 loc) · 1.47 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
var http = require('http');
var xml2js = require('xml2js');
function usps(username) {
this.username = username;
}
usps.prototype.getShippingInfo = function (trackingNumber, callback) {
var path = '<TrackRequest USERID="'+this.userName+'">'+'<TrackID ID="'+trackingNumber+'"></TrackID></TrackRequest>';
sendRequest(path, callback);
}
usps.prototype.getMultipleShippingInfo = function (trackingNumberArr, callback) {
var path = '<TrackRequest USERID="'+this.userName+'">';
for (var i = 0; i < trackingNumberArr.length; i++) {
path += '<TrackID ID="'+trackingNumberArr[i]+'"></TrackID>';
}
path += '</TrackRequest>';
sendRequest(path, callback);
}
function sendRequest(path, callback) {
//production.shippingapis.com TODO change
var reqObj = {
host: 'testing.shippingapis.com',
port: 80,
path: '/ShippingAPITest.dll?API=TrackV2&XML='+encodeURIComponent(path),
headers: {
'Content-Type': 'text/xml'
}
};
http.get(reqObj, function(res) {
var str = '';
res.on('data', function(data) {
str += data;
});
res.on("end", function() {
var parser = new xml2js.Parser();
parser.parseString(str, function(err, result){
if (!err) {
if (result.TrackResponse) {
//console.log(result.TrackResponse);
callback(result.TrackResponse);
}
} else {
console.log("Err first: " + err);
callback(err);
}
});
});
}).on('error', function(e) {
console.log("Err second: " + e.message);
callback(e.message);
});
}
module.exports = usps;