mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-17 14:54:04 +00:00
5.2 - Removing the Need to Cancel Orders
This commit is contained in:
@@ -52,15 +52,26 @@ class Concert extends Model
|
|||||||
|
|
||||||
public function orderTickets($email, $ticketQuantity)
|
public function orderTickets($email, $ticketQuantity)
|
||||||
{
|
{
|
||||||
$tickets = $this->tickets()->available()->take($ticketQuantity)->get();
|
$tickets = $this->findTickets($ticketQuantity);
|
||||||
|
return $this->createOrder($email, $tickets);
|
||||||
|
}
|
||||||
|
|
||||||
if ($tickets->count() < $ticketQuantity) {
|
public function findTickets($quantity)
|
||||||
|
{
|
||||||
|
$tickets = $this->tickets()->available()->take($quantity)->get();
|
||||||
|
|
||||||
|
if ($tickets->count() < $quantity) {
|
||||||
throw new NotEnoughTicketsException;
|
throw new NotEnoughTicketsException;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $tickets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createOrder($email, $tickets)
|
||||||
|
{
|
||||||
$order = $this->orders()->create([
|
$order = $this->orders()->create([
|
||||||
'email' => $email,
|
'email' => $email,
|
||||||
'amount' => $ticketQuantity * $this->ticket_price,
|
'amount' => $tickets->count() * $this->ticket_price,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($tickets as $ticket) {
|
foreach ($tickets as $ticket) {
|
||||||
|
|||||||
@@ -30,16 +30,17 @@ class ConcertOrdersController extends Controller
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
// Find some tickets
|
// Find some tickets
|
||||||
// Charge the customer for the tickets
|
$tickets = $concert->findTickets(request('ticket_quantity'));
|
||||||
// Create an order for those tickets
|
|
||||||
|
|
||||||
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
// Charge the customer for the tickets
|
||||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||||
|
|
||||||
|
// Create an order for those tickets
|
||||||
|
$order = $concert->createOrder(request('email'), $tickets);
|
||||||
|
|
||||||
return response()->json($order, 201);
|
return response()->json($order, 201);
|
||||||
|
|
||||||
} catch (PaymentFailedException $e) {
|
} catch (PaymentFailedException $e) {
|
||||||
$order->cancel();
|
|
||||||
return response()->json([], 422);
|
return response()->json([], 422);
|
||||||
} catch (NotEnoughTicketsException $e) {
|
} catch (NotEnoughTicketsException $e) {
|
||||||
return response()->json([], 422);
|
return response()->json([], 422);
|
||||||
|
|||||||
Reference in New Issue
Block a user