mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-19 11:54:57 +00:00
2.4 - Faking the Payment Gateway
This commit is contained in:
28
app/Billing/FakePaymentGateway.php
Normal file
28
app/Billing/FakePaymentGateway.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Billing;
|
||||||
|
|
||||||
|
class FakePaymentGateway implements PaymentGateway
|
||||||
|
{
|
||||||
|
private $charges;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->charges = collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidTestToken()
|
||||||
|
{
|
||||||
|
return "valid-token";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function charge($amount, $token)
|
||||||
|
{
|
||||||
|
$this->charges[] = $amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function totalCharges()
|
||||||
|
{
|
||||||
|
return $this->charges->sum();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
app/Billing/PaymentGateway.php
Normal file
8
app/Billing/PaymentGateway.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Billing;
|
||||||
|
|
||||||
|
interface PaymentGateway
|
||||||
|
{
|
||||||
|
public function charge($amount, $token);
|
||||||
|
}
|
||||||
@@ -44,6 +44,7 @@ class Handler extends ExceptionHandler
|
|||||||
*/
|
*/
|
||||||
public function render($request, Exception $exception)
|
public function render($request, Exception $exception)
|
||||||
{
|
{
|
||||||
|
throw $exception;
|
||||||
return parent::render($request, $exception);
|
return parent::render($request, $exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
app/Http/Controllers/ConcertOrdersController.php
Normal file
27
app/Http/Controllers/ConcertOrdersController.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Concert;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Billing\PaymentGateway;
|
||||||
|
|
||||||
|
class ConcertOrdersController extends Controller
|
||||||
|
{
|
||||||
|
private $paymentGateway;
|
||||||
|
|
||||||
|
public function __construct(PaymentGateway $paymentGateway)
|
||||||
|
{
|
||||||
|
$this->paymentGateway = $paymentGateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store($concertId)
|
||||||
|
{
|
||||||
|
$concert = Concert::find($concertId);
|
||||||
|
$ticketQuantity = request('ticket_quantity');
|
||||||
|
$amount = $ticketQuantity * $concert->ticket_price;
|
||||||
|
$token = request('payment_token');
|
||||||
|
$this->paymentGateway->charge($amount, $token);
|
||||||
|
return response()->json([], 201);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,3 +12,5 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/concerts/{id}', 'ConcertsController@show');
|
Route::get('/concerts/{id}', 'ConcertsController@show');
|
||||||
|
|
||||||
|
Route::post('/concerts/{id}/orders', 'ConcertOrdersController@store');
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Concert;
|
||||||
|
use App\Billing\PaymentGateway;
|
||||||
|
use App\Billing\FakePaymentGateway;
|
||||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
|
||||||
class PurchaseTicketsTest extends TestCase
|
class PurchaseTicketsTest extends TestCase
|
||||||
{
|
{
|
||||||
|
use DatabaseMigrations;
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function customer_can_purchase_concert_tickets()
|
function customer_can_purchase_concert_tickets()
|
||||||
{
|
{
|
||||||
|
$paymentGateway = new FakePaymentGateway;
|
||||||
|
$this->app->instance(PaymentGateway::class, $paymentGateway);
|
||||||
|
|
||||||
// Arrange
|
// Arrange
|
||||||
// Create a concert
|
// Create a concert
|
||||||
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
||||||
@@ -22,6 +30,8 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
$this->assertResponseStatus(201);
|
||||||
|
|
||||||
// Make sure the customer was charged the correct amount
|
// Make sure the customer was charged the correct amount
|
||||||
$this->assertEquals(9750, $paymentGateway->totalCharges());
|
$this->assertEquals(9750, $paymentGateway->totalCharges());
|
||||||
|
|
||||||
|
|||||||
19
tests/unit/Billing/FakePaymentGatewayTest.php
Normal file
19
tests/unit/Billing/FakePaymentGatewayTest.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Billing\FakePaymentGateway;
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
|
||||||
|
class FakePaymentGatewayTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
function charges_with_a_valid_payment_token_are_successful()
|
||||||
|
{
|
||||||
|
$paymentGateway = new FakePaymentGateway;
|
||||||
|
|
||||||
|
$paymentGateway->charge(2500, $paymentGateway->getValidTestToken());
|
||||||
|
|
||||||
|
$this->assertEquals(2500, $paymentGateway->totalCharges());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user