|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services\Customer; |
| 4 | + |
| 5 | +use App\Repositories\Interfaces\CartRepositoryInterface; |
| 6 | +use App\Repositories\Interfaces\ItemRepositoryInterface; |
| 7 | +use App\Repositories\Interfaces\OrderRepositoryInterface; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | + |
| 10 | +class CheckoutService |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Inject required repositories |
| 14 | + * |
| 15 | + * @param CartRepositoryInterface $cartRepo |
| 16 | + * @param OrderRepositoryInterface $orderRepo |
| 17 | + * @param ItemRepositoryInterface $itemRepo |
| 18 | + */ |
| 19 | + public function __construct( |
| 20 | + protected CartRepositoryInterface $cartRepo, |
| 21 | + protected OrderRepositoryInterface $orderRepo, |
| 22 | + protected ItemRepositoryInterface $itemRepo |
| 23 | + ) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Process checkout and create order from cart |
| 27 | + * |
| 28 | + * @param int $userId |
| 29 | + * @param array $checkoutData |
| 30 | + * @return int Order ID |
| 31 | + * @throws \Exception |
| 32 | + */ |
| 33 | + public function processCheckout(int $userId, array $checkoutData): int |
| 34 | + { |
| 35 | + // Validate cart has items |
| 36 | + $cartItems = $this->cartRepo->getUserCart($userId); |
| 37 | + |
| 38 | + if ($cartItems->isEmpty()) { |
| 39 | + throw new \Exception('Your cart is empty'); |
| 40 | + } |
| 41 | + |
| 42 | + // Validate stock availability |
| 43 | + $this->validateStock($cartItems); |
| 44 | + |
| 45 | + // Calculate totals |
| 46 | + $totalAmount = $this->calculateTotal($cartItems); |
| 47 | + |
| 48 | + DB::beginTransaction(); |
| 49 | + try { |
| 50 | + // Create order |
| 51 | + $order = $this->orderRepo->create([ |
| 52 | + 'user_id' => $userId, |
| 53 | + 'status' => 'pending', |
| 54 | + 'payment_method' => $checkoutData['payment_method'], |
| 55 | + 'delivery_method' => $checkoutData['delivery_method'], |
| 56 | + 'delivery_address' => $checkoutData['delivery_address'] ?? null, |
| 57 | + 'total_amount' => $totalAmount, |
| 58 | + ]); |
| 59 | + |
| 60 | + // Create order items and deduct stock |
| 61 | + foreach ($cartItems as $cartItem) { |
| 62 | + // Create order item |
| 63 | + $this->orderRepo->query() |
| 64 | + ->find($order->id) |
| 65 | + ->orderItems() |
| 66 | + ->create([ |
| 67 | + 'item_id' => $cartItem->item_id, |
| 68 | + 'quantity' => $cartItem->quantity, |
| 69 | + 'unit_price' => $cartItem->price |
| 70 | + ]); |
| 71 | + |
| 72 | + // Deduct stock from product |
| 73 | + $product = $this->itemRepo->find($cartItem->item_id); |
| 74 | + $this->itemRepo->update($product->id, [ |
| 75 | + 'quantity' => $product->quantity - $cartItem->quantity |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + // Clear cart after successful order |
| 80 | + $this->cartRepo->clearUserCart($userId); |
| 81 | + |
| 82 | + DB::commit(); |
| 83 | + |
| 84 | + return $order->id; |
| 85 | + } catch (\Exception $e) { |
| 86 | + DB::rollBack(); |
| 87 | + throw new \Exception('Failed to process checkout. Please try again.'); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Validate stock availability for cart items |
| 93 | + * |
| 94 | + * @param \Illuminate\Database\Eloquent\Collection $cartItems |
| 95 | + * @return void |
| 96 | + * @throws \Exception |
| 97 | + */ |
| 98 | + protected function validateStock($cartItems): void |
| 99 | + { |
| 100 | + foreach ($cartItems as $cartItem) { |
| 101 | + $product = $this->itemRepo->find($cartItem->item_id); |
| 102 | + |
| 103 | + if (! $product) { |
| 104 | + throw new \Exception('Product not found'); |
| 105 | + } |
| 106 | + |
| 107 | + if ($product->quantity < $cartItem->quantity) { |
| 108 | + throw new \Exception('Insufficient stock'); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Calculate total amount from cart items |
| 115 | + * |
| 116 | + * @param \Illuminate\Database\Eloquent\Collection $cartItems |
| 117 | + * @return float |
| 118 | + */ |
| 119 | + protected function calculateTotal($cartItems): float |
| 120 | + { |
| 121 | + return $cartItems->sum(function ($cartItem) { |
| 122 | + return $cartItem->price * $cartItem->quantity; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get checkout summary |
| 128 | + * |
| 129 | + * @param int $userId |
| 130 | + * @return array |
| 131 | + * @throws \Exception |
| 132 | + */ |
| 133 | + public function getCheckoutSummary(int $userId): array |
| 134 | + { |
| 135 | + $cartItems = $this->cartRepo->getUserCart($userId); |
| 136 | + |
| 137 | + if ($cartItems->isEmpty()) { |
| 138 | + throw new \Exception('Your cart is empty'); |
| 139 | + } |
| 140 | + |
| 141 | + $subTotal = $this->calculateTotal($cartItems); |
| 142 | + |
| 143 | + return [ |
| 144 | + 'items' => $cartItems, |
| 145 | + 'subTotal' => $subTotal, |
| 146 | + 'total' => $subTotal, |
| 147 | + 'item_count' => $cartItems->sum('quantity') |
| 148 | + ]; |
| 149 | + } |
| 150 | +} |
0 commit comments