Skip to content

Commit

Permalink
Merge pull request #440 from liberu-ecommerce/sweep/Enhance-Checkout-…
Browse files Browse the repository at this point in the history
…Process-with-Guest-Checkout-and-Comprehensive-Order-Flow

Enhance Checkout Process with Guest Checkout and Comprehensive Order Flow
  • Loading branch information
curtisdelicata authored Dec 25, 2024
2 parents 320a340 + 4713b75 commit 2aacca3
Showing 1 changed file with 95 additions and 6 deletions.
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)
}

0 comments on commit 2aacca3

Please sign in to comment.