| 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 |  |  |         return $this->parseResponse($response, $action. 'Result', 'Orders.Order'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function get($ids) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $ids = is_array($ids) ? $ids : func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         foreach ($ids as $key => $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $keyName = 'AmazonOrderId.Id.'.($key + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $params[$keyName] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $response = $this->client->post('GetOrder', '/Orders/'.self::VERSION, self::VERSION, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $this->parseResponse($response, 'GetOrderResult', 'Orders.Order'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function getItems($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'AmazonOrderId' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $response = $this->client->post('ListOrderItems', '/Orders/'.self::VERSION, self::VERSION, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->parseResponse($response, 'ListOrderItemsResult', 'OrderItems.OrderItem'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |     protected function parseResponse($response, $resultTypeName, $dataName) | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         $requestId = data_get($response, 'ResponseMetadata.RequestId'); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         $data = data_get($response, $resultTypeName . '.' . $dataName); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         $nextToken = data_get($response, $resultTypeName . '.NextToken'); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $createdBefore = data_get($response, $resultTypeName . '.CreatedBefore'); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         //Check if single list item and wrap | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         if ((! data_get($data, '0')) && $resultTypeName == 'ListOrderItemsResult') { | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             $data = [$data]; | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         $data = [ | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             'request_id' => $requestId, | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             'data' => $data, | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         if ($nextToken) { | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             $data['next_token'] = $nextToken; | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         if ($createdBefore) { | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             $data['created_before'] = $createdBefore; | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         if ($resultTypeName == 'ListOrderItemsResult') { | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             $data['order_id'] = data_get($response, $resultTypeName.'.AmazonOrderId'); | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         return $data; | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |