File Coverage

blib/lib/Business/PayPal/NVP.pm
Criterion Covered Total %
statement 34 81 41.9
branch 2 18 11.1
condition 4 20 20.0
subroutine 8 16 50.0
pod 4 6 66.6
total 52 141 36.8


line stmt bran cond sub pod time code
1             package Business::PayPal::NVP;
2              
3 1     1   1041 use 5.008001;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         15  
5 1     1   12 use warnings;
  1         1  
  1         73  
6              
7             our $VERSION = '1.10';
8             our $AUTOLOAD;
9              
10             our $Debug = 0;
11             our $Branch = 'test';
12             our $Timeout= 0;
13             our $UserAgent;
14              
15 1     1   537 use LWP::UserAgent ();
  1         29997  
  1         19  
16 1     1   6 use URI::Escape ();
  1         2  
  1         14  
17 1     1   3 use Carp 'croak';
  1         2  
  1         719  
18              
19 0     0 0 0 sub API_VERSION { 98 }
20              
21             ## NOTE: This is an inside-out object; remove members in
22             ## NOTE: the DESTROY() sub if you add additional members.
23              
24             my %errors = ();
25             my %test = ();
26             my %live = ();
27              
28             sub new {
29 1     1 1 2243 my $class = shift;
30 1         5 my %args = @_;
31              
32 1         1 my $self = bless \(my $ref), $class;
33              
34 1   50     5 $Branch = $args{branch} || 'test';
35 1         1 $Timeout = $args{timeout};
36 1   33     3 $UserAgent = $args{ua} || LWP::UserAgent->new;
37              
38 1 50       3 if (ref $UserAgent ne 'LWP::UserAgent') {
39 0         0 die "ua must be a LWP::UserAgent object\n";
40             }
41              
42 1         4 $errors {$self} = [ ];
43 1   50     3 $test {$self} = $args{test} || { };
44 1   50     6 $live {$self} = $args{live} || { };
45              
46 1         2 return $self;
47             }
48              
49             sub AUTH_CRED {
50 0     0 0 0 my $self = shift;
51 0         0 my $cred = shift;
52 0   0     0 my $branch = shift || $Branch || 'test';
53              
54             return { testuser => $test{$self}->{user},
55             testpwd => $test{$self}->{pwd},
56             testsig => $test{$self}->{sig},
57             testurl => $test{$self}->{url} || 'https://api-3t.sandbox.paypal.com/nvp',
58             testsubj => $test{$self}->{subject},
59             testver => $test{$self}->{version},
60              
61             liveuser => $live{$self}->{user},
62             livepwd => $live{$self}->{pwd},
63             livesig => $live{$self}->{sig},
64             liveurl => $live{$self}->{url} || 'https://api-3t.paypal.com/nvp',
65             livesubj => $live{$self}->{subject},
66             livever => $live{$self}->{version},
67 0   0     0 }->{$branch . $cred};
      0        
68             }
69              
70             sub _do_request {
71 0     0   0 my $self = shift;
72 0         0 my %args = @_;
73              
74 0         0 my $lwp = $UserAgent;
75 0 0       0 $lwp->timeout($Timeout) if $Timeout;
76 0         0 $lwp->agent("perl-Business-PayPal-NVP/$VERSION");
77 0         0 my $req = HTTP::Request->new( POST => $self->AUTH_CRED('url') );
78 0         0 $req->content_type( 'application/x-www-form-urlencoded' );
79              
80             my $content = _build_content( USER => $self->AUTH_CRED('user'),
81             PWD => $self->AUTH_CRED('pwd'),
82             SIGNATURE => $self->AUTH_CRED('sig'),
83 0   0     0 VERSION => delete $args{VERSION} || $self->AUTH_CRED('ver') || API_VERSION,
84             SUBJECT => $self->AUTH_CRED('subj'),
85             %args );
86 0         0 $req->content( $content );
87              
88 0 0       0 if ($Debug) {
89 0         0 require Data::Dumper;
90 0         0 print STDERR "Making request: " . Data::Dumper::Dumper($req);
91             }
92              
93 0         0 my $res = $lwp->request($req);
94              
95 0 0       0 unless( $res->code == 200 ) {
96 0         0 $self->errors("Failure: " . $res->code . ': ' . $res->message);
97 0         0 return ();
98             }
99              
100 0         0 return map { URI::Escape::uri_unescape($_) }
101 0         0 map { split /=/, $_, 2 }
  0         0  
102             split /&/, $res->content;
103             }
104              
105             sub _build_content {
106 0     0   0 my %args = @_;
107              
108 0         0 my @args = ();
109 0         0 for my $key ( keys %args ) {
110 0 0       0 $args{$key} = ( defined $args{$key} ? $args{$key} : '' );
111 0         0 push @args, URI::Escape::uri_escape($key) . '=' . URI::Escape::uri_escape($args{$key});
112             }
113              
114 0   0     0 return join('&', @args) || '';
115             }
116              
117             sub AUTOLOAD {
118 0     0   0 my $self = shift;
119 0         0 my $method = $AUTOLOAD;
120 0         0 $method =~ s/^.*:://;
121 0 0       0 return if $method eq 'DESTROY';
122 0 0       0 croak "Undefined subroutine $method" unless $method =~ /^[A-Z]/;
123 0         0 $self->_do_request(METHOD => $method, @_);
124             }
125              
126             sub send {
127 0     0 1 0 shift->_do_request(@_);
128             }
129              
130             sub errors {
131 0     0 1 0 my $self = shift;
132              
133 0 0       0 if( @_ ) {
134 0         0 push @{ $errors{$self} }, @_;
  0         0  
135 0         0 return;
136             }
137              
138 0         0 return @{ $errors{$self} };
  0         0  
139             }
140              
141             sub clear_errors {
142 0     0 1 0 my $self = shift;
143 0         0 $errors{$self} = [];
144             }
145              
146             sub DESTROY {
147 1     1   531 my $self = $_[0];
148              
149 1         2 delete $errors {$self};
150 1         1 delete $test {$self};
151 1         2 delete $live {$self};
152              
153 1         11 my $super = $self->can("SUPER::DESTROY");
154 1 50       28 goto &$super if $super;
155             }
156              
157             1;
158             __END__