1 <?php
2 /**
3 * Copyright 2019 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 Instant Shopping Orders class.
18 */
19
20 namespace Klarna\Rest\InstantShopping;
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 * Instant shopping Order resource.
29 */
30 class Orders extends Resource
31 {
32 /**
33 * {@inheritDoc}
34 */
35 const ID_FIELD = 'authorization_token';
36
37 /**
38 * {@inheritDoc}
39 */
40 public static $path = '/instantshopping/v1/authorizations';
41
42 /**
43 * Constructs an Order instance.
44 *
45 * @param ConnectorInterface $connector HTTP transport connector
46 * @param string $authorizationToken Authorization Token
47 */
48 public function __construct(ConnectorInterface $connector, $authorizationToken)
49 {
50 parent::__construct($connector);
51
52 $this->setLocation(self::$path . "/{$authorizationToken}");
53 $this[static::ID_FIELD] = $authorizationToken;
54 }
55
56 /**
57 * Retrieves an authorized order based on the authorization token.
58 *
59 * @codingStandardsIgnoreStart
60 * @see https://developers.klarna.com/api/#instant-shopping-api-retrieves-an-authorized-order-based-on-the-authorization-token
61 * @codingStandardsIgnoreEnd
62 *
63 * @throws ConnectorException When the API replies with an error response
64 * @throws RequestException When an error is encountered
65 * @throws \RuntimeException On an unexpected API response
66 * @throws \RuntimeException If the response content type is not JSON
67 * @throws \InvalidArgumentException If the JSON cannot be parsed
68 * @throws \LogicException When Guzzle cannot populate the response
69 *
70 * @return self
71 */
72 public function retrieve()
73 {
74 return $this->fetch();
75 }
76
77 /**
78 * Declines an authorized order identified by the authorization token.
79 *
80 * @codingStandardsIgnoreStart
81 * @see https://developers.klarna.com/api/#instant-shopping-api-declines-an-authorized-order-identified-by-the-authorization-token
82 * @codingStandardsIgnoreEnd
83 *
84 * @param array $data Decline data
85 *
86 * @throws ConnectorException When the API replies with an error response
87 * @throws RequestException When an error is encountered
88 * @throws \RuntimeException If the location header is missing
89 * @throws \RuntimeException If the API replies with an unexpected response
90 * @throws \LogicException When Guzzle cannot populate the response
91 *
92 * @return self
93 */
94 public function decline(array $data = null)
95 {
96 $this->delete($this->getLocation(), $data)
97 ->expectSuccessfull()
98 ->status('204');
99
100 return $this;
101 }
102
103 /**
104 * Approves the authorized order and places an order identified by the authorization token.
105 *
106 * @codingStandardsIgnoreStart
107 * @see https://developers.klarna.com/api/#instant-shopping-api-approve-the-authorized-order-and-place-an-order-identified-by-the-authorization-token
108 * @codingStandardsIgnoreEnd
109 *
110 * @param array $data Order data
111 *
112 * @throws ConnectorException When the API replies with an error response
113 * @throws RequestException When an error is encountered
114 * @throws \RuntimeException On an unexpected API response
115 * @throws \RuntimeException If the response content type is not JSON
116 * @throws \InvalidArgumentException If the JSON cannot be parsed
117 * @throws \LogicException When Guzzle cannot populate the response
118 *
119 * @return array approving status
120 */
121 public function approve(array $data)
122 {
123 return $this->post($this->getLocation() . '/orders', $data)
124 ->expectSuccessfull()
125 ->status('200')
126 ->getJson();
127 }
128 }
129