44 * php -S localhost:8000 -t example/
55 */
66
7- use SpojeNet \KbAccountsApi \Entity \Tokens ;
8- use SpojeNet \KbAccountsApi \Entity \ClientReq ;
97use SpojeNet \KbAccountsApi \Entity \ApplicationReq ;
10- use SpojeNet \KbAccountsApi \Entity \TransactionSelection ;
8+ use SpojeNet \KbAccountsApi \Entity \ClientReq ;
9+ use SpojeNet \KbAccountsApi \Entity \Tokens ;
1110use SpojeNet \KbAccountsApi \Exception \KbClientException ;
1211use SpojeNet \KbAccountsApi \KbClient ;
12+ use SpojeNet \KbAccountsApi \Selection \StatementPdfSelection ;
13+ use SpojeNet \KbAccountsApi \Selection \StatementsSelection ;
14+ use SpojeNet \KbAccountsApi \Selection \TransactionSelection ;
1315use SpojeNet \KbAccountsApi \Utils \Random ;
1416use Tracy \Debugger ;
1517
@@ -202,7 +204,7 @@ function application(): void
202204 type: $ app ->client ->type ,
203205 encryptionKey: $ app ->client ->encryptionKey ,
204206 ), $ app ->id );
205- $ clientIdLabel = '<a href=" ' . $ uri . '" target="_blank">authorize</a> ' ;
207+ $ clientIdLabel = '<a href=" ' . $ uri . '" target="_blank" rel="opener" >authorize</a> ' ;
206208 }
207209
208210 $ tokenLabel = 'Missing Client ID! ' ;
@@ -211,7 +213,7 @@ function application(): void
211213 $ tokenLabel .= $ app ->tokens ->refresh ->isValid () ? ' ✅ ' : ' 🚫 ' ;
212214 } elseif (isset ($ app ->client ->clientId )) {
213215 $ uri = $ kbClient ->clientAuthorizationCodeUri ($ app ->client ->clientId );
214- $ tokenLabel = '<a href=" ' . $ uri . '" target="_blank">authorize</a> ' ;
216+ $ tokenLabel = '<a href=" ' . $ uri . '" target="_blank" rel="opener" >authorize</a> ' ;
215217 }
216218
217219 echo <<<HTML
@@ -246,25 +248,41 @@ function application(): void
246248 <table>
247249 <tr><th>ID</th><th>IBAN</th><th>Currency</th></tr>
248250 HTML ;
249- $ accountIdForTransactions = null ;
251+ $ firstAccountId = null ;
250252 foreach ($ accounts as $ account ) {
251- $ accountIdForTransactions ??= $ account ->accountId ;
253+ $ firstAccountId ??= $ account ->accountId ;
252254 $ accountId = short ($ account ->accountId );
253255 echo <<<HTML
254256 <tr><td> {$ accountId }</td><td> {$ account ->iban }</td><td> {$ account ->currency }</td></tr>
255257 HTML ;
256258 }
257259 echo '</table> ' ;
258260
259- writeLabel ("Transactions for account ID {$ accountIdForTransactions }" );
260- $ transactions = $ kbClient ->transactions ($ app ->tokens ->access ->token , new TransactionSelection ($ accountIdForTransactions ));
261+ writeLabel ("Transactions for account ID {$ firstAccountId }" );
262+ $ transactions = $ kbClient ->transactions ($ app ->tokens ->access ->token , new TransactionSelection ($ firstAccountId ));
261263 echo <<<HTML
262264 <table>
263265 <tr><th>Items:</th><td> {$ transactions ->numberOfElements }</td></tr>
264266 <tr><th>Pages:</th><td> {$ transactions ->totalPages }</td></tr>
265267 </table>
266268 HTML ;
267269 array_map (fn ($ item ) => writeOutput ($ item ), $ transactions ->content );
270+
271+ writeLabel ("Statements for account ID {$ firstAccountId }" );
272+ $ statements = $ kbClient ->statements ($ app ->tokens ->access ->token , new StatementsSelection ($ firstAccountId , new DateTimeImmutable ()));
273+ echo <<<HTML
274+ <table>
275+ <tr><th>ID</th><th>Issued</th><th>Archived</th><th></th></tr>
276+ HTML ;
277+ foreach ($ statements as $ statement ) {
278+ $ issued = $ statement ->issued ->format ('Y-m-d H:i:s ' );
279+ $ archive = $ statement ->archive ? 'yes ' : 'no ' ;
280+ $ link = "/dl?account= {$ firstAccountId }&statement= {$ statement ->statementId }" ;
281+ echo <<<HTML
282+ <tr><td> {$ statement ->statementId }</td><td> {$ issued }</td><td> {$ archive }</td><td><a href=" {$ link }" target="_blank">PDF</a></td></tr>
283+ HTML ;
284+ }
285+ echo '</table> ' ;
268286}
269287
270288function delete (): void {
@@ -309,7 +327,7 @@ function callback(): void {
309327 <tr><th>API key</th><td> {$ apiKey }</td></tr>
310328 </table>
311329 <br />
312- <button onclick="window.onbeforeunload = () => {window. opener.location.href = '/app'} ; window.close()">app detail</button>
330+ <button onclick="window.opener.location.href = '/app'; window.close()">app detail</button>
313331 HTML ;
314332 }
315333 /** Callback from client's authorization of scope */
@@ -327,11 +345,40 @@ function callback(): void {
327345 <tr><th>Access token</th><td> {$ accessToken }</td></tr>
328346 </table>
329347 <br />
330- <button onclick="window.onbeforeunload = () => window. opener.location.reload(); window.close()">app detail</button>
348+ <button onclick="window.opener.location.reload(); window.close()">app detail</button>
331349 HTML ;
332350 }
333351}
334352
353+ function download (): void {
354+ global $ storage , $ kbClient ;
355+
356+ $ app = $ storage ->get ();
357+
358+ if ($ app === null ) {
359+ writeLabel ('Application not found ' );
360+ return ;
361+ }
362+
363+ if (isset ($ _GET ['statement ' ])) {
364+ $ content = $ kbClient ->statementPdf ($ app ->tokens ->access ->token , new StatementPdfSelection (
365+ accountId: sanitizeInput ('account ' ),
366+ statementId: sanitizeInput ('statement ' ),
367+ ));
368+
369+ ob_clean ();
370+ header ('Content-Type: application/pdf ' ); // nebo jiný vhodný MIME type
371+ header ('Content-Disposition: attachment; filename="statement.pdf" ' );
372+ header ('Content-Length: ' . strlen ($ content ));
373+ header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
374+ header ('Pragma: public ' );
375+ header ('Expires: 0 ' );
376+
377+ echo $ content ;
378+ exit ;
379+ }
380+ }
381+
335382// Application logic **************************************************************************************************
336383function run (): void
337384{
@@ -343,6 +390,7 @@ function run(): void
343390 '/app ' => fn () => application (),
344391 '/del ' => fn () => delete (),
345392 '/back ' => fn () => callback (),
393+ '/dl ' => fn () => download (),
346394 default => null ,
347395 };
348396
@@ -366,6 +414,7 @@ function run(): void
366414}
367415
368416// HTML ***************************************************************************************************************
417+ ob_start ();
369418?>
370419<!DOCTYPE html>
371420<html lang="en">
@@ -406,3 +455,4 @@ function run(): void
406455<?php run (); ?>
407456</body>
408457</html>
458+ <?php echo ob_get_clean ();
0 commit comments