update
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user