mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
3.5 - Cancelling Failed Orders
This commit is contained in:
@@ -32,6 +32,7 @@ class ConcertOrdersController extends Controller
|
||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||
return response()->json([], 201);
|
||||
} catch (PaymentFailedException $e) {
|
||||
$order->cancel();
|
||||
return response()->json([], 422);
|
||||
} catch (NotEnoughTicketsException $e) {
|
||||
return response()->json([], 422);
|
||||
|
||||
@@ -12,4 +12,13 @@ class Order extends Model
|
||||
{
|
||||
return $this->hasMany(Ticket::class);
|
||||
}
|
||||
|
||||
public function cancel()
|
||||
{
|
||||
foreach ($this->tickets as $ticket) {
|
||||
$ticket->update(['order_id' => null]);
|
||||
}
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Ticket extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function scopeAvailable($query)
|
||||
{
|
||||
return $query->whereNull('order_id');
|
||||
|
||||
26
tests/unit/OrderTest.php
Normal file
26
tests/unit/OrderTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use App\Order;
|
||||
use App\Concert;
|
||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class OrderTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
/** @test */
|
||||
function tickets_are_released_when_an_order_is_cancelled()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(10);
|
||||
$order = $concert->orderTickets('jane@example.com', 5);
|
||||
$this->assertEquals(5, $concert->ticketsRemaining());
|
||||
|
||||
$order->cancel();
|
||||
|
||||
$this->assertEquals(10, $concert->ticketsRemaining());
|
||||
$this->assertNull(Order::find($order->id));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user