-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfedex.js
More file actions
75 lines (68 loc) · 2 KB
/
Copy pathfedex.js
File metadata and controls
75 lines (68 loc) · 2 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
var https = require('https');
var xml2js = require('xml2js');
function fedex(key, password, accountNumber, meterNumber) {
this.authReq = buildAuth(key, password, accountNumber, meterNumber);
}
fedex.prototype.getShippingInfo = function(trackingNumber, callback) {
//TODO change to live
//ws.fedex.com
req = https.request({
host: 'wsbeta.fedex.com',
path: '/xml',
method: 'POST'
});
var xml = buildCommon(this.authReq + buildTrack(trackingNumber));
req.write(xml);
req.on('response', 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.TrackReply) {
callback(result.TrackReply);
}
} else {
console.log("Err first: " + err);
callback(err);
}
});
});
});
req.end();
}
function buildAuth(key, password, accountNumber, meterNumber) {
return "<WebAuthenticationDetail>"+
"<UserCredential>" +
"<Key>"+key+"</Key>"+
"<Password>"+password+"</Password>"+
"</UserCredential>"+
"</WebAuthenticationDetail>"+
"<ClientDetail>"+
"<AccountNumber>"+accountNumber+"</AccountNumber>"+
"<MeterNumber>"+meterNumber+"</MeterNumber>"+
"</ClientDetail>";
}
function buildTrack(trackingNumber) {
return "<TransactionDetail>"+
"<CustomerTransactionId>ActiveShipping</CustomerTransactionId>"+
"</TransactionDetail>"+
"<Version>"+
"<ServiceId>trck</ServiceId>"+
"<Major>6</Major>"+
"<Intermediate>0</Intermediate>"+
"<Minor>0</Minor>"+
"</Version>"+
"<PackageIdentifier>"+
"<Value>"+trackingNumber+"</Value>"+
"<Type>TRACKING_NUMBER_OR_DOORTAG</Type>"+
"</PackageIdentifier>"+
"<IncludeDetailedScans>true</IncludeDetailedScans>";
}
function buildCommon(xmlInsert) {
return "<TrackRequest xmlns='http://fedex.com/ws/track/v6'>"+xmlInsert+"</TrackRequest>";
}
module.exports = fedex;