This introduces new steps to check the format of price
check format of price to insure that it "looks like" a float coerce price to a float check precision of price against currency
This commit is contained in:
parent
9027ce67e4
commit
397db094c2
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@ -0,0 +1,9 @@
|
||||
root = true
|
||||
|
||||
[*.py]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.php]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
@ -122,6 +122,8 @@ class Client implements ClientInterface
|
||||
$buyer = $invoice->getBuyer();
|
||||
$buyerAddress = $buyer->getAddress();
|
||||
|
||||
$this->checkPriceAndCurrency($item->getPrice(), $currency->getCode());
|
||||
|
||||
$body = array(
|
||||
'price' => $item->getPrice(),
|
||||
'currency' => $currency->getCode(),
|
||||
@ -671,4 +673,14 @@ class Client implements ClientInterface
|
||||
$request->setHeader('Content-Type', 'application/json');
|
||||
$request->setHeader('X-Accept-Version', '2.0.0');
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -101,9 +101,10 @@ class Item implements ItemInterface
|
||||
*/
|
||||
public function setPrice($price)
|
||||
{
|
||||
if (1 !== preg_match('/^[0-9]+(?:\.[0-9]{1,2})?$/', $price)) {
|
||||
throw new \Bitpay\Client\ArgumentException("Price must be formatted as a float");
|
||||
if(is_string($price)){
|
||||
$this->checkPriceFormat($price);
|
||||
}
|
||||
$price = (float)$price;
|
||||
$this->price = $price;
|
||||
|
||||
return $this;
|
||||
@ -148,4 +149,11 @@ class Item implements ItemInterface
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function checkPriceFormat($price)
|
||||
{
|
||||
if (1 !== preg_match('/^[0-9]+?[\.,][0-9]{1,6}?$/', $price)) {
|
||||
throw new \Bitpay\Client\ArgumentException("Price must be formatted as a float");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -210,6 +210,71 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->client->createInvoice($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testCreateInvoiceWithTooMuchPrecisionForAnythingButBitcoin()
|
||||
{
|
||||
$item = $this->getMockItem();
|
||||
$currency = $this->getMockCurrency();
|
||||
$currency->method('getCode')->will($this->returnValue("USD"));
|
||||
$item->method('getPrice')->will($this->returnValue(9.999));
|
||||
|
||||
$buyer = $this->getMockBuyer();
|
||||
$buyer->method('getAddress')->will($this->returnValue(array()));
|
||||
|
||||
$invoice = $this->getMockInvoice();
|
||||
$invoice->method('getItem')->willReturn($item);
|
||||
$invoice->method('getBuyer')->willReturn($buyer);
|
||||
$invoice->method('setId')->will($this->returnSelf());
|
||||
$invoice->method('setUrl')->will($this->returnSelf());
|
||||
$invoice->method('setStatus')->will($this->returnSelf());
|
||||
$invoice->method('setBtcPrice')->will($this->returnSelf());
|
||||
$invoice->method('setPrice')->will($this->returnSelf());
|
||||
$invoice->method('setInvoiceTime')->will($this->returnSelf());
|
||||
$invoice->method('setExpirationTime')->will($this->returnSelf());
|
||||
$invoice->method('setCurrentTime')->will($this->returnSelf());
|
||||
$invoice->method('setBtcPaid')->will($this->returnSelf());
|
||||
$invoice->method('setRate')->will($this->returnSelf());
|
||||
$invoice->method('setExceptionStatus')->will($this->returnSelf());
|
||||
$invoice->method('getCurrency')->willReturn($currency);
|
||||
|
||||
$this->client->createInvoice($invoice);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testCreateInvoiceWithTooMuchPrecisionEvenForBitcoin()
|
||||
{
|
||||
$item = $this->getMockItem();
|
||||
$currency = $this->getMockCurrency();
|
||||
$currency->method('getCode')->will($this->returnValue("BTC"));
|
||||
$item->method('getPrice')->will($this->returnValue(.9999999));
|
||||
|
||||
$buyer = $this->getMockBuyer();
|
||||
$buyer->method('getAddress')->will($this->returnValue(array()));
|
||||
|
||||
$invoice = $this->getMockInvoice();
|
||||
$invoice->method('getItem')->willReturn($item);
|
||||
$invoice->method('getBuyer')->willReturn($buyer);
|
||||
$invoice->method('setId')->will($this->returnSelf());
|
||||
$invoice->method('setUrl')->will($this->returnSelf());
|
||||
$invoice->method('setStatus')->will($this->returnSelf());
|
||||
$invoice->method('setBtcPrice')->will($this->returnSelf());
|
||||
$invoice->method('setPrice')->will($this->returnSelf());
|
||||
$invoice->method('setInvoiceTime')->will($this->returnSelf());
|
||||
$invoice->method('setExpirationTime')->will($this->returnSelf());
|
||||
$invoice->method('setCurrentTime')->will($this->returnSelf());
|
||||
$invoice->method('setBtcPaid')->will($this->returnSelf());
|
||||
$invoice->method('setRate')->will($this->returnSelf());
|
||||
$invoice->method('setExceptionStatus')->will($this->returnSelf());
|
||||
$invoice->method('getCurrency')->willReturn($currency);
|
||||
|
||||
$this->client->createInvoice($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreateInvoice
|
||||
*/
|
||||
|
||||
@ -65,8 +65,25 @@ class ItemTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->item->setPrice("9.99");
|
||||
$this->assertNotNull($this->item->getPrice());
|
||||
$this->assertSame("9.99", $this->item->getPrice());
|
||||
$this->assertInternalType('string', $this->item->getPrice());
|
||||
$this->assertSame(9.99, $this->item->getPrice());
|
||||
$this->assertInternalType('float', $this->item->getPrice());
|
||||
|
||||
$this->item->setPrice("0.999999");
|
||||
$this->assertNotNull($this->item->getPrice());
|
||||
$this->assertSame(.999999, $this->item->getPrice());
|
||||
$this->assertInternalType('float', $this->item->getPrice());
|
||||
|
||||
// if the argument is a string
|
||||
// and the string is not "floatlike"
|
||||
// throw an error
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Bitpay\Client\ArgumentException
|
||||
*/
|
||||
public function testBadStringPrice()
|
||||
{
|
||||
$this->item->setPrice("I'm not a price");
|
||||
}
|
||||
|
||||
public function testGetQuantity()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user