blib/lib/Net/PayPal.pm

filter: Statementstmt 20.3% 27 / 133 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 35.0% 7 / 20 Podpod 72.7% 8 / 11 totaltotal 17.6% 42 / 238 CC 31 SCAR 35.5
CC
31
Cov
14%
store_cc
90.0
cc_payment
56.0
rest
42.0
CRAP 34.9
↑ Summary
1 package Net::PayPal;
2
3 # $Id$
4
5 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp ("croak");
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cache::FileCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 1 our $VERSION = '0.02';
14
15 1 our $ENDPOINT_SANDBOX = "https://api.sandbox.paypal.com";
16 1 our $ENDPOINT_LIVE = "https://api.paypal.com";
17
18 1 my $live = 0;
19
20 0 ▶sub live {
Subroutine live: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my $class = shift;
22 0 my ($value) = @_;
23 0 return $live = $value;
24 }
25
26 0 ▶sub endpoint {
Subroutine endpoint: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my $class = shift;
28
29 0 ▶ if ( $live == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($live == 1) 0 0 0
30 0 return $ENDPOINT_LIVE;
31 }
32 0 return $ENDPOINT_SANDBOX;
33 }
34
35 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
36 0 my $class = shift;
37
38 0 my %args = (
39 client_id => $_[0],
40 secret => $_[1],
41 user_agent => LWP::UserAgent->new,
42 app_id => undef,
43 access_token => undef,
44 @_
45 );
46
47 0 ▶ unless ( $args{client_id} && $args{secret} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"client_id"} and $args{"secret"}) 0 0 0
Condition0%0/3
$args{"client_id"} and $args{"secret"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"client_id"} and $args{"secret"}
$args{"client_id"} $args{"secret"}
Truth table0%0/3
A and B
A= $args{"client_id"}B= $args{"secret"}
ABresult
0X0
100
111
48 0 croak " new() : client_id and secret are missing ";
49 }
50
51 #
52 # checking if access_token is available from previous requests
53 #
54 0 my $cache = Cache::FileCache->new( { cache_root => File::Spec->tmpdir, namespace => 'NetPayPal' } );
55
56 0 my $cipher = Crypt::CBC->new( -key => $args{"secret"}, -cipher => 'Blowfish' );
57
58 0 ▶ if ( my $e_token = $cache->get( $args{"client_id"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e_token = $cache->get($args{'client_id'})) { } 0 0 0
59 0 $args{access_token} = $cipher->decrypt($e_token);
60 }
61
62 else {
63
64 # if access_token cannot be found in the cache we need to authenticate ourselves to get one
65 0 my $ua = $args{user_agent};
66
67 0 my $h = HTTP::Headers->new(
68 Accept => "application/json",
69 'Accept-Language' => 'en_US'
70 );
71
72 0 $h->authorization_basic( $args{client_id}, $args{secret} );
73
74 0 my $endpoint = $class->endpoint;
75
76 0 my $req = HTTP::Request->new( "POST", $endpoint . '/v1/oauth2/token', $h );
77 0 $req->content("grant_type=client_credentials");
78
79 0 my $res = $ua->request($req);
80 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
81 0 croak "Authorization failed : " . $res->status_line . ', ' . $res->content;
82 }
83
84 0 my $res_hash = _json_decode( $res->content );
85
86 0 $args{access_token} = $res_hash->{access_token};
87 0 $args{app_id} = $res_hash->{app_id};
88
89 0 $cache->set( $args{"client_id"}, $cipher->encrypt( $args{access_token} ), $res_hash->{expires_in} - 5 );
90 }
91 0 return bless( \%args, $class );
92 }
93
94 1 my $json = JSON->new->allow_nonref;
95
96 0 ▶sub _json_decode {
Subroutine _json_decode: not called CC=2 SCAR=17.9
97 0 my $text = shift;
98 0 my $hashref;
99 0 eval { $hashref = $json->decode($text); };
100
101 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
102 0 croak "_json_decode(): cannot decode $text: $error";
103 }
104 0 return $hashref;
105 }
106
107 0 ▶sub _json_encode {
Subroutine _json_encode: not called CC=1 SCAR=6.9
108 0 my $hashref = shift;
109 0 return $json->encode($hashref);
110 }
111
112 0 ▶sub rest {
Subroutine rest: not called CC=6 SCAR=37.4
Pod documented
113 0 my $self = shift;
114 0 my ( $method, $path, $json, $dump_responce ) = @_;
115
116 0 ▶ unless ( $path =~ /\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[/$]) 0 0 0
117 0 $path = $path . '/';
118 }
119
120 0 my $endpoint = $self->endpoint;
121 0 $endpoint = sprintf( " % s%s", $endpoint, $path );
122 0 my $a_token = $self->{access_token};
123 0 my $req = HTTP::Request->new( $method, $endpoint, [ 'Content-Type', 'application/json', 'Authorization', "Bearer $a_token" ] );
124
125 0 ▶ if ($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) 0 0 0
126 0 $req->content($json);
127 }
128
129 0 my $ua = $self->{user_agent};
130 0 my $res = $ua->request($req);
131
132 0 ▶ if ($dump_responce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_responce) 0 0 0
133 0 require Data::Dumper;
134 0 return Data::Dumper::Dumper($res);
135 }
136
137 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
138 0 ▶ if ( my $content = $res->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = $res->content) 0 0 0
139 0 my $error = _json_decode( $res->content );
140 0 $self->error( sprintf( "%s: %s. See: %s", $error->{name}, $error->{message}, $error->{information_link} ) );
141 0 return undef;
142 }
143 0 $self->error( $res->status_line );
144 0 return undef;
145 }
146 0 return _json_decode( $res->content );
147 }
148
149 0 ▶sub cc_payment {
Subroutine cc_payment: not called CC=7 SCAR=40.3
Pod documented
150 0 my $self = shift;
151 0 my ($data) = @_;
152
153 0 foreach my $field (qw/cc_number cc_type cc_expire_month cc_expire_year/) {
154 0 ▶ unless ( $data->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{$field}) 0 0 0
155 0 croak "payment(): $field is a required field";
156 }
157 }
158
159 my %credit_card = (
160 number => $data->{cc_number},
161 type => $data->{cc_type},
162 expire_month => $data->{cc_expire_month},
163 expire_year => $data->{cc_expire_year}
164 0 );
165
166 0 foreach my $field (qw/first_name last_name billing_address/) {
167 0 ▶ if ( $data->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$field}) 0 0 0
168 0 $credit_card{$field} = $data->{$field};
169 }
170 }
171
172 my $request_hash = {
173 intent => 'sale',
174 payer => {
175 payment_method => "credit_card",
176 funding_instruments => [ { credit_card => \%credit_card } ]
177 },
178 transactions => [
179 {
180 amount => {
181 total => $data->{amount},
182 0 ▶ currency => $data->{currency} || "USD"
Condition0%0/2
$data->{'currency'} || 'USD'
l!l
00
MC/DC0%0/1
$data->{'currency'} || 'USD'
$data->{'currency'}
Truth table0%0/2
A
A= $data->{'currency'}
Aresult
11
00
183 },
184 }
185 ]
186 };
187
188 0 ▶ if ( $data->{redirect_urls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"redirect_urls"}) 0 0 0
189 0 $request_hash->{redirect_urls} = $data->{redirect_urls};
190 }
191
192 0 return $self->rest( 'POST', "/v1/payments/payment", _json_encode($request_hash) );
193 }
194
195 0 ▶sub stored_cc_payment {
Subroutine stored_cc_payment: not called CC=4 SCAR=30.0
Pod documented
196 0 my $self = shift;
197 0 my ($data) = @_;
198
199 0 ▶ unless ( $data->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"id"}) 0 0 0
200 0 croak "stored_cc_payment(): 'id' is missing";
201 }
202
203 my $request_hash = {
204 intent => 'sale',
205 payer => {
206 payment_method => "credit_card",
207 funding_instruments => [ { credit_card_token => { credit_card_id => $data->{id} } } ]
208 },
209 transactions => [
210 {
211 amount => {
212 total => $data->{amount},
213 0 ▶ currency => $data->{currency} || "USD"
Condition0%0/2
$data->{'currency'} || 'USD'
l!l
00
MC/DC0%0/1
$data->{'currency'} || 'USD'
$data->{'currency'}
Truth table0%0/2
A
A= $data->{'currency'}
Aresult
11
00
214 },
215 }
216 ]
217 };
218
219 0 ▶ if ( $data->{redirect_urls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"redirect_urls"}) 0 0 0
220 0 $request_hash->{redirect_urls} = $data->{redirect_urls};
221 }
222
223 0 return $self->rest( 'POST', "/v1/payments/payment", _json_encode($request_hash) );
224 }
225
226 0 ▶sub get_payment {
Subroutine get_payment: not called CC=2 SCAR=17.9
Pod documented
227 0 my $self = shift;
228 0 my ($id) = @_;
229
230 0 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
231 0 croak "get_payment(): Invalid Payment ID";
232 }
233
234 0 return $self->rest( "GET", "/v1/payments/payment/$id" );
235 }
236
237 0 ▶sub get_payments {
Subroutine get_payments: not called CC=1 SCAR=6.9
Pod documented
238 0 my $self = shift;
239
240 0 return $self->rest( "GET", "/v1/payments/payment" );
241 }
242
243 0 ▶sub store_cc {
Subroutine store_cc: not called CC=9 SCAR=45.0
Pod documented
244 0 my $self = shift;
245 0 my ($data) = @_;
246
247 my %credit_card = (
248 number => $data->{number} || $data->{cc_number},
249 type => $data->{type} || $data->{cc_type},
250 expire_month => $data->{expire_month} || $data->{cc_expire_month},
251 expire_year => $data->{expire_year} || $data->{cc_expire_year}
252 0 ▶ );
Condition0%0/12
$data->{'number'} || $data->{'cc_number'}
l!l&&r!l&&!r
000
$data->{'type'} || $data->{'cc_type'}
l!l&&r!l&&!r
000
$data->{'expire_month'} || $data->{'cc_expire_month'}
l!l&&r!l&&!r
000
$data->{'expire_year'} || $data->{'cc_expire_year'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{'number'} || $data->{'cc_number'}
$data->{'number'} $data->{'cc_number'}
MC/DC0%0/2
$data->{'type'} || $data->{'cc_type'}
$data->{'type'} $data->{'cc_type'}
MC/DC0%0/2
$data->{'expire_month'} || $data->{'cc_expire_month'}
$data->{'expire_month'} $data->{'cc_expire_month'}
MC/DC0%0/2
$data->{'expire_year'} || $data->{'cc_expire_year'}
$data->{'expire_year'} $data->{'cc_expire_year'}
Truth table0%0/3
A || B
A= $data->{'number'}B= $data->{'cc_number'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $data->{'type'}B= $data->{'cc_type'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $data->{'expire_month'}B= $data->{'cc_expire_month'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $data->{'expire_year'}B= $data->{'cc_expire_year'}
ABresult
1X1
011
000
253
254 0 ▶ if ( my $cvv2 = $data->{cvv2} || $data->{cc_cvv2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cvv2 = $data->{"cvv2"} || $data->{"cc_cvv2"}) 0 0 0
Condition0%0/3
$data->{'cvv2'} || $data->{'cc_cvv2'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{'cvv2'} || $data->{'cc_cvv2'}
$data->{'cvv2'} $data->{'cc_cvv2'}
Truth table0%0/3
A || B
A= $data->{'cvv2'}B= $data->{'cc_cvv2'}
ABresult
1X1
011
000
255 0 $credit_card{cvv2} = $cvv2;
256 }
257
258 0 foreach my $field (qw/first_name last_name billing_address/) {
259 0 ▶ if ( $data->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$field}) 0 0 0
260 0 $credit_card{$field} = $data->{$field};
261 }
262 }
263 0 return $self->rest( 'POST', "/v1/vault/credit-card", _json_encode( \%credit_card ) );
264 }
265
266 0 ▶sub get_cc {
Subroutine get_cc: not called CC=1 SCAR=6.9
Pod documented
267 0 my $self = shift;
268 0 my ($id) = @_;
269 0 return $self->rest( "GET", "/v1/vault/credit-card/$id" );
270 }
271
272 1 my $last_error;
273
274 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my $self = shift;
276 0 my ($new_message) = @_;
277
278 0 ▶ unless ($new_message) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_message) 0 0 0
279 0 return $last_error;
280 }
281
282 0 $last_error = $new_message;
283 }
284
285 1 1;
286 __END__
↑ Summary