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 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 Connector $connector HTTP transport connector
47 * @param string $sessionId Session ID
48 */
49 public function __construct(Connector $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 $data = $this->post(self::$path, $data)
77 ->status('201')
78 ->contentType('application/json')
79 ->getJson();
80
81 return $data;
82 }
83
84 /**
85 * Distributes link to the HPP session.
86 *
87 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
88 *
89 * @param array $data Distribute data
90 *
91 * @throws ConnectorException When the API replies with an error response
92 * @throws RequestException When an error is encountered
93 * @throws \RuntimeException On an unexpected API response
94 * @throws \RuntimeException If the response content type is not JSON
95 * @throws \InvalidArgumentException If the JSON cannot be parsed
96 * @throws \LogicException When Guzzle cannot populate the response
97 *
98 * @return self
99 */
100 public function distributeLink(array $data)
101 {
102 $this->post($this->getLocation() . '/distribution', $data)
103 ->status('200');
104
105 return $this;
106 }
107
108 /**
109 * @deprecated HPP API no longer suppors getting the status. Use fetch (getSession) to fetch data;
110 * @deprecated This method will be removed in the future versions of SDK.
111 */
112 public function getSessionStatus()
113 {
114 return $this->fetch();
115 }
116 }
117