Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a feature flag/switch for PayPal payment method #4

Merged
merged 1 commit into from Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/Http/Controllers/CheckoutController.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/routes.php
Expand Up @@ -15,6 +15,6 @@
return view('welcome');
});

Route::get('/place/{paymentMethod}', [
Route::get('/place/{paymentMethod}/{email?}', [
'as' => 'order-place', 'uses' => 'CheckoutController@placeOrder'
]);
27 changes: 25 additions & 2 deletions app/Services/Checkout.php
Expand Up @@ -18,29 +18,52 @@ 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;
}

return $this->subTotal + $this->cashOnDeliveryFee;
}

protected function isPaymentMethodValid($paymentMethod)
{
return in_array($paymentMethod, self::ALLOWED_PAYMENT_METHODS, true);
}
}
8 changes: 7 additions & 1 deletion resources/views/checkout.blade.php
Expand Up @@ -38,7 +38,13 @@
<body>
<div class="container">
<div class="content">
<div class="title">Checkout for {{ $paymentMethod }} with total {{ $orderTotal }}</div>
<div class="title">
@if ($orderTotal)
Checkout for {{ $paymentMethod }} with total {{ $orderTotal }}
@else
Payment method {{ $paymentMethod }}, is not valid.
@endif
</div>
</div>
</div>
</body>
Expand Down
22 changes: 14 additions & 8 deletions tests/Integrated/ExampleTest.php
Expand Up @@ -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.'],
];
}

Expand All @@ -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);
}
}
10 changes: 6 additions & 4 deletions tests/Services/CheckoutTest.php
Expand Up @@ -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]
];
}

Expand All @@ -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)
);
Expand Down