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 @@