GoCardless/Webhook.pm

filter: Statementstmt 91.0% 51 / 56 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 92.3% 72 / 78 CC 5 SCAR 6.0
CC
5
Cov
90%
__ANON__
3.1
resources
3.0
BEGIN
1.0
CRAP 1.8
« GoCardless/Payment.pm ↑ Summary GoCardless/OutboundPayment.pm »
1 package Business::GoCardless::Webhook;
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/#webhook-overview
12  
13 =cut
14  
15 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 55 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 55 extends 'Business::GoCardless::Resource';
20 55 with 'Business::GoCardless::Utils';
21  
22 55 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 55 ▶use Business::GoCardless::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 ATTRIBUTES
26  
27 resource_type
28 action
29  
30 =cut
31  
32 55 has [ qw/
33 resource_type
34 action
35 _payload
36 / ] => (
37 is => 'rw',
38 clearer => 1,
39 );
40  
41 =head1 Operations on a webhook
42  
43 =head2 json
44  
45 Allows you to set the json data sent to you in the webhook:
46  
47 $Webhook->json( $json_data )
48  
49 Will throw a L<Business::GoCardless::Exception> exception if the json fails to
50 parse or if the signature does not match the payload data.
51  
52 =cut
53  
54 has json => (
55 is => 'rw',
56 required => 1,
57 5 ▶ trigger => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
58 5 my ( $self,$json ) = @_;
59  
60 # defensive decoding
61 5 my $params;
62 5 eval { $params = JSON->new->decode( $json ) };
63 5 ▶ $@ && do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { $self->_clear_payload; $self->clear_resource_type; $self->clear_action; "Business::GoCardless::Exception"->throw({"message", "Failed to parse json: $@"}) } 0 5 5
64 0 $self->_clear_payload;
65 0 $self->clear_resource_type;
66 0 $self->clear_action;
67  
68 0 Business::GoCardless::Exception->throw({
69 message => "Failed to parse json: $@",
70 });
71 };
72  
73 5 $self->resource_type( $params->{payload}{resource_type} );
74 5 $self->action( $params->{payload}{action} );
75 5 $self->_payload( $params->{payload} );
76  
77 5 ▶ if ( ! $self->signature_valid(
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->signature_valid($params->{"payload"}, $self->client->app_secret)) 2 3 5
78 $params->{payload},$self->client->app_secret )
79 ) {
80 2 $self->_clear_payload;
81 2 $self->clear_resource_type;
82 2 $self->clear_action;
83  
84 2 Business::GoCardless::Exception->throw({
85 message => "Invalid signature for webhook",
86 });
87 }
88  
89 3 return $json;
90 }
91 55 );
92  
93 =head2 resources
94  
95 Returns an array of resource objects (Bill, Subscription, etc) that are present
96 in webhook allowing you to do things with them or update your own data:
97  
98 if ( $Webhook->is_bill ) {
99 foreach my $Bill ( $Webhook->resources ) {
100 ...
101 }
102 } elsif ( $Webhook->is_subscription ) {
103 ...
104  
105 =cut
106  
107 4 ▶sub resources {
Subroutine resources: called CC=3 SCAR=11.0
Pod documented
108 4 my ( $self ) = @_;
109  
110 4 my @resources;
111  
112 4 ▶ return if ! $self->resource_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_type 2 2 4
113  
114 my $key = {
115 bill => 'bills',
116 pre_authorization => 'pre_authorizations',
117 subscription => 'subscriptions',
118 2 }->{ $self->resource_type };
119  
120 2 my $class_suffix = ucfirst( $self->resource_type );
121 0 $class_suffix =~ s/_([A-z])/uc($1)/ge;
122 2 my $class = "Business::GoCardless::$class_suffix";
123  
124 2 foreach my $hash ( @{ $self->_payload->{ $key } } ) {
125  
126 my $obj = $class->new(
127 client => $self->client,
128 4 %{ $hash },
129 );
130  
131 4 push( @resources,$obj );
132 }
133  
134 2 return @resources;
135 }
136  
137 =pod
138  
139 Shortcut methods to get the type of data in the webhook, and thus the type of
140 objects that will be returned by the call to ->resources
141  
142 =head2 is_bill
143  
144 True if the webhook is for bills
145  
146 =head2 is_pre_authorization
147  
148 True if the webhook is for pre authorizations
149  
150 =head2 is_subscription
151  
152 True if the webhook is for subscriptions
153  
154 =cut
155  
156 2 ▶sub is_bill { return shift->resource_type eq 'bill' }
Subroutine is_bill: called CC=1 SCAR=0.0
Pod documented
157 2 ▶sub is_pre_authorization { return shift->resource_type eq 'pre_authorization' }
Subroutine is_pre_authorization: called CC=1 SCAR=0.0
Pod documented
158 2 ▶sub is_subscription { return shift->resource_type eq 'subscription' }
Subroutine is_subscription: called CC=1 SCAR=0.0
Pod documented
159  
160 =head2 is_legacy
161  
162 See if the webhook is a legacy (Basic API) webhook
163  
164 if ( $Webhook->is_legacy ) {
165 ...
166 }
167  
168 =cut
169  
170 2 ▶sub is_legacy { 1 }
Subroutine is_legacy: called CC=1 SCAR=0.0
Pod documented
171  
172 =head1 CONFIRMING WEBHOOKS
173  
174 According to the gocardless API docs you should respond once the signature of the
175 webhook has been checked. The response is a HTTP status 200 code:
176  
177 HTTP/1.1 200 OK
178  
179 You should handle this in your own code, the library will not do it for you. See
180 https://developer.gocardless.com/#response for more information
181  
182 =head1 AUTHOR
183  
184 Lee Johnson - C<lee@payprop.com>
185  
186 This library is free software; you can redistribute it and/or modify it under
187 the same terms as Perl itself. If you would like to contribute documentation,
188 features, bug fixes, or anything else then please raise an issue / pull request:
189  
190 https://github.com/payprop/business-gocardless
191  
192 =cut
193  
194 55 1;
195  
196 # vim: ts=4:sw=4:et
« GoCardless/Payment.pm ↑ Summary GoCardless/OutboundPayment.pm »