File Coverage

blib/lib/Business/GoCardless/Webhook/Event.pm
Criterion Covered Total %
statement 37 39 94.8
branch 2 4 50.0
condition n/a
subroutine 15 15 100.0
pod 6 8 75.0
total 60 66 90.9


line stmt bran cond sub pod time code
1             package Business::GoCardless::Webhook::Event;
2              
3             =head1 NAME
4              
5             Business::GoCardless::Webhook::Event
6              
7             =head1 DESCRIPTION
8              
9             A class for gocardless webhook events, extends L.
10             For more details see the gocardless API documentation specific to webhooks:
11             https://developer.gocardless.com/api-reference/#appendix-webhooks
12              
13             =cut
14              
15 4     4   297467 use strict;
  4         11  
  4         162  
16 4     4   21 use warnings;
  4         9  
  4         252  
17              
18 4     4   23 use Moo;
  4         48  
  4         80  
19             extends 'Business::GoCardless::Resource';
20             with 'Business::GoCardless::Utils';
21              
22 4     4   2137 use Business::GoCardless::Exception;
  4         41  
  4         142  
23 4     4   1195 use Business::GoCardless::Payment;
  4         10  
  4         172  
24 4     4   24 use Business::GoCardless::Subscription;
  4         8  
  4         135  
25 4     4   21 use Business::GoCardless::Mandate;
  4         6  
  4         2045  
26              
27             =head1 ATTRIBUTES
28              
29             id
30             created_at
31             action
32             resource_type
33             links
34             details
35              
36             =cut
37              
38             has [ qw/
39             id
40             created_at
41             action
42             resource_type
43             links
44             / ] => (
45             is => 'ro',
46             required => 1,
47             );
48              
49             has [ qw/
50             details
51             / ] => (
52             is => 'rw',
53             required => 0,
54             );
55              
56             =head2 resources
57              
58             Returns an array of resource objects (Payment, Subscription, etc) that are present
59             in the event allowing you to do things with them or update your own data:
60              
61             if ( $Event->is_payment ) {
62              
63             foreach my $Payment ( $Event->resources ) {
64              
65             if ( $Event->action eq 'paid_out' ) {
66             ...
67             }
68             }
69             }
70              
71             =cut
72              
73             sub resources {
74 7     7 1 4617 my ( $self ) = @_;
75              
76 7 50       30 return if ! $self->resource_type;
77              
78 7         63 my $mapping = {
79             payments => 'payment',
80             payouts => 'payout',
81             subscriptions => 'subscription',
82             mandates => 'mandate',
83             };
84              
85 7         21 my $resource = $mapping->{ $self->resource_type };
86              
87 7 50       25 $resource || Business::GoCardless::Exception->throw({
88 0         0 message => "Unknown resource_type (@{[$self->resource_type]}) in ->resources",
89             });
90              
91 7         15 my $class_suffix = ucfirst( $resource );
92 7         20 $class_suffix =~ s/_([A-z])/uc($1)/ge;
  0         0  
93 7         15 my $class = "Business::GoCardless::$class_suffix";
94              
95             return $class->new(
96             client => $self->client,
97 7         203 id => $self->links->{ $resource },
98             links => $self->links,
99             );
100             }
101              
102             =head2 is_payment
103              
104             =head2 is_subscription
105              
106             =head2 is_payout
107              
108             =head2 is_mandate
109              
110             =head2 is_refund
111              
112             Shortcut methods to get the type of data in the event, and thus the type of
113             objects that will be returned by the call to ->resources
114              
115             =cut
116              
117 2     2 1 561 sub is_payment { return shift->resource_type eq 'payments' }
118 1     1 1 11 sub is_subscription { return shift->resource_type eq 'subscriptions' }
119 1     1 1 10 sub is_payout { return shift->resource_type eq 'payouts' }
120 2     2 1 17 sub is_mandate { return shift->resource_type eq 'mandates' }
121 1     1 1 11 sub is_refund { return shift->resource_type eq 'refunds' }
122              
123             # BACK COMPATIBILITY
124 1     1 0 4 sub is_pre_authorization { return shift->is_mandate }
125 1     1 0 4 sub is_bill { return shift->is_payment }
126              
127             1;
128              
129             # vim: ts=4:sw=4:et