FleetCart/Modules/Checkout/Http/Controllers/CheckoutCompleteController.php
2023-06-11 13:14:03 +01:00

62 lines
1.5 KiB
PHP

<?php
namespace Modules\Checkout\Http\Controllers;
use Exception;
use Modules\Order\Entities\Order;
use Modules\Payment\Facades\Gateway;
use Modules\Checkout\Events\OrderPlaced;
use Modules\Checkout\Services\OrderService;
class CheckoutCompleteController
{
/**
* Store a newly created resource in storage.
*
* @param int $orderId
* @param \Modules\Checkout\Services\OrderService $orderService
* @return \Illuminate\Http\Response
*/
public function store($orderId, OrderService $orderService)
{
$order = Order::findOrFail($orderId);
$gateway = Gateway::get(request('paymentMethod'));
try {
$response = $gateway->complete($order);
} catch (Exception $e) {
$orderService->delete($order);
return response()->json([
'message' => $e->getMessage(),
], 403);
}
$order->storeTransaction($response);
event(new OrderPlaced($order));
if (! request()->ajax()) {
return redirect()->route('checkout.complete.show');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show()
{
$order = session('placed_order');
if (is_null($order)) {
return redirect()->route('home');
}
return view('public.checkout.complete.show', compact('order'));
}
}