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\Connector;
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 Connector $connector HTTP transport connector
45 */
46 public function __construct(Connector $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 ->status('200')
79 ->contentType('text/csv')
80 ->getBody();
81 }
82
83 /**
84 * Returns a single settlement summed up in pdf format.
85 *
86 * @param string $paymentReference The reference id of the payout.
87 *
88 * @throws ConnectorException When the API replies with an error response
89 * @throws RequestException When an error is encountered
90 * @throws \RuntimeException On an unexpected API response
91 * @throws \RuntimeException If the response content type is not JSON
92 * @throws \InvalidArgumentException If the JSON cannot be parsed
93 * @throws \LogicException When Guzzle cannot populate the response
94 *
95 * @return string Binary PDF representation of Payout report
96 */
97 public function getPDFPayoutReport($paymentReference)
98 {
99 return $this->get(self::$path . "/payout?payment_reference={$paymentReference}")
100 ->status('200')
101 ->contentType('application/pdf')
102 ->getBody();
103 }
104
105 /**
106 * Returns CSV summary.
107 *
108 * @param array $params Additional query params to filter payouts.
109 *
110 * @see https://developers.klarna.com/api/#settlements-api-get-payouts-summary-report-with-transactions
111 *
112 * @throws ConnectorException When the API replies with an error response
113 * @throws RequestException When an error is encountered
114 * @throws \RuntimeException On an unexpected API response
115 * @throws \RuntimeException If the response content type is not JSON
116 * @throws \InvalidArgumentException If the JSON cannot be parsed
117 * @throws \LogicException When Guzzle cannot populate the response
118 *
119 * @return string CSV Summary report
120 */
121 public function getCSVPayoutsSummaryReport(array $params = [])
122 {
123 return $this->get(self::$path . '/payouts-summary-with-transactions?' . http_build_query($params))
124 ->status('200')
125 ->contentType('text/csv')
126 ->getBody();
127 }
128
129 /**
130 * Returns PDF summary.
131 *
132 * @param array $params Additional query params to filter payouts.
133 *
134 * @see https://developers.klarna.com/api/#settlements-api-get-payouts-summary-report-with-transactions
135 *
136 * @throws ConnectorException When the API replies with an error response
137 * @throws RequestException When an error is encountered
138 * @throws \RuntimeException On an unexpected API response
139 * @throws \RuntimeException If the response content type is not JSON
140 * @throws \InvalidArgumentException If the JSON cannot be parsed
141 * @throws \LogicException When Guzzle cannot populate the response
142 *
143 * @return string PDF Summary report
144 */
145 public function getPDFPayoutsSummaryReport(array $params = [])
146 {
147 return $this->get(self::$path . '/payouts-summary?' . http_build_query($params))
148 ->status('200')
149 ->contentType('application/pdf')
150 ->getBody();
151 }
152 }
153