Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
Examples/Tutorial, price checking
Browse files Browse the repository at this point in the history
- Upgraded examples to show how to interact with the config.yml file and encrypted file storage
- checkPriceAndCurrency will now allow prices that do not have fractions of main units (e.g. $1000)
  • Loading branch information
Chris Kleeschulte committed Mar 11, 2016
1 parent c7b8263 commit 0e14400
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
14 changes: 2 additions & 12 deletions examples/CreateInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,7 @@
* Create a new client. You can see the example of how to configure this using
* a yml file as well.
*/
$bitpay = new \Bitpay\Bitpay(
array(
'bitpay' => array(
'network' => 'testnet', // testnet or livenet, default is livenet
'public_key' => getenv('HOME').'/.bitpay/api.pub',
'private_key' => getenv('HOME').'/.bitpay/api.key',
'key_storage' => 'Bitpay\Storage\EncryptedFilesystemStorage',
'key_storage_password' => 'TopSecret',
)
)
);
$bitpay = new \Bitpay\Bitpay(__DIR__ . '/config.yml');

/**
* Create the client that will be used to send requests to BitPay's API
Expand All @@ -91,7 +81,7 @@
* keys.
*/
$token = new \Bitpay\Token();
$token->setToken('Put your token here');
$token->setToken('your token here');

$client->setToken($token);

Expand Down
2 changes: 1 addition & 1 deletion examples/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require __DIR__ . '/../vendor/autoload.php';

$bitpay = new \Bitpay\Bitpay();
$bitpay = new \Bitpay\Bitpay(__DIR__ . '/config.yml');
$client = $bitpay->get('client');
$currencies = $client->getCurrencies();

Expand Down
7 changes: 4 additions & 3 deletions examples/GetTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
array(
'bitpay' => array(
'network' => 'testnet', // testnet or livenet, default is livenet
'public_key' => getenv('HOME').'/.bitpayphp/api.pub',
'private_key' => getenv('HOME').'/.bitpayphp/api.key',
'key_storage' => 'Bitpay\Storage\FilesystemStorage',
'public_key' => '/tmp/bitpay.pub', //see tutorial/001.php and 002.php
'private_key' => '/tmp/bitpay.pri',
'key_storage' => 'Bitpay\Storage\EncryptedFilesystemStorage',
'key_storage_password' => 'YourTopSecretPassword'
)
)
);
Expand Down
5 changes: 3 additions & 2 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#
bitpay:
network: testnet
adapter: mock
key_storage_password: satoshi
public_key: /tmp/bitpay.pub
private_key: /tmp/bitpay.pri
key_storage_password: YourTopSecretPassword
5 changes: 4 additions & 1 deletion examples/tutorial/004.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
* comment/uncomment.
*/
$pairingCode = 'YCBrKpr';
$tokenString = 'S9s6ZHAV4xzCjjnW4NXLjK';
$tokenString = 'realtokengoeshere';
$privateKeyPath = '/tmp/bitpay.pri';
$publicKeyPath = '/tmp/bitpay.pub';
$keyStoragePassword = 'YourTopSecretPassword';
/*** end options ***/

/**
Expand All @@ -42,6 +43,7 @@
'network' => 'testnet', // Valid values are testnet/livenet
'public_key' => $publicKeyPath,
'private_key' => $privateKeyPath,
'key_storage_password' => $keyStoragePassword,
)
)
);
Expand Down Expand Up @@ -97,6 +99,7 @@
->setDescription('General Description of Item')
->setPrice('1.99');
$invoice->setCurrency(new \Bitpay\Currency('USD'));
$invoice->setItem($item);
$client = $bitpay->get('client');
$client->setToken($token);
try {
Expand Down
12 changes: 8 additions & 4 deletions src/Bitpay/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,14 @@ protected function prepareRequestHeaders(RequestInterface $request)

protected function checkPriceAndCurrency($price, $currency)
{
//get the decimal precision of the price
$decimalPrecision = strlen(substr($price, strpos($price, '.')+1));
if (($decimalPrecision > 2 && $currency != "BTC") || $decimalPrecision > 6) {
throw new ArgumentException("Incorrect price format");
$decimalPosition = strpos($price, '.');
if ($decimalPosition == 0) {
$decimalPrecision = 0;
} else {
$decimalPrecision = strlen(substr($price, $decimalPosition + 1));
}
if (($decimalPrecision > 2 && $currency != 'BTC') || $decimalPrecision > 6) {
throw new \Exception('Incorrect price format or currency type.');
}
}
}
34 changes: 33 additions & 1 deletion tests/Bitpay/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

namespace Bitpay\Client;

date_default_timezone_set('UTC');

class ChildOfClient extends Client
{
public function checkPriceAndCurrency($price, $currency) {
return parent::checkPriceAndCurrency($price, $currency);
}
}

class ClientTest extends \PHPUnit_Framework_TestCase
{
protected $client;
Expand All @@ -22,6 +31,29 @@ public function setUp()
$this->client->setAdapter($adapter);
}

public function testCheckPriceAndCurrency() {
$client = new ChildOfClient();
$res = $client->checkPriceAndCurrency(.999999, 'BTC');
$this->assertNull($res);
$res = $client->checkPriceAndCurrency(1000, 'USD');
$this->assertNull($res);
$res = $client->checkPriceAndCurrency(0, 'USD');
$this->assertNull($res);
$res = $client->checkPriceAndCurrency(.01, 'USD');
$this->assertNull($res);
$res = $client->checkPriceAndCurrency(99, 'USD');
$this->assertNull($res);
$res = $client->checkPriceAndCurrency(100.9, 'USD');
$this->assertNull($res);
}

/**
* @expectedException \Exception
*/
public function testCheckPriceAndCurrencyWithException() {
$client = new ChildOfClient();
$res = $client->checkPriceAndCurrency(.991, 'ABC');
}

/**
* @expectedException \Exception
Expand Down Expand Up @@ -275,7 +307,7 @@ public function testCreateInvoiceWithTooMuchPrecisionEvenForBitcoin()

$this->client->createInvoice($invoice);
}

/**
* @depends testCreateInvoice
*/
Expand Down

0 comments on commit 0e14400

Please sign in to comment.