Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Checkout Process with Guest Checkout and Comprehensive Order Flow #440

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 95 additions & 6 deletions app/Http/Controllers/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Models\User;
use App\Notifications\LowStockNotification;
use Illuminate\Support\Facades\Notification;

use App\Factories\PaymentGatewayFactory;

class CheckoutController extends Controller
Expand All @@ -26,17 +25,107 @@ public function __construct(ShippingService $shippingService)
$this->shippingService = $shippingService;
}

// ... (keep other methods unchanged)
public function initiateCheckout(Request $request)
{
$isGuest = Session::get('is_guest', false);
$cart = Session::get('cart', []);

if (empty($cart)) {
return redirect()->route('products.index')
->with('error', 'Your cart is empty');
}

$shippingMethods = $this->shippingService->getAvailableShippingMethods();

return view('checkout.checkout', [
'cart' => $cart,
'shippingMethods' => $shippingMethods,
'isGuest' => $isGuest
]);
}

public function processCheckout(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'shipping_address' => 'required|string',
'shipping_method_id' => 'required|exists:shipping_methods,id',
'payment_method' => 'required|string'
]);

if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}

$cart = Session::get('cart', []);

if (empty($cart)) {
return redirect()->route('products.index')
->with('error', 'Your cart is empty');
}

// Calculate total amount
$subtotal = collect($cart)->sum(function($item) {
return $item['price'] * $item['quantity'];
});

$shippingMethod = ShippingMethod::find($request->shipping_method_id);
$totalAmount = $subtotal + $shippingMethod->price;

// Create order
$order = Order::create([
'customer_email' => $request->email,
'shipping_address' => $request->shipping_address,
'shipping_method_id' => $request->shipping_method_id,
'payment_method' => $request->payment_method,
'total_amount' => $totalAmount,
'status' => 'pending'
]);

// Process payment
$paymentResult = $this->processPayment($order, $request->payment_method);

if ($paymentResult['success']) {
$order->update(['status' => 'paid']);

// Clear cart
Session::forget('cart');

return redirect()->route('checkout.confirmation', ['order' => $order->id])
->with('success', 'Order placed successfully!');
}

$order->update(['status' => 'failed']);
return redirect()->back()
->with('error', 'Payment failed. Please try again.');
}

public function showConfirmation(Order $order)
{
return view('checkout.confirmation', [
'order' => $order
]);
}

public function guestCheckout(Request $request)
{
$cart = Session::get('cart', []);

// Store cart in guest session
Session::put('guest_cart', $cart);
Session::put('is_guest', true);

return redirect()->route('checkout.initiate');
}

protected function processPayment($order, $paymentMethod)
{
$paymentGateway = PaymentGatewayFactory::create($paymentMethod);
return $paymentGateway->processPayment($order->total_amount, [
'order_id' => $order->id,
'customer_email' => $order->customer_email,
// Add any other necessary payment details
'customer_email' => $order->customer_email
]);
}

// ... (keep other methods unchanged)
}
Loading