blib/lib/Business/OnlinePayment/BitPay/Client.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 244 CC 16 SCAR 20.5
CC
16
Cov
0%
validate_price
20.0
create_invoice
12.0
get
12.0
CRAP 7.7
1 package Business::OnlinePayment::BitPay::Client;
2 $VERSION = '2.3.1';
3  
4 use Carp;
5 use warnings;
6 use Mozilla::CA;
7 use LWP::UserAgent;
8 use HTTP::Request;
9 use LWP::Protocol::https;
10 use Business::OnlinePayment::BitPay::KeyUtils;
11 use JSON;
12 use JSON::Parse 'parse_json';
13 require IO::Socket::SSL;
14 use Net::SSLeay;
15 use Data::Dumper;
16  
17 sub new {
18 my $class = shift;
19 my %opts = @_;
20 my $pem = $opts{"pem"}
21 or croak "no pem passed to constructor";
22 my $apiUri = "https://bitpay.com";
23 my $id = Business::OnlinePayment::BitPay::KeyUtils::bpGenerateSinFromPem($pem);
24 $apiUri = $opts{"apiUri"} if exists $opts{"apiUri"};
25 return bless({pem => $pem, apiUri => $apiUri, id => $id}, $class);
26 }
27  
28 sub pair_client{
29 my $self = shift;
30 my %opts = @_;
31 if($opts{"pairingCode"}){
32 $code = $opts{"pairingCode"};
33 pair_pos_client($self, $code);
34 } else {
35 pair_with_facade($self, %opts);
36 }
37 }
38  
39 sub create_invoice{
40 my $self = shift;
41 my %opts = @_;
42 my $price = $opts{'price'};
43 my $currency = $opts{'currency'};
44 croak "BitPay Error: Price must be formatted as a float" unless validate_price($price, $currency);
45 croak "BitPay Error: Currency is invalid" unless validate_currency($currency);
46 my $content = {};
47 $content->{'price'} = $price;
48 $content->{'currency'} = $currency;
49 my $id = $self->{id};
50 $content->{'id'} = $id;
51 $token = retrieve_pos_or_merchant_token_from_server($self);
52 $content->{'token'} = $token;
53 $response = post($self, path => "invoices", params => $content);
54 my $data = parse_json($response->content)->{'data'};
55 return %{$data};
56 }
57  
58 sub get_invoice{
59 my $self = shift;
60 my %opts = @_;
61 my $id = $opts{'id'};
62 my $path = "invoices/" . $id;
63 my $public = $opts{'public'};
64 my $response = get($self, path => $path, public => $public);
65 my $data = parse_json($response->content)->{'data'};
66 return %{$data};
67 }
68  
69 sub get{
70 my $self = shift;
71 my %opts = @_;
72 my $path = $opts{"path"};
73 my $public = $opts{"public"};
74 my $uri = $self->{apiUri} or croak "no api_uri exists for object";
75 $uri = $uri . "/" . $path;
76 my $request = HTTP::Request->new(GET => $uri);
77 $request->content($jsonc);
78 $request->header('content-type' => 'application/json');
79 $request->header('X-BitPay-Plugin-Info' => 'PerlLib');
80 $request->header('User-Agent' => 'perl-bitpay-client');
81 unless($public){
82 my $signature = Business::OnlinePayment::BitPay::KeyUtils::bpSignMessageWithPem($self->{pem}, $uri);
83 my $pubkey = Business::OnlinePayment::BitPay::KeyUtils::bpGetPublicKeyFromPem($self->{pem});
84 $request->header('X-Signature' => $signature, 'X-Identity' => $pubkey);
85 }
86 my $ua = LWP::UserAgent->new;
87 $ua->ssl_opts( verify_hostname=> 0, SSL_ca_file => Mozilla::CA::SSL_ca_file(), SSL_Version => 'TLSv2', SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE);
88 my $response = $ua->request($request);
89 return $response if $response->is_success;
90 my $code = $response->code;
91 my $error = decode_json($response->content)->{'error'};
92 croak "$code: $error";
93 }
94  
95 sub post{
96 my $self = shift;
97 my %opts = @_;
98 my $path = $opts{"path"};
99 my %content = %{%opts->{"params"}};
100 my $uri = $self->{apiUri} or croak "no api_uri exists for object";
101 $uri = $uri . "/" . $path;
102 my $request = HTTP::Request->new(POST => $uri);
103 my $ua = LWP::UserAgent->new;
104 $ua->ssl_opts( verify_hostname=> 0, SSL_ca_file => Mozilla::CA::SSL_ca_file(), SSL_Version => 'TLSv2', SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE);
105 my $jsonc = encode_json \%content;
106 $request->content($jsonc);
107 $request->header('content-type' => 'application/json');
108 $request->header('X-BitPay-Plugin-Info' => 'PerlLib');
109 $request->header('User-Agent' => 'perl-bitpay-client');
110 if ($content{"token"}){
111 my $signature = Business::OnlinePayment::BitPay::KeyUtils::bpSignMessageWithPem($self->{pem}, $uri . $jsonc);
112 my $pubkey = Business::OnlinePayment::BitPay::KeyUtils::bpGetPublicKeyFromPem($self->{pem});
113 $request->header('X-Signature' => $signature, 'X-Identity' => $pubkey);
114 }
115 my $response = $ua->request($request);
116 return $response if $response->is_success;
117 my $code = $response->code;
118 my $error = decode_json($response->content)->{'error'};
119 croak "$code: $error";
120 }
121  
122 sub process_response{
123 my $response = @_[1];
124 my @data = decode_json($response->content)->{'data'};
125 return @data;
126 }
127  
128 sub pair_pos_client{
129 my $self = shift;
130 $code = $_[0];
131 croak "BitPay Error: Pairing Code is not legal" unless $code =~ /^\w{7}$/;
132 my $id = $self->{'id'};
133 my $content = {pairingCode => $code, id => $id};
134 my $response = post($self, path => "tokens", params => $content);
135 my @data = parse_json($response->content)->{'data'};
136 return @data;
137 }
138  
139 sub pair_with_facade{
140 my $self = shift;
141 my %opts = @_;
142 my $content = {};
143 $content->{'facade'} = %opts{'facade'};
144 my $id = $self->{id};
145 $content->{'id'} = $id;
146 $response = post($self, path => "tokens", params => $content);
147 my @data = parse_json($response->content)->{'data'};
148 return @data;
149 }
150  
151 sub validate_price{
152 my $price = $_[0];
153 my $currency = $_[1];
154 if($currency eq "BTC"){
155 return 0 unless $price =~ /^\d+(\.\d{1,8})?$/;
156 } else {
157 return 0 unless $price =~ /^\d+(\.\d{2})?$/;
158 };
159 return 1;
160 }
161  
162 sub validate_currency{
163 my $currency = $_[0];
164 return 0 unless $currency =~/^[A-Z]{3}$/;
165 return 1;
166 }
167  
168 sub retrieve_pos_or_merchant_token_from_server{
169 my $token;
170 my $self = shift;
171 my $response = $self->get(path => "tokens");
172 my @data = $self->process_response($response);
173 for my $mapp (values @data[0]){
174 for my $key (keys %$mapp) {
175 if ($key eq "merchant" or $key eq "pos"){
176 $token = %$mapp{$key};
177 break;
178 }
179 }
180 }
181 return $token if $token;
182 croak "BitPay Error: No tokens on server";
183 }
184 1;