| 1 | package Business::Bitpay; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use 5.008_001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | our $VERSION = '0.05'; | |||||||||
| 8 | 2 | eval $VERSION; | |||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use JSON qw(encode_json decode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | require Carp; | |||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 17 | 1 | ▶ | my ($class, $api_key, @args) = @_ or Carp::croak("api key missed"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | bless { | |||||||||
| 20 | key => $api_key, | ||||||||||
| 21 | gateway => 'https://bitpay.com/api/', | ||||||||||
| 22 | ua => LWP::UserAgent->new, | ||||||||||
| 23 | @args | ||||||||||
| 24 | }, $class; | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | sub prepare_request { | ||||||||
|
Subroutine
prepare_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 28 | 2 | my ($self, $api, $data) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 2 | my $uri = URI->new($self->{gateway}); | |||||||||
| 31 | 2 | $uri->userinfo($self->{key} . ':'); | |||||||||
| 32 | 2 | $uri->path($uri->path . $api); | |||||||||
| 33 | |||||||||||
| 34 | 2 | my $method = 'GET'; | |||||||||
| 35 | 2 | my @fields; | |||||||||
| 36 | 2 | ▶ | if ($data) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1 | $method = 'POST'; | |||||||||
| 38 | 1 | $data = encode_json $data; | |||||||||
| 39 | 1 | push @fields, 'Content-Type' => 'application/json'; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 2 | my $request = HTTP::Request->new( | |||||||||
| 43 | $method => $uri, [ | ||||||||||
| 44 | 'User-Agent' => 'bitpay api', | ||||||||||
| 45 | 'X-BitPay-Plugin-Info' => 'perl' . $VERSION, | ||||||||||
| 46 | @fields, | ||||||||||
| 47 | ], | ||||||||||
| 48 | $data | ||||||||||
| 49 | ); | ||||||||||
| 50 | 2 | $request; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub request { | ||||||||
|
Subroutine
request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 54 | 0 | my $self = shift; | |||||||||
| 55 | |||||||||||
| 56 | 0 | my $http_response = $self->{ua}->request($self->prepare_request(@_)); | |||||||||
| 57 | 0 | ▶ | Carp::croak($http_response->status_line) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | unless $http_response->is_success; | ||||||||||
| 59 | |||||||||||
| 60 | 0 | my $response = decode_json($http_response->decoded_content); | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | if (my $error = $response->{error}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | my $messages = $error->{messages}; | |||||||||
| 64 | Carp::croak("$error->{message}: ", | ||||||||||
| 65 | 0 | join(', ', map {"$_ ($messages->{$_})"} keys %$messages)); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | $response; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub create_invoice { | ||||||||
|
Subroutine
create_invoice: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 73 | 0 | my ($self, %args) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | Carp::croak('price missed') unless exists $args{price}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | ▶ | Carp::croak('currency missed') unless exists $args{currency}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | 0 | $self->request('invoice', \%args); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub get_invoice { | ||||||||
|
Subroutine
get_invoice: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 0 | my ($self, $id) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 0 | $self->request("invoice/$id"); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 2 | 1; | |||||||||
| 88 | __END__ | ||||||||||