diff --git a/src/Endpoints/Orders.php b/src/Endpoints/Orders.php index acfeafa..7f3d07f 100644 --- a/src/Endpoints/Orders.php +++ b/src/Endpoints/Orders.php @@ -25,7 +25,7 @@ public function getOrder(int $id): Order try { $response = $this->client->get('/orders/' . $id); - SendcloudRequestException::fromResponse($response); + SendcloudRequestException::fromResponse($response, 200); $body = $response->getBody()->getContents(); @@ -156,7 +156,7 @@ public function getOrders( // Send request $response = $this->client->get($uri); - SendcloudRequestException::fromResponse($response); + SendcloudRequestException::fromResponse($response, 200); // Parse response @@ -195,7 +195,7 @@ public function updateOrder( $body = json_encode($order); $response = $this->client->patch('/orders/' . $order->id, [], $body); - SendcloudRequestException::fromResponse($response); + SendcloudRequestException::fromResponse($response, 200); $body = $response->getBody()->getContents(); $json = json_decode($body, true); @@ -224,7 +224,7 @@ public function createOrder( $body = json_encode($orders); $response = $this->client->post('/orders', [], $body); - SendcloudRequestException::fromResponse($response); + SendcloudRequestException::fromResponse($response, 201); $body = $response->getBody()->getContents(); $json = json_decode($body, true); @@ -252,7 +252,7 @@ public function deleteOrder( try { $response = $this->client->post('/orders/' . $id); - SendcloudRequestException::fromResponse($response); + SendcloudRequestException::fromResponse($response, 204); } catch (Throwable $throwable) { SendcloudRequestException::fromException($throwable); } diff --git a/src/Exceptions/SendcloudRequestException.php b/src/Exceptions/SendcloudRequestException.php index c869453..9f8dbb0 100644 --- a/src/Exceptions/SendcloudRequestException.php +++ b/src/Exceptions/SendcloudRequestException.php @@ -33,17 +33,27 @@ public function __construct( $message = $message ?? ''; $code = $code ?? SendcloudRequestException::CODE_UNKNOWN; + if (isset($sendcloudCode)) { + $message .= sprintf(' [%s]', $sendcloudCode); + } + + if (isset($sendcloudSource)) { + $message .= " (at " . json_encode($sendcloudSource) . ")"; + } + parent::__construct($message, $code, $previous); } /** * Checks response for errors code and throws exception if needed + * + * @param $expected_status_code Expected HTTP status code in response * * @throws SendcloudRequestException */ - public static function fromResponse(ResponseInterface $response) : void + public static function fromResponse(ResponseInterface $response, int $expected_status_code = 200) : void { - if ($response->getStatusCode() === 200) { + if ($response->getStatusCode() === $expected_status_code) { return; } diff --git a/src/Factory/ClientFactory.php b/src/Factory/ClientFactory.php index 537e400..535e2c8 100644 --- a/src/Factory/ClientFactory.php +++ b/src/Factory/ClientFactory.php @@ -51,7 +51,9 @@ public static function create( // Headers - $headers = []; + $headers = [ + 'Content-Type' => 'application/json' + ]; if (isset($partnerId)) { $headers['Sendcloud-Partner-Id'] = $partnerId; } diff --git a/src/Models/Order/OrderDetails.php b/src/Models/Order/OrderDetails.php index c35ac34..cf0ec80 100644 --- a/src/Models/Order/OrderDetails.php +++ b/src/Models/Order/OrderDetails.php @@ -31,8 +31,8 @@ public function __construct( public readonly Status $status, public readonly DateTimeImmutable $order_created_at, public readonly array $order_items, - public readonly DateTimeImmutable $order_updated_at, - public readonly string $notes, + public readonly ?DateTimeImmutable $order_updated_at = null, + public readonly ?string $notes = null, public readonly ?array $tags = null ) { @@ -50,8 +50,8 @@ public static function fromData(array $data) : self status: Status::fromData($data['status']), order_created_at: DateUtils::iso8601ToDateTime($data['order_created_at']), order_items: $order_items, - order_updated_at: DateUtils::iso8601ToDateTime($data['order_updated_at']), - notes: (string) $data['notes'], + order_updated_at: isset($data['order_updated_at']) ? DateUtils::iso8601ToDateTime($data['order_updated_at']) : null, + notes: isset($data['notes']) ? (string) $data['notes'] : null, tags: isset($data['tags']) ? $data['tags'] : null ); } @@ -62,12 +62,15 @@ public function jsonSerialize() : array 'integration' => $this->integration, 'status' => $this->status, 'order_created_at' => DateUtils::dateTimeToIso8601($this->order_created_at), - 'order_items' => $this->order_items, - 'order_updated_at' => DateUtils::dateTimeToIso8601($this->order_updated_at), - 'notes' => $this->notes + 'order_items' => $this->order_items ]; + if (isset($this->order_updated_at)) { + $json['order_updated_at'] = DateUtils::dateTimeToIso8601($this->order_updated_at); + } + JsonUtils::addIfNotNull($json, 'tags', $this->tags); + JsonUtils::addIfNotNull($json, 'notes', $this->notes); return $json; } diff --git a/tests/Endpoints/OrdersTest.php b/tests/Endpoints/OrdersTest.php index 3090b39..6b385db 100644 --- a/tests/Endpoints/OrdersTest.php +++ b/tests/Endpoints/OrdersTest.php @@ -525,7 +525,7 @@ public function testCreateOrder() : void ] } JSON; - $endpoint = $this->getEndpoint($json, 200); + $endpoint = $this->getEndpoint($json, 201); // -- Act @@ -561,7 +561,7 @@ public function testDeleteOrder() : void // -- Arrange $order_id = 1; - $endpoint = $this->getEndpoint('', 200); + $endpoint = $this->getEndpoint('', 204); // -- Act & Assert