File Coverage

blib/lib/Business/Giropay/Role/Request.pm
Criterion Covered Total %
statement 26 37 70.2
branch 2 6 33.3
condition n/a
subroutine 8 11 72.7
pod 2 2 100.0
total 38 56 67.8


line stmt bran cond sub pod time code
1             package Business::Giropay::Role::Request;
2              
3             =head1 NAME
4              
5             Business::Giropay::Role::Request - Moo::Role consumed by all Request classes
6              
7             =cut
8              
9 5     5   31115 use Business::Giropay::Types qw/Bool Enum HashRef Int Str/;
  5         8  
  5         35  
10 5     5   3322 use Carp;
  5         7  
  5         251  
11 5     5   1825 use Digest::HMAC_MD5 'hmac_md5_hex';
  5         5109  
  5         214  
12 5     5   3031 use HTTP::Tiny;
  5         174567  
  5         190  
13 5     5   28 use Module::Runtime 'use_module';
  5         8  
  5         32  
14              
15 5     5   234 use Moo::Role;
  5         5  
  5         39  
16             with 'Business::Giropay::Role::Core', 'Business::Giropay::Role::Network';
17              
18             requires qw(parameters uri);
19              
20             =head1 ATTRIBUTES
21              
22             See also L and
23             L.
24              
25             =head2 base_uri
26              
27             The base URI used for all requests. Defaults to:
28             C
29              
30             =cut
31              
32             has base_uri => (
33             is => 'ro',
34             isa => Str,
35             default => 'https://payment.girosolution.de/girocheckout/api/v2',
36             );
37              
38             =head2 data
39              
40             The constructed hash reference of parameters to be used in the POST request
41             to Giropay.
42              
43             =cut
44              
45             has data => (
46             is => 'lazy',
47             isa => HashRef,
48             init_arg => undef,
49             );
50              
51             sub _build_data {
52 0     0   0 my $self = shift;
53              
54             # we might need to cleanup some data if we are sandboxed
55 0 0       0 $self->sandbox_data if $self->sandbox;
56              
57 0         0 my $data = {
58             merchantId => $self->merchantId,
59             projectId => $self->projectId,
60             secret => $self->secret,
61             hash => $self->hash,
62             };
63 0         0 foreach my $parameter ( @{ $self->parameters } ) {
  0         0  
64 0 0       0 $data->{$parameter} = $self->$parameter if $self->$parameter;
65             }
66 0         0 return $data;
67             }
68              
69             =head2 hash
70              
71             The constructed HMAC hash of the constructed parameter values sent in the
72             request. This is built automatically.
73              
74             =cut
75              
76             has hash => (
77             is => 'lazy',
78             isa => Str, # Varchar[32]
79             init_arg => undef,
80             );
81              
82             sub _build_hash {
83 9     9   23080 my $self = shift;
84              
85 9         13 my @parameters;
86 9         11 foreach my $parameter ( @{ $self->parameters } ) {
  9         27  
87 59 100       117 push @parameters, $self->$parameter if $self->$parameter;
88             }
89 9         77 return hmac_md5_hex(
90             join( '', $self->merchantId, $self->projectId, @parameters ),
91             $self->secret );
92             }
93              
94             =head2 url
95              
96             The full URL for the request. This is built from L along with
97             the C attribute of the specific request class.
98              
99             =cut
100              
101             has url => (
102             is => 'lazy',
103             isa => Str,
104             init_arg => undef,
105             );
106              
107             sub _build_url {
108 9     9   5180 my $self = shift;
109 9         36 return join( '/', $self->base_uri, $self->uri );
110             }
111              
112             =head1 METHODS
113              
114             =head2 submit
115              
116             Submits the request to Giropay and returns an appropriate response object.
117              
118             =cut
119              
120             sub submit {
121 0     0 1   my $self = shift;
122 0           my $http = HTTP::Tiny->new( verify_SSL => 1 );
123 0           my $response = $http->post_form( $self->url, $self->data );
124              
125             return use_module( $self->response_class )->new(
126             network => $self->network,
127             hash => $response->{headers}->{hash},
128             json => $response->{content},
129 0           secret => $self->secret,
130             );
131             }
132              
133             =head2 sandbox_data
134              
135             Individual Request class can override this method to massage data when
136             L is true.
137              
138             =cut
139              
140       0 1   sub sandbox_data {}
141              
142             1;