-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestCalcCanadaPostShipping.js
More file actions
250 lines (199 loc) · 8.39 KB
/
Copy pathtestCalcCanadaPostShipping.js
File metadata and controls
250 lines (199 loc) · 8.39 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
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
var TestLopez = TestLopez || {};
TestLopez.getCanadaRates = (function(){
/**
* Please develop a script that will allow us to click the "calculate" button
* in a SO (next to shipping cost field) under the shipping subtab and it will
* do an API call to the Canada Post to obtain real time rates. This should
* apply when Shipping methods ID = 5323, 5320 and 5322 are selected.
* Canada Post Credentials
* User Name : ******
* Password: ******
* - use C197794 as test customer.
* - Use SO form VW Sales Order Manager
* - set department to retail
* - set location = Warehouses : Canada
* - item use PL-PAX2-BLK
* - use 3400 Ridgeway Drive address for ship to address
* - if saving the SO, under the billing tab set Payment Method to Cash.
*
* Author: David P. Lopez 12/2016
**/
var output = {};
/**
* Event fired just before a
* database write operation
*
* @function beforeSubmit
**/
function beforeSubmit(type){
var salesOrder = nlapiGetNewRecord();
getCanadaPostRates(salesOrder);
}
//Query REST SERVICE
//URI & FORMAT OF REQUEST
//SUPPORTED VERBS GET POST PUT DELETE
/**
* Development URI Endpoint
* ReST Request:
* POST https://ct.soa-gw.canadapost.ca/rs/ship/price/{Accept}/{Content-Type}/{Authorization}/{Accept Language en-CA} + {QueryString}
**/
function getCanadaPostRates(salesOrder){
// Check if Order is eligible for Canada Post shipping rates
// done
if(!hasEligibleShippingMethod(salesOrder)){
nlapiLogExecution("AUDIT", "CP Shipping Method: ", "This order is not eligible for Canada Post Shipping"); //2
return;
}
getRates(salesOrder);
//addRates(salesOrder);
}
// done
function hasEligibleShippingMethod(salesOrder){
// 1st get value of selected shipping method
// done
var selectedShippingMethod = salesOrder.getFieldValue("shipmethod");
nlapiLogExecution("DEBUG", "selectedShippingMethod: ", selectedShippingMethod); //1
// return true if the selectedShippingMethod matches Method ID's == 5323, 5320, or 5322
// done
return (selectedShippingMethod == 1008 || selectedShippingMethod == 1008 || selectedShippingMethod == 1008);
}
function addRates(salesOrder){
getRates(salesOrder);
//salesOrder.setLineItemValue();
// etc etc etc
// not complete!!!!!!
}
function getRates(salesOrder){
nlapiLogExecution("AUDIT", "CP Getting Rates: ", "***Connecting to Canada Post API***");
/**
* Canada Post API credentials:
* Base64 encoding needed
**/
var apiKey = nlapiEncrypt("***************:******************", "base64");
nlapiLogExecution("AUDIT", "EncodedApiKey: ", apiKey);
// assign value to shipToCountry;
var shipToCountry = salesOrder.getFieldValue("shipcountry");
nlapiLogExecution("AUDIT", "shipToCountry: ", shipToCountry);
nlapiLogExecution("AUDIT", "shipToCountry Type: ", typeof(shipToCountry));
// assign value to postalCode;
var postalCode = hasCorrectPostalCodeFormat(salesOrder.getFieldValue("shipzip"));
nlapiLogExecution("AUDIT", "postalCode: ", postalCode);
nlapiLogExecution("AUDIT", "postalCode Type: ", typeof(postalCode));
// assign value to zipCode;
var zipCode = parseInt(hasCorrectPostalCodeFormat(salesOrder.getFieldValue("shipzip")));
nlapiLogExecution("AUDIT", "zipCode: ", zipCode);
nlapiLogExecution("AUDIT", "zipCode Type: ", typeof(zipCode));
// access weight from each item in item sublist and accumulate using totalWeight
var itemID;
var itemWeightLbs;
var totalWeightKg = 0;
var itemCount = salesOrder.getLineItemCount("item");
for(var i = 1; i <= itemCount; i++){
itemID = salesOrder.getLineItemValue("item", "item", i);
itemWeightLbs = nlapiLookupField("item", itemID, "weight");
totalWeightKg += convertWeight(parseInt(itemWeightLbs));
}
nlapiLogExecution("AUDIT", "totalWeightKg: ", totalWeightKg);
nlapiLogExecution("AUDIT", "totalWeightKg Type: ", typeof(totalWeightKg));
// get the originating postalCode from the location record
// Format ANANAN
var originPostalCode = hasCorrectPostalCodeFormat(nlapiLookupField("location", "name", "zip"));
nlapiLogExecution("AUDIT", "originPostalCode: ", originPostalCode);
nlapiLogExecution("AUDIT", "originPostalCode Type: ", typeof(originPostalCode));
// XML Request Template
var quoteType = "counter";
var xml;
if(shipToCountry == "US"){
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<mailing-scenario xmlns=\"http://www.canadapost.ca/ws/ship/rate-v3\">" +
"<quote-type>" + quoteType + "</quote-type>" +
"<parcel-characteristics>" +
"<weight>" + totalWeightKg + "</weight>" +
"</parcel-characteristics>" +
"<origin-postal-code>" + originPostalCode + "</origin-postal-code>" +
"<destination>" +
"<united-states>" +
"<zip-code>" + zipCode + "</zip-code>" +
"</united-states>" +
"</destination>" +
"</mailing-scenario>";
}else if(shipToCountry == "CA"){
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<mailing-scenario xmlns=\"http://www.canadapost.ca/ws/ship/rate-v3\">" +
"<quote-type>" + quoteType + "</quote-type>" +
"<parcel-characteristics>" +
"<weight>" + totalWeightKg + "</weight>" +
"</parcel-characteristics>" +
"<origin-postal-code>" + originPostalCode + "</origin-postal-code>" +
"<destination>" +
"<domestic>" +
"<postal-code>" + postalCode + "</postal-code>" +
"</domestic>" +
"</destination>" +
"</mailing-scenario>";
}
//ENDPOINT POST https://XX/rs/ship/price
// XX (Development): ct.soa-gw.canadapost.ca*
// XX (Production): soa-gw.canadapost.ca
var url = "https://ct.soa-gw.canadapost.ca/rs/ship/price"; // + accept + "/" + contentType + "/" + authorization + "/" + acceptLanguage ;
// RBody used for POST Request
var postData = nlapiStringToXML(xml);
nlapiLogExecution("AUDIT", "xml: ", xml);
// header variables
// application/vnd.cpc.ship.rate-v3+xml (Note: */* in place of the header value will return an error)
var xmlRequest = "/application/vnd.cpc.ship.rate-v3+xml";
// application/vnd.cpc.ship.rate-v3+xml (Note: */* in place of the header value will return an error)
var isXml = "/application/vnd.cpc.ship.rate-v3+xml";
// Basic {Base64 encoding of userid:password}
var credentials = "Basic " + apiKey;
nlapiLogExecution("AUDIT", "credentials: ", credentials);
nlapiLogExecution("AUDIT", "credentials Type: ", typeof(credentials));
// en-CA or fr-CA
var isEnglish = "en-CA";
// POST Request Headers
var headers = { "Accept" : xmlRequest,
"Content-Type" : isXml,
"Authorization" : credentials,
"Accept-language" : isEnglish
};
// Call nlapiRequestURL(url, postdata, headers, callback, httpMethod) and save response
var response = nlapiRequestURL(url, postData, headers);
nlapiLogExecution("AUDIT", "response: ", response);
var responseHeaderTitles = response.getAllHeaders();
var responseHeaderValues = [];
for(var header = 0; header < responseHeaderTitles.length; header++){
responseHeaderValues.push(response.getHeader(responseHeaderTitles[header]));
nlapiLogExecution("AUDIT", responseHeaderTitles[header] + ": " , responseHeaderValues[header]);
}
var responseBody = response.getBody();
nlapiLogExecution("AUDIT", "responseBody: ", responseBody);
var responseCode = response.getCode();
nlapiLogExecution("AUDIT", "responseCode: ", responseCode);
//parse xml in javascript with xpath
//you get xml as a string
//pass it to NS xml api --> turns string into xml doc object
//on xml object use Xpath
}
function convertWeight(weightInLbs){
var Lbs = 2.2;
var kg = weightInLbs / Lbs;
return Math.round(kg*1000)/1000;
}
function hasCorrectPostalCodeFormat(postalCode){
// Format ANANAN
var newPostalCode = postalCode.replace(/\s+/g, '');
return newPostalCode.toUpperCase();
}
output.beforeSubmit = beforeSubmit;
return output;
}) ();
/*
Status Code: 400 Bad Request
Content-Type: text/xml; charset=UTF-8
Date: Thu, 08 Dec 2016 22:44:55 GMT
Server: Oracle-iPlanet-Web-Server/7.0
Transfer-Encoding: chunked
X-backside-transport: FAIL FAIL,FAIL FAIL
X-client-ip: 10.229.109.82
X-global-transaction-id: 94113857
*/