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\ConnectorInterface;
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 ConnectorInterface $connector HTTP transport connector
47 * @param string $sessionId Session ID
48 */
49 public function __construct(ConnectorInterface $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 $response = $this->post(self::$path, $data)
82 ->expectSuccessfull()
83 ->status('201')
84 ->contentType('application/json')
85 ->getJson();
86
87 return $response;
88 }
89
90 /**
91 * Retrieve an existing settlement.
92 *
93 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
94 *
95 * @param array $data Distribute data
96 *
97 * @throws ConnectorException When the API replies with an error response
98 * @throws RequestException When an error is encountered
99 * @throws \RuntimeException On an unexpected API response
100 * @throws \RuntimeException If the response content type is not JSON
101 * @throws \InvalidArgumentException If the JSON cannot be parsed
102 * @throws \LogicException When Guzzle cannot populate the response
103 *
104 * @return array Settlement data
105 */
106 public function retrieveSettlement($settlementId, $keyId)
107 {
108 $response = $this->request(
109 'GET',
110 self::$path . "/$settlementId",
111 ['KeyId' => $keyId]
112 )
113 ->expectSuccessfull()
114 ->status('200')
115 ->contentType('application/json')
116 ->getJson();
117
118 return $response;
119 }
120
121 /**
122 * Retrieves a settled order's settlement.
123 *
124 * @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
125 *
126 * @param array $data Distribute data
127 *
128 * @throws ConnectorException When the API replies with an error response
129 * @throws RequestException When an error is encountered
130 * @throws \RuntimeException On an unexpected API response
131 * @throws \RuntimeException If the response content type is not JSON
132 * @throws \InvalidArgumentException If the JSON cannot be parsed
133 * @throws \LogicException When Guzzle cannot populate the response
134 *
135 * @return array Order's settlement data
136 */
137 public function retrieveOrderSettlement($orderId, $keyId)
138 {
139 $response = $this->request(
140 'GET',
141 self::$path . "/order/$orderId",
142 ['KeyId' => $keyId]
143 )
144 ->expectSuccessfull()
145 ->status('200')
146 ->contentType('application/json')
147 ->getJson();
148
149 return $response;
150 }
151 }
152