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/checkout/create_checkout.php Create the checkout order
31 * @example docs/examples/checkout/create_checkout_attachment.php EMD attachment
32 * @example docs/examples/checkout/fetch_checkout.php Retrieve a checkout order
33 * @example docs/examples/checkout/update_checkout.php Update a checkout order
34 */
35 class Order extends Resource
36 {
37 /**
38 * {@inheritDoc}
39 */
40 const ID_FIELD = 'order_id';
41
42 /**
43 * {@inheritDoc}
44 */
45 public static $path = '/checkout/v3/orders';
46
47 /**
48 * Constructs an order instance.
49 *
50 * @param Connector $connector HTTP transport connector
51 * @param string $orderId Order ID
52 */
53 public function __construct(Connector $connector, $orderId = null)
54 {
55 parent::__construct($connector);
56
57 if ($orderId !== null) {
58 $this->setLocation(self::$path . "/{$orderId}");
59 $this[static::ID_FIELD] = $orderId;
60 }
61 }
62
63 /**
64 * Creates the resource.
65 *
66 * @param array $data Creation data
67 *
68 * @throws ConnectorException When the API replies with an error response
69 * @throws RequestException When an error is encountered
70 * @throws \RuntimeException If the location header is missing
71 * @throws \RuntimeException If the API replies with an unexpected response
72 * @throws \LogicException When Guzzle cannot populate the response
73 *
74 * @return self
75 */
76 public function create(array $data)
77 {
78 $url = $this->post(self::$path, $data)
79 ->status('201')
80 ->getLocation();
81
82 $this->setLocation($url);
83
84 return $this;
85 }
86
87 /**
88 * Updates the resource.
89 *
90 * @param array $data Update data
91 *
92 * @throws ConnectorException When the API replies with an error response
93 * @throws RequestException When an error is encountered
94 * @throws \RuntimeException On an unexpected API response
95 * @throws \RuntimeException If the response content type is not JSON
96 * @throws \InvalidArgumentException If the JSON cannot be parsed
97 * @throws \LogicException When Guzzle cannot populate the response
98 *
99 * @return self
100 */
101 public function update(array $data)
102 {
103 $data = $this->post($this->getLocation(), $data)
104 ->status('200')
105 ->contentType('application/json')
106 ->getJson();
107
108 $this->exchangeArray($data);
109
110 return $this;
111 }
112 }
113