XEM CODE TRONG PHẦN BACKEND Hãy chỉnh sửa cập nhật lại sao cho phần gửi tenants.email của đại diện phòng room.id tạo được hoá đơn và gửi được link thanh toán vnpay với giá trị bằng total của invoice
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Từ code được cung cấp, tôi thấy đây là phần frontend của ứng dụng và không thấy code backend được cung cấp. Tuy nhiên, dựa trên logic hiện tại trong frontend, tôi có thể gợi ý cách thực hiện như sau:
Trong backend, bạn cần:
[Redacted
// Controller để tạo và gửi hóa đơn
@PostMapping("/create-invoice-payment")
public ResponseEntity<?> createInvoiceAndPayment(@RequestBody InvoicePaymentRequest request) {
try {
// 1. Tìm tenant đại diện của phòng
Tenant representative = tenantRepository.findRepresentativeByRoomId(request.getRoomId());
// 2. Tạo hóa đơn mới
Invoice invoice = new Invoice();
invoice.setRoom(roomRepository.findById(request.getRoomId()).get());
invoice.setTotal(request.getTotal());
invoice.setCreatedDate(new Date());
// Set các thông tin khác của hóa đơn
Invoice savedInvoice = invoiceRepository.save(invoice);
// 3. Tạo URL thanh toán VNPay
String vnpayUrl = createVNPayUrl(savedInvoice.getId(),
savedInvoice.getTotal(),
representative.getEmail());
// 4. Gửi email chứa link thanh toán
emailService.sendPaymentLink(representative.getEmail(), vnpayUrl);
return ResponseEntity.ok(new PaymentResponse(vnpayUrl));
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
// Helper method để tạo URL VNPay
private String createVNPayUrl(Long invoiceId, BigDecimal amount, String email) {
// Cấu hình các tham số cho VNPay
String vnp_Version = "2.1.0";
String vnp_Command = "pay";
String vnp_TxnRef = String.valueOf(invoiceId);
String vnp_Amount = String.valueOf(amount.multiply(new BigDecimal("100")).longValue());
// ... các tham số khác của VNPay
// Tạo URL thanh toán
return VNPayConfig.createPaymentUrl(vnpParams);
}
Trong frontend, bạn cần cập nhật phần gọi API:
javascript
// Trong file RoomManager/index.js, thêm hàm xử lý thanh toán:
const handlePayment = async (roomId) => {
try {
const response = await axios.post(
`${process.env.REACT_APP_API_BASE_URL}/api/create-invoice-payment`,
{
roomId: roomId,
total: calculateTotal(roomId) // Hàm tính tổng tiền cần thanh toán
},
{
headers: {
Authorization: `Bearer ${getToken()}`,
'Content-Type': 'application/json',
}
}
);
// Chuyển hướng đến trang thanh toán VNPay
if (response.data.paymentUrl) {
window.location.href = response.data.paymentUrl;
}
} catch (error) {
console.error('Lỗi khi tạo hóa đơn:', error);
alert('Đã xảy ra lỗi khi tạo hóa đơn thanh toán.');
}
};