Net-Xero

 view release on metacpan or  search on metacpan

lib/Net/Xero.pm  view on Meta::CPAN

has 'request_token'  => (is => 'rw');
has 'request_secret' => (is => 'rw');
has 'access_token'   => (is => 'rw');
has 'access_secret'  => (is => 'rw');

has 'template_path'  => (
    is      => 'rw',
    default => ( dist_dir('Net-Xero') ),
);

#has 'template_path' => (is => 'rw', isa => 'Str');

=head1 SYNOPSIS

Quick summary of what the module does.

For a private application you will receive the access_token/secret when you
submit your X509 to xero. You can ignore login/auth in this instance as follows:
use Net::Xero;

my $foo = Net::Xero->new(
  access_token => 'YY',
  access_secret => 'XX',
);

=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 FUNCTIONS

=cut

=head2 login

This sets up the initial OAuth handshake and returns the login URL. This
URL has to be clicked by the user and the the user then has to accept
the application in xero.

Xero then redirects back to the callback URL defined with
C<$self-E<gt>callback_url>. If the user already accepted the application the
redirect may happen without the user actually clicking anywhere.

=cut

sub login {
    my $self = shift;

    my $request = Net::OAuth->request("request token")->new(
        consumer_key     => $self->key,
        consumer_secret  => $self->secret,
        request_url      => $self->api_url . '/oauth/RequestToken',
        request_method   => 'POST',
        signature_method => 'RSA-SHA1',
        timestamp        => time,
        nonce            => $self->nonce,
        callback         => $self->callback_url,
    );

    my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
    $request->sign($private_key);
    my $res = $self->ua->request(GET $request->to_url);

    if ($res->is_success) {
        my $response =
            Net::OAuth->response('request token')
            ->from_post_body($res->content);
        $self->request_token($response->token);
        $self->request_secret($response->token_secret);
        print STDERR "Got Request Token ", $response->token, "\n"
            if $self->debug;
        print STDERR "Got Request Token Secret ", $response->token_secret, "\n"
            if $self->debug;
        return
              $self->api_url
            . '/oauth/Authorize?oauth_token='
            . $response->token
            . '&oauth_callback='
            . $self->callback_url;
    }
    else {
        $self->error($res->status_line);
        warn "Something went wrong: " . $res->status_line;
    }
}

=head2 auth

The auth method changes the initial request token into access token that we need
for subsequent access to the API. This method only has to be called once
after login.

=cut

sub auth {
    my $self = shift;

    my $request = Net::OAuth->request("access token")->new(
        consumer_key     => $self->key,
        consumer_secret  => $self->secret,
        request_url      => $self->api_url . '/oauth/AccessToken',
        request_method   => 'POST',
        signature_method => 'RSA-SHA1',
        timestamp        => time,
        nonce            => $self->nonce,
        callback         => $self->callback_url,
        token            => $self->request_token,
        token_secret     => $self->request_secret,
    );
    my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
    $request->sign($private_key);
    my $res = $self->ua->request(GET $request->to_url);

    if ($res->is_success) {
        my $response =
            Net::OAuth->response('access token')->from_post_body($res->content);
        $self->access_token($response->token);
        $self->access_secret($response->token_secret);
        print STDERR "Got Access Token ", $response->token, "\n"
            if $self->debug;
        print STDERR "Got Access Token Secret ", $response->token_secret, "\n"
            if $self->debug;
    }
    else {
        $self->error($res->status_line);
        $self->error($res->status_line . "\n" . $res->content);
    }
}

=head2 set_cert

=cut

sub set_cert {
  my ($self, $path) = @_;
  my $cert = io $path;
  $self->cert($cert->all);
}

=head2 get_inv_by_ref

=cut

sub get_inv_by_ref {
    my ($self, @ref) = @_;

    my $path = 'Invoices?where=Reference.ToString()=="' . (shift @ref) . '"';
    $path .= ' OR Reference.ToString()=="' . $_ . '"' foreach (@ref);

    return $self->_talk($path, 'GET');
}

=head2 get_invoices

=cut

sub get_invoices {
    my ($self, $where) = @_;

    my $path = 'Invoices';

    return $self->_talk($path, 'GET') unless (ref $where eq 'HASH');

    $path .= '?where=';
    my $conjunction =
        (exists $where->{'conjunction'}) ? uc $where->{'conjunction'} : 'OR';
    my $first = 1;

    foreach my $key (%{$where}) {
        $path .= " $conjunction " unless $first;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.671 second using v1.00-cache-2.02-grep-82fe00e-cpan-2cc899e4a130 )