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\HostedPaymentPage;
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 * HPP session resource.
29 */
30 class Sessions extends Resource
31 {
32 /**
33 * {@inheritDoc}
34 */
35 const ID_FIELD = 'session_id';
36
37 /**
38 * {@inheritDoc}
39 */
40 public static $path = '/hpp/v1/sessions';
41
42 /**
43 * Constructs a session instance.
44 *
45 * @param Connector $connector HTTP transport connector
46 * @param string $sessionId Session ID
47 */
48 public function __construct(Connector $connector, $sessionId = null)
49 {
50 parent::__construct($connector);
51
52 if ($sessionId !== null) {
53 $this->setLocation(self::$path . "/{$sessionId}");
54 $this[static::ID_FIELD] = $sessionId;
55 }
56 }
57
58 /**
59 * Creates the resource.
60 *
61 * @param array $data Creation data
62 *
63 * @see https://developers.klarna.com/api/#hosted-payment-page-api-create-a-new-hpp-session
64 *
65 * @throws ConnectorException When the API replies with an error response
66 * @throws RequestException When an error is encountered
67 * @throws \RuntimeException If the location header is missing
68 * @throws \RuntimeException If the API replies with an unexpected response
69 * @throws \LogicException When Guzzle cannot populate the response
70 *
71 * @return array Session data
72 */
73 public function create(array $data)
74 {
75 $data = $this->post(self::$path, $data)
76 ->status('201')
77 ->contentType('application/json')
78 ->getJson();
79
80 return $data;
81 }
82
83 /**
84 * Not applicable.
85 *
86 * @throws NotApplicableException
87 */
88 public function fetch()
89 {
90 throw new NotApplicableException('Not applicable');
91 }
92
93 /**
94 * Distributes link to the HPP session.
95 *
96 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
97 *
98 * @param array $data Distribute data
99 *
100 * @throws ConnectorException When the API replies with an error response
101 * @throws RequestException When an error is encountered
102 * @throws \RuntimeException On an unexpected API response
103 * @throws \RuntimeException If the response content type is not JSON
104 * @throws \InvalidArgumentException If the JSON cannot be parsed
105 * @throws \LogicException When Guzzle cannot populate the response
106 *
107 * @return self
108 */
109 public function distributeLink(array $data)
110 {
111 $this->post($this->getLocation() . '/distribution', $data)
112 ->status('200');
113 // ->contentType('application/json');
114 // TODO: We cannot check the Content-type here because of an inconsistency
115 // between service and documentation. The real Content-Type is
116 // "application/octet-stream but not the "application/json" as in the docs.
117
118 return $this;
119 }
120
121 /**
122 * Gets HPP session status.
123 *
124 * @param array $params Additional query params to filter transactions.
125 *
126 * @throws ConnectorException When the API replies with an error response
127 * @throws RequestException When an error is encountered
128 * @throws \RuntimeException On an unexpected API response
129 * @throws \RuntimeException If the response content type is not JSON
130 * @throws \InvalidArgumentException If the JSON cannot be parsed
131 * @throws \LogicException When Guzzle cannot populate the response
132 *
133 * @return array Session status
134 */
135 public function getSessionStatus()
136 {
137 return $this->get($this->getLocation() . '/status')
138 ->status('200')
139 ->contentType('application/json')
140 ->getJson();
141 }
142 }
143