blib/lib/Finance/Bank/US/INGDirect.pm

filter: Statementstmt 19.3% 24 / 124 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 53.8% 7 / 13 Podpod 100% 5 / 5 totaltotal 16.5% 36 / 217 CC 32 SCAR 41.1
CC
32
Cov
13%
_login
132.0
accounts
72.0
transfer
56.0
CRAP 60.7
↑ Summary
1 package Finance::Bank::US::INGDirect;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =head1 NAME
15  
16 Finance::Bank::US::INGDirect - Check balances and transactions for US INGDirect accounts
17  
18 =head1 VERSION
19  
20 Version 0.08
21  
22 =cut
23  
24 2 our $VERSION = '0.08';
25  
26 =head1 SYNOPSIS
27  
28 use Finance::Bank::US::INGDirect;
29 use Finance::OFX::Parse::Simple;
30  
31 my $ing = Finance::Bank::US::INGDirect->new(
32 saver_id => '...',
33 customer => '########',
34 questions => {
35 # Your questions may differ; examine the form to find them
36 'AnswerQ1.4' => '...', # In what year was your mother born?
37 'AnswerQ1.5' => '...', # In what year was your father born?
38 'AnswerQ1.8' => '...', # What is the name of your hometown newspaper?
39 },
40 pin => '########',
41 );
42  
43 my $parser = Finance::OFX::Parse::Simple->new;
44 my @txs = @{$parser->parse_scalar($ing->recent_transactions)};
45 my %accounts = $ing->accounts;
46  
47 for (@txs) {
48 print "Account: $_->{account_id}\n";
49 printf "%s %-50s %8.2f\n", $_->{date}, $_->{name}, $_->{amount} for @{$_->{transactions}};
50 print "\n";
51 }
52  
53 =head1 DESCRIPTION
54  
55 This module provides methods to access data from US INGdirect accounts,
56 including account balances and recent transactions in OFX format (see
57 Finance::OFX and related modules). It also provides a method to transfer
58 money from one account to another on a given date.
59  
60 =cut
61  
62 2 my $base = 'https://secure.ingdirect.com/myaccount';
63  
64 =pod
65  
66 =head1 METHODS
67  
68 =head2 new( saver_id => '...', customer => '...', questions => {...}, pin => '...' )
69  
70 Return an object that can be used to retrieve account balances and statements.
71 See SYNOPSIS for examples of challenge questions.
72  
73 =cut
74  
75 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($class, %opts) = @_;
77 0 my $self = bless \%opts, $class;
78  
79 0 ▶ $self->{ua} ||= LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new);
Condition0%0/3
$self->{"ua"} ||= "LWP::UserAgent"->new("cookie_jar", "HTTP::Cookies"->new)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ua"} ||= "LWP::UserAgent"->new("cookie_jar", "HTTP::Cookies"->new)
$self->{"ua"} "LWP::UserAgent"->new("cookie_jar", "HTTP::Cookies"->new)
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= "LWP::UserAgent"->new("cookie_jar", "HTTP::Cookies"->new)
ABresult
1X1
011
000
80  
81 0 _login($self);
82 0 $self;
83 }
84  
85 0 ▶sub _login {
Subroutine _login: not called CC=11 SCAR=48.8
86 0 my ($self) = @_;
87  
88 0 my $response = $self->{ua}->get("$base/INGDirect/login.vm");
89  
90 $response = $self->{ua}->post("$base/INGDirect/login.vm", [
91 publicUserId => $self->{saver_id},
92 0 ]);
93 0 ▶ $response->is_redirect && $response->header('location') =~ /security_questions.vm/
Branch: 1 branch
BranchTrueFalseTotal
$response->is_redirect and $response->header("location") =~ /security_questions.vm/ or croak("Initial login failed.") 0 0 0
Condition0%0/3
$response->is_redirect and $response->header("location") =~ /security_questions.vm/
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_redirect and $response->header("location") =~ /security_questions.vm/
$response->is_redirect $response->header("location") =~ /security_questions.vm/
Truth table0%0/3
A and B
A= $response->is_redirectB= $response->header("location") =~ /security_questions.vm/
ABresult
0X0
100
111
94 or croak "Initial login failed.";
95  
96 0 $response = $self->{ua}->get("$base/INGDirect/security_questions.vm");
97 0 ▶ $response->is_success or croak "Retrieving challenge questions failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("Retrieving challenge questions failed.") 0 0 0
98  
99 0 my @questions = map { s/^.*(AnswerQ.*)span".*$/$1/; $_ }
100 grep /AnswerQ/,
101 split('\n', $response->content);
102 0 ▶ croak "Didn't understand questions." if @questions != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @questions != 2 0 0 0
103  
104 $response = $self->{ua}->post("$base/INGDirect/security_questions.vm", [
105 TLSearchNum => $self->{customer},
106 'customerAuthenticationResponse.questionAnswer[0].answerText' => $self->{questions}{$questions[0]},
107 0 'customerAuthenticationResponse.questionAnswer[1].answerText' => $self->{questions}{$questions[1]},
108 '_customerAuthenticationResponse.device[0].bind' => 'false',
109 ]);
110 0 ▶ $response->is_redirect && $response->header('location') =~ /login_pinpad.vm/
Branch: 1 branch
BranchTrueFalseTotal
$response->is_redirect and $response->header("location") =~ /login_pinpad.vm/ or croak("Submitting challenge responses failed.") 0 0 0
Condition0%0/3
$response->is_redirect and $response->header("location") =~ /login_pinpad.vm/
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_redirect and $response->header("location") =~ /login_pinpad.vm/
$response->is_redirect $response->header("location") =~ /login_pinpad.vm/
Truth table0%0/3
A and B
A= $response->is_redirectB= $response->header("location") =~ /login_pinpad.vm/
ABresult
0X0
100
111
111 or croak "Submitting challenge responses failed.";
112  
113 0 $response = $self->{ua}->get("$base/INGDirect/login_pinpad.vm");
114 0 ▶ $response->is_success or croak "Loading PIN form failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("Loading PIN form failed.") 0 0 0
115  
116 0 my @keypad = map { s/^.*mouseUpKb\("([A-Z])".*$/$1/; $_ }
117 grep /pinKeyboard[A-Z]number/,
118 split('\n', $response->content);
119  
120 0 unshift(@keypad, pop @keypad);
121  
122 $response = $self->{ua}->post("$base/INGDirect/login_pinpad.vm", [
123 0 'customerAuthenticationResponse.PIN' => join '', map { $keypad[$_] } split//, $self->{pin},
124 0 ]);
125 0 ▶ $response->is_redirect && $response->header('location') =~ /postlogin/
Branch: 1 branch
BranchTrueFalseTotal
$response->is_redirect and $response->header("location") =~ /postlogin/ or croak("Submitting PIN failed.") 0 0 0
Condition0%0/3
$response->is_redirect and $response->header("location") =~ /postlogin/
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_redirect and $response->header("location") =~ /postlogin/
$response->is_redirect $response->header("location") =~ /postlogin/
Truth table0%0/3
A and B
A= $response->is_redirectB= $response->header("location") =~ /postlogin/
ABresult
0X0
100
111
126 or croak "Submitting PIN failed.";
127  
128 0 $response = $self->{ua}->get("$base/INGDirect/postlogin");
129 # XXX This is how it behaves in my browser, but not with
130 # LWP::UserAgent, so we can apparently just skip this step...
131 #$response->is_redirect && $response->header('location') =~ /account_summary.vm/
132 # or croak "Post login redirect failed.";
133  
134 #$response = $self->{ua}->get("$base/INGDirect/account_summary.vm");
135 # XXX ...and the postlogin screen has the account summary.
136 0 ▶ $response->is_success or croak "Account summary fetch failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("Account summary fetch failed.") 0 0 0
137 0 $self->{_account_screen} = $response->content;
138 }
139  
140 =pod
141  
142 =head2 accounts( )
143  
144 Retrieve a list of accounts:
145  
146 ( '####' => [ number => '####', type => 'Orange Savings', nickname => '...',
147 available => ###.##, balance => ###.## ],
148 ...
149 )
150  
151 =cut
152  
153 0 ▶sub accounts {
Subroutine accounts: not called CC=8 SCAR=42.8
Pod documented
154 0 my ($self) = @_;
155  
156 0 my $te = HTML::TableExtract->new(
157 attribs => { cellpadding => 0, cellspacing => 0 }
158 );
159 0 my $account_screen = $self->{_account_screen};
160 0 $account_screen =~ s/ / /g; #   makes TableExtract unhappy
161 0 $te->parse($account_screen);
162  
163 0 my %accounts;
164 0 my $seen_header = 0;
165  
166 0 ▶ $te->tables or croak "Can't extract accounts table.";
Branch: 1 branch
BranchTrueFalseTotal
$te->tables or croak("Can't extract accounts table.") 0 0 0
167  
168 0 foreach my $row (($te->tables)[0]->rows) {
169 0 ▶ if ($row->[0] =~ /Account Type/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[0] =~ /Account Type/) 0 0 0
170 0 $seen_header++;
171 0 next;
172 }
173 0 ▶ next unless $seen_header;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_header 0 0 0
174  
175 0 foreach (@$row) {
176 0 s/^\s*//; s/\s*$//; s/[\n\r]/ /g; s/\s+/ /g;
177 }
178  
179 0 my %account;
180 0 ($account{type}, $account{nickname}) = split / - /, shift @$row;
181 ($account{number},
182 $account{balance},
183 0 $account{available}) = map { s/^.+:\s+//; s/,//g; $_ ; } @$row;
184 0 ▶ next unless $account{type}; # don't include total row
Branch: 1 branch
BranchTrueFalseTotal
unless $account{"type"} 0 0 0
185 $accounts{$account{number}} = \%account
186 0 ▶ if $account{number};
Branch: 1 branch
BranchTrueFalseTotal
if $account{"number"} 0 0 0
187 }
188  
189 0 %accounts;
190 }
191  
192 =pod
193  
194 =head2 recent_transactions( $account, $days )
195  
196 Retrieve a list of transactions in OFX format for the given account
197 (default: all accounts) for the past number of days (default: 30).
198  
199 =cut
200  
201 0 ▶sub recent_transactions {
Subroutine recent_transactions: not called CC=4 SCAR=30.0
Pod documented
202 0 my ($self, $account, $days) = @_;
203  
204 0 ▶ $account ||= 'ALL';
Condition0%0/2
$account ||= "ALL"
l!l
00
MC/DC0%0/1
$account ||= "ALL"
$account
Truth table0%0/2
A
A= $account
Aresult
11
00
205 0 ▶ $days ||= 30;
Condition0%0/2
$days ||= 30
l!l
00
MC/DC0%0/1
$days ||= 30
$days
Truth table0%0/2
A
A= $days
Aresult
11
00
206  
207 0 my $response = $self->{ua}->post("$base/download.qfx", [
208 type => 'OFX',
209 TIMEFRAME => 'STANDARD',
210 account => $account,
211 FREQ => $days,
212 ]);
213 0 ▶ $response->is_success or croak "OFX download failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("OFX download failed.") 0 0 0
214  
215 0 $response->content;
216 }
217  
218 =pod
219  
220 =head2 transactions( $account, $from, $to )
221  
222 Retrieve a list of transactions in OFX format for the given account
223 (default: all accounts) in the given time frame (default: pretty far in the
224 past to pretty far in the future).
225  
226 =cut
227  
228 0 ▶sub transactions {
Subroutine transactions: not called CC=5 SCAR=34.0
Pod documented
229 0 my ($self, $account, $from, $to) = @_;
230  
231 0 ▶ $account ||= 'ALL';
Condition0%0/2
$account ||= "ALL"
l!l
00
MC/DC0%0/1
$account ||= "ALL"
$account
Truth table0%0/2
A
A= $account
Aresult
11
00
232 0 ▶ $from ||= '2000-01-01';
Condition0%0/2
$from ||= "2000-01-01"
l!l
00
MC/DC0%0/1
$from ||= "2000-01-01"
$from
Truth table0%0/2
A
A= $from
Aresult
11
00
233 0 ▶ $to ||= '2038-01-01';
Condition0%0/2
$to ||= "2038-01-01"
l!l
00
MC/DC0%0/1
$to ||= "2038-01-01"
$to
Truth table0%0/2
A
A= $to
Aresult
11
00
234  
235 0 my @from = strptime($from);
236 0 my @to = strptime($to);
237  
238 0 $from[4]++;
239 0 $to[4]++;
240 0 $from[5] += 1900;
241 0 $to[5] += 1900;
242  
243 0 my $response = $self->{ua}->post("$base/download.qfx", [
244 type => 'OFX',
245 TIMEFRAME => 'VARIABLE',
246 account => $account,
247 startDate => sprintf("%02d/%02d/%d", @from[4,3,5]),
248 endDate => sprintf("%02d/%02d/%d", @to[4,3,5]),
249 ]);
250 0 ▶ $response->is_success or croak "OFX download failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("OFX download failed.") 0 0 0
251  
252 0 $response->content;
253 }
254  
255 =pod
256  
257 =head2 transfer( $from, $to, $amount, $when )
258  
259 Transfer money from one account number to another on the given date
260 (default: immediately). Returns the confirmation number. Use at your
261 own risk.
262  
263 =cut
264  
265 0 ▶sub transfer {
Subroutine transfer: not called CC=7 SCAR=40.3
Pod documented
266 0 my ($self, $from, $to, $amount, $when) = @_;
267 0 ▶ my $type = $when ? 'SCHEDULED' : 'NOW';
Branch: 1 branch
BranchTrueFalseTotal
$when ? : 0 0 0
268  
269 0 ▶ if($when) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when) 0 0 0
270 0 my @when = strptime($when);
271 0 $when[4]++;
272 0 $when[5] += 1900;
273 0 $when = sprintf("%02d/%02d/%d", @when[4,3,5]);
274 }
275  
276 0 my $response = $self->{ua}->get("$base/INGDirect/money_transfer.vm");
277 0 my ($page_token) = map { s/^.*value="(.*?)".*$/$1/; $_ }
278 grep /<input.*name="pageToken"/,
279 split('\n', $response->content);
280  
281 0 ▶ $response = $self->{ua}->post("$base/INGDirect/deposit_transfer_input.vm", [
Branch: 1 branch
BranchTrueFalseTotal
$when ? : 0 0 0
282 pageToken => $page_token,
283 action => 'continue',
284 amount => $amount,
285 sourceAccountNumber => $from,
286 destinationAccountNumber => $to,
287 depositTransferType => $type,
288 $when ? (scheduleDate => $when) : (),
289 ]);
290 0 ▶ $response->is_redirect or croak "Transfer setup failed.";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_redirect or croak("Transfer setup failed.") 0 0 0
291  
292 0 $response = $self->{ua}->get("$base/INGDirect/deposit_transfer_validate.vm");
293 0 ($page_token) = map { s/^.*value="(.*?)".*$/$1/; $_ }
294 grep /<input.*name="pageToken"/,
295 split('\n', $response->content);
296  
297 0 $response = $self->{ua}->post("$base/INGDirect/deposit_transfer_validate.vm", [
298 pageToken => $page_token,
299 action => 'submit',
300 ]);
301 0 ▶ $response->is_redirect or croak "Transfer validation failed. Check your account!";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_redirect or croak("Transfer validation failed. Check your account!") 0 0 0
302  
303 0 $response = $self->{ua}->get("$base/INGDirect/deposit_transfer_confirmation.vm");
304 0 ▶ $response->is_success or croak "Transfer confirmation failed. Check your account!";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or croak("Transfer confirmation failed. Check your account!") 0 0 0
305 0 my ($confirmation) = map { s/^.*Number">(\d+)<.*$/$1/; $_ }
306 grep /<span.*id="confirmationNumber">/,
307 split('\n', $response->content);
308  
309 0 $confirmation;
310 }
311  
312 2 1;
313  
314 =pod
315  
316 =head1 AUTHOR
317  
318 This version by Steven N. Severinghaus <sns-perl@severinghaus.org>
319 with contributions by Robert Spier.
320  
321 =head1 COPYRIGHT
322  
323 Copyright (c) 2011 Steven N. Severinghaus. All rights reserved. This
324 program is free software; you can redistribute it and/or modify it under
325 the same terms as Perl itself.
326  
327 =head1 SEE ALSO
328  
329 Finance::Bank::INGDirect, Finance::OFX::Parse::Simple
330  
331 =cut
332  
↑ Summary