diff --git a/README.md b/README.md index 11082b0..57cc78a 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ PayPal\Validation\ModelAccessorValidator: WARNING: Missing Accessor: PayPal\Api\ ## Contributing * If you find solution to an [issue/improvements](https://github.com/paypal/PayPal-PHP-SDK/issues) in sdk that would be helpful to everyone, feel free to send us a pull request. +* The best help we could get from everyone is in writing more and more samples. We have a limited set of samples, and would appreciate if the community can help us write more and more of those, covering corner cases, that may be extremely useful to anyone using this SDK. * The ideal approach to create a fix would be to fork the repository, create a branch in your repository, and make a pull request out of it. * It is desirable if there is enough comments/documentation and Tests included in the pull request. * For general idea of contribution, please follow the guidelines mentioned [here](https://guides.github.com/activities/contributing-to-open-source/). diff --git a/lib/PayPal/Api/Amount.php b/lib/PayPal/Api/Amount.php index 13367a6..877db80 100644 --- a/lib/PayPal/Api/Amount.php +++ b/lib/PayPal/Api/Amount.php @@ -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; } diff --git a/lib/PayPal/Api/Currency.php b/lib/PayPal/Api/Currency.php index 3e4c982..cfbe165 100644 --- a/lib/PayPal/Api/Currency.php +++ b/lib/PayPal/Api/Currency.php @@ -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; } diff --git a/lib/PayPal/Api/Details.php b/lib/PayPal/Api/Details.php index 9a5895c..5d5c627 100644 --- a/lib/PayPal/Api/Details.php +++ b/lib/PayPal/Api/Details.php @@ -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; } diff --git a/lib/PayPal/Api/Item.php b/lib/PayPal/Api/Item.php index 2bc0cf7..6814066 100644 --- a/lib/PayPal/Api/Item.php +++ b/lib/PayPal/Api/Item.php @@ -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; } diff --git a/lib/PayPal/Common/FormatConverter.php b/lib/PayPal/Common/FormatConverter.php new file mode 100644 index 0000000..bc35ee6 --- /dev/null +++ b/lib/PayPal/Common/FormatConverter.php @@ -0,0 +1,34 @@ +setName('Regular Payments') ->setFrequency('Month') ->setFrequencyInterval("2") ->setCycles("12") - ->setAmount(new Currency(['value' => '100', 'currency' => 'USD'])); + ->setAmount(new Currency(['value' => 100, 'currency' => 'USD'])); // Charge Models $chargeModel = new ChargeModel(); $chargeModel->setType('SHIPPING') - ->setAmount(new Currency(['value' => '10', 'currency' => 'USD'])); + ->setAmount(new Currency(['value' => 10, 'currency' => 'USD'])); $paymentDefinition->setChargeModels(array($chargeModel)); @@ -49,7 +49,7 @@ $merchantPreferences->setReturnUrl("$baseUrl/ExecuteAgreement.php?success=true") ->setAutoBillAmount("yes") ->setInitialFailAmountAction("CONTINUE") ->setMaxFailAttempts("0") - ->setSetupFee(new Currency(['value' => '1', 'currency' => 'USD'])); + ->setSetupFee(new Currency(['value' => 1, 'currency' => 'USD'])); $plan->setPaymentDefinitions(array($paymentDefinition)); diff --git a/sample/payments/AuthorizationCapture.php b/sample/payments/AuthorizationCapture.php index ca48cd1..c985ab9 100644 --- a/sample/payments/AuthorizationCapture.php +++ b/sample/payments/AuthorizationCapture.php @@ -21,7 +21,7 @@ try { $amt = new Amount(); $amt->setCurrency("USD") - ->setTotal("1.00"); + ->setTotal(1); ### Capture $capture = new Capture(); diff --git a/sample/payments/AuthorizePayment.php b/sample/payments/AuthorizePayment.php index 3f98cf3..ae27af0 100644 --- a/sample/payments/AuthorizePayment.php +++ b/sample/payments/AuthorizePayment.php @@ -44,7 +44,7 @@ $payer->setPaymentMethod("credit_card") $amount = new Amount(); $amount->setCurrency("USD") - ->setTotal("1.00"); + ->setTotal(1); $transaction = new Transaction(); $transaction->setAmount($amount) diff --git a/sample/payments/CreatePayment.php b/sample/payments/CreatePayment.php index b4bd46f..e4c67ff 100644 --- a/sample/payments/CreatePayment.php +++ b/sample/payments/CreatePayment.php @@ -54,15 +54,15 @@ $item1->setName('Ground Coffee 40 oz') ->setDescription('Ground Coffee 40 oz') ->setCurrency('USD') ->setQuantity(1) - ->setTax('0.30') - ->setPrice('7.50'); + ->setTax(0.3) + ->setPrice(7.50); $item2 = new Item(); $item2->setName('Granola bars') ->setDescription('Granola Bars with Peanuts') ->setCurrency('USD') ->setQuantity(5) - ->setTax('0.20') - ->setPrice('2.00'); + ->setTax(0.2) + ->setPrice(2); $itemList = new ItemList(); $itemList->setItems(array($item1, $item2)); @@ -72,9 +72,9 @@ $itemList->setItems(array($item1, $item2)); // payment information such as tax, shipping // charges etc. $details = new Details(); -$details->setShipping('1.20') - ->setTax('1.30') - ->setSubtotal('17.50'); +$details->setShipping(1.2) + ->setTax(1.3) + ->setSubtotal(17.5); // ### Amount // Lets you specify a payment amount. @@ -82,7 +82,7 @@ $details->setShipping('1.20') // such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") - ->setTotal("20.00") + ->setTotal(20) ->setDetails($details); // ### Transaction diff --git a/sample/payments/CreatePaymentUsingPayPal.php b/sample/payments/CreatePaymentUsingPayPal.php index 0e28f8c..029f61b 100644 --- a/sample/payments/CreatePaymentUsingPayPal.php +++ b/sample/payments/CreatePaymentUsingPayPal.php @@ -29,12 +29,12 @@ $item1 = new Item(); $item1->setName('Ground Coffee 40 oz') ->setCurrency('USD') ->setQuantity(1) - ->setPrice('7.50'); + ->setPrice(7.5); $item2 = new Item(); $item2->setName('Granola bars') ->setCurrency('USD') ->setQuantity(5) - ->setPrice('2.00'); + ->setPrice(2); $itemList = new ItemList(); $itemList->setItems(array($item1, $item2)); @@ -44,9 +44,9 @@ $itemList->setItems(array($item1, $item2)); // payment information such as tax, shipping // charges etc. $details = new Details(); -$details->setShipping('1.20') - ->setTax('1.30') - ->setSubtotal('17.50'); +$details->setShipping(1.2) + ->setTax(1.3) + ->setSubtotal(17.50); // ### Amount // Lets you specify a payment amount. @@ -54,7 +54,7 @@ $details->setShipping('1.20') // such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") - ->setTotal("20.00") + ->setTotal(20) ->setDetails($details); // ### Transaction diff --git a/sample/payments/CreatePaymentUsingSavedCard.php b/sample/payments/CreatePaymentUsingSavedCard.php index d66f6f1..94775dc 100644 --- a/sample/payments/CreatePaymentUsingSavedCard.php +++ b/sample/payments/CreatePaymentUsingSavedCard.php @@ -46,12 +46,12 @@ $item1 = new Item(); $item1->setName('Ground Coffee 40 oz') ->setCurrency('USD') ->setQuantity(1) - ->setPrice('7.50'); + ->setPrice(7.5); $item2 = new Item(); $item2->setName('Granola bars') ->setCurrency('USD') ->setQuantity(5) - ->setPrice('2.00'); + ->setPrice(2); $itemList = new ItemList(); $itemList->setItems(array($item1, $item2)); @@ -61,9 +61,9 @@ $itemList->setItems(array($item1, $item2)); // payment information such as tax, shipping // charges etc. $details = new Details(); -$details->setShipping('1.20') - ->setTax('1.30') - ->setSubtotal('17.50'); +$details->setShipping(1.2) + ->setTax(1.3) + ->setSubtotal(17.5); // ### Amount // Lets you specify a payment amount. @@ -71,7 +71,7 @@ $details->setShipping('1.20') // such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") - ->setTotal("20.00") + ->setTotal(20) ->setDetails($details); // ### Transaction diff --git a/sample/payments/Reauthorization.php b/sample/payments/Reauthorization.php index a6b6982..5ae75d4 100644 --- a/sample/payments/Reauthorization.php +++ b/sample/payments/Reauthorization.php @@ -20,7 +20,7 @@ try { $amount = new Amount(); $amount->setCurrency("USD"); - $amount->setTotal("1.00"); + $amount->setTotal(1); // ### Reauthorize with amount being reauthorized $authorization->setAmount($amount); diff --git a/sample/sale/RefundSale.php b/sample/sale/RefundSale.php index dfe674c..87fe2c5 100644 --- a/sample/sale/RefundSale.php +++ b/sample/sale/RefundSale.php @@ -20,7 +20,7 @@ use PayPal\Api\Sale; // field to mention fees refund details. $amt = new Amount(); $amt->setCurrency('USD') - ->setTotal('0.01'); + ->setTotal(0.01); // ### Refund object $refund = new Refund(); diff --git a/tests/PayPal/Test/Api/CurrencyTest.php b/tests/PayPal/Test/Api/CurrencyTest.php index 218618e..0579c90 100644 --- a/tests/PayPal/Test/Api/CurrencyTest.php +++ b/tests/PayPal/Test/Api/CurrencyTest.php @@ -18,7 +18,7 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase */ public static function getJson() { - return '{"currency":"TestSample","value":"TestSample"}'; + return '{"currency":"TestSample","value":"12.34"}'; } /** diff --git a/tests/PayPal/Test/Common/FormatConverterTest.php b/tests/PayPal/Test/Common/FormatConverterTest.php new file mode 100644 index 0000000..1970b2c --- /dev/null +++ b/tests/PayPal/Test/Common/FormatConverterTest.php @@ -0,0 +1,91 @@ +assertEquals($expected, $result); + + } + + /** + * @dataProvider apiModelSettersProvider + * + * @param PPModel $class Class Object + * @param string $method Method Name where the format is being applied + * @param array $values array of ['input', 'expectedResponse'] is provided + */ + public function testSettersOfKnownApiModel($class, $method, $values) + { + $obj = new $class(); + $setter = "set" . $method; + $getter = "get" . $method; + $result = $obj->$setter($values[0]); + $this->assertEquals($values[1], $result->$getter()); + } + + /** + * @dataProvider apiModelSettersInvalidProvider + * @expectedException \InvalidArgumentException + */ + public function testSettersOfKnownApiModelInvalid($class, $methodName, $values) + { + $obj = new $class(); + $setter = "set" . $methodName; + $obj->$setter($values[0]); + } +} diff --git a/tests/PayPal/Test/Validation/NumericValidatorTest.php b/tests/PayPal/Test/Validation/NumericValidatorTest.php new file mode 100644 index 0000000..09dcfdd --- /dev/null +++ b/tests/PayPal/Test/Validation/NumericValidatorTest.php @@ -0,0 +1,61 @@ +assertTrue(NumericValidator::validate($input, "Test Value")); + } + + /** + * + * @dataProvider invalidProvider + * @expectedException \InvalidArgumentException + */ + public function testValidateException($input) + { + NumericValidator::validate($input, "Test Value"); + } + +}