Files

126 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Mail\OrderConfirmationMail;
use App\Models\Order;
use App\Models\OrderItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class CheckoutController extends Controller
{
public function index()
{
$cart = session()->get('cart', []);
if (empty($cart)) {
return redirect()->route('cart.index')->with('error', 'Je winkelwagen is nog leeg.');
}
$subtotal = 0;
foreach ($cart as $item) {
$subtotal += $item['price'] * $item['quantity'];
}
$shipping = $subtotal > 50 ? 0.00 : 4.95;
$total = $subtotal + $shipping;
return view('checkout.index', compact('cart', 'subtotal', 'shipping', 'total'));
}
public function store(Request $request)
{
$cart = session()->get('cart', []);
if (empty($cart)) {
return redirect()->route('cart.index')->with('error', 'Je winkelwagen is leeg.');
}
$validated = $request->validate([
'customer_name' => 'required|string|max:255',
'customer_email' => 'required|email|max:255',
'customer_phone' => 'nullable|string|max:50',
'shipping_street' => 'required|string|max:255',
'shipping_number' => 'required|string|max:50',
'shipping_postal_code' => 'required|string|max:20',
'shipping_city' => 'required|string|max:100',
'notes' => 'nullable|string|max:1000',
'payment_method' => 'required|string|in:ideal,bancontact,overboeking',
]);
$subtotal = 0;
foreach ($cart as $item) {
$subtotal += $item['price'] * $item['quantity'];
}
$shipping = $subtotal > 50 ? 0.00 : 4.95;
$total = $subtotal + $shipping;
$orderNumber = 'J3D-'.date('Y').'-'.strtoupper(Str::random(6));
$order = Order::create([
'user_id' => auth()->id(),
'order_number' => $orderNumber,
'customer_name' => $validated['customer_name'],
'customer_email' => $validated['customer_email'],
'customer_phone' => $validated['customer_phone'] ?? null,
'shipping_street' => $validated['shipping_street'],
'shipping_number' => $validated['shipping_number'],
'shipping_postal_code' => $validated['shipping_postal_code'],
'shipping_city' => $validated['shipping_city'],
'shipping_country' => 'Nederland',
'notes' => $validated['notes'] ?? null,
'status' => 'in_behandeling',
'subtotal' => $subtotal,
'shipping_cost' => $shipping,
'total' => $total,
'payment_status' => 'in_afwachting',
'payment_method' => $validated['payment_method'],
]);
foreach ($cart as $item) {
$productName = $item['name'];
if (! empty($item['custom_model_url'])) {
$productName .= ' (Link: '.$item['custom_model_url'].')';
}
if (! empty($item['custom_file_name'])) {
$productName .= ' [Bestand: '.$item['custom_file_name'].']';
}
OrderItem::create([
'order_id' => $order->id,
'product_id' => $item['id'] ?? null,
'product_name' => $productName,
'material' => $item['material'] ?? null,
'color' => $item['color'] ?? null,
'scale' => $item['scale'] ?? '100%',
'infill' => $item['infill'] ?? 'Standaard (20%)',
'quantity' => $item['quantity'],
'unit_price' => $item['price'],
'total_price' => $item['price'] * $item['quantity'],
]);
}
// Stuur mooie bestelbevestiging e-mail
try {
Mail::to($order->customer_email)->send(new OrderConfirmationMail($order->load('items')));
} catch (\Throwable $e) {
report($e);
}
// Leeg winkelwagen na succesvolle plaatsing
session()->forget('cart');
return redirect()->route('checkout.success', $order->order_number);
}
public function success($orderNumber)
{
$order = Order::with('items')->where('order_number', $orderNumber)->firstOrFail();
return view('checkout.success', compact('order'));
}
}