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 Sessions class.
18 */
19
20 namespace Klarna\Rest\Payments;
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 * Payments session resource.
29 *
30 * @example docs/examples/PaymentsAPI/Sessions/create_new_credit_session.php
31 * @example docs/examples/PaymentsAPI/Sessions/read_credit_session.php
32 * @example docs/examples/PaymentsAPI/Sessions/update_credit_session.php
33 */
34 class Sessions extends Resource
35 {
36 /**
37 * {@inheritDoc}
38 */
39 const ID_FIELD = 'session_id';
40
41 /**
42 * {@inheritDoc}
43 */
44 public static $path = '/payments/v1/sessions';
45
46 /**
47 * Constructs a session instance.
48 *
49 * @param ConnectorInterface $connector HTTP transport connector
50 * @param string $sessionId Session ID
51 */
52 public function __construct(ConnectorInterface $connector, $sessionId = null)
53 {
54 parent::__construct($connector);
55
56 if ($sessionId !== null) {
57 $this->setLocation(self::$path . "/{$sessionId}");
58 $this[static::ID_FIELD] = $sessionId;
59 }
60 }
61
62 /**
63 * Creates the resource.
64 *
65 * @param array $data Creation data
66 *
67 * @throws ConnectorException When the API replies with an error response
68 * @throws RequestException When an error is encountered
69 * @throws \RuntimeException If the location header is missing
70 * @throws \RuntimeException If the API replies with an unexpected response
71 * @throws \LogicException When Guzzle cannot populate the response
72 *
73 * @return self
74 */
75 public function create(array $data)
76 {
77 $response = $this->post(self::$path, $data)
78 ->expectSuccessfull()
79 ->status('200')
80 ->contentType('application/json');
81
82 $this->exchangeArray($response->getJson());
83
84 // Payments API does not send Location header after creating a new session.
85 // Use workaround to set new location.
86 $this->setLocation(self::$path . '/' . $this->getId());
87
88 return $this;
89 }
90
91 /**
92 * Updates the resource.
93 *
94 * @param array $data Update data
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 update(array $data)
106 {
107 $this->post($this->getLocation(), $data)
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