2.9 - Handling Failed Charges

This commit is contained in:
Adam Wathan
2016-11-11 10:30:02 -05:00
parent 62bf031a17
commit 3f9ff57785
5 changed files with 47 additions and 9 deletions

View File

@@ -18,6 +18,9 @@ class FakePaymentGateway implements PaymentGateway
public function charge($amount, $token)
{
if ($token !== $this->getValidTestToken()) {
throw new PaymentFailedException;
}
$this->charges[] = $amount;
}

View File

@@ -0,0 +1,5 @@
<?php
namespace App\Billing;
class PaymentFailedException extends \RuntimeException {}

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Concert;
use Illuminate\Http\Request;
use App\Billing\PaymentGateway;
use App\Billing\PaymentFailedException;
class ConcertOrdersController extends Controller
{
@@ -23,10 +24,14 @@ class ConcertOrdersController extends Controller
'payment_token' => ['required'],
]);
$concert = Concert::find($concertId);
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
try {
$concert = Concert::find($concertId);
$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);
}
}
}

View File

@@ -32,26 +32,37 @@ class PurchaseTicketsTest extends TestCase
/** @test */
function customer_can_purchase_concert_tickets()
{
// Arrange
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
// Act
$this->orderTickets($concert, [
'email' => 'john@example.com',
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
// Assert
$this->assertResponseStatus(201);
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
$order = $concert->orders()->where('email', 'john@example.com')->first();
$this->assertNotNull($order);
$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 */
function email_is_required_to_purchase_tickets()
{

View File

@@ -1,6 +1,7 @@
<?php
use App\Billing\FakePaymentGateway;
use App\Billing\PaymentFailedException;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -16,4 +17,17 @@ class FakePaymentGatewayTest extends TestCase
$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();
}
}