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 Orders class.
18 */
19
20 namespace Klarna\Rest\Payments;
21
22 use GuzzleHttp\Exception\RequestException;
23 use Klarna\Exceptions\NotApplicableException;
24 use Klarna\Rest\Resource;
25 use Klarna\Rest\Transport\ConnectorInterface;
26 use Klarna\Rest\Transport\Exception\ConnectorException;
27
28 /**
29 * Payments order resource.
30 *
31 * @example docs/examples/PaymentsAPI/Orders/cancel_existing_authorization.php Cancel an existing authorization
32 * @example docs/examples/PaymentsAPI/Orders/create_order.php Create a new order
33 * @example docs/examples/PaymentsAPI/Orders/generate_customer_token.php Generate a consumer token
34 */
35 class Orders extends Resource
36 {
37 /**
38 * {@inheritDoc}
39 */
40 const ID_FIELD = 'authorization_token';
41
42 /**
43 * {@inheritDoc}
44 */
45 public static $path = '/payments/v1/authorizations';
46
47 /**
48 * Constructs an order instance.
49 *
50 * @param ConnectorInterface $connector HTTP transport connector
51 * @param string $authorizationToken Authorization Token
52 */
53 public function __construct(ConnectorInterface $connector, $authorizationToken)
54 {
55 parent::__construct($connector);
56
57 $this->setLocation(self::$path . "/{$authorizationToken}");
58 $this[static::ID_FIELD] = $authorizationToken;
59 }
60
61 /**
62 * Not applicable.
63 *
64 * @throws NotApplicableException
65 */
66 public function fetch()
67 {
68 throw new NotApplicableException('Not applicable');
69 }
70
71 /**
72 * Creates the resource.
73 *
74 * @param array $data Creation data
75 *
76 * @throws ConnectorException When the API replies with an error response
77 * @throws RequestException When an error is encountered
78 * @throws \RuntimeException If the location header is missing
79 * @throws \RuntimeException If the API replies with an unexpected response
80 * @throws \LogicException When Guzzle cannot populate the response
81 *
82 * @return array Order data
83 */
84 public function create(array $data)
85 {
86 return $this->post($this->getLocation() . '/order', $data)
87 ->expectSuccessfull()
88 ->status('200')
89 ->contentType('application/json')
90 ->getJson();
91 }
92
93 /**
94 * Cancels the authorization.
95 *
96 * @throws ConnectorException When the API replies with an error response
97 * @throws RequestException When an error is encountered
98 * @throws \RuntimeException On an unexpected API response
99 * @throws \RuntimeException If the response content type is not JSON
100 * @throws \InvalidArgumentException If the JSON cannot be parsed
101 * @throws \LogicException When Guzzle cannot populate the response
102 *
103 * @return self
104 */
105 public function cancelAuthorization()
106 {
107 $this->delete($this->getLocation())
108 ->expectSuccessfull()
109 ->status('204');
110 // ->contentType('application/json');
111 // TODO: We cannot check the Content-type here because of an inconsistency
112 // between service and documentation. The real Content-Type is
113 // "application/octet-stream but not the "application/json" as in the docs.
114
115 return $this;
116 }
117
118 /**
119 * Generates consumer token.
120 *
121 * @param array $data Token data
122 *
123 * @throws ConnectorException When the API replies with an error response
124 * @throws RequestException When an error is encountered
125 * @throws \RuntimeException If the location header is missing
126 * @throws \RuntimeException If the API replies with an unexpected response
127 * @throws \LogicException When Guzzle cannot populate the response
128 *
129 * @return array Token data
130 */
131 public function generateToken(array $data)
132 {
133 $response = $this->post($this->getLocation() . '/customer-token', $data)
134 ->expectSuccessfull()
135 ->status('200')
136 ->contentType('application/json')
137 ->getJson();
138
139 return $response;
140 }
141 }
142