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 Virtual Credit Card Settlements class.
18 */
19
20 namespace Klarna\Rest\MerchantCardService;
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 * Virtual Credit Card Settlements resource.
30 */
31 class VCCSettlements extends Resource
32 {
33 /**
34 * {@inheritDoc}
35 */
36 const ID_FIELD = 'settlement_id';
37
38 /**
39 * {@inheritDoc}
40 */
41 public static $path = '/merchantcard/v3/settlements';
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)
50 {
51 parent::__construct($connector);
52 }
53
54 /**
55 * Not applicable.
56 *
57 * @throws NotApplicableException
58 */
59 public function fetch()
60 {
61 throw new NotApplicableException('Not applicable');
62 }
63
64 /**
65 * Creates a new settlement.
66 *
67 * @param array $data Creation data
68 *
69 * @see https://developers.klarna.com/api/#merchant-card-service-api-create-a-new-settlement
70 *
71 * @throws ConnectorException When the API replies with an error response
72 * @throws RequestException When an error is encountered
73 * @throws \RuntimeException If the location header is missing
74 * @throws \RuntimeException If the API replies with an unexpected response
75 * @throws \LogicException When Guzzle cannot populate the response
76 *
77 * @return array Settlement data
78 */
79 public function create(array $data)
80 {
81 $data = $this->post(self::$path, $data)
82 ->status('201')
83 ->contentType('application/json')
84 ->getJson();
85
86 return $data;
87 }
88
89 /**
90 * Retrieve an existing settlement.
91 *
92 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
93 *
94 * @param array $data Distribute 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 array Settlement data
104 */
105 public function retrieveSettlement($settlementId, $keyId)
106 {
107 $data = $this->request(
108 'GET',
109 self::$path . "/$settlementId",
110 ['KeyId' => $keyId]
111 )->status('200')
112 ->contentType('application/json')
113 ->getJson();
114
115 return $data;
116 }
117
118 /**
119 * Retrieves a settled order's settlement.
120 *
121 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
122 *
123 * @param array $data Distribute data
124 *
125 * @throws ConnectorException When the API replies with an error response
126 * @throws RequestException When an error is encountered
127 * @throws \RuntimeException On an unexpected API response
128 * @throws \RuntimeException If the response content type is not JSON
129 * @throws \InvalidArgumentException If the JSON cannot be parsed
130 * @throws \LogicException When Guzzle cannot populate the response
131 *
132 * @return array Order's settlement data
133 */
134 public function retrieveOrderSettlement($orderId, $keyId)
135 {
136 $data = $this->request(
137 'GET',
138 self::$path . "/order/$orderId",
139 ['KeyId' => $keyId]
140 )->status('200')
141 ->contentType('application/json')
142 ->getJson();
143
144 return $data;
145 }
146 }
147