PayPal/API/Webhooks.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 81.2% 13 / 16 Conditioncond 76.9% 10 / 13 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 84.6% 11 / 13 Podpod 100% 5 / 5 totaltotal 85.9% 92 / 107 CC 15 SCAR 14.4
CC
15
Cov
88%
verify
7.0
create
6.1
get
6.0
CRAP 4.2
« PayPal.pm ↑ Summary PayPal/Order.pm »
1 package WWW::PayPal::API::Webhooks;
2  
3 # ABSTRACT: PayPal Webhooks API (v1) — management + signature verification
4  
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use WWW::PayPal::Webhook;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.003';
12  
13  
14 3 has client => (
15 is => 'ro',
16 required => 1,
17 weak_ref => 1,
18 );
19  
20 has openapi_operations => (
21 is => 'lazy',
22 0 ▶ builder => sub {
Subroutine _build_openapi_operations: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
23 return {
24 0 'notifications.verify-webhook-signature' => { method => 'POST', path => '/v1/notifications/verify-webhook-signature' },
25 'notifications.webhooks.create' => { method => 'POST', path => '/v1/notifications/webhooks' },
26 'notifications.webhooks.list' => { method => 'GET', path => '/v1/notifications/webhooks' },
27 'notifications.webhooks.get' => { method => 'GET', path => '/v1/notifications/webhooks/{id}' },
28 'notifications.webhooks.delete' => { method => 'DELETE', path => '/v1/notifications/webhooks/{id}' },
29 };
30 },
31 3 );
32  
33 3 with 'WWW::PayPal::Role::OpenAPI';
34  
35 4 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
36 4 my ($self, $data) = @_;
37 4 return WWW::PayPal::Webhook->new(client => $self->client, data => $data);
38 }
39  
40 9 ▶sub verify {
Subroutine verify: called CC=7 SCAR=19.5
Pod documented
41 9 my ($self, %args) = @_;
42 9 for my $k (qw( webhook_id raw_body transmission_id transmission_time transmission_sig cert_url auth_algo )) {
43 52 ▶ croak $k.' required' unless defined $args{$k} && length $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$k} and length $args{$k} 2 50 52
Condition100%3/3
defined $args{$k} and length $args{$k}
!ll&&!rl&&r
1150
MC/DC100%2/2
defined $args{$k} and length $args{$k}
defined $args{$k} length $args{$k}
Truth table100%3/3
A and B
A= defined $args{$k}B= length $args{$k}
ABresult
0X0
100
111
44 }
45 croak 'raw_body must be a byte string, not a reference'
46 7 ▶ if ref $args{raw_body};
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"raw_body"} 1 6 7
47  
48 6 my $raw_body = $args{raw_body};
49  
50 # Encode ONLY the metadata, then splice the raw event bytes in verbatim.
51 # The event body must never pass through a JSON decode+encode round-trip —
52 # that would reorder keys / re-whitespace and break the signature. This
53 # produces a non-ref scalar body, which Role::HTTP::request sends as-is
54 # (the ref branch would re-serialise it).
55 my $meta = encode_json({
56 auth_algo => $args{auth_algo},
57 cert_url => $args{cert_url},
58 transmission_id => $args{transmission_id},
59 transmission_sig => $args{transmission_sig},
60 transmission_time => $args{transmission_time},
61 webhook_id => $args{webhook_id},
62 6 });
63 6 $meta =~ s/\}\z/,"webhook_event":$raw_body}/;
64  
65 6 my $data = $self->call_operation('notifications.verify-webhook-signature', body => $meta);
66  
67 # Return a real boolean, never the raw status string: "FAILURE" is truthy
68 # and a caller doing `if ($ok)` on it would grant access to a forged event.
69 6 ▶ return ( ($data->{verification_status} // '') eq 'SUCCESS' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($data->{'verification_status'} // '') eq 'SUCCESS' ? : 3 3 6
Condition100%2/2
$data->{"verification_status"} // ""
l!l
42
MC/DC100%1/1
$data->{"verification_status"} // ""
$data->{"verification_status"}
Truth table100%2/2
A
A= $data->{"verification_status"}
Aresult
11
00
70 }
71  
72  
73 3 ▶sub create {
Subroutine create: called CC=6 SCAR=18.1
Pod documented
74 3 my ($self, %args) = @_;
75 3 ▶ croak 'url required' unless $args{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"url"} 0 3 3
76 croak 'event_types required (arrayref of event names)'
77 3 ▶ unless $args{event_types} && ref $args{event_types} eq 'ARRAY' && @{ $args{event_types} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"event_types"} and ref $args{"event_types"} eq "ARRAY" and @{$args{"event_types"};} 1 2 3
Condition66%4/6
$args{"event_types"} and ref $args{"event_types"} eq "ARRAY" and @{$args{"event_types"};}
!ll&&!rl&&r
102
$args{"event_types"} and ref $args{"event_types"} eq "ARRAY"
!ll&&!rl&&r
102
MC/DC33%1/3
$args{"event_types"} and ref $args{"event_types"} eq "ARRAY" and @{$args{"event_types"};}
$args{"event_types"} ref $args{"event_types"} eq "ARRAY" @{$args{"event_types"};}
Truth table50%2/4
A and B and C
A= $args{"event_types"}B= ref $args{"event_types"} eq "ARRAY"C= @{$args{"event_types"};}
ABCresult
0XX0
10X0
1100
1111
78  
79 my %body = (
80 url => $args{url},
81 2 ▶ event_types => [ map { ref $_ eq 'HASH' ? $_ : { name => $_ } } @{ $args{event_types} } ],
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 1 3 4
82 );
83 2 return $self->_wrap($self->call_operation('notifications.webhooks.create', body => \%body));
84 }
85  
86  
87 1 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
88 1 my ($self) = @_;
89 1 my $data = $self->call_operation('notifications.webhooks.list');
90 1 ▶ return [ map { $self->_wrap($_) } @{ $data->{webhooks} || [] } ];
Condition50%1/2
$data->{'webhooks'} || []
l!l
10
MC/DC0%0/1
$data->{'webhooks'} || []
$data->{'webhooks'}
Truth table50%1/2
A
A= $data->{'webhooks'}
Aresult
11
00
91 }
92  
93  
94 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
95 0 my ($self, $id) = @_;
96 0 ▶ croak 'webhook id required' unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
97 0 return $self->_wrap($self->call_operation('notifications.webhooks.get', path => { id => $id }));
98 }
99  
100  
101 2 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
102 2 my ($self, $id) = @_;
103 2 ▶ croak 'webhook id required' unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 1 2
104 # PayPal returns 204 No Content; Role::HTTP hands back undef. There is
105 # nothing to wrap — just report success.
106 1 $self->call_operation('notifications.webhooks.delete', path => { id => $id });
107 1 return 1;
108 }
109  
110  
111  
112 3 1;
113  
114 __END__
« PayPal.pm ↑ Summary PayPal/Order.pm »