source: trunk/Modules/FinanceBankAPI/FioAPI.php@ 550

Last change on this file since 550 was 550, checked in by chronos, 12 years ago
  • Upraveno: Přepracována třída pro import plateb přes Fio API. Nyní se načítají položky přes CSV formát.
  • Přidáno: Import z Fio do tabulky FinanceBankImport a odsud pak pokud je nalezen variabilní symbol tak do FinanceOperation.
File size: 4.6 KB
Line 
1<?php
2
3// Specifikace API: http://www.fio.cz/docs/cz/API_Bankovnictvi.pdf
4
5include('GPC.php');
6
7function RemoveComma($Text)
8{
9 if((mb_strlen($Text) >= 2) and ($Text[0] == '"') and (mb_substr($Text, -1, 1) == '"')) return(mb_substr($Text, 1, -1));
10 else return($Text);
11}
12
13class FioAPI
14{
15 var $Token;
16 var $Encoding;
17 var $Format;
18
19 function __construct()
20 {
21 $this->Encoding = 'utf-8';
22 $this->Format = 'csv';
23 }
24
25 function Import($TimeFrom, $TimeTo)
26 {
27 if($this->Token == '') throw new Exception('Missing value for Token property.');
28
29 // URL format: https://www.fio.cz/ib_api/rest/periods/{token}/{datum od}/{datum do}/transactions.{format}
30 // Send request
31 $RequestURL = '/ib_api/rest/periods/'.$this->Token.'/'.
32 date('Y-m-d', $TimeFrom).'/'.date('Y-m-d', $TimeTo).'/transactions.'.$this->Format;
33 echo($RequestURL);
34 $Response = file_get_contents('https://www.fio.cz'.$RequestURL);
35 if($Response == FALSE)
36 {
37 throw new Exception('Connection error');
38 } else
39 {
40 echo($Response);
41 if($this->Format == 'gpc') $Response = iconv('windows-1250', $this->Encoding, $Response);
42 $Response = explode("\n", $Response);
43
44 if($this->Format == 'gpc')
45 {
46 // Parse all GPC lines
47 $GPC = new GPC();
48 $Result = array();
49 foreach($Response as $Index => $Line)
50 {
51 if(($Index == 0) and (substr($Line, 0, strlen(GPC_TYPE_REPORT)) != GPC_TYPE_REPORT)) $this->NoValidDataError($Response);
52 $GPCLine = $GPC->ParseLine($Line);
53 if($GPCLine != NULL) $Result[] = $GPCLine;
54 }
55 } else
56 if($this->Format == 'csv')
57 {
58 $Result = array(
59 'Items' => array(),
60 );
61
62 // CVS header
63 while($Response[0] != '')
64 {
65 $Line = explode(';', $Response[0]);
66 if($Line[0] == 'accountId') $Result['AccountNumber'] = $Line[0];
67 else if($Line[0] == 'bankId') $Result['BankId'] = $Line[0];
68 else if($Line[0] == 'currency') $Result['Currency'] = $Line[0];
69 else if($Line[0] == 'iban') $Result['IBAN'] = $Line[0];
70 else if($Line[0] == 'bic') $Result['BIC'] = $Line[0];
71 else if($Line[0] == 'openingBalance') $Result['OpeningBalance'] = $Line[0];
72 else if($Line[0] == 'closingBalance') $Result['ClosingBalance'] = $Line[0];
73 else if($Line[0] == 'dateStart') $Result['DateStart'] = $Line[0];
74 else if($Line[0] == 'dateEnd') $Result['DateEnd'] = $Line[0];
75 else if($Line[0] == 'idFrom') $Result['IdFrom'] = $Line[0];
76 else if($Line[0] == 'idTo') $Result['IdTo'] = $Line[0];
77 array_shift($Response);
78 }
79 array_shift($Response); // Remove empty line
80
81 if($Response[0] != 'ID pohybu;Datum;Objem;Měna;Protiúčet;Název protiúčtu;Kód banky;Název banky;KS;VS;SS;Uživatelská identifikace;Zpráva pro příjemce;Typ;Provedl;Upřesnění;Komentář;BIC;ID pokynu'
82 ) throw new Exception('Unsupported CSV header');
83 array_shift($Response);
84 array_pop($Response);
85 foreach($Response as $Index => $Line)
86 {
87 $Line = explode(';', $Line);
88 $Date = explode('.', $Line[1]);
89 $Date = mktime(0, 0, 0, $Date[1], $Date[0], $Date[2]);
90 $NewRecord = array('ID' => $Line[0], 'Date' => $Date, 'Value' => $Line[2], 'CurrencyCode' => $Line[3],
91 'OffsetAccount' => $Line[4], 'OffsetAccountName' => $Line[5], 'BankCode' => $Line[6], 'BankName' => RemoveComma($Line[7]),
92 'ConstantSymbol' => $Line[8], 'VariableSymbol' => $Line[9], 'SpecificSymbol' => $Line[10],
93 'UserIdent' => RemoveComma($Line[11]), 'Message' => RemoveComma($Line[12]), 'Type' => RemoveComma($Line[13]),
94 'User' => RemoveComma($Line[14]), 'Details' => RemoveComma($Line[15]), 'Comment' => RemoveComma($Line[16]),
95 'BIC' => $Line[17], 'OrderID' => $Line[18]);
96 $Result['Items'][] = $NewRecord;
97 }
98 }
99 return($Result);
100 }
101 }
102
103 function NoValidDataError($Response)
104 {
105 // Try to get error message
106 // If something go wrong fio show HTML login page and display error message
107 $Response = implode('', $Response);
108 $ErrorMessageStart = '<div id="oldform_warning">';
109 if(strpos($Response, $ErrorMessageStart) !== false)
110 {
111 $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
112 $ErrorMessage = trim(substr($Response, 0, strpos($Response, '</div>')));
113 } else $ErrorMessage = '';
114 throw new Exception('No valid GPC data: '.$ErrorMessage);
115 }
116}
Note: See TracBrowser for help on using the repository browser.