mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-10 03:51:16 +00:00
2.9 - Handling Failed Charges
This commit is contained in:
@@ -18,6 +18,9 @@ class FakePaymentGateway implements PaymentGateway
|
|||||||
|
|
||||||
public function charge($amount, $token)
|
public function charge($amount, $token)
|
||||||
{
|
{
|
||||||
|
if ($token !== $this->getValidTestToken()) {
|
||||||
|
throw new PaymentFailedException;
|
||||||
|
}
|
||||||
$this->charges[] = $amount;
|
$this->charges[] = $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
app/Billing/PaymentFailedException.php
Normal file
5
app/Billing/PaymentFailedException.php
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Billing;
|
||||||
|
|
||||||
|
class PaymentFailedException extends \RuntimeException {}
|
||||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
|||||||
use App\Concert;
|
use App\Concert;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Billing\PaymentGateway;
|
use App\Billing\PaymentGateway;
|
||||||
|
use App\Billing\PaymentFailedException;
|
||||||
|
|
||||||
class ConcertOrdersController extends Controller
|
class ConcertOrdersController extends Controller
|
||||||
{
|
{
|
||||||
@@ -23,10 +24,14 @@ class ConcertOrdersController extends Controller
|
|||||||
'payment_token' => ['required'],
|
'payment_token' => ['required'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$concert = Concert::find($concertId);
|
try {
|
||||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
$concert = Concert::find($concertId);
|
||||||
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||||
|
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
||||||
|
|
||||||
return response()->json([], 201);
|
return response()->json([], 201);
|
||||||
|
} catch (PaymentFailedException $e) {
|
||||||
|
return response()->json([], 422);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,26 +32,37 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function customer_can_purchase_concert_tickets()
|
function customer_can_purchase_concert_tickets()
|
||||||
{
|
{
|
||||||
// Arrange
|
|
||||||
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
||||||
|
|
||||||
// Act
|
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Assert
|
|
||||||
$this->assertResponseStatus(201);
|
$this->assertResponseStatus(201);
|
||||||
|
|
||||||
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
|
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
|
||||||
|
|
||||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||||
$this->assertNotNull($order);
|
$this->assertNotNull($order);
|
||||||
$this->assertEquals(3, $order->tickets()->count());
|
$this->assertEquals(3, $order->tickets()->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function an_order_is_not_created_if_payment_fails()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
||||||
|
|
||||||
|
$this->orderTickets($concert, [
|
||||||
|
'email' => 'john@example.com',
|
||||||
|
'ticket_quantity' => 3,
|
||||||
|
'payment_token' => 'invalid-payment-token',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertResponseStatus(422);
|
||||||
|
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||||
|
$this->assertNull($order);
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function email_is_required_to_purchase_tickets()
|
function email_is_required_to_purchase_tickets()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Billing\FakePaymentGateway;
|
use App\Billing\FakePaymentGateway;
|
||||||
|
use App\Billing\PaymentFailedException;
|
||||||
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;
|
||||||
@@ -16,4 +17,17 @@ class FakePaymentGatewayTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals(2500, $paymentGateway->totalCharges());
|
$this->assertEquals(2500, $paymentGateway->totalCharges());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function charges_with_an_invalid_payment_token_fail()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$paymentGateway = new FakePaymentGateway;
|
||||||
|
$paymentGateway->charge(2500, 'invalid-payment-token');
|
||||||
|
} catch (PaymentFailedException $e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user