File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/AdminPaymentController.php
Back
<?php /** * AdminPaymentController * Admin: Payment monitoring, slip verification, Payment Account management */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/PaymentService.php'; require_once __DIR__ . '/../Services/PaymentAccountService.php'; require_once __DIR__ . '/../Services/AdminPermissionService.php'; require_once __DIR__ . '/../Helpers/Auth.php'; require_once __DIR__ . '/../Helpers/Session.php'; class AdminPaymentController extends Controller { private PaymentService $paymentService; private PaymentAccountService $accountService; private AdminPermissionService $permService; public function __construct() { $this->paymentService = new PaymentService(); $this->accountService = new PaymentAccountService(); $this->permService = new AdminPermissionService(); } // ── Payment Listing ──────────────────────────────────────────── /** * GET /admin/payments */ public function index(): void { $adminId = Auth::id(); if (!$this->permService->has($adminId, 'finance.view') && !$this->permService->has($adminId, 'payment.verify')) { Session::setFlash('danger', 'คุณไม่มีสิทธิ์เข้าถึงส่วนนี้'); $this->redirect('/admin'); } $filters = [ 'q' => trim($_GET['q'] ?? ''), 'status' => $_GET['status'] ?? 'all', 'method' => $_GET['method'] ?? 'all', 'date_from' => $_GET['date_from'] ?? '', 'date_to' => $_GET['date_to'] ?? '', ]; $page = max(1, (int)($_GET['page'] ?? 1)); $perPage = 20; $offset = ($page - 1) * $perPage; require_once __DIR__ . '/../Models/Payment.php'; $paymentModel = new Payment(); $payments = $paymentModel->getAdminPayments($filters, $perPage, $offset); $total = $paymentModel->countAdminPayments($filters); $stats = $paymentModel->getPaymentStats(); $this->render('admin/payments/index', [ 'payments' => $payments, 'stats' => $stats, 'filters' => $filters, 'page' => $page, 'perPage' => $perPage, 'totalPages' => (int)ceil($total / $perPage), 'total' => $total, 'pageTitle' => 'จัดการการชำระเงิน — Admin', 'activeNav' => 'payments', ], 'admin'); } /** * GET /admin/payments/{order_no} */ public function show(string $orderNo): void { $adminId = Auth::id(); if (!$this->permService->has($adminId, 'finance.view') && !$this->permService->has($adminId, 'payment.verify')) { Session::setFlash('danger', 'คุณไม่มีสิทธิ์เข้าถึงส่วนนี้'); $this->redirect('/admin/payments'); } $payment = $this->paymentService->getPaymentForAdmin($orderNo); if (!$payment) { Session::setFlash('danger', 'ไม่พบรายการชำระเงินนี้'); $this->redirect('/admin/payments'); } $this->render('admin/payments/show', [ 'payment' => $payment, 'orderNo' => $orderNo, 'pageTitle' => 'Payment Detail #' . $orderNo . ' — Admin', 'activeNav' => 'payments', ], 'admin'); } // ── Verification ──────────────────────────────────────────────── /** * POST /admin/payments/verify * Confirm or Reject a payment attempt or direct payment */ public function verify(): void { $adminId = Auth::id(); if (!$this->permService->has($adminId, 'payment.verify') && !Auth::isAdmin()) { $this->json(['success' => false, 'message' => 'ไม่มีสิทธิ์ verify การชำระเงิน'], 403); } $action = $_POST['action'] ?? ''; // 'confirm' | 'reject' $attemptId = (int)($_POST['attempt_id'] ?? 0); $paymentId = (int)($_POST['payment_id'] ?? 0); $reason = trim($_POST['reason'] ?? ''); $note = trim($_POST['note'] ?? ''); $orderNo = trim($_POST['order_no'] ?? ''); $redirect = trim($_POST['redirect_to'] ?? ''); if (!in_array($action, ['confirm', 'reject'], true)) { $this->json(['success' => false, 'message' => 'ข้อมูลการดำเนินการไม่ถูกต้อง'], 422); } if ($action === 'reject' && empty($reason)) { $this->json(['success' => false, 'message' => 'กรุณาระบุเหตุผลในการปฏิเสธ'], 422); } if ($attemptId > 0) { if ($action === 'confirm') { $result = $this->paymentService->confirmPayment($attemptId, $adminId, $note ?: null); } else { $result = $this->paymentService->rejectPayment($attemptId, $adminId, $reason); } } elseif ($paymentId > 0 || !empty($orderNo)) { if (!$paymentId && !empty($orderNo)) { $p = $this->paymentService->getPaymentForAdmin($orderNo); $paymentId = $p ? (int)$p['id'] : 0; } if ($paymentId > 0) { if ($action === 'confirm') { $result = $this->paymentService->confirmPaymentDirect($paymentId, $adminId, $note ?: null); } else { $result = $this->paymentService->rejectPaymentDirect($paymentId, $adminId, $reason); } } else { $result = ['success' => false, 'message' => 'ไม่พบรายการชำระเงินสำหรับคำสั่งซื้อนี้']; } } else { $result = ['success' => false, 'message' => 'ไม่พบข้อมูลสลิปหรือรายการชำระเงินที่ต้องการตรวจสอบ']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) || (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json'))) { $this->json($result, $result['success'] ? 200 : 422); } if ($result['success']) { Session::setFlash('success', $result['message']); } else { Session::setFlash('danger', $result['message']); } if (!empty($redirect)) { $this->redirect($redirect); } elseif (!empty($orderNo)) { $this->redirect('/admin/payments/' . $orderNo); } else { $this->redirect('/admin/payments'); } } // ── Payment Accounts ─────────────────────────────────────────── /** * GET /admin/payments/accounts */ public function accounts(): void { $adminId = Auth::id(); if (!$this->permService->has($adminId, 'finance.view') && !$this->permService->has($adminId, 'payment.manage_accounts')) { Session::setFlash('danger', 'คุณไม่มีสิทธิ์เข้าถึงส่วนนี้'); $this->redirect('/admin'); } $accounts = $this->accountService->getAll(); $this->render('admin/payments/accounts', [ 'accounts' => $accounts, 'pageTitle' => 'บัญชีรับเงิน — Admin', 'activeNav' => 'payments', ], 'admin'); } /** * GET /admin/payments/accounts/create */ public function createAccount(): void { $this->requireAccountPermission(); $this->render('admin/payments/account_form', [ 'account' => null, 'pageTitle' => 'เพิ่มบัญชีรับเงิน — Admin', 'activeNav' => 'payments', ], 'admin'); } /** * POST /admin/payments/accounts */ public function storeAccount(): void { $adminId = Auth::id(); $this->requireAccountPermission(); $data = $_POST; $data['_qr_file'] = $_FILES['qr_image'] ?? []; $result = $this->accountService->create($data, $adminId); if ($result['success']) { Session::setFlash('success', $result['message']); $this->redirect('/admin/payments/accounts'); } else { Session::setFlash('danger', implode(' / ', $result['errors'] ?? [$result['message'] ?? 'เกิดข้อผิดพลาด'])); $this->redirect('/admin/payments/accounts/create'); } } /** * GET /admin/payments/accounts/{id}/edit */ public function editAccount(int $id): void { $this->requireAccountPermission(); $account = $this->accountService->findById($id); if (!$account) { Session::setFlash('danger', 'ไม่พบบัญชีรับเงิน'); $this->redirect('/admin/payments/accounts'); } $this->render('admin/payments/account_form', [ 'account' => $account, 'pageTitle' => 'แก้ไขบัญชีรับเงิน — Admin', 'activeNav' => 'payments', ], 'admin'); } /** * POST /admin/payments/accounts/{id}/edit */ public function updateAccount(int $id): void { $adminId = Auth::id(); $this->requireAccountPermission(); $data = $_POST; $data['_qr_file'] = $_FILES['qr_image'] ?? []; $result = $this->accountService->update($id, $data, $adminId); if ($result['success']) { Session::setFlash('success', $result['message']); $this->redirect('/admin/payments/accounts'); } else { Session::setFlash('danger', implode(' / ', $result['errors'] ?? [$result['message'] ?? 'เกิดข้อผิดพลาด'])); $this->redirect('/admin/payments/accounts/' . $id . '/edit'); } } /** * POST /admin/payments/accounts/{id}/toggle */ public function toggleAccount(int $id): void { $adminId = Auth::id(); $this->requireAccountPermission(); $result = $this->accountService->toggleActive($id, $adminId); $this->json($result); } /** * POST /admin/payments/accounts/{id}/set-default */ public function setDefaultAccount(int $id): void { $adminId = Auth::id(); $this->requireAccountPermission(); $result = $this->accountService->setDefault($id, $adminId); $this->json($result); } private function requireAccountPermission(): void { $adminId = Auth::id(); if (!$this->permService->has($adminId, 'payment.manage_accounts')) { Session::setFlash('danger', 'คุณไม่มีสิทธิ์จัดการบัญชีรับเงิน'); $this->redirect('/admin/payments/accounts'); } } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings