blib/lib/Finance/Bank/BNPParibas.pm

filter: Statementstmt 18.1% 20 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 30.0% 6 / 20 Podpod 100% 1 / 1 totaltotal 15.4% 27 / 175 CC 21 SCAR 47.1
CC
21
Cov
14%
check_balance
272.0
as_string
12.0
_normalize_date
6.0
CRAP 111.2
↑ Summary
1 package Finance::Bank::BNPParibas;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use LWP::Debug qw(+);
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = 0.09;
10  
11 1 ▶use constant BASE_URL => 'https://www.secure.bnpparibas.net/controller?type=homeconnex';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant LOGIN_FORM_NAME => 'logincanalnet';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =pod
15  
16 =head1 NAME
17  
18 Finance::Bank::BNPParibas - Check your BNP bank accounts from Perl
19  
20 =head1 SYNOPSIS
21  
22 use Finance::Bank::BNPParibas;
23  
24 my @accounts = Finance::Bank::BNPParibas->check_balance(
25 username => "$username", # Be sure to put the numbers
26 password => "$password", # between quote.
27 );
28  
29 foreach my $account ( @accounts ){
30 local $\ = "\n";
31 print " Name ", $account->name;
32 print " Account_no ", $account->account_no;
33 print " Balance ", $account->balance;
34 print " Statement\n";
35  
36 foreach my $statement ( $account->statements ){
37 print $statement->as_string;
38 }
39 }
40  
41 =head1 DESCRIPTION
42  
43 This module provides a rudimentary interface to the BNPNet online
44 banking system at L<https://www.bnpnet.bnp.fr/>. You will need
45 either Crypt::SSLeay or IO::Socket::SSL installed for HTTPS support
46 to work with LWP.
47  
48 The interface of this module is directly taken from Simon Cozens'
49 Finance::Bank::LloydsTSB.
50  
51 =head1 WARNING
52  
53 This is code for B<online banking>, and that means B<your money>, and
54 that means B<BE CAREFUL>. You are encouraged, nay, expected, to audit
55 the source of this module yourself to reassure yourself that I am not
56 doing anything untoward with your banking data. This software is useful
57 to me, but is provided under B<NO GUARANTEE>, explicit or implied.
58  
59 =head1 METHODS
60  
61 =head2 check_balance( username => $username, password => $password, ua => $ua )
62  
63 Return a list of account (F::B::B::Account) objects, one for each of
64 your bank accounts. You can provide to this method a WWW::Mechanize
65 object as third argument.
66  
67 =cut
68  
69 0 ▶sub check_balance {
Subroutine check_balance: not called CC=16 SCAR=56.1
Pod documented
70 0 my ( $class, %opts ) = @_;
71 0 ▶ croak "Must provide a password" unless exists $opts{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"password"} 0 0 0
72 0 ▶ croak "Must provide a username" unless exists $opts{username};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"username"} 0 0 0
73  
74 0 my @accounts;
75  
76 0 ▶ $opts{ua} ||= WWW::Mechanize->new(
Condition0%0/3
$opts{"ua"} ||= "WWW::Mechanize"->new("agent", "Finance::Bank::BNPParibas/$Finance::Bank::BNPParibas::VERSION ($^O)", "cookie_jar", {})
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"ua"} ||= "WWW::Mechanize"->new("agent", "Finance::Bank::BNPParibas/$Finance::Bank::BNPParibas::VERSION ($^O)", "cookie_jar", {})
$opts{"ua"} "WWW::Mechanize"->new("agent", "Finance::Bank::BNPParibas/$Finance::Bank::BNPParibas::VERSION ($^O)", "cookie_jar", {})
Truth table0%0/3
A ||= B
A= $opts{"ua"}B= "WWW::Mechanize"->new("agent", "Finance::Bank::BNPParibas/$Finance::Bank::BNPParibas::VERSION ($^O)", "cookie_jar", {})
ABresult
1X1
011
000
77 agent => "Finance::Bank::BNPParibas/$VERSION ($^O)",
78 cookie_jar => {},
79 );
80  
81 0 my $self = bless {%opts}, $class;
82  
83 0 my $orig_r;
84 0 my $count = 0;
85 {
86 0 $orig_r = $self->{ua}->get(BASE_URL);
87  
88 # loop detected, try again
89 0 ++$count;
90 0 ▶ redo unless $orig_r->content || $count > 13;
Branch: 1 branch
BranchTrueFalseTotal
unless $orig_r->content or $count > 13 0 0 0
Condition0%0/3
$orig_r->content or $count > 13
l!l&&r!l&&!r
000
MC/DC0%0/2
$orig_r->content or $count > 13
$orig_r->content $count > 13
Truth table0%0/3
A or B
A= $orig_r->contentB= $count > 13
ABresult
1X1
011
000
91 }
92 0 ▶ croak $orig_r->error_as_HTML if $orig_r->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $orig_r->is_error 0 0 0
93  
94 # As of 2005-04-19, BNP changed their default login form to a fancy
95 # imagemap to compose the password, thankfully, they still provide
96 # access to the old login form:
97 0 $self->{ua}->follow_link( url_regex => qr/identifiant=secure_bnpparibas_net/ );
98  
99 # Check if the login form is in the page.
100 0 $self->{ua}->quiet(1);
101 0 ▶ $self->{ua}->form_name(LOGIN_FORM_NAME)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ua"}->form_name("logincanalnet") or croak("Cannot find the login form 'logincanalnet'") 0 0 0
102 or croak "Cannot find the login form '" . LOGIN_FORM_NAME . "'";
103  
104 $self->{ua}->set_fields(
105 login => $self->{username},
106 password => $self->{password},
107 0 );
108  
109 0 my $click_r = $self->{ua}->submit;
110  
111 0 $self->{ua}->quiet(0);
112 0 ▶ croak $click_r->error_as_HTML if $click_r->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $click_r->is_error 0 0 0
113  
114 # XXX Without this header, bnpnet won't send the next page.
115 0 $self->{ua}->add_header( Accept => 'text/html' );
116  
117 0 $self->{ua}->get('/SAF_TLC');
118  
119 # Check if the 100 login limit is reached:
120 0 ▶ if ( $self->{ua}->content =~ /Code erreur=13/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ua"}->content =~ /Code erreur=13/) 0 0 0
121 0 carp "Trying to login more than 100 times with the same password\n";
122  
123 # SAF_CHM is the page to chang password
124 0 $self->{ua}->get('/SAF_CHM');
125  
126 0 my @numbers = ( 0 .. 9 );
127 0 my $temp_password = join ( '', @numbers[ map { rand @numbers } ( 1 .. 6 ) ] );
128  
129 0 carp "temp password: '$temp_password'\n";
130
131 $self->{ua}->set_fields(
132 ch1 => $self->{password},
133 0 ch2 => $temp_password,
134 ch3 => $temp_password,
135 );
136 0 $self->{ua}->submit;
137  
138 0 $self->{ua}->get('/SAF_CHM');
139 $self->{ua}->set_fields(
140 ch1 => $temp_password,
141 ch2 => $self->{password},
142 ch3 => $self->{password},
143 0 );
144 0 $self->{ua}->submit;
145
146 0 $self->{ua}->get('/SAF_TLC');
147 }
148  
149
150 # Check if the account download form is in the page.
151 0 $self->{ua}->quiet(1);
152 0 ▶ $self->{ua}->form_number(1)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ua"}->form_number(1) or croak("Cannot find the account download form") 0 0 0
153 or croak "Cannot find the account download form";
154 0 $self->{ua}->quiet(0);
155  
156 # If there is only one account, no radio button is present in the form.
157 # We need to add one manually.
158 # see http://rt.cpan.org/Ticket/Display.html?id=3156
159 0 ▶ unless ( $self->{ua}->{form}->find_input( "ch_rop", "radio" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}{"form"}->find_input("ch_rop", "radio")) 0 0 0
160 $self->{ua}->{form}
161 0 ->push_input( "radio", { type => "radio", name => "ch_rop", value => "tous" } );
162 }
163
164 $self->{ua}->set_fields(
165 0 ch_rop => 'tous',
166 ch_rop_fmt_fic => 'RTEXC',
167 ch_rop_fmt_dat => 'JJMMAA',
168 ch_rop_fmt_sep => 'VG',
169 ch_rop_dat => 'tous',
170 ch_rop_dat_deb => '',
171 ch_rop_dat_fin => '',
172 ch_memo => 'OUI',
173 );
174
175 0 $self->{ua}->submit;
176  
177 0 foreach ( @{ $self->{ua}->{links} } ) {
178 0 my $qif = $_->[0];
179 0 ▶ next unless $qif =~ /\.exl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $qif =~ /\.exl$/ 0 0 0
180  
181 0 my $qif_r = $self->{ua}->get($qif);
182 0 ▶ carp $qif_r->error_as_HTML if $qif_r->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $qif_r->is_error 0 0 0
183  
184 next
185 if $self->{ua}->{content} =~
186 0 ▶ /<html>/i; # no operation for this account
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua"}{"content"} =~ /<html>/i 0 0 0
187 push @accounts,
188 0 Finance::Bank::BNPParibas::Account->new( $self->{ua}->content );
189 }
190 0 @accounts;
191 }
192  
193 # The format of the date from BNPNet is DD/MM/YY, so we have to transform it to
194 # an ISO format: YYYY-MM-DD
195 0 ▶sub _normalize_date {
Subroutine _normalize_date: not called CC=2 SCAR=17.9
196 0 my $date = shift;
197 0 my ( $d, $m, $y ) = split ( /\//, $date );
198 0 ▶ $y = $y =~ /^[789]\d$/ ? $y + 1900 : $y + 2000;
Branch: 1 branch
BranchTrueFalseTotal
$y =~ /^[789]\d$/ ? : 0 0 0
199 0 return "$y-$m-$d";
200 }
201  
202 package Finance::Bank::BNPParibas::Account;
203  
204 =pod
205  
206 =head1 Account methods
207  
208 =head2 sort_code()
209  
210 Return the sort code of the account. Currently, it returns an
211 undefined value.
212  
213 =head2 name()
214  
215 Returns the human-readable name of the account.
216  
217 =head2 account_no()
218  
219 Return the account number, in the form C<XXX YYYYYYYYY ZZ>, where X, Y
220 and Z are numbers.
221  
222 =head2 balance()
223  
224 Returns the balance of the account. Note that the BNP site displays them
225 in French format (i.e C<123,75>), but the string returns a number perl
226 understands (i.e C<123.75>).
227  
228 =head2 statements()
229  
230 Return a list of Statement object (Finance::Bank::BNPParibas::Statement).
231  
232 =cut
233  
234 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
235 0 my $class = shift;
236 0 chomp( my @content = split ( /\n/, shift ) );
237 0 my $header = shift @content;
238  
239 0 my ( $name, $account_no, $date, $balance ) =
240 ( $header =~
241 m/^(.+)\s+(\d{5}\s+\d{9}\s+\d{2})\t+(\d{2}\/\d{2}\/\d{2})\t+(\d+,\d+)/
242 );
243  
244 0 $balance =~ s/,/./;
245  
246 0 my @statements;
247 push @statements,
248 0 Finance::Bank::BNPParibas::Statement->new($_) foreach @content;
249  
250 0 $date = Finance::Bank::BNPParibas::_normalize_date($date);
251  
252 0 bless {
253 name => $name,
254 account_no => $account_no,
255 sort_code => undef,
256 date => $date,
257 balance => $balance,
258 statements => [@statements],
259 }, $class;
260 }
261  
262 0 ▶sub sort_code { undef }
Subroutine sort_code: not called CC=1 SCAR=6.9
263 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
264 0 ▶sub account_no { $_[0]->{account_no} }
Subroutine account_no: not called CC=1 SCAR=6.9
265 0 ▶sub balance { $_[0]->{balance} }
Subroutine balance: not called CC=1 SCAR=6.9
266 0 ▶sub statements { @{ $_[0]->{statements} } }
Subroutine statements: not called CC=1 SCAR=6.9
267  
268 package Finance::Bank::BNPParibas::Statement;
269  
270 =pod
271  
272 =head1 Statement methods
273  
274 =head2 date()
275  
276 Returns the date when the statement occured, in YYYY-MM-DD format.
277  
278 =head2 value_date()
279  
280 Returns the date the transfer entry to an account is considered
281 effective, in YYYY-MM-DD format.
282  
283 =head2 description()
284  
285 Returns a brief description of the statement.
286  
287 =head2 amount()
288  
289 Returns the amount of the statement (expressed in Euros).
290  
291 =head2 as_string($separator)
292  
293 Returns a tab-delimited representation of the statement. By default, it
294 uses a tabulation to separate the fields, but the user can provide its
295 own separator.
296  
297 =cut
298  
299 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
300 0 my $class = shift;
301 0 my $statement = shift;
302  
303 0 my @entry = split ( /\t/, $statement );
304  
305 0 pop @entry;
306  
307 0 my $self = {};
308  
309 0 $self->{date} = Finance::Bank::BNPParibas::_normalize_date( $entry[0] );
310 0 $entry[1] =~ s/\s+/ /g;
311 0 $self->{description} = $entry[1];
312 0 ▶ if ( scalar @entry == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entry == 3) { } 0 0 0
313 0 $entry[2] =~ s/,/./;
314 0 $self->{amount} = $entry[2];
315 }
316 else {
317 $self->{value_date} =
318 0 Finance::Bank::BNPParibas::_normalize_date( $entry[2] );
319 0 $entry[3] =~ s/,/./;
320 0 $self->{amount} = $entry[3];
321 }
322  
323 0 bless $self, $class;
324 }
325  
326 0 ▶sub date { $_[0]->{date} }
Subroutine date: not called CC=1 SCAR=6.9
327 0 ▶sub value_date { $_[0]->{value_date} }
Subroutine value_date: not called CC=1 SCAR=6.9
328 0 ▶sub description { $_[0]->{description} }
Subroutine description: not called CC=1 SCAR=6.9
329 0 ▶sub amount { $_[0]->{amount} }
Subroutine amount: not called CC=1 SCAR=6.9
330  
331 0 ▶sub as_string {
Subroutine as_string: not called CC=3 SCAR=24.8
332 join ( $_[1] || "\t", $_[0]->{date}, $_[0]->{description}, ($_[0]->{value_date} ||''), $_[0]->{amount} )
333 0 ▶}
Condition0%0/4
$_[1] || "\t"
l!l
00
$_[0]{'value_date'} || ''
l!l
00
MC/DC0%0/1
$_[1] || "\t"
$_[1]
MC/DC0%0/1
$_[0]{'value_date'} || ''
$_[0]{'value_date'}
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'value_date'}
Aresult
11
00
334  
335 1 1;
336  
337 __END__
↑ Summary