1 <?php
2 /**
3 * Copyright 2014 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the Order class.
18 */
19
20 namespace Klarna\Rest\Checkout;
21
22 use GuzzleHttp\Exception\RequestException;
23 use Klarna\Rest\Resource;
24 use Klarna\Rest\Transport\ConnectorInterface;
25 use Klarna\Rest\Transport\Exception\ConnectorException;
26
27 /**
28 * Checkout order resource.
29 *
30 * @example docs/examples/CheckoutAPI/create_checkout.php Create the checkout order
31 * @example docs/examples/CheckoutAPI/create_checkout_attachment.php EMD attachment
32 * @example docs/examples/CheckoutAPI/fetch_checkout.php Retrieve a checkout order
33 * @example docs/examples/CheckoutAPI/update_checkout.php Update a checkout order
34 * @example docs/examples/CheckoutAPI/handling_exceptions.php Handling possible exceptions
35 */
36 class Order extends Resource
37 {
38 /**
39 * {@inheritDoc}
40 */
41 const ID_FIELD = 'order_id';
42
43 /**
44 * {@inheritDoc}
45 */
46 public static $path = '/checkout/v3/orders';
47
48 /**
49 * Constructs an order instance.
50 *
51 * @param ConnectorInterface $connector HTTP transport connector
52 * @param string $orderId Order ID
53 */
54 public function __construct(ConnectorInterface $connector, $orderId = null)
55 {
56 parent::__construct($connector);
57
58 if ($orderId !== null) {
59 $this->setLocation(self::$path . "/{$orderId}");
60 $this[static::ID_FIELD] = $orderId;
61 }
62 }
63
64 /**
65 * Creates the resource.
66 *
67 * @param array $data Creation data
68 *
69 * @throws ConnectorException When the API replies with an error response
70 * @throws RequestException When an error is encountered
71 * @throws \RuntimeException If the location header is missing
72 * @throws \RuntimeException If the API replies with an unexpected response
73 * @throws \LogicException When Guzzle cannot populate the response
74 *
75 * @return self
76 */
77 public function create(array $data)
78 {
79 $response = $this->post(self::$path, $data)
80 ->expectSuccessfull()
81 ->status('201')
82 ->contentType('application/json');
83
84 $this->exchangeArray($response->getJson());
85 $this->setLocation($response->getLocation());
86
87 return $this;
88 }
89
90 /**
91 * Updates the resource.
92 *
93 * @param array $data Update data
94 *
95 * @throws ConnectorException When the API replies with an error response
96 * @throws RequestException When an error is encountered
97 * @throws \RuntimeException On an unexpected API response
98 * @throws \RuntimeException If the response content type is not JSON
99 * @throws \InvalidArgumentException If the JSON cannot be parsed
100 * @throws \LogicException When Guzzle cannot populate the response
101 *
102 * @return self
103 */
104 public function update(array $data)
105 {
106 $response = $this->post($this->getLocation(), $data)
107 ->expectSuccessfull()
108 ->status('200')
109 ->contentType('application/json')
110 ->getJson();
111
112 $this->exchangeArray($response);
113
114 return $this;
115 }
116 }
117