Sanitize Input for Price Variables

- Updated the model to automatically format the price
- Updated the samples to reflect the new changes
- More Unit Tests
This commit is contained in:
japatel
2014-11-03 16:02:23 -06:00
parent 3c02790138
commit d11533110b
19 changed files with 290 additions and 46 deletions

View File

@@ -4,6 +4,8 @@ namespace PayPal\Api;
use PayPal\Common\PPModel;
use PayPal\Rest\ApiContext;
use PayPal\Validation\NumericValidator;
use PayPal\Common\FormatConverter;
/**
* Class Amount
@@ -46,12 +48,14 @@ class Amount extends PPModel
* Total amount charged from the Payer account (or card) to Payee. In case of a refund, this is the refunded amount to the original Payer from Payee account.
*
*
* @param string $total
* @param string|double $total
*
* @return $this
*/
public function setTotal($total)
{
NumericValidator::validate($total, "Total");
$total = FormatConverter::formatToTwoDecimalPlaces($total);
$this->total = $total;
return $this;
}

View File

@@ -3,6 +3,8 @@
namespace PayPal\Api;
use PayPal\Common\PPModel;
use PayPal\Common\FormatConverter;
use PayPal\Validation\NumericValidator;
/**
* Class Currency
@@ -42,12 +44,14 @@ class Currency extends PPModel
/**
* amount up to N digit after the decimals separator as defined in ISO 4217 for the appropriate currency code.
*
* @param string $value
* @param string|double $value
*
* @return $this
*/
public function setValue($value)
{
NumericValidator::validate($value, "Value");
$value = FormatConverter::formatToTwoDecimalPlaces($value);
$this->value = $value;
return $this;
}

View File

@@ -4,6 +4,8 @@ namespace PayPal\Api;
use PayPal\Common\PPModel;
use PayPal\Rest\ApiContext;
use PayPal\Validation\NumericValidator;
use PayPal\Common\FormatConverter;
/**
* Class Details
@@ -27,12 +29,14 @@ class Details extends PPModel
* Amount being charged for shipping.
*
*
* @param string $shipping
* @param string|double $shipping
*
* @return $this
*/
public function setShipping($shipping)
{
NumericValidator::validate($shipping, "Shipping");
$shipping = FormatConverter::formatToTwoDecimalPlaces($shipping);
$this->shipping = $shipping;
return $this;
}
@@ -51,12 +55,14 @@ class Details extends PPModel
* Sub-total (amount) of items being paid for.
*
*
* @param string $subtotal
* @param string|double $subtotal
*
* @return $this
*/
public function setSubtotal($subtotal)
{
NumericValidator::validate($subtotal, "SubTotal");
$subtotal = FormatConverter::formatToTwoDecimalPlaces($subtotal);
$this->subtotal = $subtotal;
return $this;
}
@@ -68,6 +74,7 @@ class Details extends PPModel
*/
public function getSubtotal()
{
return $this->subtotal;
}
@@ -75,12 +82,14 @@ class Details extends PPModel
* Amount being charged as tax.
*
*
* @param string $tax
* @param string|double $tax
*
* @return $this
*/
public function setTax($tax)
{
NumericValidator::validate($tax, "Tax");
$tax = FormatConverter::formatToTwoDecimalPlaces($tax);
$this->tax = $tax;
return $this;
}
@@ -99,12 +108,14 @@ class Details extends PPModel
* Fee charged by PayPal. In case of a refund, this is the fee amount refunded to the original receipient of the payment.
*
*
* @param string $fee
* @param string|double $fee
*
* @return $this
*/
public function setFee($fee)
{
NumericValidator::validate($fee, "Fee");
$fee = FormatConverter::formatToTwoDecimalPlaces($fee);
$this->fee = $fee;
return $this;
}
@@ -123,12 +134,14 @@ class Details extends PPModel
* Amount being charged as shipping discount.
*
*
* @param string $shipping_discount
* @param string|double $shipping_discount
*
* @return $this
*/
public function setShippingDiscount($shipping_discount)
{
NumericValidator::validate($shipping_discount, "Shipping Discount");
$shipping_discount = FormatConverter::formatToTwoDecimalPlaces($shipping_discount);
$this->shipping_discount = $shipping_discount;
return $this;
}
@@ -172,12 +185,14 @@ class Details extends PPModel
* Amount being charged as insurance.
*
*
* @param string $insurance
* @param string|double $insurance
*
* @return $this
*/
public function setInsurance($insurance)
{
NumericValidator::validate($insurance, "Insurance");
$insurance = FormatConverter::formatToTwoDecimalPlaces($insurance);
$this->insurance = $insurance;
return $this;
}
@@ -196,12 +211,14 @@ class Details extends PPModel
* Amount being charged as handling fee.
*
*
* @param string $handling_fee
* @param string|double $handling_fee
*
* @return $this
*/
public function setHandlingFee($handling_fee)
{
NumericValidator::validate($handling_fee, "Handling Fee");
$handling_fee = FormatConverter::formatToTwoDecimalPlaces($handling_fee);
$this->handling_fee = $handling_fee;
return $this;
}
@@ -243,14 +260,15 @@ class Details extends PPModel
/**
* Amount being charged as gift wrap fee.
*
*
* @param string $gift_wrap
* @param string|double $gift_wrap
*
* @return $this
*/
public function setGiftWrap($gift_wrap)
{
NumericValidator::validate($gift_wrap, "Gift Wrap");
$gift_wrap = FormatConverter::formatToTwoDecimalPlaces($gift_wrap);
$this->gift_wrap = $gift_wrap;
return $this;
}

View File

@@ -5,6 +5,8 @@ namespace PayPal\Api;
use PayPal\Common\PPModel;
use PayPal\Rest\ApiContext;
use PayPal\Validation\UrlValidator;
use PayPal\Validation\NumericValidator;
use PayPal\Common\FormatConverter;
/**
* Class Item
@@ -103,12 +105,15 @@ class Item extends PPModel
* Cost of the item.
*
*
* @param string $price
* @param double $price
*
* @return $this
*/
public function setPrice($price)
{
NumericValidator::validate($price, "Price");
$price = FormatConverter::formatToTwoDecimalPlaces($price);
$this->price = $price;
return $this;
}
@@ -127,12 +132,14 @@ class Item extends PPModel
* tax of the item.
*
*
* @param string $tax
* @param double $tax
*
* @return $this
*/
public function setTax($tax)
{
NumericValidator::validate($tax, "Tax");
$tax = FormatConverter::formatToTwoDecimalPlaces($tax);
$this->tax = $tax;
return $this;
}

View File

@@ -0,0 +1,34 @@
<?php
namespace PayPal\Common;
class FormatConverter {
const TWO_DECIMAL_PLACES = '%0.2f';
/**
* Format the data based on the input formatter value
*
* @param $value
* @param $formatter
* @return string
*/
public static function format($value, $formatter)
{
return sprintf($formatter, $value);
}
/**
* Format the input data to two decimal places
*
* @param $value
* @return string
*/
public static function formatToTwoDecimalPlaces($value)
{
if (trim($value) != null) {
return static::format($value, self::TWO_DECIMAL_PLACES);
}
return null;
}
}

View File

@@ -21,11 +21,7 @@ class ArgumentValidator
{
if (
$argument != null &&
(
(gettype($argument) == 'string' && $argument == '')
||
is_array($argument) && empty($argument)
)
((gettype($argument) == 'string' && $argument == '') || is_array($argument) && empty($argument))
) {
//Throw an Exception for string or array
throw new \InvalidArgumentException("$argumentName cannot be null or empty");

View File

@@ -0,0 +1,28 @@
<?php
namespace PayPal\Validation;
/**
* Class NumericValidator
*
* @package PayPal\Validation
*/
class NumericValidator
{
/**
* Helper method for validating an argument if it is numeric
*
* @param mixed $argument
* @param string|null $argumentName
* @return bool
*/
public static function validate($argument, $argumentName = null)
{
if (trim($argument) != null && !is_numeric($argument)) {
throw new \InvalidArgumentException("$argumentName is not a valid numeric value");
}
return true;
}
}