File Coverage

blib/lib/WWW/ModulrFinance.pm
Criterion Covered Total %
statement 26 66 39.3
branch 0 12 0.0
condition 0 5 0.0
subroutine 9 22 40.9
pod 10 12 83.3
total 45 117 38.4


line stmt bran cond sub pod time code
1             package WWW::ModulrFinance;
2              
3 1     1   16751 use strict;
  1         1  
  1         31  
4 1     1   26 use 5.008_005;
  1         3  
5             our $VERSION = '0.01';
6              
7 1     1   693 use LWP::UserAgent;
  1         40955  
  1         34  
8 1     1   7 use HTTP::Date qw/time2str/;
  1         1  
  1         67  
9 1     1   442 use Digest::HMAC_SHA1 qw(hmac_sha1);
  1         4938  
  1         47  
10 1     1   506 use MIME::Base64;
  1         628  
  1         58  
11 1     1   6 use URI::Escape qw/uri_escape/;
  1         1  
  1         48  
12 1     1   4 use Carp qw/croak/;
  1         1  
  1         41  
13 1     1   702 use JSON;
  1         10003  
  1         5  
14              
15             sub new {
16 0     0 0   my $class = shift;
17 0 0         my %params = @_ % 2 ? %{$_[0]} : @_;
  0            
18              
19 0 0         $params{api_key} or croak "api_key is required";
20 0 0         $params{hmac_secret} or croak "hmac_secret is required";
21              
22 0   0       $params{base_url} ||= 'https://api-sandbox.modulrfinance.com/api-sandbox/';
23 0   0       $params{ua} ||= LWP::UserAgent->new(agent => "WWW-ModulrFinance-$VERSION");
24              
25 0           return bless \%params, $class;
26             }
27              
28             sub get_accounts {
29 0     0 1   (shift)->request('GET', 'accounts');
30             }
31              
32             sub get_account {
33 0     0 1   my ($self, $id) = @_;
34 0           return $self->request('GET', 'accounts/' . int($id));
35             }
36              
37             sub update_account {
38 0     0 1   my ($self, $id, $data) = @_;
39 0           return $self->request('PUT', 'accounts/' . int($id), $data);
40             }
41              
42             sub get_customer_accounts {
43 0     0 1   my ($self, $cid) = @_;
44 0           return $self->request('GET', 'customers/' . int($cid) . '/accounts');
45             }
46              
47             sub create_customer_account {
48 0     0 1   my ($self, $cid, $data) = @_;
49 0           return $self->request('POST', 'customers/' . int($cid) . '/accounts', $data);
50             }
51              
52             sub get_transactions {
53 0     0 1   my ($self, $id) = @_;
54 0           return $self->request('GET', 'accounts/' . int($id) . '/transactions');
55             }
56              
57             sub get_payments {
58 0     0 1   (shift)->request('GET', 'payments');
59             }
60              
61             sub post_payments {
62 0     0 1   my ($self, $data) = @_;
63 0           return $self->request('POST', 'payments', $data);
64             }
65              
66             sub post_batchpayments {
67 0     0 1   my ($self, $data) = @_;
68 0           return $self->request('POST', 'batchpayments', $data);
69             }
70              
71             sub get_batchpayment {
72 0     0 1   my ($self, $id) = @_;
73 0           return $self->request('GET', 'batchpayments/' . int($id));
74             }
75              
76             sub request {
77 0     0 0   my ($self, $method, $uri, $data) = @_;
78              
79 0           my $req = HTTP::Request->new($method => $self->{base_url} . $uri => $self->__signature());
80 0 0         $req->content(encode_json($data)) if $data;
81              
82             # print Dumper(\$req); use Data::Dumper;
83              
84 0           my $res = $self->{ua}->request($req);
85 0 0         if ($res->header('Content-Type') =~ 'json') {
86 0           return decode_json($res->content);
87             }
88              
89 0 0         croak $res->status_line unless $res->is_success;
90 0           return $res->content;
91             }
92              
93             sub __signature {
94 0     0     my ($self) = @_;
95              
96 0           my $date = time2str(time());
97 0           my $nonce = time() . '-' . $$ . '-' . rand(100000);
98 0           my $str = "date: $date\nx-mod-nonce: $nonce";
99 0           my $sig = uri_escape(encode_base64(hmac_sha1($str, $self->{hmac_secret}), ''));
100              
101             # Authorization: Signature keyId="57502612d1bb2c0001000025fd53850cd9a94861507a5f7cca236882",algorithm="hmac-sha1",headers="date x-mod-nonce",signature="WBMr%2FYdhysbmiIEkdTrf2hP7SfA%3D"
102             return [
103             'Date' => $date,
104             'x-mod-nonce' => $nonce,
105 0           'Authorization' => qq~Signature keyId="~ . $self->{api_key} . qq~",algorithm="hmac-sha1",headers="date x-mod-nonce",signature="$sig"~
106             ];
107             }
108              
109             1;
110             __END__