1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Looxis\LaravelAmazonMWS; |
4
|
|
|
|
5
|
|
|
class MWSOrders |
6
|
|
|
{ |
7
|
|
|
const VERSION = '2013-09-01'; |
8
|
|
|
|
9
|
|
|
protected $client; |
10
|
|
|
|
11
|
|
|
public function __construct(MWSClient $client) |
12
|
|
|
{ |
13
|
|
|
$this->client = $client; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function list($params = []) |
17
|
|
|
{ |
18
|
|
|
$nextToken = data_get($params, 'NextToken'); |
19
|
|
|
$action = 'ListOrders'; |
20
|
|
|
|
21
|
|
|
if ($nextToken) { |
22
|
|
|
$action = 'ListOrdersByNextToken'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$response = $this->client->post($action, '/Orders/'.self::VERSION, self::VERSION, $params); |
26
|
|
|
|
27
|
|
|
return $this->parseResponse($response, $action.'Result', 'Orders.Order'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function get($ids) |
31
|
|
|
{ |
32
|
|
|
$ids = is_array($ids) ? $ids : func_get_args(); |
33
|
|
|
$params = []; |
34
|
|
|
|
35
|
|
|
foreach ($ids as $key => $id) { |
36
|
|
|
$keyName = 'AmazonOrderId.Id.'.($key + 1); |
37
|
|
|
$params[$keyName] = $id; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$response = $this->client->post('GetOrder', '/Orders/'.self::VERSION, self::VERSION, $params); |
41
|
|
|
|
42
|
|
|
return $this->parseResponse($response, 'GetOrderResult', 'Orders.Order'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getItems($id) |
46
|
|
|
{ |
47
|
|
|
$params = [ |
48
|
|
|
'AmazonOrderId' => $id, |
49
|
|
|
]; |
50
|
|
|
$response = $this->client->post('ListOrderItems', '/Orders/'.self::VERSION, self::VERSION, $params); |
51
|
|
|
|
52
|
|
|
return $this->parseResponse($response, 'ListOrderItemsResult', 'OrderItems.OrderItem'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function parseResponse($response, $resultTypeName, $dataName) |
56
|
|
|
{ |
57
|
|
|
$requestId = data_get($response, 'ResponseMetadata.RequestId'); |
58
|
|
|
$data = data_get($response, $resultTypeName.'.'.$dataName); |
59
|
|
|
$nextToken = data_get($response, $resultTypeName.'.NextToken'); |
60
|
|
|
$createdBefore = data_get($response, $resultTypeName.'.CreatedBefore'); |
61
|
|
|
|
62
|
|
|
//Check if single list item and wrap |
63
|
|
|
if (! is_null($data) && (! data_get($data, '0')) && in_array($resultTypeName, [ |
64
|
|
|
'ListOrderItemsResult', |
65
|
|
|
'ListOrderItemsByNextTokenResult', |
66
|
|
|
'ListOrdersResult', |
67
|
|
|
'ListOrdersByNextTokenResult', |
68
|
|
|
])) { |
69
|
|
|
$data = [$data]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$data = [ |
73
|
|
|
'request_id' => $requestId, |
74
|
|
|
'data' => $data, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
if ($nextToken) { |
78
|
|
|
$data['next_token'] = $nextToken; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($createdBefore) { |
82
|
|
|
$data['created_before'] = $createdBefore; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($resultTypeName == 'ListOrderItemsResult') { |
86
|
|
|
$data['order_id'] = data_get($response, $resultTypeName.'.AmazonOrderId'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $data; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|