Finance-Bank-LloydsTSB

 view release on metacpan or  search on metacpan

lib/Finance/Bank/LloydsTSB.pm  view on Meta::CPAN

package Finance::Bank::LloydsTSB;

=head1 NAME

Finance::Bank::LloydsTSB - Check your bank accounts from Perl

=head1 SYNOPSIS

  use Finance::Bank::LloydsTSB;
  my @accounts = Finance::Bank::LloydsTSB->check_balance(
        username  => $username,
        password  => $password
        memorable => $memorable_phrase
  );

  my $total = 0;
  my $format = "%20s : %21s : GBP %9.2f\n";
  for my $acc (@accounts) {
    $total += $acc->balance;
    printf $format, $acc->name, $acc->descr_num, $acc->balance;
  }
  print "-" x 70, "\n";
  printf $format, 'TOTAL', '', $total;

  my $statement = $accounts[0]->fetch_statement;

  # Retrieve QIF for all transactions in January 2008.
  my $qif = $accounts[1]->download_statement(2008, 01, 01, 5);

See F<fetch-statement.pl> for a working example.

=head1 DESCRIPTION

This module provides a rudimentary interface to the LloydsTSB online
banking system at C<https://online.lloydstsb.co.uk/>. You will need
either C<Crypt::SSLeay> or C<IO::Socket::SSL> installed for HTTPS
support to work with LWP.

=cut

use strict;
use warnings;

use Carp;

our $VERSION = '1.35';
our $DEBUG = 0;

use Carp qw(carp cluck croak confess);
use HTML::TableExtract qw(tree);
use WWW::Mechanize;

use Finance::Bank::LloydsTSB::utils qw(debug trim);
use Finance::Bank::LloydsTSB::Account;

our $ua = WWW::Mechanize->new(
    env_proxy  => 1, 
    keep_alive => 1, 
    timeout    => 30,
    autocheck  => 1,
); 

our $logged_in = 0;

=head1 CLASS METHODS

=cut

sub _login {
    my $self = shift;

    $ua->get("https://online.lloydstsb.co.uk/customer.ibc");
    my $form = $ua->current_form;
    die "Couldn't get current_form" unless $form && $form->isa("HTML::Form");
    my $field = $form->find_input("UserId1");
    die "Couldn't find UserId1 input field" unless $field;
    $ua->field(UserId1  => $self->{username});
    $ua->field(Password => $self->{password});
    $ua->click;

    croak "Couldn't log in; check your password and username\n" . $ua->content
      unless $ua->content =~ /memorable\s+information/i;

    # Now we're at the new "memorable information" page, so parse that
    # and input the right form data.

    for my $i (0..2) {
        my $key;
        eval { $key = $ua->current_form->find_input("ResponseKey$i")->value; };
        die "Couldn't find ResponseKey$i on memorable info page; has the login process changed?" if $@;
        my $value = substr(lc $self->{memorable}, $key-1, 1);
        $ua->field("ResponseValue$i" => $value);
    }

    $ua->click;
    $logged_in = 1;



( run in 2.871 seconds using v1.01-cache-2.11-cpan-364913b4093 )