blib/lib/Finance/Bank/AU/StGeorge.pm

filter: Statementstmt 11.5% 18 / 156 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 13.1% 5 / 38 Podpod 0.0% 0 / 5 totaltotal 7.3% 23 / 315 CC 54 SCAR 44.5
CC
54
Cov
7%
_transfer
210.0
accounts
182.0
login
110.0
CRAP 85.8
↑ Summary
1 package Finance::Bank::AU::StGeorge;
2  
3 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::Request::Common qw/POST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw($VERSION $IBANK_URL $ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 $VERSION = '0.01';
11 1 $IBANK_URL = "https://ibank.stgeorge.com.au/scripts/ibank.dll?ibank";
12 1 $ERROR = '';
13  
14 sub new
15 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my ($class, %args) = @_;
17  
18 0 my $self = bless {
19 _ua => WWW::Mechanize->new(autocheck => 1),
20 card => "",
21 pin => "",
22 pass => "",
23 issue => 1,
24 }, $class;
25  
26 0 return $self->_set(%args);
27 }
28  
29 sub _set
30 0 ▶{
Subroutine _set: not called CC=3 SCAR=24.8
31 0 my ($self, %args) = @_;
32  
33 $self->{lc $_} = $args{$_}
34 0 ▶ foreach grep !/^_/ && exists $self->{lc $_}, keys %args;
Condition0%0/3
!/^_/ && exists $$self{lc $_}
!ll&&!rl&&r
000
MC/DC0%0/2
!/^_/ && exists $$self{lc $_}
!/^_/ exists $$self{lc $_}
Truth table0%0/3
not(A) && B
A= !/^_/B= exists $$self{lc $_}
ABresult
0X0
100
111
35  
36 0 return $self;
37 }
38  
39 sub logged_in
40 0 ▶{
Subroutine logged_in: not called CC=3 SCAR=24.8
Pod undocumented
41 0 my ($self) = @_;
42 0 ▶ return unless $self->{_params};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_params"} 0 0 0
43 0 ▶ return unless $self->{_params}{Session};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_params"}{"Session"} 0 0 0
44 }
45  
46 sub login
47 0 ▶{
Subroutine login: not called CC=10 SCAR=47.0
Pod undocumented
48 0 my ($self, %args) = @_;
49  
50 0 ▶ $self = $self->new(%args) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
51  
52 my $params = $self->{_params} ||= {
53 0 ▶ Route => "IBS",
Condition0%0/2
$self->{"_params"} ||= {"Route", "IBS", "Id", "JBANK.12.P", "origin", "ABA", "Session", ""}
l!l
00
MC/DC0%0/1
$self->{"_params"} ||= {"Route", "IBS", "Id", "JBANK.12.P", "origin", "ABA", "Session", ""}
$self->{"_params"}
Truth table0%0/2
A
A= $self->{"_params"}
Aresult
11
00
54 Id => "JBANK.12.P",
55 origin => "ABA",
56 Session => "",
57 };
58  
59 0 ▶ return $self if $params->{Session};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"Session"} 0 0 0
60  
61 0 my $ua = $self->{_ua};
62 0 $ua->env_proxy;
63 0 $ua->get("https://ibank.stgeorge.com.au/html/index.asp");
64  
65 0 my ($popup_url) = $ua->content =~ m|window\.open\(\"([^\"]+)\"|;
66  
67 0 $ua->get($popup_url);
68  
69 # one-time warnings
70 0 $ua->form(1);
71 0 $ua->submit();
72  
73 # login
74 0 my $form = $ua->form(1);
75 0 $form->value(Card => $self->{card});
76 0 $form->value(Pin => $self->{pin});
77 0 $form->value(LastName => $self->{pass});
78 0 $form->value(hWidth => 800);
79 0 $form->value(hHeight => 600);
80 0 $form->value(Issue => $self->{issue});
81 0 $ua->click();
82  
83 # scrape out session id's and stuff
84 0 for ($ua->content)
85 {
86 0 ▶ ($params->{Route}) = $1 if /route=([^\&]+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if /route=([^\&]+)/m 0 0 0
87 0 ▶ ($params->{Id}) = $1 if /clid=([^\&]+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if /clid=([^\&]+)/m 0 0 0
88 0 ▶ ($params->{origin}) = $1 if /origin=([^\&]+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if /origin=([^\&]+)/m 0 0 0
89 0 ▶ ($params->{Session}) = $1 if /Session=([a-f0-9]{32})/m;
Branch: 1 branch
BranchTrueFalseTotal
if /Session=([a-f0-9]{32})/m 0 0 0
90 }
91  
92 0 ▶ return $self if $params->{Session};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"Session"} 0 0 0
93 0 return;
94 }
95  
96 sub logout
97 0 ▶{
Subroutine logout: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($self) = @_;
99  
100 0 ▶ $self->logged_in or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->logged_in or return 0 0 0
101  
102 0 my $ua = $self->{_ua};
103  
104 $ua->request(POST $IBANK_URL, [
105 route => "IBS",
106 0 params => _format_params(%{ $self->{_params} }, Tran => "Logout"),
107 ]);
108  
109 0 return 1;
110 }
111  
112 sub accounts
113 0 ▶{
Subroutine accounts: not called CC=13 SCAR=52.0
Pod undocumented
114 0 my ($self, %args) = @_;
115  
116 0 ▶ $self = $self->new(%args) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
117 0 ▶ $self->login or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->login or return 0 0 0
118  
119 0 my $ua = $self->{_ua};
120  
121 0 my $accounts = $self->{_accounts};
122  
123 0 ▶ if (not $accounts or $args{reload})
Branch: 1 branch
BranchTrueFalseTotal
if (not $accounts or $args{"reload"}) 0 0 0
Condition0%0/3
not $accounts or $args{"reload"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $accounts or $args{"reload"}
not $accounts $args{"reload"}
Truth table0%0/3
not(A) or B
A= not $accountsB= $args{"reload"}
ABresult
1X1
011
000
124 {
125 $ua->request(POST $IBANK_URL, [
126 route => "IBS",
127 0 params => _format_params(%{ $self->{_params} }, Tran => "BrowseAccounts"),
128 ]);
129  
130 0 $accounts = $self->{_accounts} = [ _parse_params($ua->content) ];
131 }
132  
133 0 my @ret;
134  
135 0 foreach (@$accounts)
136 {
137 0 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 0 0
138 0 ▶ next unless ($args{type} ||= "ACC") eq "ALL" or $args{type} eq $_->{Type};
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"type"} ||= "ACC") eq "ALL" or $args{"type"} eq $_->{"Type"} 0 0 0
Condition0%0/5
($args{"type"} ||= "ACC") eq "ALL" or $args{"type"} eq $_->{"Type"}
l!l&&r!l&&!r
000
$args{"type"} ||= "ACC"
l!l
00
MC/DC0%0/2
($args{"type"} ||= "ACC") eq "ALL" or $args{"type"} eq $_->{"Type"}
($args{"type"} ||= "ACC") eq "ALL" $args{"type"} eq $_->{"Type"}
MC/DC0%0/1
$args{"type"} ||= "ACC"
$args{"type"}
Truth table0%0/3
A or B
A= ($args{"type"} ||= "ACC") eq "ALL"B= $args{"type"} eq $_->{"Type"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
139  
140 0 $_->{_parent} = $self;
141  
142 0 ▶ if ($_->{Type} eq "ACC")
Branch: 2 branches
BranchTrueFalseTotal
if ($_->{'Type'} eq 'ACC') { } 0 0 0
elsif ($_->{'Type'} eq 'ThirdParty') { } 0 0 0
143 {
144 0 push @ret, bless $_, "Finance::Bank::AU::StGeorge::Account";
145 }
146 elsif ($_->{Type} eq "ThirdParty")
147 {
148 0 push @ret, bless $_, "Finance::Bank::AU::StGeorge::ForeignAccount";
149 }
150 }
151  
152 0 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
153 }
154  
155 sub _account_detail
156 0 ▶{
Subroutine _account_detail: not called CC=1 SCAR=6.9
157 0 my ($self, $acc, %args) = @_;
158  
159 0 my $ua = $self->{_ua};
160  
161 $ua->request(POST $IBANK_URL, [
162 route => "IBS",
163 0 params => _format_params(%{ $self->{_params} },
164 Tran => "BrowseDetail",
165 Type => "ACC", # ALL w/o Account and AccountCode
166 Account => $acc->number,
167 AccountCode => $acc->code,
168 RequestFlag => "BCT",
169 ),
170 ]);
171  
172 0 return _parse_params($ua->content);
173 }
174  
175 sub _account_history_csv
176 0 ▶{
Subroutine _account_history_csv: not called CC=5 SCAR=34.0
177 0 my ($self, $acc, %args) = @_;
178  
179 0 my $ua = $self->{_ua};
180  
181 $ua->request(POST $IBANK_URL, [
182 route => "IBS",
183 0 ▶ params => _format_params(%{ $self->{_params} },
Branch: 2 branches
BranchTrueFalseTotal
$args{'start'} ? : 0 0 0
$args{'end'} ? : 0 0 0
184 Tran => "ExportAccountHistory",
185 Type => "ACC",
186 Account => $acc->number,
187 AccountCode => $acc->code,
188 Format => "CSV",
189 $args{start} ? (FromDate => $args{start}) : (), # 20050123
190 0 $args{end} ? (ToDate => $args{end}) : (), # 20050123
191 DateFormat => "%d/%m/%Y",
192 ),
193 ]);
194  
195 0 my @ret;
196 my @fields;
197  
198 0 foreach (split /\r?\n/, (_parse_params($ua->content))[0])
199 {
200 0 ▶ if (@fields)
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
201 {
202 0 my %ret;
203 0 @ret{ @fields } = split /,/, $_;
204 0 push @ret, bless \%ret, "Finance::Bank::AU::StGeorge::History";
205 }
206 else
207 {
208 0 @fields = split /,/, $_;
209 }
210 }
211  
212 0 return @ret;
213 }
214  
215 sub _transfer
216 0 ▶{
Subroutine _transfer: not called CC=14 SCAR=53.5
217 0 my ($self, $from, $to, %args) = @_;
218  
219 0 ▶ unless ($from->type eq "ACC")
Branch: 1 branch
BranchTrueFalseTotal
unless ($from->type eq "ACC") 0 0 0
220 {
221 0 die "Can only tranfer from a local account: ".$from->type."\n";
222 }
223  
224 0 ▶ unless ($args{amount} =~ /^\d+\.\d\d$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"amount"} =~ /^\d+\.\d\d$/) 0 0 0
225 {
226 0 die "You must specify a valid amount to transfer\n";
227 }
228  
229 0 ▶ unless ($to->type eq "ACC" or $args{payer})
Branch: 1 branch
BranchTrueFalseTotal
unless ($to->type eq "ACC" or $args{"payer"}) 0 0 0
Condition0%0/3
$to->type eq "ACC" or $args{"payer"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$to->type eq "ACC" or $args{"payer"}
$to->type eq "ACC" $args{"payer"}
Truth table0%0/3
A or B
A= $to->type eq "ACC"B= $args{"payer"}
ABresult
1X1
011
000
230 {
231 0 die "You must specify a payer name to third party transfers\n";
232 }
233  
234 0 my $ua = $self->{_ua};
235  
236 $ua->request(POST $IBANK_URL, [
237 route => "IBS",
238 0 ▶ params => _format_params(%{ $self->{_params} },
Branch: 2 branches
BranchTrueFalseTotal
$to->type eq 'ACC' ? : 0 0 0
$to->type ne 'ACC' ? : 0 0 0
Condition0%0/2
$args{'reference'} || 'Funding Terrorism'
l!l
00
MC/DC0%0/1
$args{'reference'} || 'Funding Terrorism'
$args{'reference'}
Truth table0%0/2
A
A= $args{'reference'}
Aresult
11
00
239 Tran => "Payment",
240 Mode => "C",
241 Frequency => "now",
242 NotifyByEmail => "false",
243 Type => $from->type,
244 Account => $from->account,
245 AccountCode => $from->code,
246 )._format_params(
247 ToType => $to->type,
248 $to->type eq "ACC" ? (
249 ToAccount => $to->account,
250 ToAccountCode => $to->code
251 ) : (
252 ToAccount => $to->account,
253 ),
254 Amount => $args{amount},
255 $to->type ne "ACC" ? (
256 Payer => $args{payer},
257 ) : (),
258 0 Reference => $args{reference} || "Funding Terrorism",
259 ),
260 ]);
261  
262 0 my @ret = _parse_params($ua->content);
263  
264 0 ▶ if (@ret == 1 and $ret[0]->{Receipt})
Branch: 1 branch
BranchTrueFalseTotal
if (@ret == 1 and $ret[0]{"Receipt"}) 0 0 0
Condition0%0/3
@ret == 1 and $ret[0]{"Receipt"}
!ll&&!rl&&r
000
MC/DC0%0/2
@ret == 1 and $ret[0]{"Receipt"}
@ret == 1 $ret[0]{"Receipt"}
Truth table0%0/3
A and B
A= @ret == 1B= $ret[0]{"Receipt"}
ABresult
0X0
100
111
265 {
266 # adjust balances on local accounts if available
267 $from->{AvailBalance} = $ret[0]->{FromAvailBalance}
268 0 ▶ if length $ret[0]->{FromAvailBalance};
Branch: 1 branch
BranchTrueFalseTotal
if length $ret[0]{"FromAvailBalance"} 0 0 0
269 $from->{Balance} = $ret[0]->{FromBalance}
270 0 ▶ if length $ret[0]->{FromBalance};
Branch: 1 branch
BranchTrueFalseTotal
if length $ret[0]{"FromBalance"} 0 0 0
271 $to->{AvailBalance} = $ret[0]->{ToAvailBalance}
272 0 ▶ if length $ret[0]->{ToAvailBalance};
Branch: 1 branch
BranchTrueFalseTotal
if length $ret[0]{"ToAvailBalance"} 0 0 0
273 $to->{Balance} = $ret[0]->{ToBalance}
274 0 ▶ if length $ret[0]->{ToBalance};
Branch: 1 branch
BranchTrueFalseTotal
if length $ret[0]{"ToBalance"} 0 0 0
275  
276 0 return bless $ret[0], "Finance::Bank::AU::StGeorge::Receipt";
277 }
278  
279 0 return;
280 }
281  
282 # sub _account_history
283 # {
284 # my ($self, $acc, %args) = @_;
285 #
286 # my $ua = $self->{_ua};
287 #
288 # $ua->request(POST $IBANK_URL, [
289 # route => "IBS",
290 # params => _format_params(
291 # %{ $self->{_params} },
292 # Tran => "BrowseDetail",
293 # Type => "ACC",
294 # Account => $acc->number,
295 # AccountCode => $acc->code,
296 # RequestFlag => "H",
297 # $args{start} ? (FromDate => $args{start}) : (), # 20050123 / -30
298 # $args{end} ? (ToDate => $args{end}) : (), # 20050123
299 # ),
300 # ]);
301 #
302 # return _parse_params($ua->content);
303 # }
304  
305 sub DESTROY
306 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
307 0 shift->logout;
308 }
309  
310 sub _format_params
311 0 ▶{
Subroutine _format_params: not called CC=2 SCAR=17.9
312 0 my $ret;
313  
314 0 ▶ while (my ($k, $v) = splice(@_, 0, 2))
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
315 {
316 0 $ret .= join(chr(0x1c), $k, $v).chr(0x1d);
317 }
318  
319 0 $ret .= chr(0x1e);
320  
321 0 return $ret;
322 }
323  
324 sub _parse_params
325 0 ▶{
Subroutine _parse_params: not called CC=10 SCAR=47.0
326 0 my @ret;
327  
328 0 foreach my $r (split /\x1e/, $_[0])
329 {
330 0 my $p = {};
331  
332 0 foreach my $g (split /\x1d/, $r)
333 {
334 0 my ($k, @v) = split /\x1c/, $g, -1;
335  
336 0 ▶ if (@v > 1 or exists $p->{$k})
Branch: 2 branches
BranchTrueFalseTotal
if (@v > 1 or exists $p->{$k}) { } 0 0 0
elsif (@v) { } 0 0 0
Condition0%0/3
@v > 1 or exists $p->{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
@v > 1 or exists $p->{$k}
@v > 1 exists $p->{$k}
Truth table0%0/3
A or B
A= @v > 1B= exists $p->{$k}
ABresult
1X1
011
000
337 {
338 unshift @v, delete $p->{$k}
339 0 ▶ if exists $p->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$k} 0 0 0
340  
341 0 push @{ $p->{$k} }, @v;
342 }
343 elsif (@v)
344 {
345 0 $p->{$k} = $v[0];
346 }
347 else
348 {
349 0 $p = $k;
350 }
351 }
352  
353 0 push @ret, $p;
354 }
355  
356 0 ▶ unless (shift(@ret) =~ /^OK\w+$/)
Branch: 1 branch
BranchTrueFalseTotal
unless (shift(@ret) =~ /^OK\w+$/) 0 0 0
357 {
358 0 ▶ $ERROR = $ret[0]->{Message} if @ret;
Branch: 1 branch
BranchTrueFalseTotal
if @ret 0 0 0
359 0 ▶ $ERROR ||= "Non-OK Response";
Condition0%0/2
$Finance::Bank::AU::StGeorge::ERROR ||= "Non-OK Response"
l!l
00
MC/DC0%0/1
$Finance::Bank::AU::StGeorge::ERROR ||= "Non-OK Response"
$Finance::Bank::AU::StGeorge::ERROR
Truth table0%0/2
A
A= $Finance::Bank::AU::StGeorge::ERROR
Aresult
11
00
360 0 return;
361 }
362  
363 0 return @ret;
364 }
365  
366 package Finance::Bank::AU::StGeorge::Account;
367  
368 # 'Icon' => 'savings.gif',
369 # 'Flags' => 'WDHBIER',
370 # 'Account' => '0000000000000',
371 # 'AccountCode' => 'SAV',
372 # 'AccountTitle' => '',
373 # 'DEUser' => '',
374 # 'Number' => '0000000000000', # same as Account
375 # 'SubProdCode' => '0000',
376 # 'Type' => 'ACC',
377 # 'IsSegmented' => 'false',
378 # 'TypeName' => 'Savings',
379 # 'Balance' => '0.00',
380 # 'Bsb' => '',
381 # 'Name' => '',
382 # 'AvailBalance' => '0.00'
383  
384 0 ▶sub type { $_[0]->{Type} }
Subroutine type: not called CC=1 SCAR=6.9
385 0 ▶sub code { $_[0]->{AccountCode} }
Subroutine code: not called CC=1 SCAR=6.9
386 0 ▶sub number { $_[0]->{Account} }
Subroutine number: not called CC=1 SCAR=6.9
387 0 ▶sub account { $_[0]->number }
Subroutine account: not called CC=1 SCAR=6.9
388 0 ▶sub name { $_[0]->{TypeName} }
Subroutine name: not called CC=1 SCAR=6.9
389 0 ▶sub balance { $_[0]->{Balance} }
Subroutine balance: not called CC=1 SCAR=6.9
390 0 ▶sub available { $_[0]->{AvailBalance} }
Subroutine available: not called CC=1 SCAR=6.9
391  
392 0 ▶sub detail { ($_[0]->{_parent}->_account_detail(@_))[0] }
Subroutine detail: not called CC=1 SCAR=6.9
393 0 ▶sub history { $_[0]->{_parent}->_account_history_csv(@_) }
Subroutine history: not called CC=1 SCAR=6.9
394 0 ▶sub transfer { $_[0]->{_parent}->_transfer(@_) }
Subroutine transfer: not called CC=1 SCAR=6.9
395  
396 package Finance::Bank::AU::StGeorge::ForeignAccount;
397  
398 # 'InternetTP' => 'true',
399 # 'Icon' => 'ithirdparty.gif',
400 # 'Payee' => '000000-000000000', # Bsb-Account
401 # 'Account' => '000000000',
402 # 'Number' => '000000-000000000', # Bsb-Account
403 # 'Type' => 'ThirdParty',
404 # 'Bsb' => '000000',
405 # 'Name' => ''
406  
407 0 ▶sub type { $_[0]->{Type} }
Subroutine type: not called CC=1 SCAR=6.9
408 0 ▶sub bsb { $_[0]->{Bsb} }
Subroutine bsb: not called CC=1 SCAR=6.9
409 0 ▶sub number { $_[0]->{Account} }
Subroutine number: not called CC=1 SCAR=6.9
410 0 ▶sub account { join("-", $_[0]->bsb, $_[0]->number) }
Subroutine account: not called CC=1 SCAR=6.9
411 0 ▶sub name { $_[0]->{Name} }
Subroutine name: not called CC=1 SCAR=6.9
412  
413 package Finance::Bank::AU::StGeorge::History;
414  
415 # 'Debit' => '0.00', # empty string if credit
416 # 'Balance' => '0.00',
417 # 'Credit' => '0.00', # empty string if debit
418 # 'Description' => '',
419 # 'Date' => '24/01/2005'
420  
421 0 ▶sub date { $_[0]->{Date} }
Subroutine date: not called CC=1 SCAR=6.9
422 0 ▶sub debit { $_[0]->{Debit} }
Subroutine debit: not called CC=1 SCAR=6.9
423 0 ▶sub credit { $_[0]->{Credit} }
Subroutine credit: not called CC=1 SCAR=6.9
424 0 ▶sub balance { $_[0]->{Balance} }
Subroutine balance: not called CC=1 SCAR=6.9
425 0 ▶sub description { $_[0]->{Description} }
Subroutine description: not called CC=1 SCAR=6.9
426  
427 package Finance::Bank::AU::StGeorge::Receipt;
428  
429 # 'Receipt' => '', # the big text field
430 # 'Message' => '',
431 # 'ToAvailBalance' => '0.00',
432 # 'ToBalance' => '0.00',
433 # 'FromAvailBalance' => '0.00',
434 # 'FromBalance' => '0.00',
435  
436 0 ▶sub receipt { $_[0]->{Receipt} }
Subroutine receipt: not called CC=1 SCAR=6.9
437  
438 1 1;
439 __END__
↑ Summary