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 Tokens class.
18 */
19
20 namespace Klarna\Rest\CustomerToken;
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 * Tokens resource.
29 *
30 * @example docs/examples/CustomerTokenAPI/Tokens/create_order.php Create a new order using customer token
31 * @example docs/examples/CustomerTokenAPI/Tokens/read_token_details.php Read customer token details
32 */
33 class Tokens extends Resource
34 {
35 /**
36 * {@inheritDoc}
37 */
38 const ID_FIELD = 'customerToken';
39
40 /**
41 * {@inheritDoc}
42 */
43 public static $path = '/customer-token/v1/tokens';
44
45 /**
46 * Constructs a Tokens instance.
47 *
48 * @param Connector $connector HTTP transport connector
49 * @param string $customerToken Customer Token
50 */
51 public function __construct(Connector $connector, $customerToken)
52 {
53 parent::__construct($connector);
54
55 $this->setLocation(self::$path . "/{$customerToken}");
56 $this[static::ID_FIELD] = $customerToken;
57 }
58
59 /**
60 * Creates order using Customer Token
61 *
62 * @param array $data Order data
63 *
64 * @throws ConnectorException When the API replies with an error response
65 * @throws RequestException When an error is encountered
66 * @throws \RuntimeException If the location header is missing
67 * @throws \RuntimeException If the API replies with an unexpected response
68 * @throws \LogicException When Guzzle cannot populate the response
69 *
70 * @return array created order data
71 */
72 public function createOrder(array $data)
73 {
74 return $this->post($this->getLocation() . '/order', $data)
75 ->status('200')
76 ->contentType('application/json')
77 ->getJson();
78 }
79 }
80