File Coverage

lib/WebService/Braintree/WebhookNotification.pm
Criterion Covered Total %
statement 14 40 35.0
branch 0 16 0.0
condition n/a
subroutine 5 9 55.5
pod 2 4 50.0
total 21 69 30.4


line stmt bran cond sub pod time code
1             package WebService::Braintree::WebhookNotification;
2             $WebService::Braintree::WebhookNotification::VERSION = '0.94';
3 20     20   396 use 5.010_001;
  20         75  
4 20     20   115 use strictures 1;
  20         129  
  20         761  
5              
6 20     20   1973 use WebService::Braintree::Util qw(is_hashref);
  20         44  
  20         1181  
7              
8             =head1 NAME
9              
10             WebService::Braintree::WebhookNotification
11              
12             =head1 PURPOSE
13              
14             This class parses and verifies webhook notifications.
15              
16             =head1 NOTES
17              
18             Unlike all other classes, this class does B<NOT> interact with a REST API.
19             Instead, this takes data you provide it and either parses it into a usable
20             object or provides a verification of it.
21              
22             =cut
23              
24 20     20   5046 use WebService::Braintree::WebhookNotification::Kind;
  20         55  
  20         519  
25              
26 20     20   108 use Moose;
  20         39  
  20         128  
27             extends 'WebService::Braintree::ResultObject';
28              
29             =head1 CLASS METHODS
30              
31             =head2 parse()
32              
33             This takes a signature and a payload and returns a parsing of the notification
34             within that payload. The payload is validated against the signature before
35             parsing.
36              
37             The return is an object of this class.
38              
39             =cut
40              
41             sub parse {
42 0     0 1   my ($class, $signature, $payload) = @_;
43 0           $class->gateway->webhook_notification->parse($signature, $payload);
44             }
45              
46             =head2 verify()
47              
48             This takes a challenge and returns a proper response.
49              
50             =cut
51              
52             sub verify {
53 0     0 1   my ($class, $challenge) = @_;
54 0           $class->gateway->webhook_notification->verify($challenge);
55             }
56              
57             sub gateway {
58 0     0 0   return WebService::Braintree->configuration->gateway;
59             }
60              
61             =head1 OBJECT METHODS
62              
63             In addition to the methods provided by the keys returned from Braintree, this
64             class provides the following methods:
65              
66             =head2 subscription()
67              
68             This returns the subscription associated with this notification (if any). This
69             will be an object of type L<WebService::Braintree::Subscription/>.
70              
71             =cut
72              
73             has subscription => (is => 'rw');
74              
75             =head2 merchant_account()
76              
77             This returns the merchant account associated with this notification (if any).
78             This will be an object of type L<WebService::Braintree::MerchantAccount/>.
79              
80             =cut
81              
82             has merchant_account => (is => 'rw');
83              
84             =head2 disbursement()
85              
86             This returns the disbursement associated with this notification (if any). This
87             will be an object of type L<WebService::Braintree::Disbursement/>.
88              
89             =cut
90              
91             has disbursement => (is => 'rw');
92              
93             =head2 transaction()
94              
95             This returns the stransaction associated with this notification (if any). This
96             will be an object of type L<WebService::Braintree::Transaction/>.
97              
98             =cut
99              
100             has transaction => (is => 'rw');
101              
102             =head2 partner_merchant()
103              
104             This returns the partner merchant associated with this notification (if any).
105             This will be an object of type L<WebService::Braintree::PartnerMerchant/>.
106              
107             =cut
108              
109             has partner_merchant => (is => 'rw');
110              
111             =head2 dispute()
112              
113             This returns the dispute associated with this notification (if any). This
114             will be an object of type L<WebService::Braintree::Dispute/>.
115              
116             =cut
117              
118             has dispute => (is => 'rw');
119              
120             =head2 errors()
121              
122             This returns the errors associated with this notification (if any). This
123             will be an object of type L<WebService::Braintree::ValidationErrorCollection/>.
124              
125             =cut
126              
127             has errors => (is => 'rw');
128              
129             =head2 message()
130              
131             This returns the message associated with this notification (if any). This will
132             be a string.
133              
134             =cut
135              
136             has message => (is => 'rw');
137              
138             sub BUILD {
139 0     0 0   my ($self, $attributes) = @_;
140              
141 0           my $wrapper_node = $attributes->{subject};
142              
143 0 0         if (is_hashref($wrapper_node->{api_error_response})) {
144 0           $wrapper_node = $wrapper_node->{api_error_response};
145             }
146              
147 0 0         if (is_hashref($wrapper_node->{subscription})) {
148 0           $self->subscription(WebService::Braintree::Subscription->new($wrapper_node->{subscription}));
149             }
150              
151 0 0         if (is_hashref($wrapper_node->{merchant_account})) {
152 0           $self->merchant_account(WebService::Braintree::MerchantAccount->new($wrapper_node->{merchant_account}));
153             }
154              
155 0 0         if (is_hashref($wrapper_node->{disbursement})) {
156 0           $self->disbursement(WebService::Braintree::Disbursement->new($wrapper_node->{disbursement}));
157             }
158              
159 0 0         if (is_hashref($wrapper_node->{transaction})) {
160 0           $self->transaction(WebService::Braintree::Transaction->new($wrapper_node->{transaction}));
161             }
162              
163 0 0         if (is_hashref($wrapper_node->{partner_merchant})) {
164 0           $self->partner_merchant(WebService::Braintree::PartnerMerchant->new($wrapper_node->{partner_merchant}));
165             }
166              
167 0 0         if (is_hashref($wrapper_node->{dispute})) {
168 0           $self->dispute(WebService::Braintree::Dispute->new($wrapper_node->{dispute}));
169             }
170              
171 0 0         if (is_hashref($wrapper_node->{errors})) {
172 0           $self->errors(WebService::Braintree::ValidationErrorCollection->new($wrapper_node->{errors}));
173 0           $self->message($wrapper_node->{message});
174             }
175              
176 0           delete($attributes->{subject});
177 0           $self->set_attributes_from_hash($self, $attributes);
178             }
179              
180             __PACKAGE__->meta->make_immutable;
181              
182             1;
183             __END__
184              
185             =head1 TODO
186              
187             =over 4
188              
189             =item Need to document the keys and values that are returned
190              
191             =item Need to document the required and optional input parameters
192              
193             =item Need to document the possible errors/exceptions
194              
195             =back
196              
197             =cut