This commit is contained in:
baodev0
2026-09-23 15:17:18 +07:00
parent cd38c5d6d0
commit 82a71244ca
107 changed files with 3332 additions and 3306 deletions
@@ -118,7 +118,13 @@ class OrderControllerRefactor extends Controller
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
}
$this->orderService->pay($order, $request->payment_method);
try {
$this->orderService->pay($order, $request->payment_method);
} catch (\Throwable $e) {
// Số dư không đủ / đơn đã thanh toán là lỗi nghiệp vụ, không phải lỗi server —
// trả 422 cho khớp với payClientInvoiceByNumber.
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
}
$this->activityLogger->log(
event: 'payment',
@@ -213,8 +219,19 @@ class OrderControllerRefactor extends Controller
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
}
// Bỏ trống `amount` = trả hết phần còn thiếu. Truyền số nhỏ hơn để trả từng phần khi
// số dư chưa đủ — hoá đơn giữ nguyên chưa thanh toán cho tới khi thu đủ.
$validated = $request->validate([
'amount' => 'nullable|integer|min:1',
]);
try {
$paidInvoice = $this->invoicePaymentService->payWithBalance($invoice, $request->user(), 'balance');
$paidInvoice = $this->invoicePaymentService->payWithBalance(
$invoice,
$request->user(),
'balance',
$validated['amount'] ?? null,
);
$this->activityLogger->log(
event: 'payment',
@@ -224,9 +241,13 @@ class OrderControllerRefactor extends Controller
newValues: ['invoice_number' => $paidInvoice->invoice_number, 'total' => $paidInvoice->total],
);
$outstanding = \App\Services\InvoicePaymentService::outstanding($paidInvoice);
return response()->json([
'success' => true,
'message' => 'Thanh toán hóa đơn thành công.',
'message' => $outstanding > 0
? 'Đã ghi nhận thanh toán một phần. Còn thiếu ' . number_format($outstanding) . ' VNĐ để kích hoạt dịch vụ.'
: 'Thanh toán hóa đơn thành công.',
'data' => new InvoiceDetailResource($paidInvoice),
]);
} catch (\Throwable $e) {