paymentGateway = $paymentGateway; } public function store($concertId) { $concert = Concert::published()->findOrFail($concertId); $this->validate(request(), [ 'email' => ['required', 'email'], 'ticket_quantity' => ['required', 'integer', 'min:1'], 'payment_token' => ['required'], ]); try { // Find some tickets $reservation = $concert->reserveTickets(request('ticket_quantity')); // Charge the customer for the tickets $this->paymentGateway->charge($reservation->totalCost(), request('payment_token')); // Create an order for those tickets $order = Order::forTickets($reservation->tickets(), request('email'), $reservation->totalCost()); return response()->json($order, 201); } catch (PaymentFailedException $e) { $reservation->cancel(); return response()->json([], 422); } catch (NotEnoughTicketsException $e) { return response()->json([], 422); } } }