Business-OnlinePayment-BitPay-Client
view release on metacpan or search on metacpan
1. A token shared between the client and the server.
1. A public key, shared between the client and the server.
1. A private key, held exclusively by the client.
In order to complete authentication, you have to associate your private key with a token, and associate that token with an account. Once this authentication is complete, as long as you have the private key, you never have to authenticate again. The t...
There are two ways to authenticate, from the client side or the server side. The Perl Client supports both.
To pair from the server side, you log in to the BitPay server, navigate to dashboard/merchant/api-tokens, and create a new token. This creates a new token, which is associated with your account. It is not associated with a key, so we provide a pairin...
To pair from the client side, you use the client to call the /tokens endpoint on the server with no parameters, or use the `client->pair_client(facade => < 'pos' | 'merchant' > )` subroutine. This creates a token on the server and associates that tok...
```perl
my $pem = BitPay::KeyUtils::bpGeneratePem(); # remember, this must be saved in order to use it again!
my $uri = "https://test.bitpay.com" # or https://bitpay.com for live use
my %options = ("pem" => $pem, "apiUri" => $uri);
my $client = Business::OnlinePayment::BitPay::Client->new(%options);
my @data = $client->pair_client(facade => 'pos');
$pairingCode = shift(shift(@data))->{'pairingCode'};
print $pairingCode;
```
The perl library provides a method for fetching an existing invoice:
```perl
$client->get_invoice(id => 'PvVhgBfA7wKPWhuVC24rJo', public => 1);
```
The "public" argument allows non-authenticated clients to retrieve invoices. This cannot presently be omitted but this will change with the introduction of refunds in v2.4.
### Make a HTTP request directly against the REST API
For API tasks which lack a dedicated library method, either the `client-get()` or `client->post()` subroutines will automatically apply the proper cryptographic parameters to a request. The post method is looking for an endpoint and a hash of params ...
## Testnet Usage
During development and testing, take advantage of the [Bitcoin TestNet](https://en.bitcoin.it/wiki/Testnet) by passing the testnet URI when creating a client:
```perl
my $uri = "https://test.bitpay.com" # or https://bitpay.com for live use
my %options = ("pem" => $pem, "apiUri" => $uri);
my $client = Business::OnlinePayment::BitPay::Client->new(%options);
```
[
500,
[ 'Content-Type' => 'application/json' ],
[ '{"error":"something has certainly gone wrong"}' ]
]
}
}
}
};
my $uri = URI->new( $httpd->endpoint );
my $string = "astring";
my $pem = Business::OnlinePayment::BitPay::KeyUtils::bpGeneratePem();
my %opt = ("pem" => $pem);
$opt{"apiUri"} = $uri;
use_ok('Business::OnlinePayment::BitPay::Client');
ok(Business::OnlinePayment::BitPay::Client->new(%opt), "accept new with pem passed");
throws_ok(sub { Business::OnlinePayment::BitPay::Client->new() }, qr/no pem passed to constructor/, "catches no pem passed to constructor");
my $client = Business::OnlinePayment::BitPay::Client->new(%opt);
my @response = $client->pair_client;
my $pairing = shift(shift(@response))->{'pairingCode'};
is($pairing, "Hgi0Tys", "retrieves token data from endpoint");
my @response = $client->pair_client(pairingCode => "abcDeF7");
my $facade = shift(shift(@response))->{'facade'};
is($facade, "pos", "connects pairing code");
@response = $client->pair_pos_client("abcDeF7");
$facade = shift(shift(@response))->{'facade'};
is($facade, "pos", "connects pairing code");
my $response = $client->post(path => "tokens", params => {});
my @data = $client->process_response($response);
my $code = shift(shift(@data))->{'pairingCode'};
is($code, "Hgi0Tys", "processes response to data array");
my $params = {token => "thisisatoken", facade => "pos"};
my $response = $client->post(path => "postthis", params => $params);
is($response->content, '{"facade":"correct", "token":"correct"}', "post formatted correctly");
$client->pair_client(facade => "pos");
throws_ok(sub { $client->pair_pos_client("abc2") }, qr/Pairing Code is not legal/, "catches incorrect pairing code");
throws_ok(sub { $client->pair_pos_client("abc2eFGG") }, qr/Pairing Code is not legal/, "catches incorrect pairing code");
throws_ok(sub { $client->post(path => "badendpoint", params => {}) }, qr/500: something has certainly gone wrong/, "passes along server errrors");
throws_ok(sub { $client->create_invoice(price => 10, currency => "ASDF") }, qr/BitPay Error: Currency is invalid/, "checks for valid currency");
throws_ok(sub { $client->create_invoice(price => "ten", currency => "ASD") }, qr/BitPay Error: Price must be formatted as a float/, "checks for valid price");
throws_ok(sub { $client->get_invoice(id => 12345) }, qr/500: something has certainly gone wrong/, "catches server errors on get requests");
ok( $client->create_invoice(price => 10, currency => "USD", params => {}), "accepts valid invoice parameters");
my %invoice = $client->create_invoice(price => 10, currency => "USD", params => {});
is(%invoice->{'rate'}, 283.17, "create invoice calls endpoint and processes response");
}
( run in 0.234 second using v1.01-cache-2.11-cpan-27979f6cc8f )