-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShippingResponse.php
241 lines (220 loc) · 6.24 KB
/
ShippingResponse.php
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
<?php
/**
* ShippingResponse Custom Shipping Endpoint Helper
*
* @author foxy.io
* @copyright foxy.io
* @version 1.1
* @license MIT http://opensource.org/licenses/MIT
*/
Class ShippingResponse {
function __construct($cart_details = false) {
if ($cart_details == false) {
http_response_code(400);
echo("ShippingResponse Error: The cart JSON payload is required to be passed to the ShippingResponse object");
exit();
}
$this->rates = (array_key_exists('fx:shipping_results', $cart_details['_embedded'])) ? $cart_details['_embedded']['fx:shipping_results'] : array();
$this->error = 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
**/
public function add($service_id, $price, $method = '', $service_name = '', $add_flat_rate = true, $add_handling = true) {
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[] = array(
"service_id" => $service_id,
"price" => $price,
"method" => $method,
"service_name" => $service_name
);
}
/**
* Hide a rate that has been added
*
* @param int|string|array $selector
**/
public function hide($selector) {
$filtered = $this->filterShippingOptions($selector);
if (is_array($filtered)) {
foreach ($filtered as $rate) {
$this->rates[$rate]['hide'] = true;
}
}
}
/**
* Show a rate that has been hidden
*
* @param int|string|array $selector
**/
public function show($selector) {
$filtered = $this->filterShippingOptions($selector);
if (is_array($filtered)) {
foreach ($filtered as $rate) {
unset($this->rates[$rate]['hide']);
}
}
}
/**
* Alias for hide();
*
* @param int|string|array $selector
**/
public function remove($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
**/
public function update($selector, $modifier, $method = false, $service_name = false) {
$filtered = $this->filterShippingOptions($selector);
if (is_array($filtered)) {
foreach ($filtered as $rate) {
if (is_numeric($modifier) || (gettype($modifier) == "string" && $modifier !== "")) {
$this->rates[$rate]['price'] = $this->modifyPrice($this->rates[$rate]['price'], $modifier);
}
if (gettype($method) == "string") {
$this->rates[$rate]['method'] = $method;
}
if (gettype($service_name) == "string") {
$this->rates[$rate]['service_name'] = $service_name;
}
}
}
}
/**
* Empty any existing rates and error message
*
**/
public function reset() {
$this->rates = array();
$this->error = false;
}
/**
* Add an error message
*
* @param string $message
**/
public function error($message = false) {
$this->error = $message;
}
/**
* Output the existing rates or error message as a JSON encoded string
*
* @return string
**/
public function output($output_as_string = true) {
$response = array();
if ($this->error !== false) {
$response["ok"] = false;
$response["details"] = $this->error;
} else {
$rates = array_filter($this->rates, function($rate) {
return array_key_exists("hide", $rate) == false || $rate['hide'] == false;
});
$response["ok"] = true;
$response["data"] = array(
"shipping_results" => $rates
);
}
if ($output_as_string) {
$response = json_encode($response);
}
return $response;
}
private function filterShippingOptions($selector) {
if (gettype($selector) == "integer") {
foreach ($this->rates as $i => $rate) {
if ($rate['service_id'] == $selector) {
return array($i);
}
}
} else if (gettype($selector) == "string") {
$rate_codes = array();
$rates = array();
foreach ($this->rates as $i => $rate) {
$rates[$i] = $rate['method'] . " " . $rate['service_name'];
}
if (strtolower($selector) != "all") {
$regex = '/(fedex|usps|ups)?\s?([\w\s]+)?/i';
if (preg_match($regex, $selector, $rate_label)) {
foreach ($rates as $i => $rate) {
if (!empty($rate_label[1])) {
if (strpos(strtolower($rate), strtolower($rate_label[1])) === false) {
unset($rates[$i]);
continue;
}
}
if (!empty($rate_label[2])) {
if (strpos(strtolower($rate), strtolower($rate_label[2])) === false) {
unset($rates[$i]);
}
}
}
} else {
return;
}
}
return array_keys($rates);
} else if (gettype($selector) == "array") {
$rate_codes = array();
foreach ($selector as $code) {
foreach ($this->rates as $i => $rate) {
if ($code == $rate["service_id"]) {
$rate_codes[] = $i;
}
}
}
return $rate_codes;
}
}
private function modifyPrice($price, $modifier) {
$regex = '/([\+\-\=\*\/])?(\d+(?:\.\d+)?)(\%)?/';
preg_match($regex, $modifier, $parts);
$price = floatval($price);
$modifyBy = floatval($parts[2]);
if (!empty($parts[3])) {
$modifyBy = $price * ($modifyBy / 100);
}
$operator = (!empty($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;
break;
}
return ($price < 0) ? 0 : $price;
}
}