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\ConnectorInterface;
25 use Klarna\Rest\Transport\Exception\ConnectorException;
26 use Klarna\Exceptions\NotApplicableException;
27
28 /**
29 * HPP session resource.
30 */
31 class Sessions extends Resource
32 {
33 /**
34 * {@inheritDoc}
35 */
36 const ID_FIELD = 'session_id';
37
38 /**
39 * {@inheritDoc}
40 */
41 public static $path = '/hpp/v1/sessions';
42
43 /**
44 * Constructs a session instance.
45 *
46 * @param ConnectorInterface $connector HTTP transport connector
47 * @param string $sessionId Session ID
48 */
49 public function __construct(ConnectorInterface $connector, $sessionId = null)
50 {
51 parent::__construct($connector);
52
53 if ($sessionId !== null) {
54 $this->setLocation(self::$path . "/{$sessionId}");
55 $this[static::ID_FIELD] = $sessionId;
56 }
57 }
58
59 /**
60 * Creates the resource.
61 *
62 * @param array $data Creation data
63 *
64 * @see https://developers.klarna.com/api/#hosted-payment-page-api-create-a-new-hpp-session
65 *
66 * @throws ConnectorException When the API replies with an error response
67 * @throws RequestException When an error is encountered
68 * @throws \RuntimeException If the location header is missing
69 * @throws \RuntimeException If the API replies with an unexpected response
70 * @throws \LogicException When Guzzle cannot populate the response
71 *
72 * @return array Session data
73 */
74 public function create(array $data)
75 {
76 $response = $this->post(self::$path, $data)
77 ->expectSuccessfull()
78 ->status('201')
79 ->contentType('application/json')
80 ->getJson();
81
82 return $response;
83 }
84
85 /**
86 * Disables HPP session.
87 *
88 * @see https://developers.klarna.com/api/#hosted-payment-page-api-disable-hpp-session
89 *
90 * @throws ConnectorException When the API replies with an error response
91 * @throws RequestException When an error is encountered
92 * @throws \RuntimeException If sessionId was not specified when creating a resource
93 * @throws \RuntimeException If the location header is missing
94 * @throws \RuntimeException If the API replies with an unexpected response
95 * @throws \LogicException When Guzzle cannot populate the response
96 *
97 * @return self
98 */
99 public function disable()
100 {
101 if (empty($this[static::ID_FIELD])) {
102 throw new \RuntimeException('HPP Session ID is not defined');
103 }
104
105 $this->delete($this->getLocation())
106 ->expectSuccessfull()
107 ->status('204');
108
109 return $this;
110 }
111
112 /**
113 * Distributes link to the HPP session.
114 *
115 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
116 *
117 * @param array $data Distribute data
118 *
119 * @throws ConnectorException When the API replies with an error response
120 * @throws RequestException When an error is encountered
121 * @throws \RuntimeException On an unexpected API response
122 * @throws \RuntimeException If the response content type is not JSON
123 * @throws \InvalidArgumentException If the JSON cannot be parsed
124 * @throws \LogicException When Guzzle cannot populate the response
125 *
126 * @return self
127 */
128 public function distributeLink(array $data)
129 {
130 $this->post($this->getLocation() . '/distribution', $data)
131 ->expectSuccessfull()
132 ->status(['200', '201']);
133
134 return $this;
135 }
136
137 /**
138 * @deprecated HPP API no longer suppors getting the status. Use fetch (getSession) to fetch data;
139 * @deprecated This method will be removed in the future versions of SDK.
140 */
141 public function getSessionStatus()
142 {
143 return $this->fetch();
144 }
145 }
146