Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CheckoutController | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| process | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\admin\Customer; |
| 6 | use App\Service\OrderService; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Illuminate\Support\Facades\Session; |
| 9 | |
| 10 | class CheckoutController extends Controller |
| 11 | { |
| 12 | /** |
| 13 | * Display checkout page |
| 14 | */ |
| 15 | public function index() |
| 16 | { |
| 17 | return view('website.checkout'); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Process checkout and create order |
| 22 | */ |
| 23 | public function process(Request $request) |
| 24 | { |
| 25 | // Validate form input |
| 26 | $validated = $request->validate([ |
| 27 | 'name' => 'required|string|max:255', |
| 28 | 'email' => 'required|email|max:255', |
| 29 | 'phone' => 'required|string|max:20', |
| 30 | 'address' => 'required|string|max:500', |
| 31 | 'shipping_method' => 'required|in:inside,outside', |
| 32 | ], [ |
| 33 | 'name.required' => 'Customer name is required', |
| 34 | 'email.required' => 'Email is required', |
| 35 | 'phone.required' => 'Phone number is required', |
| 36 | 'address.required' => 'Address is required', |
| 37 | 'shipping_method.required' => 'Please select a shipping method', |
| 38 | ]); |
| 39 | |
| 40 | try { |
| 41 | // Set shipping cost in session based on selected method |
| 42 | $shippingCost = $request->shipping_method === 'inside' ? 70 : 120; |
| 43 | Session::put('shipping_cost', $shippingCost); |
| 44 | Session::put('shipping_method', $request->shipping_method); |
| 45 | |
| 46 | // Create or update customer |
| 47 | $customer = Customer::updateOrCreate( |
| 48 | ['email' => $request->email], |
| 49 | [ |
| 50 | 'name' => $request->name, |
| 51 | 'phone' => $request->phone, |
| 52 | 'address' => $request->address, |
| 53 | ] |
| 54 | ); |
| 55 | |
| 56 | // Create order using OrderService |
| 57 | $order = OrderService::storeOrder($customer); |
| 58 | |
| 59 | if ($order) { |
| 60 | // Clear cart from session after successful order |
| 61 | Session::forget('cart'); |
| 62 | Session::forget('shipping_cost'); |
| 63 | Session::forget('shipping_method'); |
| 64 | |
| 65 | // Redirect to thank-you page with reference number (not order ID for security) |
| 66 | return redirect('/thank-you?ref=' . $order->bar_code); |
| 67 | } else { |
| 68 | return redirect()->back()->with('error', 'Failed to create order. Please try again.'); |
| 69 | } |
| 70 | |
| 71 | } catch (\Exception $e) { |
| 72 | return redirect()->back()->with('error', 'An error occurred: ' . $e->getMessage()); |
| 73 | } |
| 74 | } |
| 75 | } |