-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestUPS.js
More file actions
104 lines (102 loc) · 3.61 KB
/
TestUPS.js
File metadata and controls
104 lines (102 loc) · 3.61 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
import System;
import System.IO;
import System.Net;
import System.Xml;
import System.Xml.Linq;
/*
Sample script for Infor Smart Office to validate addresses with the UPS Street Level API
PENDING: replace authentication and address values + error handling + background thread + user interface
https://www.ups.com/upsdeveloperkit
*/
package MForms.JScript {
class TestUPS {
public function Init(element: Object, args: Object, controller : Object, debug : Object) {
// authentication
var doc1: XDocument = new XDocument(
new XDeclaration("1.0", "utf-8"),
new XElement("AccessRequest",
new XElement("AccessLicenseNumber", "****************"),
new XElement("UserId", "******"),
new XElement("Password", "********")
)
);
// address
var doc2: XDocument = new XDocument(
new XDeclaration("1.0", "utf-8"),
new XElement("AddressValidationRequest",
new XElement("Request",
new XElement("TransactionReference",
new XElement("CustomerContext", "Infor Smart Office"),
new XElement("XpciVersion", "1.0"),
),
new XElement("RequestAction", "XAV"),
new XElement("RequestOption", "3")
),
new XElement("AddressKeyFormat",
new XElement("ConsigneeName", "Ciber"), // Name
new XElement("BuildingName", ""),
new XElement("AddressLine", "Fiddlers Green"), // Address line 1
new XElement("AddressLine", ""), // Address line 2
new XElement("AddressLine", ""), // Address line 3
new XElement("AddressLine", ""), // Address line 4
new XElement("Region", ""),
new XElement("PoliticalDivision2", "Greenwd"), // City
new XElement("PoliticalDivision1", "CO"), // State
new XElement("PostcodePrimaryLow", ""), // Zip5
new XElement("PostcodeExtendedLow", ""), // Zip4
new XElement("Urbanization", ""),
new XElement("CountryCode", "US") // Country
)
)
);
// concatenate both XML docs
var sw: StringWriter = new StringWriter();
doc1.Save(sw);
doc2.Save(sw);
var docs: String = sw.GetStringBuilder().ToString();
// HTTP request
var request: HttpWebRequest = HttpWebRequest(WebRequest.Create("https://onlinetools.ups.com/ups.app/xml/XAV"));
request.Method = "POST";
var byteArray: byte[] = System.Text.Encoding.UTF8.GetBytes(docs);
var dataStream: Stream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
// HTTP response
var response: HttpWebResponse = request.GetResponse();
var data: Stream = response.GetResponseStream();
var doc: XmlDocument = new XmlDocument();
doc.Load(data);
data.Close();
response.Close();
// check for errors
var error: XmlNode = doc.SelectSingleNode("//Response/Error");
if (error != null) {
debug.WriteLine("Error " + error.SelectSingleNode("ErrorCode").InnerText + ": " + error.SelectSingleNode("ErrorDescription").InnerText);
return;
}
// show results
var nodes: XmlNodeList = doc.SelectNodes("//AddressKeyFormat");
var keys : String[] = [
"AddressClassification/Description",
"ConsigneeName",
"BuildingName",
"AddressLine[1]",
"AddressLine[2]",
"PoliticalDivision2",
"PoliticalDivision1",
"PostcodePrimaryLow",
"PostcodeExtendedLow",
//"Region",
"Urbanization",
"CountryCode"
];
for (var node: XmlNode in nodes) {
for (var i: int in keys) {
var value: XmlNode = node.SelectSingleNode(keys[i]);
debug.Write(value != null ? value.InnerText + ", " : "");
}
debug.WriteLine("");
}
}
}
}