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 Reports 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 * Reports resource.
30 *
31 * @example docs/examples/SettlementsAPI/Reports/payout_report.php Get payout report with transactions
32 * @example docs/examples/SettlementsAPI/Reports/summary_report.php Get payouts summary report with transactions
33 */
34 class Reports extends Resource
35 {
36 /**
37 * {@inheritDoc}
38 */
39 public static $path = '/settlements/v1/reports';
40
41 /**
42 * Constructs a Reports instance.
43 *
44 * @param ConnectorInterface $connector HTTP transport connector
45 */
46 public function __construct(ConnectorInterface $connector)
47 {
48 parent::__construct($connector);
49 }
50
51 /**
52 * Not applicable.
53 *
54 * @throws NotApplicableException
55 */
56 public function fetch()
57 {
58 throw new NotApplicableException('Not applicable');
59 }
60
61 /**
62 * Returns CSV payout report
63 *
64 * @param string $paymentReference The reference id of the payout.
65 *
66 * @throws ConnectorException When the API replies with an error response
67 * @throws RequestException When an error is encountered
68 * @throws \RuntimeException On an unexpected API response
69 * @throws \RuntimeException If the response content type is not JSON
70 * @throws \InvalidArgumentException If the JSON cannot be parsed
71 * @throws \LogicException When Guzzle cannot populate the response
72 *
73 * @return string CSV Payout report
74 */
75 public function getCSVPayoutReport($paymentReference)
76 {
77 return $this->get(self::$path . "/payout-with-transactions?payment_reference={$paymentReference}")
78 ->expectSuccessfull()
79 ->status('200')
80 ->contentType('text/csv')
81 ->getBody();
82 }
83
84 /**
85 * Returns a single settlement summed up in pdf format.
86 *
87 * @param string $paymentReference The reference id of the payout.
88 *
89 * @throws ConnectorException When the API replies with an error response
90 * @throws RequestException When an error is encountered
91 * @throws \RuntimeException On an unexpected API response
92 * @throws \RuntimeException If the response content type is not JSON
93 * @throws \InvalidArgumentException If the JSON cannot be parsed
94 * @throws \LogicException When Guzzle cannot populate the response
95 *
96 * @return string Binary PDF representation of Payout report
97 */
98 public function getPDFPayoutReport($paymentReference)
99 {
100 return $this->get(self::$path . "/payout?payment_reference={$paymentReference}")
101 ->expectSuccessfull()
102 ->status('200')
103 ->contentType('application/pdf')
104 ->getBody();
105 }
106
107 /**
108 * Returns CSV summary.
109 *
110 * @param array $params Additional query params to filter payouts.
111 *
112 * @see https://developers.klarna.com/api/#settlements-api-get-payouts-summary-report-with-transactions
113 *
114 * @throws ConnectorException When the API replies with an error response
115 * @throws RequestException When an error is encountered
116 * @throws \RuntimeException On an unexpected API response
117 * @throws \RuntimeException If the response content type is not JSON
118 * @throws \InvalidArgumentException If the JSON cannot be parsed
119 * @throws \LogicException When Guzzle cannot populate the response
120 *
121 * @return string CSV Summary report
122 */
123 public function getCSVPayoutsSummaryReport(array $params = [])
124 {
125 return $this->get(self::$path . '/payouts-summary-with-transactions?' . http_build_query($params))
126 ->expectSuccessfull()
127 ->status('200')
128 ->contentType('text/csv')
129 ->getBody();
130 }
131
132 /**
133 * Returns PDF summary.
134 *
135 * @param array $params Additional query params to filter payouts.
136 *
137 * @see https://developers.klarna.com/api/#settlements-api-get-payouts-summary-report-with-transactions
138 *
139 * @throws ConnectorException When the API replies with an error response
140 * @throws RequestException When an error is encountered
141 * @throws \RuntimeException On an unexpected API response
142 * @throws \RuntimeException If the response content type is not JSON
143 * @throws \InvalidArgumentException If the JSON cannot be parsed
144 * @throws \LogicException When Guzzle cannot populate the response
145 *
146 * @return string PDF Summary report
147 */
148 public function getPDFPayoutsSummaryReport(array $params = [])
149 {
150 return $this->get(self::$path . '/payouts-summary?' . http_build_query($params))
151 ->expectSuccessfull()
152 ->status('200')
153 ->contentType('application/pdf')
154 ->getBody();
155 }
156 }
157