From 46640c5096d187cac8fe6ed29662c7e6f4d57aed Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Tue, 8 Nov 2016 13:14:52 -0500 Subject: [PATCH] 2.4 - Faking the Payment Gateway --- app/Billing/FakePaymentGateway.php | 28 +++++++++++++++++++ app/Billing/PaymentGateway.php | 8 ++++++ app/Exceptions/Handler.php | 1 + .../Controllers/ConcertOrdersController.php | 27 ++++++++++++++++++ routes/web.php | 2 ++ tests/features/PurchaseTicketsTest.php | 10 +++++++ tests/unit/Billing/FakePaymentGatewayTest.php | 19 +++++++++++++ 7 files changed, 95 insertions(+) create mode 100644 app/Billing/FakePaymentGateway.php create mode 100644 app/Billing/PaymentGateway.php create mode 100644 app/Http/Controllers/ConcertOrdersController.php create mode 100644 tests/unit/Billing/FakePaymentGatewayTest.php diff --git a/app/Billing/FakePaymentGateway.php b/app/Billing/FakePaymentGateway.php new file mode 100644 index 0000000..5d62847 --- /dev/null +++ b/app/Billing/FakePaymentGateway.php @@ -0,0 +1,28 @@ +charges = collect(); + } + + public function getValidTestToken() + { + return "valid-token"; + } + + public function charge($amount, $token) + { + $this->charges[] = $amount; + } + + public function totalCharges() + { + return $this->charges->sum(); + } +} diff --git a/app/Billing/PaymentGateway.php b/app/Billing/PaymentGateway.php new file mode 100644 index 0000000..21b1f5b --- /dev/null +++ b/app/Billing/PaymentGateway.php @@ -0,0 +1,8 @@ +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); + } +} diff --git a/routes/web.php b/routes/web.php index c3ef6cb..af1ddbb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,3 +12,5 @@ */ Route::get('/concerts/{id}', 'ConcertsController@show'); + +Route::post('/concerts/{id}/orders', 'ConcertOrdersController@store'); diff --git a/tests/features/PurchaseTicketsTest.php b/tests/features/PurchaseTicketsTest.php index ce53c79..6e6b170 100644 --- a/tests/features/PurchaseTicketsTest.php +++ b/tests/features/PurchaseTicketsTest.php @@ -1,14 +1,22 @@ app->instance(PaymentGateway::class, $paymentGateway); + // Arrange // Create a concert $concert = factory(Concert::class)->create(['ticket_price' => 3250]); @@ -22,6 +30,8 @@ class PurchaseTicketsTest extends TestCase ]); // Assert + $this->assertResponseStatus(201); + // Make sure the customer was charged the correct amount $this->assertEquals(9750, $paymentGateway->totalCharges()); diff --git a/tests/unit/Billing/FakePaymentGatewayTest.php b/tests/unit/Billing/FakePaymentGatewayTest.php new file mode 100644 index 0000000..c1b0886 --- /dev/null +++ b/tests/unit/Billing/FakePaymentGatewayTest.php @@ -0,0 +1,19 @@ +charge(2500, $paymentGateway->getValidTestToken()); + + $this->assertEquals(2500, $paymentGateway->totalCharges()); + } +}