| 1 | # Filename: OFX.pm | ||||||||||||||||||||
| 2 | # Serialize, Parse and Query using the Open Financial Exchange format | ||||||||||||||||||||
| 3 | # http://www.ofx.net/ | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # Created January 30, 2008 Brandon Fosdick <bfoz@bfoz.net> | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # Copyright 2008 Brandon Fosdick <bfoz@bfoz.net> (BSD License) | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # $Id: OFX.pm,v 1.2 2008/03/04 04:22:24 bfoz Exp $ | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | package Finance::OFX; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | our $VERSION = '2'; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | ▶ | use Finance::OFX::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | ▶ | use HTTP::Date; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub new | ||||||||||||||||||||
| 23 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 24 | 0 | my ($this, %options) = @_; | |||||||||||||||||||
| 25 | 0 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition0%0/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 26 | 0 | my $self = {}; | |||||||||||||||||||
| 27 | 0 | bless $self, $class; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Assume any unprocessed options are meant for the UserAgent, | ||||||||||||||||||||
| 30 | # which implies that this object will be used to generate and process | ||||||||||||||||||||
| 31 | # requests instead of processing stored files | ||||||||||||||||||||
| 32 | 0 | ▶ | $self->{ua} = Finance::OFX::UserAgent->new(%options) if scalar keys %options; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | return $self; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # --- Getters and Setters --- | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub institution | ||||||||||||||||||||
| 40 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
institution: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 41 | 0 | my $s = shift; | |||||||||||||||||||
| 42 | 0 | $s->{ua}->institution(@_); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub response | ||||||||||||||||||||
| 46 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | my $s = shift; | |||||||||||||||||||
| 48 | 0 | $s->{ua}->{response}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | sub user_id | ||||||||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
user_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 53 | 0 | my $s = shift; | |||||||||||||||||||
| 54 | 0 | $s->{ua}->user_id(@_); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub user_pass | ||||||||||||||||||||
| 58 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
user_pass: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 59 | 0 | my $s = shift; | |||||||||||||||||||
| 60 | 0 | $s->{ua}->user_pass(@_); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # --- Public Methods --- | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | sub accounts | ||||||||||||||||||||
| 66 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
accounts: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 67 | 0 | my $s = shift; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | my $r = $s->{ua}->account_info; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | return -1 unless $r->is_success; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | ▶ | return -2 if $r->signon_status_code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | ▶ | return undef unless $r->ofx->{signupmsgsrsv1}{acctinfotrnrs}{acctinfors}{acctinfo}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | my @accounts; | |||||||||||||||||||
| 76 | 0 | for( @{$r->ofx->{signupmsgsrsv1}{acctinfotrnrs}{acctinfors}{acctinfo}} ) | |||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | 0 | my %b = flatten($_); | |||||||||||||||||||
| 79 | 0 | push @accounts, \%b; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 0 | return @accounts; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub balance | ||||||||||||||||||||
| 85 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
balance: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 86 | 0 | my ($s, $acct) = @_; | |||||||||||||||||||
| 87 | 0 | ▶ | return undef unless ref($acct) eq 'Finance::OFX::Account'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # Use the FID from the given FI if none is set in the given Account | ||||||||||||||||||||
| 90 | 0 | ▶ | $acct->fid($s->institution()->fid()) unless $acct->fid(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | my $r = $s->{ua}->statement($acct); | |||||||||||||||||||
| 93 | 0 | ▶ | return -1 unless $r->is_success; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | ▶ | return -2 if $r->signon_status_code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | ▶ | return undef unless $r->ofx->{bankmsgsrsv1}{stmttrnrs}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $transaction = $r->ofx->{bankmsgsrsv1}{stmttrnrs}; | |||||||||||||||||||
| 98 | 0 | ▶ | return "Statement request error: ".($transaction->{status}{message}) if transactionStatusCode($transaction); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | ▶ | return "No statement info returned" unless exists $transaction->{stmtrs}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | return $transaction->{stmtrs}; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub transactions | ||||||||||||||||||||
| 105 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
transactions: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 106 | 0 | my ($s, $acct, $start, $end) = @_; | |||||||||||||||||||
| 107 | 0 | ▶ | return undef unless ref($acct) eq 'Finance::OFX::Account'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # Use the FID from the given FI if none is set in the given Account | ||||||||||||||||||||
| 110 | 0 | ▶ | $acct->fid($s->institution()->fid()) unless $acct->fid(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | my %options; | |||||||||||||||||||
| 113 | 0 | $options{transactions} = 'Y'; | |||||||||||||||||||
| 114 | 0 | ▶ | $options{start} = $start if $start; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | ▶ | $options{end} = $end if $end; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | my $r = $s->{ua}->statement($acct, %options); | |||||||||||||||||||
| 118 | 0 | ▶ | return -1 unless $r->is_success; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 0 | ▶ | return -2 if $r->signon_status_code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | my $ofx = $r->ofx; | |||||||||||||||||||
| 121 | 0 | ▶ | return undef unless $ofx->{bankmsgsrsv1}{stmttrnrs}{stmtrs}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | my $transaction = $ofx->{bankmsgsrsv1}{stmttrnrs}; | |||||||||||||||||||
| 123 | 0 | ▶ | return "Statement request error: ".($transaction->{status}{message}) if transactionStatusCode($transaction); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | return $transaction->{stmtrs}{banktranlist}; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | sub transactionStatusCode | ||||||||||||||||||||
| 129 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
transactionStatusCode: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 130 | 0 | my $tree = shift; | |||||||||||||||||||
| 131 | 0 | ▶ | return undef unless ref($tree) eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | return $tree->{status}{code}; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # --- Internal use only --- | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # Blindly flatten a HoH | ||||||||||||||||||||
| 138 | sub flatten | ||||||||||||||||||||
| 139 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
flatten: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 140 | 0 | my $tree = shift; | |||||||||||||||||||
| 141 | 0 | ▶ | return $tree unless ref($tree) eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | my %a; | |||||||||||||||||||
| 143 | 0 | for( keys %{$tree} ) | |||||||||||||||||||
| 144 | { | ||||||||||||||||||||
| 145 | 0 | ▶ | $a{$_} = $tree->{$_}, next unless ref($tree->{$_}) eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | my %b = flatten($tree->{$_}); | |||||||||||||||||||
| 147 | 0 | @a{keys %b} = values %b; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | 0 | return %a; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 2 | 1; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||