/
TargoBank.php
176 lines (155 loc) · 5.3 KB
/
TargoBank.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace splitbrain\TheBankster\Backend;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\RequestOptions;
use splitbrain\TheBankster\Entity\Transaction;
/**
* Class TargoBank
*
* Imports credit card transactions from TargoBank by scraping the web interface
*
* @package splitbrain\TheBankster\Backend
*/
class TargoBank extends AbstractBackend
{
protected $jar;
protected $client;
/** @inheritdoc */
public function __construct($config, $accountid)
{
parent::__construct($config, $accountid);
$this->jar = new CookieJar();
$this->client = new Client([
'cookies' => $this->jar,
RequestOptions::HEADERS => [
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language' => 'en-US,en;q=0.8,de;q=0.6',
'Upgrade-Insecure-Requests' => '1',
'Cache-Control' => 'max-age=0',
],
RequestOptions::ALLOW_REDIRECTS => [
'max' => 10, // allow at most 10 redirects.
'strict' => true, // use "strict" RFC compliant redirects.
'referer' => true, // add a Referer header
'track_redirects' => true,
]
]);
}
/** @inheritdoc */
public static function configDescription()
{
return [
'user' => ['help' => 'Your online banking user name',],
'pass' => ['help' => 'Your online banking password', 'type' => 'password'],
];
}
/** @inheritdoc */
public function checkSetup()
{
if (!$this->login($this->config['user'], $this->config['pass'])) {
throw new \Exception('Failed to log in to TargoBank');
}
return 'Log in to TargoBank webinterface successful';
}
/** @inheritdoc */
public function importTransactions(\DateTime $since)
{
if (!$this->login($this->config['user'], $this->config['pass'])) {
throw new \Exception('Failed to log in to TargoBank');
}
$body = $this->getStatementPage();
\phpQuery::newDocumentHTML((string)$body);
$table = pq('table[summary="Activity Details"]');
if ($table->length !== 1) {
$this->logger->warning('Seems like there are no statements available for this month');
return;
}
$trs = $table->find('tr');
foreach ($trs as $tr) {
$tds = pq($tr)->find('td');
if ($tds->length !== 6) continue;
$tx = new Transaction();
$tx->datetime = $this->fixDate($tds->get(0)->textContent);
$tx->amount = $this->fixAmount($tds->get(4)->textContent);
$tx->description = join("\n", [
$tds->get(1)->textContent, // details
$tds->get(2)->textContent, // city
$tds->get(3)->textContent, // country
$tds->get(5)->textContent, // foreign currency
]);
$this->storeTransaction($tx);
}
}
/**
* Normalize dates
*
* @param string $date
* @return string
*/
protected function fixDate($date)
{
return join('-', array_reverse(explode('/', $date)));
}
/**
* Normalize amount
*
* @param string $amt
* @return float
*/
protected function fixAmount($amt)
{
$amt = preg_replace('/[^0-9,\\.\\-]+/', '', $amt);
$amt = str_replace(',', '.', $amt);
return floatval($amt);
}
/**
* Get the HTML of the statement page
*
* @return string
* @throws \Exception
*/
protected function getStatementPage()
{
if (!$this->login($this->config['user'], $this->config['pass'])) {
throw new \Exception('Failed to log in to TargoBank');
}
$this->logger->debug('Reading TargoBank webinterface');
$response = $this->client->get('https://www.targobank.de/de/banque/mouvements_icard.cgi');
if ($response->getStatusCode() !== 200) {
throw new \Exception('Got wrong status code from TargoBank');
}
return (string)$response->getBody();
}
/**
* Login to the web interface to open a session
*
* @param string $user
* @param string $pass
* @return bool true if the login succeeded
*/
protected function login($user, $pass)
{
$this->logger->debug('Logging into TargoBank webinterface');
$this->client->get('https://www.targobank.de/');
$url = 'https://www.targobank.de/de/identification/login.cgi';
$this->client->post(
$url,
[
RequestOptions::FORM_PARAMS => [
'_cm_user' => $user,
'flag' => 'password',
'_charset_' => 'windows-1252',
'_cm_pwd' => $pass,
'submit.x' => '74',
'submit.y' => '16',
],
RequestOptions::HEADERS => [
'Referer' => $url
]
]
);
return ($this->jar->getCookieByName('IdSes') !== null);
}
}