mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Order;
|
|
use App\Concert;
|
|
use App\Reservation;
|
|
use Illuminate\Http\Request;
|
|
use App\Billing\PaymentGateway;
|
|
use App\Billing\PaymentFailedException;
|
|
use App\Exceptions\NotEnoughTicketsException;
|
|
|
|
class ConcertOrdersController extends Controller
|
|
{
|
|
private $paymentGateway;
|
|
|
|
public function __construct(PaymentGateway $paymentGateway)
|
|
{
|
|
$this->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'), request('email'));
|
|
|
|
// 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(), $reservation->email(), $reservation->totalCost());
|
|
|
|
return response()->json($order, 201);
|
|
|
|
} catch (PaymentFailedException $e) {
|
|
$reservation->cancel();
|
|
return response()->json([], 422);
|
|
} catch (NotEnoughTicketsException $e) {
|
|
return response()->json([], 422);
|
|
}
|
|
}
|
|
}
|