From c02db52e9d732e853de8aff834b6f7398443c96f Mon Sep 17 00:00:00 2001 From: Geshan Manandhar Date: Sat, 24 Sep 2016 08:58:36 +0400 Subject: [PATCH] Adds a feature flag/switch for PayPal payment method - [x] Adds a payment method `PayPal` - [x] Paypal is only available for `@gmail.com` emails - [x] Changes the tests for the same --- app/Http/Controllers/CheckoutController.php | 9 ++++--- app/Http/routes.php | 2 +- app/Services/Checkout.php | 27 +++++++++++++++++++-- resources/views/checkout.blade.php | 8 +++++- tests/Integrated/ExampleTest.php | 22 +++++++++++------ tests/Services/CheckoutTest.php | 10 +++++--- 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/CheckoutController.php b/app/Http/Controllers/CheckoutController.php index 3c72394..f21d8f9 100644 --- a/app/Http/Controllers/CheckoutController.php +++ b/app/Http/Controllers/CheckoutController.php @@ -30,12 +30,15 @@ public function __construct(Checkout $checkout) /** * Place order * - * @return \Illuminate\View\View + * @param string $paymentMethod + * @param string $email + * + * @return $this */ - public function placeOrder($paymentMethod) + public function placeOrder($paymentMethod, $email='test@mydomain.com') { $view = view('checkout')->with('paymentMethod', $paymentMethod); - $view->with('orderTotal', $this->checkout->calculateTotal($paymentMethod)); + $view->with('orderTotal', $this->checkout->calculateTotal($paymentMethod, $email)); return $view; } diff --git a/app/Http/routes.php b/app/Http/routes.php index 626114e..8592ed2 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -15,6 +15,6 @@ return view('welcome'); }); -Route::get('/place/{paymentMethod}', [ +Route::get('/place/{paymentMethod}/{email?}', [ 'as' => 'order-place', 'uses' => 'CheckoutController@placeOrder' ]); diff --git a/app/Services/Checkout.php b/app/Services/Checkout.php index a2f2747..fe92234 100644 --- a/app/Services/Checkout.php +++ b/app/Services/Checkout.php @@ -18,24 +18,42 @@ class Checkout /** * @var float */ - protected $subTotal = 0.0; + protected $subTotal = 0.0; /** * @var string */ protected $paymentMethod; + const ALLOWED_PAYMENT_METHODS = ['Cash', 'CreditCard', 'PayPal']; + + const PAYAPL_ALLLOWED_DOMAIN = '@gmail.com'; + const CASH_ON_DELIVERY_FEE = 5.00; /** * Calculates total, if payment method is cash 5.00 is added as * post payment fees. * + * If payment method is invalid, will return 0. + * + * Payment method `PayPal` has a feature switch/flag, it is available ony for email ending in @gmail.com. + * * @param $paymentMethod * @return float */ - public function calculateTotal($paymentMethod) + public function calculateTotal($paymentMethod, $email) { + $invalidOrderTotal = 0; + + if (!$this->isPaymentMethodValid($paymentMethod)) { + return $invalidOrderTotal; + } + + if ('PayPal' === $paymentMethod && !ends_with($email, self::PAYAPL_ALLLOWED_DOMAIN)) { + return $invalidOrderTotal; + } + $this->subTotal = 95.00; if ('Cash' === $paymentMethod) { $this->cashOnDeliveryFee = self::CASH_ON_DELIVERY_FEE; @@ -43,4 +61,9 @@ public function calculateTotal($paymentMethod) return $this->subTotal + $this->cashOnDeliveryFee; } + + protected function isPaymentMethodValid($paymentMethod) + { + return in_array($paymentMethod, self::ALLOWED_PAYMENT_METHODS, true); + } } diff --git a/resources/views/checkout.blade.php b/resources/views/checkout.blade.php index 1dec294..dc58fb0 100644 --- a/resources/views/checkout.blade.php +++ b/resources/views/checkout.blade.php @@ -38,7 +38,13 @@
-
Checkout for {{ $paymentMethod }} with total {{ $orderTotal }}
+
+ @if ($orderTotal) + Checkout for {{ $paymentMethod }} with total {{ $orderTotal }} + @else + Payment method {{ $paymentMethod }}, is not valid. + @endif +
diff --git a/tests/Integrated/ExampleTest.php b/tests/Integrated/ExampleTest.php index e6fe68c..73061d8 100644 --- a/tests/Integrated/ExampleTest.php +++ b/tests/Integrated/ExampleTest.php @@ -23,10 +23,15 @@ public function testBasicExample() public function providerAllUrisWithResponseCode() { return [ - ['/', 200], - ['/place/cc', 200], - ['/place/cod', 200], - ['/non-existing', 404], + ['/', 200, 'Laravel 5'], + ['/place/CreditCard', 200, 'Checkout for CreditCard with total 95'], + ['/place/CreditCard/me@mydomain.com', 200, 'Checkout for CreditCard with total 95'], + ['/place/Cash', 200, 'Checkout for Cash with total 100'], + ['/place/Cash/me@mydomain.com', 200, 'Checkout for Cash with total 100'], + ['/place/PayPal/myName@gmail.com', 200, 'Checkout for PayPal with total 95'], + ['/place/PayPal/me@mydomain.com', 200, 'Payment method PayPal, is not valid.'], + ['/place/iDeal/me@mydomain.com', 200, 'Payment method iDeal, is not valid.'], + ['/non-existing', 404, 'the page you are looking for could not be found.'], ]; } @@ -35,11 +40,12 @@ public function providerAllUrisWithResponseCode() * * @dataProvider providerAllUrisWithResponseCode **/ - public function testApplicationUriResponses($uri, $responseCode) + public function testApplicationUriResponses($uri, $responseCode, $visibleText) { - print sprintf('checking URI : %s - to be %d - %s', $uri, $responseCode, PHP_EOL); - $response = $this->call('GET', $uri); + print sprintf('checking URI : %s - to be %d - %s', $uri, $responseCode, PHP_EOL); + $response = $this->call('GET', $uri); - $this->assertEquals($responseCode, $response->status()); + $this->assertEquals($responseCode, $response->status()); + $this->see($visibleText); } } diff --git a/tests/Services/CheckoutTest.php b/tests/Services/CheckoutTest.php index aad7b51..20bed90 100644 --- a/tests/Services/CheckoutTest.php +++ b/tests/Services/CheckoutTest.php @@ -34,8 +34,10 @@ public function setup() public function paymentMethodProvider() { return [ - ['Cash', 100.00], - ['Credit Card', 95.00] + ['Cash', 'me@mydomain.com', 100.00], + ['CreditCard', 'me@mydomain.com', 95.00], + ['PayPal', 'myName@gmail.com', 95.00], + ['PayPal', 'me@mydomain.com', 0.00] ]; } @@ -48,10 +50,10 @@ public function paymentMethodProvider() * * @dataProvider paymentMethodProvider */ - public function testCalculateTotal($paymentMethod, $expectedTotal) + public function testCalculateTotal($paymentMethod, $email, $expectedTotal) { $this->assertEquals( - $this->checkout->calculateTotal($paymentMethod), + $this->checkout->calculateTotal($paymentMethod, $email), $expectedTotal, sprintf('Testing total calculation for %s.', $paymentMethod) );