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   860 use strict;
  4         16  
  4         118  
16 4     4   24 use warnings;
  4         9  
  4         90  
17              
18 4     4   21 use Moo;
  4         8  
  4         38  
19             extends 'Business::GoCardless::Resource';
20             with 'Business::GoCardless::Utils';
21              
22 4     4   1675 use Business::GoCardless::Exception;
  4         13  
  4         108  
23 4     4   918 use Business::GoCardless::Payment;
  4         10  
  4         129  
24 4     4   34 use Business::GoCardless::Subscription;
  4         9  
  4         140  
25 4     4   28 use Business::GoCardless::Mandate;
  4         12  
  4         1871  
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 1     1 1 921 my ( $self ) = @_;
75              
76 1 50       8 return if ! $self->resource_type;
77              
78 1         4 my $mapping = {
79             payments => 'payment',
80             payouts => 'payout',
81             subscriptions => 'subscription',
82             mandates => 'mandate',
83             };
84              
85 1         3 my $resource = $mapping->{ $self->resource_type };
86              
87 1 50       3 $resource || Business::GoCardless::Exception->throw({
88 0         0 message => "Unknown resource_type (@{[$self->resource_type]}) in ->resources",
89             });
90              
91 1         5 my $class_suffix = ucfirst( $resource );
92 1         3 $class_suffix =~ s/_([A-z])/uc($1)/ge;
  0         0  
93 1         3 my $class = "Business::GoCardless::$class_suffix";
94              
95             return $class->new(
96             client => $self->client,
97 1         15 id => $self->links->{ $resource },
98             );
99             }
100              
101             =head2 is_payment
102              
103             =head2 is_subscription
104              
105             =head2 is_payout
106              
107             =head2 is_mandate
108              
109             =head2 is_refund
110              
111             Shortcut methods to get the type of data in the event, and thus the type of
112             objects that will be returned by the call to ->resources
113              
114             =cut
115              
116 2     2 1 411 sub is_payment { return shift->resource_type eq 'payments' }
117 1     1 1 7 sub is_subscription { return shift->resource_type eq 'subscriptions' }
118 1     1 1 8 sub is_payout { return shift->resource_type eq 'payouts' }
119 2     2 1 12 sub is_mandate { return shift->resource_type eq 'mandates' }
120 1     1 1 7 sub is_refund { return shift->resource_type eq 'refunds' }
121              
122             # BACK COMPATIBILITY
123 1     1 0 3 sub is_pre_authorization { return shift->is_mandate }
124 1     1 0 6 sub is_bill { return shift->is_payment }
125              
126             1;
127              
128             # vim: ts=4:sw=4:et