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 Payouts class.
18 */
19
20 namespace Klarna\Rest\Settlements;
21
22 use GuzzleHttp\Exception\RequestException;
23 use Klarna\Exceptions\NotApplicableException;
24 use Klarna\Rest\Resource;
25 use Klarna\Rest\Transport\ConnectorInterface;
26 use Klarna\Rest\Transport\Exception\ConnectorException;
27
28 /**
29 * Payouts resource.
30 *
31 * @example docs/examples/SettlementsAPI/Payouts/get_all_payouts.php Returns a collection of payouts
32 * @example docs/examples/SettlementsAPI/Payouts/get_payout.php Returns a specific payout based on a payment reference
33 * @example docs/examples/SettlementsAPI/Payouts/get_summary.php Returns a summary of payouts for each currency code
34 */
35 class Payouts extends Resource
36 {
37 /**
38 * {@inheritDoc}
39 */
40 const ID_FIELD = 'payment_reference';
41
42 /**
43 * {@inheritDoc}
44 */
45 public static $path = '/settlements/v1/payouts';
46
47 /**
48 * Constructs Payouts instance.
49 *
50 * @param ConnectorInterface $connector HTTP transport connector
51 */
52 public function __construct(ConnectorInterface $connector)
53 {
54 parent::__construct($connector);
55 }
56
57 /**
58 * Not applicable.
59 *
60 * @throws NotApplicableException
61 */
62 public function fetch()
63 {
64 throw new NotApplicableException('Not applicable');
65 }
66
67 /**
68 * Returns a specific payout based on a given payment reference.
69 *
70 * @param string $paymentReference The reference id of the payout
71 *
72 * @throws ConnectorException When the API replies with an error response
73 * @throws RequestException When an error is encountered
74 * @throws \RuntimeException On an unexpected API response
75 * @throws \RuntimeException If the response content type is not JSON
76 * @throws \InvalidArgumentException If the JSON cannot be parsed
77 * @throws \LogicException When Guzzle cannot populate the response
78 *
79 * @return array Payout data
80 */
81 public function getPayout($paymentReference)
82 {
83 return $this->get(self::$path . "/{$paymentReference}")
84 ->expectSuccessfull()
85 ->status('200')
86 ->contentType('application/json')
87 ->getJson();
88 }
89
90 /**
91 * Returns a collection of payouts.
92 *
93 * @param array $params Additional query params to filter payouts.
94 *
95 * @see https://developers.klarna.com/api/#settlements-api-get-all-payouts
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 Payouts data
105 */
106 public function getAllPayouts(array $params = [])
107 {
108 return $this->get(self::$path . '?' . http_build_query($params))
109 ->expectSuccessfull()
110 ->status('200')
111 ->contentType('application/json')
112 ->getJson();
113 }
114
115 /**
116 * Returns a summary of payouts for each currency code in a date range.
117 *
118 * @param array $params Additional query params to filter summary data.
119 *
120 * @see https://developers.klarna.com/api/#settlements-api-get-summary-of-payouts
121 *
122 * @throws ConnectorException When the API replies with an error response
123 * @throws RequestException When an error is encountered
124 * @throws \RuntimeException On an unexpected API response
125 * @throws \RuntimeException If the response content type is not JSON
126 * @throws \InvalidArgumentException If the JSON cannot be parsed
127 * @throws \LogicException When Guzzle cannot populate the response
128 *
129 * @return array summary of payouts
130 */
131 public function getSummary(array $params = [])
132 {
133 return $this->get(self::$path . '/summary?' . http_build_query($params))
134 ->expectSuccessfull()
135 ->status('200')
136 ->contentType('application/json')
137 ->getJson();
138 }
139 }
140