GoCardless/Webhook/V2.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 95.1% 79 / 83 CC 5 SCAR 8.2
CC
5
Cov
94%
__ANON__
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 2.3
« GoCardless/Paginator.pm ↑ Summary GoCardless/OutboundPaymentImport.pm »
1 package Business::GoCardless::Webhook::V2;
2  
3 =head1 NAME
4  
5 Business::GoCardless::Webhook
6  
7 =head1 DESCRIPTION
8  
9 A class for gocardless webhooks, extends L<Business::GoCardless::Resource>.
10 For more details see the gocardless API documentation specific to webhooks:
11 https://developer.gocardless.com/api-reference/#appendix-webhooks
12  
13 Note to use webhooks you must set the webhook_secret on the client object
14  
15 =cut
16  
17 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 extends 'Business::GoCardless::Webhook';
22 5 with 'Business::GoCardless::Utils';
23  
24 5 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Business::GoCardless::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Business::GoCardless::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Business::GoCardless::Webhook::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 ATTRIBUTES
31  
32 json
33 signature
34 has_legacy_data
35  
36 The following attributes are only populated when the object comes from the
37 webhooks list endpoint (i.e. L<Business::GoCardless::Pro/webhooks>), and contain
38 details of the webhook delivery itself:
39  
40 id
41 created_at
42 url
43 request_headers
44 response_code
45 response_body
46 response_body_truncated
47 response_headers
48 response_headers_content_truncated
49 response_headers_count_truncated
50 is_test
51 successful
52  
53 =cut
54  
55 5 has [ qw/
56 events
57 signature
58 _signature
59  
60 has_legacy_data
61 legacy_webhook
62 / ] => (
63 is => 'rw',
64 clearer => 1,
65 );
66  
67 5 has [ qw/
68 id
69 created_at
70 url
71 request_headers
72 response_code
73 response_body
74 response_body_truncated
75 response_headers
76 response_headers_content_truncated
77 response_headers_count_truncated
78 is_test
79 successful
80 / ] => (
81 is => 'rw',
82 );
83  
84 =head1 Operations on a webhook
85  
86 =head2 events
87  
88 Get a list of L<Business::GoCardless::Webhook::Event> objects for processing:
89  
90 foreach my $Event ( @{ $Webhook->events // [] } ) {
91 ...
92 }
93  
94 =head2 json
95  
96 Allows you to set the json data sent to you in the webhook:
97  
98 $Webhook->json( $json_data )
99  
100 Will throw a L<Business::GoCardless::Exception> exception if the json fails to
101 parse or if the signature does not match the payload data.
102  
103 =cut
104  
105 has json => (
106 is => 'rw',
107 required => 1,
108 9 ▶ trigger => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
109 9 my ( $self,$json ) = @_;
110  
111 # defensive decoding
112 9 my $params;
113 # the json is the raw (UTF-8 encoded) request body, as that is what
114 # the signature is generated from
115 9 eval { $params = JSON->new->utf8->decode( $json ) };
116 9 ▶ $@ && do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { $self->clear_events; $self->clear_signature; "Business::GoCardless::Exception"->throw({"message", "Failed to parse json: $@"}) } 0 9 9
117 0 $self->clear_events;
118 0 $self->clear_signature;
119  
120 0 Business::GoCardless::Exception->throw({
121 message => "Failed to parse json: $@",
122 });
123 };
124  
125 9 ▶ if ( $params->{payload} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'payload'}) { } 1 8 9
126  
127 # this is a legacy API webhook
128 1 $self->has_legacy_data( 1 );
129  
130 my $LegacyWebhook = Business::GoCardless::Webhook->new(
131 json => $json,
132 client => Business::GoCardless::Client->new(
133 app_secret => $self->client->webhook_secret, # bad assumption?
134 1 %{ $self->client },
135 ),
136 );
137  
138 1 $self->legacy_webhook( $LegacyWebhook );
139  
140 } else {
141  
142 # coerce the events into objects
143 $self->events([
144 map { Business::GoCardless::Webhook::Event->new(
145 client => $self->client,
146 13 %{ $_ }
147 ) }
148 8 @{ $params->{events} }
149 ]);
150  
151 8 ▶ $self->signature( $self->_signature ) if ! $self->signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->signature 7 1 8
152  
153 8 ▶ if ( ! $self->signature_valid(
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->signature_valid($json, $self->client->webhook_secret, $self->signature)) 2 6 8
154 $json,$self->client->webhook_secret,$self->signature
155 ) ) {
156 2 $self->clear_events;
157 2 $self->clear_signature;
158  
159 2 Business::GoCardless::Exception->throw({
160 message => "Invalid signature for webhook",
161 });
162 }
163 }
164  
165 7 return $json;
166 }
167 5 );
168  
169 =head2 is_legacy
170  
171 See if the webhook is a legacy (Basic API) webhook
172  
173 if ( $Webhook->is_legacy ) {
174 ...
175 }
176  
177 =cut
178  
179 1 ▶sub is_legacy { 0 }
Subroutine is_legacy: called CC=1 SCAR=0.0
Pod documented
180  
181 =head2 retry
182  
183 Retry the delivery of a webhook, only for webhooks from the webhooks list
184 endpoint (see L<Business::GoCardless::Pro/webhooks>). Returns a new
185 Webhook::V2 object for the new delivery:
186  
187 my $Retried = $Webhook->retry if ! $Webhook->successful;
188  
189 =cut
190  
191 1 ▶sub retry {
Subroutine retry: called CC=1 SCAR=0.0
Pod documented
192 1 my ( $self ) = @_;
193  
194 1 my $data = $self->client->api_post(
195 1 "/webhooks/@{[ $self->id ]}/actions/retry"
196 );
197  
198 1 return ref( $self )->_new_from_api( $self->client,$data->{webhooks} );
199 }
200  
201 # create an object from the webhooks API, which has the details of the
202 # delivery as well as the original request
203 4 ▶sub _new_from_api {
Subroutine _new_from_api: called CC=1 SCAR=0.0
204 4 my ( $class,$client,$data ) = @_;
205  
206 return $class->new(
207 client => $client,
208 4 %{ $data }, # delivery details: id, url, response_code, etc
209 # the signature is of the raw (UTF-8 encoded) request body
210 json => Encode::encode_utf8( $data->{request_body} ),
211 # load ordering handled by setting _signature rather than signature
212 # signature will be set in the json trigger
213 4 _signature => $data->{request_headers}{'Webhook-Signature'},
214 );
215 }
216  
217 =head1 CONFIRMING WEBHOOKS
218  
219 According to the gocardless API docs you should respond once the signature of the
220 webhook has been checked. The response is a HTTP status 204 code:
221  
222 HTTP/1.1 204 OK
223  
224 You should handle this in your own code, the library will not do it for you. See
225 https://developer.gocardless.com/#response for more information
226  
227 =head1 AUTHOR
228  
229 Lee Johnson - C<lee@payprop.com>
230  
231 This library is free software; you can redistribute it and/or modify it under
232 the same terms as Perl itself. If you would like to contribute documentation,
233 features, bug fixes, or anything else then please raise an issue / pull request:
234  
235 https://github.com/payprop/business-gocardless
236  
237 =cut
238  
239 5 1;
240  
241 # vim: ts=4:sw=4:et
« GoCardless/Paginator.pm ↑ Summary GoCardless/OutboundPaymentImport.pm »