File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/CheckoutController.php
Back
<?php /** * CheckoutController * Handles Multi-Vendor checkout review, shipping address selection, and order placement */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/CartService.php'; require_once __DIR__ . '/../Services/OrderService.php'; require_once __DIR__ . '/../Services/AddressService.php'; require_once __DIR__ . '/../Services/PaymentService.php'; require_once __DIR__ . '/../Helpers/Auth.php'; require_once __DIR__ . '/../Helpers/Session.php'; require_once __DIR__ . '/../Helpers/Url.php'; class CheckoutController extends Controller { private CartService $cartService; private OrderService $orderService; private AddressService $addressService; private PaymentService $paymentService; public function __construct() { $this->cartService = new CartService(); $this->orderService = new OrderService(); $this->addressService = new AddressService(); $this->paymentService = new PaymentService(); } public function index(): void { $userId = Auth::id(); $cartData = $this->cartService->getCart($userId); // Filter only stores with selected and available items $checkoutStores = []; $checkoutTotal = 0; $totalShipping = 0; foreach ($cartData['stores'] as $store) { $selectedItems = array_filter($store['items'], fn($it) => $it['is_selected'] && $it['is_available']); if (!empty($selectedItems)) { $subtotal = 0; foreach ($selectedItems as $it) { $subtotal += (float)$it['price'] * (int)$it['quantity']; } $shipping = 50.00; $store['selected_items'] = $selectedItems; $store['store_subtotal'] = $subtotal; $store['shipping_fee'] = $shipping; $store['store_total'] = $subtotal + $shipping; $checkoutStores[] = $store; $checkoutTotal += $subtotal; $totalShipping += $shipping; } } if (empty($checkoutStores)) { Session::setFlash('warning', 'กรุณาเลือกสินค้าในรถเข็นก่อนดำเนินการสั่งซื้อ'); $this->redirect('/cart'); } $addresses = $this->addressService->getAddresses($userId); require_once __DIR__ . '/../Models/PaymentAccount.php'; $accountModel = new PaymentAccount(); $primaryBank = $accountModel->getActiveByMethod('BankTransfer'); $primaryPromptpay = $accountModel->getActiveByMethod('PromptPay'); $this->render('cart/checkout', [ 'stores' => $checkoutStores, 'addresses' => $addresses, 'primaryBank' => $primaryBank, 'primaryPromptpay' => $primaryPromptpay, 'subtotal' => $checkoutTotal, 'total_shipping' => $totalShipping, 'grand_total' => $checkoutTotal + $totalShipping, 'pageTitle' => __('checkout') . ' - CARGOO' ], 'main'); } public function placeOrder(): void { $userId = Auth::id(); $addressId = (int)($_POST['shipping_address_id'] ?? 0); $paymentMethod = $_POST['payment_method'] ?? 'PromptPay'; $note = trim($_POST['note'] ?? ''); if (!$addressId) { Session::setFlash('danger', 'กรุณาเลือกหรือเพิ่มที่อยู่ในการจัดส่งสินค้า'); $this->redirect('/checkout'); } $result = $this->orderService->placeOrder($userId, $addressId, $paymentMethod, $note); if (!$result['success']) { Session::setFlash('danger', $result['message']); $this->redirect('/checkout'); } // Initiate Payment for each created order // We redirect to the primary order's payment page $primaryOrderNo = $result['primary_order_no']; $primaryOrderId = null; require_once __DIR__ . '/../Models/Order.php'; $orderModel = new Order(); foreach ($result['order_nos'] as $orderNo) { $order = $orderModel->fetchOne( "SELECT id, total_amount FROM `orders` WHERE order_no = :no LIMIT 1", [':no' => $orderNo] ); if ($order) { $this->paymentService->initiatePayment( (int)$order['id'], $paymentMethod, $userId, (float)$order['total_amount'] ); if ($orderNo === $primaryOrderNo) { $primaryOrderId = (int)$order['id']; } } } // For COD: go to success page directly; for others: go to payment page if ($paymentMethod === 'COD') { Session::setFlash('success', 'สั่งซื้อสินค้าสำเร็จแล้ว! ทีมงานจะเตรียมสินค้าให้คุณเร็วๆ นี้'); $this->redirect('/payment/' . $primaryOrderNo . '/success'); } else { Session::setFlash('success', 'สั่งซื้อสินค้าเรียบร้อยแล้ว! กรุณาชำระเงินเพื่อดำเนินการต่อ'); $this->redirect('/payment/' . $primaryOrderNo); } } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings