Finance/Bank/Postbank_de.pm

filter: Statementstmt 44.1% 53 / 120 Branchbran 16.6% 4 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 38.8% 14 / 36 Podpod 44.4% 8 / 18 totaltotal 37.0% 79 / 213 CC 17 SCAR 20.1
CC
17
Cov
37%
get_account_statement
30.0
_build_account_numbers
20.0
new_session
12.0
CRAP 7.4
« Finance/Bank/Postbank_de/Account.pm ↑ Summary HAL/Resource.pm »
1 package Finance::Bank::Postbank_de;
2  
3 9 ▶use 5.006; # we use lexical filehandles now
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Finance::Bank::Postbank_de::Account;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Finance::Bank::Postbank_de::APIv1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #use IO::Socket::SSL qw(SSL_VERIFY_PEER SSL_VERIFY_NONE);
18  
19 9 our $VERSION = '0.57';
20  
21 9 has 'login' => (
22 is => 'ro',
23 );
24  
25 9 has 'password' => (
26 is => 'ro',
27 );
28  
29 has 'urls' => (
30 is => 'ro',
31 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 9 );
33  
34 has 'logger' => (
35 is => 'ro',
36 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 9 );
38  
39 has 'past_days' => (
40 is => 'ro',
41 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 9 );
43  
44 has 'api' => (
45 is => 'rw',
46 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
47 8 my $api = Finance::Bank::Postbank_de::APIv1->new();
48 8 $api->configure_ua();
49 0 $api
50 },
51 9 );
52  
53 9 has 'session' => (
54 is => 'rw',
55 );
56  
57 9 has '_account_numbers' => (
58 is => 'rw',
59 );
60  
61 9 our %functions;
62 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
63 9 %functions = (
64 quit => [ text_regex => qr'\bBanking\s+beenden\b' ],
65 accountstatement => [ text_regex => qr'\bUms.*?tze\b' ],
66 );
67 };
68  
69 8 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
70 8 my ($orig,$class,%args) = @_;
71  
72 croak "Login/Account number must be specified"
73 8 ▶ unless $args{login};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"login"} 0 8 8
74 croak "Password/PIN must be specified"
75 8 ▶ unless $args{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"password"} 0 8 8
76 8 ▶ if( exists $args{ status }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"status"}) 7 1 8
77 7 $args{ logger } = delete $args{ status };
78 };
79  
80 8 $orig->($class, %args);
81 9 };
82  
83 0 ▶sub log { $_[0]->logger->(@_); };
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
84 0 ▶sub log_httpresult { $_[0]->log("HTTP Code",$_[0]->agent->status,$_[0]->agent->res->headers->as_string . $_[0]->agent->content) };
Subroutine log_httpresult: not called CC=1 SCAR=6.9
Pod undocumented
85  
86 0 ▶sub new_session {
Subroutine new_session: not called CC=3 SCAR=24.8
Pod documented
87 0 my ($self) = @_;
88  
89 0 ▶ $self->close_session()
Branch: 1 branch
BranchTrueFalseTotal
if $self->session 0 0 0
90 if ($self->session);
91 0 my $pb;
92 0 my $ok = eval {
93 0 $pb = $self->api->login( $self->login, $self->password );
94 0 1
95 };
96 0 ▶ if( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) { } 0 0 0
97 #warn sprintf "Got HTTP error %d, message %s", $self->api->ua->status, $self->api->ua->message;
98 #croak $@;
99 } else {
100 0 $self->session( $pb );
101 }
102 };
103  
104 0 ▶sub is_security_advice {
Subroutine is_security_advice: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self) = @_;
106 #$self->agent->content() =~ /\bZum\s+Finanzstatus\b/;
107 };
108  
109 0 ▶sub is_nutzungshinweis {
Subroutine is_nutzungshinweis: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my ($self) = @_;
111 #$self->agent->content() =~ /\bAus Sicherheitsgr.*?nden haben wir einige\b/;
112 };
113  
114  
115 0 ▶sub skip_security_advice {
Subroutine skip_security_advice: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self) = @_;
117 #$self->log('Skipping security advice page');
118 #$self->agent->follow_link(text_regex => qr/\bZum\s+Finanzstatus\b/);
119 # $self->agent->content() =~ /Sicherheitshinweis/;
120 };
121  
122 0 ▶sub skip_nutzungshinweis {
Subroutine skip_nutzungshinweis: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ($self) = @_;
124 #$self->log('Skipping nutzungshinweis page');
125 #$self->agent->follow_link(text_regex => qr/\bZur\s+Konten.bersicht\b/);
126 # $self->agent->content() =~ /Sicherheitshinweis/;
127 };
128  
129 0 ▶sub error_page {
Subroutine error_page: not called CC=1 SCAR=6.9
Pod undocumented
130 # Check if an error page is shown
131 0 my ($self) = @_;
132 0 $self->api->ua->status != 200
133 #return unless $self->agent;
134 #
135 #$self->agent->content =~ m!<p\s+class="form-error">!sm
136 # or
137 #$self->agent->content =~ m!<p\s+class="field-error">!sm
138 # or $self->maintenance;
139 };
140  
141 0 ▶sub error_message {
Subroutine error_message: not called CC=1 SCAR=6.9
Pod undocumented
142 0 my ($self) = @_;
143 #return unless $self->agent;
144 #die "No error condition detected in:\n" . $self->agent->content
145 # unless $self->error_page;
146 #if(
147 #$self->agent->content =~ m!<p\s+class="form-error">\s*<strong>\s*(.*?)\s*</strong>\s*</p>!sm
148 # or
149 #$self->agent->content =~ m!<p\s+class="field-error">\s*(.*?)\s*</p>!sm
150 # ) { return $1 }
151 # #or croak "No error message found in:\n" . $self->agent->content;
152 0 return ''
153 };
154  
155 0 ▶sub maintenance {
Subroutine maintenance: not called CC=1 SCAR=6.9
Pod documented
156 0 my ($self) = @_;
157 #return unless $self->agent;
158 ##$self->error_page and
159 #$self->agent->content =~ m!Sehr geehrter <span lang="en">Online-Banking</span>\s+Nutzer,\s+wegen einer hohen Auslastung kommt es derzeit im Online-Banking zu\s*l&auml;ngeren Wartezeiten.!sm
160 #or $self->agent->content =~ m!&nbsp;Wartung\b!
161 #or $self->agent->content =~ m!<p class="important">\s*<strong>\s*Diese Funktion steht auf Grund einer technischen St.*?rung derzeit leider nicht zur Verf.*?gung.*?</strong>\s*</p>!sm # Testumgebung...
162 ()
163 0 };
164  
165 0 ▶sub access_denied {
Subroutine access_denied: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my ($self) = @_;
167 0 $self->api->ua->status == 401
168 #if ($self->error_page) {
169 # my $message = $self->error_message;
170 #
171 # return (
172 # $message =~ m!^Die Kontonummer ist nicht für das Internet Online-Banking freigeschaltet. Bitte verwenden Sie zur Freischaltung den Link "Online-Banking freischalten"\.<br />\s*$!sm
173 # or $message =~ m!^Sie haben zu viele Zeichen in das Feld eingegeben.<br />\s*$!sm
174 # or $message =~ m!^Die eingegebene Postbank Girokontonummer ist zu lang. Bitte überprüfen Sie Ihre Eingabe.$!sm
175 # or $message =~ m!^Die Anmeldung ist fehlgeschlagen. Bitte vergewissern Sie sich der Richtigkeit Ihrer Eingaben und f.*?hren Sie den Anmeldevorgang erneut durch.\s*$!sm
176 # )
177 #} else {
178 # return;
179 #};
180 };
181  
182 0 ▶sub session_timed_out {
Subroutine session_timed_out: not called CC=1 SCAR=6.9
Pod documented
183 0 my ($self) = @_;
184 #$self->agent->content =~ /Die Sitzungsdaten sind ung&uuml;ltig, bitte f&uuml;hren Sie einen erneuten Login durch.\s+\(27000\)/;
185 ()
186 0 };
187  
188 0 ▶sub select_function {
Subroutine select_function: not called CC=3 SCAR=24.8
Pod documented
189 0 my ($self,$function) = @_;
190 0 ▶ if (! $self->session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->session) 0 0 0
191 0 $self->new_session;
192 };
193 croak "Unknown account function '$function'"
194 0 ▶ unless exists $functions{$function};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Finance::Bank::Postbank_de::functions{$function} 0 0 0
195 0 my $method = $functions{ $function };
196  
197 0 my $res = $self->session->navigate($method);
198 0 $res
199 };
200  
201 0 ▶sub close_session {
Subroutine close_session: not called CC=1 SCAR=6.9
Pod documented
202 0 my ($self) = @_;
203 0 $self->session(undef);
204 0 $self->api(undef);
205 0 1
206 };
207  
208 0 ▶sub finanzstatus {
Subroutine finanzstatus: not called CC=2 SCAR=17.9
Pod undocumented
209 0 my( $self ) = @_;
210 0 ▶ $self->new_session unless $self->session;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->session 0 0 0
211 0 my $finanzstatus = $self->session->navigate(
212 class => 'Finance::Bank::Postbank_de::APIv1::Finanzstatus',
213 path => ['banking_v1' => 'financialstatus']
214 );
215 }
216  
217 0 ▶sub _build_account_numbers {
Subroutine _build_account_numbers: not called CC=4 SCAR=30.0
218 0 my ($self,%args) = @_;
219
220 0 my $finanzstatus = $self->finanzstatus;
221 0 (my $bp) = $finanzstatus->get_businesspartners; # always take the first...
222 0 my %numbers;
223 # this currently includes the credit card numbers ...
224 0 for my $acc ( $bp->get_accounts() ) {
225 0 ▶ $numbers{ $acc->iban } = $acc if (!$acc->is_depot and !$acc->is_mortgage);
Branch: 1 branch
BranchTrueFalseTotal
unless $acc->is_depot or $acc->is_mortgage 0 0 0
Condition0%0/3
$acc->is_depot or $acc->is_mortgage
l!l&&r!l&&!r
000
MC/DC0%0/2
$acc->is_depot or $acc->is_mortgage
$acc->is_depot $acc->is_mortgage
Truth table0%0/3
A or B
A= $acc->is_depotB= $acc->is_mortgage
ABresult
1X1
011
000
226 };
227  
228 0 return $self->_account_numbers( \%numbers );
229 }
230  
231 0 ▶sub account_numbers {
Subroutine account_numbers: not called CC=2 SCAR=17.9
Pod documented
232 0 my ($self,%args) = @_;
233  
234 0 ▶ my $n = $self->_account_numbers || $self->_build_account_numbers;
Condition0%0/3
$self->_account_numbers || $self->_build_account_numbers
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_account_numbers || $self->_build_account_numbers
$self->_account_numbers $self->_build_account_numbers
Truth table0%0/3
A || B
A= $self->_account_numbersB= $self->_build_account_numbers
ABresult
1X1
011
000
235
236 0 sort keys %{ $n };
237 };
238  
239 0 ▶sub get_account_statement {
Subroutine get_account_statement: not called CC=5 SCAR=34.0
Pod documented
240 0 my ($self,%args) = @_;
241  
242 #my $past_days = $args{past_days} || $self->{past_days};
243 #if($past_days) {
244 # my ($day, $month, $year) = split/\./, $agent->current_form->value('umsatzanzeigeGiro:salesForm:umsatzFilterOptionenAufklappbarSuchfeldPanel:accordion:vonBisDatum:datumForm:bisGruppe:bisDatum');
245 # my $end_epoch = timegm(0, 0, 0, $day, $month-1, $year);
246 # my $from_date = strftime '%d.%m.%Y', localtime($end_epoch-($past_days-1)*60*60*24);
247 # $agent->current_form->value('umsatzanzeigeGiro:salesForm:umsatzFilterOptionenAufklappbarSuchfeldPanel:accordion:vonBisDatum:datumForm:vonGruppe:vonDatum' => $from_date);
248 #};
249  
250 0 ▶ my $accounts = $self->_account_numbers || $self->_build_account_numbers;
Condition0%0/3
$self->_account_numbers || $self->_build_account_numbers
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_account_numbers || $self->_build_account_numbers
$self->_account_numbers $self->_build_account_numbers
Truth table0%0/3
A || B
A= $self->_account_numbersB= $self->_build_account_numbers
ABresult
1X1
011
000
251  
252 0 ▶ if( ! $args{ account_number }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"account_number"}) 0 0 0
253 # Hopefully we only got one account (?!)
254 0 ($args{ account_number }) = keys %$accounts;
255 };
256  
257 0 my $account = $accounts->{ $args{ account_number }};
258  
259 #if (exists $args{account_number}) {
260 # $self->log("Getting account statement for $args{account_number}");
261 # # Load the account numbers if not already loaded
262 # $self->account_numbers;
263 # if(! exists $self->{account_numbers}->{$args{account_number}}) {
264 # croak "Unknown account number '$args{account_number}'";
265 # };
266 # my $index = $self->{account_numbers}->{$args{account_number}};
267 # $agent->current_form->param( 'selectForm:kontoauswahl' => $index );
268 #} else {
269 # my @accounts = $agent->current_form->value('selectForm:kontoauswahl');
270 # $self->log("Getting account statement via default (@accounts)");
271 #};
272 0 my $content = $account->transactions_csv();
273 0 ▶ if( $args{ file }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file"}) 0 0 0
274 open my $fh, '>', $args{ file }
275 0 ▶ or croak "Couldn't create '$args{ file }': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $args{"file"} or croak(qq[Couldn't create '$args{"file"}': $!]) 0 0 0
276 0 binmode $fh, ':encoding(UTF-8)';
277 0 print $fh $content;
278 };
279 #if ($agent->status == 200) {
280 0 my $result = $content;
281 # Result is in UTF-8
282 0 return Finance::Bank::Postbank_de::Account->parse_statement(content => $result);
283 };
284  
285 0 ▶sub unread_messages {
Subroutine unread_messages: not called CC=1 SCAR=6.9
Pod documented
286 0 my( $self )= @_;
287 0 $self->finanzstatus->available_messages
288 }
289  
290 9 1;
291 __END__
« Finance/Bank/Postbank_de/Account.pm ↑ Summary HAL/Resource.pm »