Business-OnlinePayment-BitPay-Client
view release on metacpan or search on metacpan
my $method = $request->method;
if ($uri->path eq '/postthis') {
}
return do {
if ($method eq "POST") {
if( $uri->path eq '/postthis' && $decoded->{"token"} && $decoded->{"facade"} ) {
[
200,
[ 'Content-Type' => 'application/json' ],
[ '{"facade":"correct", "token":"correct"}']
]
} elsif( $decoded->{"pairingCode"} && $uri->path eq '/tokens' ) {
[
200,
[ 'Content-Type' => 'application/json' ],
[ $paircodeResponse ]
]
} elsif( $uri->path eq '/tokens' ){
[
200,
[ 'Content-Type' => 'application/json' ],
[ $tokensResponse ]
]
} elsif( $uri->path eq '/invoices' ){
[
200,
[ 'Content-Type' => 'application/json' ],
[ $invoiceResponse ]
]
} else {
[
500,
[ 'Content-Type' => 'application/json' ],
[ '{"error":"something has certainly gone wrong"}' ]
]
}
} elsif ($method eq "GET") {
if( $uri->path eq '/tokens' ){
[
200,
[ 'Content-Type' => 'application/json' ],
[ $getTokens ]
]
} elsif ($uri->path eq '/getthis') {
[
200,
[ 'Content-Type' => 'application/json' ],
[ '{"hello":"world"}']
]
} else {
[
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.535 second using v1.01-cache-2.11-cpan-97f6503c9c8 )