File Coverage

lib/PagSeguro/API.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package PagSeguro::API;
2             # ABSTRACT: PagSeguro::API - UOL PagSeguro Payment Gateway API Module
3 6     6   88134 use strict;
  6         10  
  6         194  
4 6     6   24 use warnings;
  6         8  
  6         211  
5              
6             our $VERSION = '0.006';
7              
8 6     6   2025 use PagSeguro::API::Checkout;
  0            
  0            
9             use PagSeguro::API::Transaction;
10             use PagSeguro::API::Notification;
11              
12             # constructor
13             sub new {
14             my $class = shift;
15             my $args = (@_ % 2 == 0)? {@_} : shift;
16              
17             # check env vars
18             $args->{email} = $ENV{PAGSEGURO_API_EMAIL} || undef
19             unless $args->{email};
20              
21             $args->{token} = $ENV{PAGSEGURO_API_TOKEN} || undef
22             unless $args->{token};
23              
24             # enable sandbox
25             $ENV{PAGSEGURO_API_SANDBOX} = $args->{sandbox}
26             if $args->{sandbox};
27              
28             # enable debug
29             $ENV{PAGSEGURO_API_DEBUG} = $args->{debug}
30             if $args->{debug};
31              
32             return bless {
33             _email => $args->{email} || undef,
34             _token => $args->{token} || undef,
35              
36             # load resources
37             _resources => PagSeguro::API::Resource->new
38             }, $class;
39             }
40              
41             # accessors
42             sub email {
43             $_[0]->{_email} = $_[1] if $_[1];
44             return shift->{_email};
45             }
46              
47             sub token {
48             $_[0]->{_token} = $_[1] if $_[1];
49             return shift->{_token};
50             }
51              
52             sub debug {
53             $_[0]->{_debug} = $_[1] if $_[1];
54             return shift->{_debug};
55             }
56              
57             sub sandbox {
58             $_[0]->{_sandbox} = $_[1] if $_[1];
59             return shift->{_sandbox};
60             }
61             sub resource {
62             return $_[0]->{_resources}->get($_[1]) if $_[1];
63             return $_[0]->{_resources};
64             }
65              
66             # methods
67             sub transaction {
68             my $self = shift;
69              
70             # setting auth
71             $self->_set_auth(@_) if @_;
72              
73             return PagSeguro::API::Transaction->new( context => $self );
74             }
75              
76             sub notification {
77             my $self = shift;
78              
79             # setting auth
80             $self->_set_auth(@_) if @_;
81              
82             return PagSeguro::API::Notification->new( context => $self );
83             }
84              
85             sub checkout {
86             my $self = shift;
87              
88             # setting auth
89             $self->_set_auth(@_) if @_;
90              
91             return PagSeguro::API::Checkout->new( context => $self );
92             }
93              
94             sub _set_auth {
95             my $self = shift;
96             my $args = (@_ % 2 == 0)? {@_}: shift;
97              
98             $self->email($args->{email}) if $args->{email};
99             $self->email($args->{token}) if $args->{token};
100             }
101              
102             1;
103             __END__