File Coverage

blib/lib/Business/Giropay/Role/Response.pm
Criterion Covered Total %
statement 22 22 100.0
branch 4 4 100.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             package Business::Giropay::Role::Response;
2              
3             =head1 NAME
4              
5             Business::Giropay::Role::Response - Moo::Role consumed by all Response classes
6              
7             =cut
8              
9 3     3   19937 use Carp;
  3         5  
  3         187  
10 3     3   1169 use Digest::HMAC_MD5 'hmac_md5_hex';
  3         3056  
  3         129  
11 3     3   13 use Business::Giropay::Types qw/Bool HashRef Int Maybe Str/;
  3         3  
  3         23  
12 3     3   3186 use JSON::MaybeXS;
  3         12748  
  3         145  
13 3     3   17 use Moo::Role;
  3         5  
  3         21  
14             with 'Business::Giropay::Role::Network';
15              
16             =head1 ATTRIBUTES
17              
18             =head2 json
19              
20             The json message data returned from giropay. Required.
21              
22             =cut
23              
24             has json => (
25             is => 'ro',
26             isa => Str,
27             required => 1,
28             );
29              
30             =head2 data
31              
32             L data converted to a hash reference.
33              
34             =cut
35              
36             has data => (
37             is => 'lazy',
38             isa => HashRef,
39             init_arg => undef,
40             );
41              
42             sub _build_data {
43 5     5   1512 return decode_json( shift->json );
44             }
45              
46             =head2 rc
47              
48             Response code / error number.
49              
50             =cut
51              
52             has rc => (
53             is => 'lazy',
54             isa => Int,
55             init_arg => undef,
56             );
57              
58             sub _build_rc {
59 5     5   1497 shift->data->{rc};
60             }
61              
62             =head2 msg
63              
64             Additional information on error (possibly empty).
65              
66             =cut
67              
68             has msg => (
69             is => 'lazy',
70             isa => Maybe [Str],
71             init_arg => undef,
72             );
73              
74             sub _build_msg {
75 5     5   3250 shift->data->{msg};
76             }
77              
78             =head2 hash
79              
80             The HMAC hash of the returned message. Required.
81              
82             =cut
83              
84             has hash => (
85             is => 'ro',
86             isa => Str,
87             required => 1,
88             );
89              
90             =head2 secret
91              
92             The Giropay shared secret for current C and C,
93              
94             =cut
95              
96             has secret => (
97             is => 'ro',
98             isa => Str,
99             required => 1,
100             );
101              
102             =head2 success
103              
104             Boolean response indicating whether L indicates success.
105              
106             =cut
107              
108             has success => (
109             is => 'lazy',
110             isa => Bool,
111             init_arg => undef,
112             );
113              
114             sub _build_success {
115 5 100   5   3085 return shift->rc == 0 ? 1 : 0;
116             }
117              
118             =head1 METHODS
119              
120             =head2 BUILD
121              
122             Check that the hash matches what we expect. Die on mismatch
123              
124             =cut
125              
126             sub BUILD {
127 8     8 1 14635 my $self = shift;
128              
129 8         47 my $verify = hmac_md5_hex( $self->json, $self->secret );
130              
131 8 100       320 croak(
132             "Returned HMAC hash ", $self->hash,
133             " does not match expected hash ", $verify, " for json ", $self->json
134             ) unless $verify eq $self->hash;
135             }
136              
137             1;