forked from flydreamers/shipwire-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.php
184 lines (165 loc) · 4.98 KB
/
Order.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
<?php
namespace flydreamers\shipwire;
use flydreamers\shipwire\base\ShipwireComponent;
/**
* Class Order
* @package flydreamers\shipwire
* @author Sebastian Thierer <sebas@flydreamers.com>
*/
class Order extends ShipwireComponent
{
/**
* Lists all orders depending on your parameters.
* @param array $params
* @param int $page
* @param int $limit
* @return array
*/
public function listing($params = [], $page = 0, $limit = 50)
{
return $this->get('orders', $params, $page, $limit);
}
/**
* Gets order details
* @param $orderId
* @param bool $expand
* @return array
*/
public function orderDetails($orderId, $expand = false)
{
$params = [];
if ($expand) {
$params['expand'] = 'all';
}
return $this->get($this->getRoute('orders/{id}', $orderId), $params);
}
/**
* Cancels an Order
* @param $orderId
* @return bool
*/
public function cancel($orderId)
{
$ret = $this->post($this->getRoute('orders/{id}/cancel', $orderId), [], null, true);
return $ret['status'] == 200;
}
/**
* Transform the route with the given orderId
* @param $route
* @param null $orderId
* @return string
*/
private function getRoute($route, $orderId = null)
{
if ($orderId !== null) {
return strtr($route, ['{id}' => $orderId]);
}
return $route;
}
/**
* Get the list of holds, if any, on an order
* @param $orderId
* @param array $params
* @param int $page
* @param int $limit
* @return array
*/
public function holds($orderId, $includeCleared = false, $params = [], $page = 0, $limit = 50)
{
if (!isset($params['includeCleared'])) {
$params['includeCleared'] = $includeCleared ? 1 : 0;
}
return $this->get($this->getRoute('orders/{id}/holds', $orderId), $params, $page, $limit);
}
/**
* Get the product details for this order
* @param $orderId
* @param array $params
* @param int $page
* @param int $limit
* @return array
*/
public function items($orderId, $params = [], $page = 0, $limit = 50)
{
return $this->get($this->getRoute('orders/{id}/items', $orderId), $params, $page, $limit);
}
/**
* Get related return information for a specific order.
* @param $orderId
* @param array $params
* @param int $page
* @param int $limit
* @return array
*/
public function returns($orderId, $params = [], $page = 0, $limit = 50)
{
return $this->get($this->getRoute('orders/{id}/returns', $orderId), $params, $page, $limit);
}
/**
* Get tracking information for a specific order.
* @param $orderId
* @param array $params
* @param int $page
* @param int $limit
* @return array
*/
public function tracking($orderId, $params = [], $page = 0, $limit = 50)
{
return $this->get($this->getRoute('orders/{id}/trackings', $orderId), $params, $page, $limit);
}
/**
* Creates an order. Validates OrderData with the ModelSchema at https://www.shipwire.com/w/developers/orders/#panel-shipwire1
* @param $orderData
* @param array $params
* @return array|bool
*/
public function create($orderData, $params = [])
{
if ($this->validateOrderData($orderData)) {
return $this->post('orders', $params, json_encode($orderData));
}
return false;
}
/**
* Modify order details. Validates OrderData with the ModelSchema at https://www.shipwire.com/w/developers/orders/#panel-shipwire1
* @param $orderData
* @param array $params
* @return array|bool
*/
public function update($orderId, $orderData, $params = [])
{
if ($this->validateOrderData($orderData)) {
return $this->put($this->getRoute('orders/{id}', $orderId), $params, json_encode($orderData));
}
return false;
}
/**
* Validates Order Data using the schema at
* @param $data
*/
private function validateOrderData($data)
{
// TODO: Not working jsonSchemaValidator
return true;
// $retriever = new \JsonSchema\Uri\UriRetriever;
// $filePath = __DIR__.'/schemas/create_order.json';
//// var_dump($filePath, realpath($filePath));die();
// $schema = $retriever->retrieve('file://' . realpath($filePath));
// $validator = new \JsonSchema\Validator();
// $validator->check($data, $schema);
// if ($validator->isValid()) {
// return true;
// } else {
// $errors = [];
// foreach ($validator->getErrors() as $error) {
// $errors[] = "[".$error['property'] . "] " . $error['message'];
// }
// $this->errors= $errors;
// }
}
/**
* Errors related to validation errors
* @var array
*/
public $errors=[];
}