-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
244 lines (217 loc) · 6.92 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
/**
* ShippingResponse Custom Shipping Endpoint Helper
*
* @author foxy.io
* @copyright foxy.io LLC
* @version 1.1.0
* @license MIT http://opensource.org/licenses/MIT
*/
function ShippingResponse(cart_details) {
if (cart_details === undefined) {
console.log("Error: The cart JSON payload is required to be passed to the ShippingResponse object");
return false;
}
this.rates = (cart_details["_embedded"].hasOwnProperty("fx:shipping_results")) ? cart_details["_embedded"]["fx:shipping_results"] : [];
this.error_message = false;
this.cart_details = cart_details;
}
/**
* Add a custom rate
*
* @param int service_id
* @param float price
* @param string method
* @param string service_name
* @param boolean add_flat_rate
* @param boolean add_handling
**/
ShippingResponse.prototype.add = function(service_id, price, method, service_name, add_flat_rate, add_handling) {
add_flat_rate = (add_flat_rate === undefined) ? true : add_flat_rate;
add_handling = (add_flat_rate === undefined) ? true : add_handling;
if (service_id < 10000) {
service_id += 10000;
}
if (add_handling) {
price += this.cart_details["_embedded"]["fx:shipment"]["total_handling_fee"];
}
if (add_flat_rate) {
price += this.cart_details["_embedded"]["fx:shipment"]["total_flat_rate_shipping"];
}
this.rates.push({"method": method, "price": price, "service_id": service_id, "service_name": service_name});
};
/**
* Hide a rate that has been added
*
* @param int|string|array selector
**/
ShippingResponse.prototype.hide = function(selector) {
var rates = this.filterShippingOptions(selector);
for (var i = 0; i < rates.length; i++) {
this.rates[rates[i]].hide = true;
}
};
/**
* Show a rate that has been hidden
*
* @param int|string|array selector
**/
ShippingResponse.prototype.show = function(selector) {
var rates = this.filterShippingOptions(selector);
for (var i = 0; i < rates.length; i++) {
this.rates[rates[i]].hide = false;
}
};
/**
* Alias for hide();
*
* @param int|string|array selector
**/
ShippingResponse.prototype.remove = function(selector) {
this.hide(selector);
};
/**
* Update an existing rate's price, service name or method
*
* @param int|string|array selector
* @param int|string modifier
* @param string method
* @param string service_name
**/
ShippingResponse.prototype.update = function(selector, modifier, method, service_name) {
var rates = this.filterShippingOptions(selector);
for (var i = 0; i < rates.length; i++) {
if (typeof(modifier) === "number" || (typeof(modifier) === "string" && modifier !== "")) {
var price = this.modifyPrice(this.rates[rates[i]].price, modifier);
this.rates[rates[i]]["price"] = price;
}
if (typeof(method) === "string") {
this.rates[rates[i]]["method"] = method;
}
if (typeof(service_name) === "string") {
this.rates[rates[i]]["service_name"] = service_name;
}
}
};
/**
* Empty any existing rates and error message
*
**/
ShippingResponse.prototype.reset = function() {
this.rates = [];
this.error_message = false;
};
/**
* Add an error message
*
* @param string message
**/
ShippingResponse.prototype.error = function(message) {
this.error_message = message;
};
/**
* Output the existing rates or error message as a JSON encoded string
*
* @param boolean output_as_string
* @return string
**/
ShippingResponse.prototype.output = function(output_as_string) {
output_as_string = (output_as_string === undefined) ? true : output_as_string;
var response = {};
if (this.error_message !== false) {
response["ok"] = false;
response["details"] = this.error_message;
} else {
var rates = this.rates.filter(function(r) {
return (!r.hasOwnProperty("hide") || !r["hide"]);
});
response["ok"] = true;
response["data"] = {
"shipping_results": rates
};
}
if (output_as_string) {
response = JSON.stringify(response);
}
return response;
};
ShippingResponse.prototype.filterShippingOptions = function(selector) {
if (typeof(selector) == "number") {
// They've just provided a rate code
for (var i = 0; i < this.rates.length; i++) {
if (this.rates[i].service_id == selector) {
return [i];
}
}
} else if (typeof(selector) == "string") {
// It's a string, must be a combination of carrier and service or all
var rate_codes = [];
var rates = {};
for (var i = 0; i < this.rates.length; i++) {
var rate = this.rates[i];
rates[i] = rate.method + " " + rate.service_name;
}
if (selector.toLowerCase() != "all") {
// Some filter has been specified
var regex = /(fedex|usps|ups)?\s?([\w\s]+)?/i,
provider = regex.exec(selector);
if (provider === undefined || provider === "") return;
for (var i in rates) {
if (provider[1] !== undefined && provider[1] !== "") {
if (rates[i].toLowerCase().indexOf(provider[1].toLowerCase()) == -1) {
delete rates[i];
continue;
}
}
if (provider[2] !== undefined && provider[2] !== "") {
if (rates[i].toLowerCase().indexOf(provider[2].toLowerCase()) == -1) {
delete rates[i];
}
}
}
}
for (var i in rates) {
rate_codes.push(parseInt(i));
}
return rate_codes;
} else if (typeof(selector) == "object") {
// Assume it's an array of codes
var rate_codes = [];
for (var i = 0; i < selector.length; i++) {
for (var r = 0; r < this.rates.length; r++) {
if (this.rates[r].service_id == selector[i]) {
rate_codes.push(r);
}
}
}
return rate_codes;
}
};
ShippingResponse.prototype.modifyPrice = function(price, modifier) {
var modifier = modifier.toString(),
regex = /([\+\-\=\*\/])?(\d+(?:\.\d+)?)(%)?/,
parts = regex.exec(modifier),
price = parseFloat(price),
modifyBy = parseFloat(parts[2]);
if (parts[3] != undefined && parts[3] != "") {
modifyBy = price * (modifyBy / 100);
}
var operator = (parts[1] === undefined && parts[1] !== "") ? "=" : parts[1];
switch(operator) {
case "+":
price = (price + modifyBy);
break;
case "-":
price = (price - modifyBy);
break;
case "/":
price = (price / modifyBy);
break;
case "*":
price = (price * modifyBy);
break;
default:
price = modifyBy;
}
return (price < 0) ? 0 : price;
};
module.exports = ShippingResponse;