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\Connector;
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 Connector $connector HTTP transport connector
51 * @param string $authorizationToken Authorization Token
52 */
53 public function __construct(Connector $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 ->status('200')
88 ->contentType('application/json')
89 ->getJson();
90 }
91
92 /**
93 * Cancels the authorization.
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 cancelAuthorization()
105 {
106 $this->delete($this->getLocation())
107 ->status('204');
108 // ->contentType('application/json');
109 // TODO: We cannot check the Content-type here because of an inconsistency
110 // between service and documentation. The real Content-Type is
111 // "application/octet-stream but not the "application/json" as in the docs.
112
113 return $this;
114 }
115
116 /**
117 * Generates consumer token.
118 *
119 * @param array $data Token data
120 *
121 * @throws ConnectorException When the API replies with an error response
122 * @throws RequestException When an error is encountered
123 * @throws \RuntimeException If the location header is missing
124 * @throws \RuntimeException If the API replies with an unexpected response
125 * @throws \LogicException When Guzzle cannot populate the response
126 *
127 * @return array Token data
128 */
129 public function generateToken(array $data)
130 {
131 $data = $this->post($this->getLocation() . '/customer-token', $data)
132 ->status('200')
133 ->contentType('application/json')
134 ->getJson();
135
136 return $data;
137 }
138 }
139