bill
------------------------------viewBill.jsp-----------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="bills-container">
<h2>Your Bills</h2>
<form action="payment" method="post">
<c:forEach items="${bills}" var="bill">
<div class="bill-item">
<div class="checkbox-container">
<input type="checkbox" name="selectedBills" value="${bill.id}"
onchange="updateTotal(this, ${bill.amount})">
<div>
<div>Bill Date: ${bill.dueDate}</div>
<div>Amount: ₹${bill.amount}</div>
</div>
</div>
</div>
</c:forEach>
<div class="total-section">
<h3>Total Amount: ₹<span id="totalAmount">0</span></h3>
<input type="hidden" name="totalAmountValue" id="totalAmountValue" value="0">
</div>
<button type="submit" class="proceed-btn">Proceed to Pay</button>
</form>
</div>
<script>
let total = 0;
function updateTotal(checkbox, amount) {
if (checkbox.checked) {
total += amount;
} else {
total -= amount;
}
document.getElementById('totalAmount').textContent = total.toFixed(2);
document.getElementById('totalAmountValue').value = total.toFixed(2);
}
</script>
</body>
</html>
--------------------------------------------PaymentServlet.java-----------------------------------
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/payment")
public class PaymentServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String[] selectedBills = request.getParameterValues("selectedBills");
String totalAmount = request.getParameter("totalAmountValue");
// Store in session
request.getSession().setAttribute("totalAmount", Double.valueOf(totalAmount));
request.getRequestDispatcher("payment.jsp").forward(request, response);
if (selectedBills != null && selectedBills.length > 0) {
request.setAttribute("selectedBills", String.join(", ", selectedBills));
request.setAttribute("totalAmount", totalAmount);
request.getRequestDispatcher("payment.jsp").forward(request, response);
} else {
response.sendRedirect("viewBill?error=noSelection");
}
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("viewBill?error=true");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("viewBill");
}
}
--------------------------------------payment.jsp-------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="payment-container">
<h2>Payment Details</h2>
<div class="bill-summary">
<h3>Bill Summary</h3>
<p>Total Amount: ₹${totalAmount}</p>
<p>Selected Bill IDs: ${selectedBills}</p>
</div>
<div class="buttons">
<button onclick="location.href='cardPayment.jsp'" class="btn-pay">Pay Now</button>
<button onclick="location.href='home.jsp'" class="btn-cancel">Back to Home</button>
</div>
</div>
</body>
</html>
----------------------------------------cardPayment.jsp-------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html> <html> <head> <title>Payment - Electricity Billing System</title> <style> .topbar { background-color: #003366; padding: 15px; display: flex; justify-content: space-between; align-items: center; color: white; } .menu { display: flex; gap: 20px; } .menu a { color: white; text-decoration: none; padding: 5px 10px; } .payment-container { width: 60%; margin: 20px auto; padding: 20px; border: 1px solid #ddd; border-radius: 5px; } .bill-summary { background-color: #f8f9fa; padding: 15px; border-radius: 4px; margin-bottom: 20px; } .button-group { display: flex; gap: 10px; margin-top: 20px; } .btn { padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; } .btn-primary { background-color: #28a745; color: white; } .btn-secondary { background-color: #6c757d; color: white; } </style> </head> <body> <div class="topbar"> <div class="menu"> <a href="home.jsp">Home</a> <a href="viewBill">Pay Bill</a> <a href="registerComplaint">Register Complaint</a> <a href="complaintStatus">Complaint Status</a> </div> <div class="welcome"> Welcome, ${username} <form action="logout" method="post" style="margin: 0;"> <button type="submit" class="logout-btn">Logout</button> </form> </div> </div><div class="payment-container">
<h2>Payment Details</h2>
<div class="bill-summary">
<h3>Bill Summary</h3>
<p>Total Amount: ₹${totalAmount}</p>
<p>Selected Bill IDs: ${selectedBills}</p>
</div>
<div class="buttons">
<button onclick="location.href='cardPayment.jsp'" class="btn-pay">Pay Now</button>
<button onclick="location.href='home.jsp'" class="btn-cancel">Back to Home</button>
</div>
</div>
</body>
</html>
-----------------------------------------ProcessPaymentServlet.java-------------------------------
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import services.PaymentService;
import java.util.UUID;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@WebServlet("/processPayment")
public class ProcessPaymentServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ProcessPaymentServlet doPost method called");
String cardNumber = request.getParameter("cardNumber");
// String cardHolderName = request.getParameter("cardHolderName");
// String expiryDate = request.getParameter("expiryDate");
//String cvv = request.getParameter("cvv");
try {
// Generate unique transaction ID
String transactionId = "TXN" + UUID.randomUUID().toString().substring(0, 8);
LocalDateTime transactionDate = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = transactionDate.format(formatter);
Double totalAmount = (Double) request.getSession().getAttribute("totalAmount");
// If somehow amount is null, use selected bill amount
if(totalAmount == null) {
System.out.println("Warning: Total amount was null in session");
totalAmount = 1500.00; // Default fallback
}
String userId = (String) request.getSession().getAttribute("userId");
PaymentService paymentService = new PaymentService();
boolean paymentSuccess = paymentService.processPayment(transactionId, userId,
totalAmount, cardNumber);
if (paymentSuccess) {
request.setAttribute("transactionId", transactionId);
request.setAttribute("transactionDate", formattedDateTime);
request.setAttribute("amountPaid", totalAmount);
request.getRequestDispatcher("paymentSuccess.jsp").forward(request, response);
} else {
response.sendRedirect("cardPayment.jsp?error=true");
}
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("cardPayment.jsp?error=true");
}
}
}
---------------------------------------------paymentSuccess.jsp-----------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html> <html> <head> <title>Payment Success - Electricity Billing System</title> <style> .topbar { background-color: #003366; padding: 15px; display: flex; justify-content: space-between; align-items: center; color: white; } .menu { display: flex; gap: 20px; } .menu a { color: white; text-decoration: none; padding: 5px 10px; } .success-container { width: 500px; margin: 20px auto; padding: 20px; border: 1px solid #ddd; border-radius: 5px; text-align: center; } .success-message { color: #28a745; font-size: 24px; margin: 20px 0; } .transaction-details { background-color: #f8f9fa; padding: 20px; border-radius: 4px; margin: 20px 0; text-align: left; } .download-btn { display: inline-block; padding: 10px 20px; background-color: #007bff; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; } .back-btn { display: inline-block; padding: 10px 20px; background-color: #6c757d; color: white; text-decoration: none; border-radius: 4px; margin-top: 10px; } </style> </head> <body> <div class="topbar"> <div class="menu"> <a href="home.jsp">Home</a> <a href="viewBill">Pay Bill</a> <a href="registerComplaint">Register Complaint</a> <a href="complaintStatus">Complaint Status</a> </div> </div> <div class="success-container"> <div class="success-message"> Payment Successful! </div> <div class="transaction-details">
<h3>Transaction Details</h3>
<p><strong>Transaction ID:</strong> ${transactionId}</p>
<p><strong>Amount Paid:</strong> ₹${amountPaid}</p>
<p><strong>Date:</strong> ${transactionDate}</p>
</div>
</div>
<center><a href="downloadReceipt?transactionId=${transactionId}" class="download-btn">
Download Receipt
</a>
<br>
<a href="home.jsp" class="back-btn">Back to Home</a>
</center>
</body>
</html>