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\Connector;
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 Connector $connector HTTP transport connector
52 * @param string $orderId Order ID
53 */
54 public function __construct(Connector $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 $data = $this->post(self::$path, $data)
80 ->status('201')
81 ->contentType('application/json');
82
83 $this->exchangeArray($data->getJson());
84 $this->setLocation($data->getLocation());
85
86 return $this;
87 }
88
89 /**
90 * Updates the resource.
91 *
92 * @param array $data Update data
93 *
94 * @throws ConnectorException When the API replies with an error response
95 * @throws RequestException When an error is encountered
96 * @throws \RuntimeException On an unexpected API response
97 * @throws \RuntimeException If the response content type is not JSON
98 * @throws \InvalidArgumentException If the JSON cannot be parsed
99 * @throws \LogicException When Guzzle cannot populate the response
100 *
101 * @return self
102 */
103 public function update(array $data)
104 {
105 $data = $this->post($this->getLocation(), $data)
106 ->status('200')
107 ->contentType('application/json')
108 ->getJson();
109
110 $this->exchangeArray($data);
111
112 return $this;
113 }
114 }
115