Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Add handler for checkout with cod payment
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingadonis committed Dec 5, 2023
1 parent 8a6a9f8 commit 3c7eec8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package io.hardingadonis.miu.controller.web;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.model.detail.*;
import io.hardingadonis.miu.services.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.json.simple.*;
import org.json.simple.parser.*;

@WebServlet(name = "CheckoutServlet", urlPatterns = {"/checkout"})
public class CheckoutServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

User user = (User) request.getSession().getAttribute("user");

if (user == null) {
response.sendRedirect("login");
return;
}

long totalPrice = getTotalPrice(getCartCookie(request));

request.setAttribute("total_price", totalPrice);

request.getRequestDispatcher("/view/web/checkout.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

User user = (User) request.getSession().getAttribute("user");

String cartCookie = getCartCookie(request);
long totalPrice = getTotalPrice(cartCookie);
String address = request.getParameter("address");
Payment payment = Payment.create(request.getParameter("payment"));

if (payment == Payment.COD) {
Order order = new Order(user.getID(), address, totalPrice, payment, OrderStatus.PROCESSING);

int id = Singleton.orderDAO.insert(order);

moveCartToOrderData(id, cartCookie, response);

response.sendRedirect("purchase-history");

return;
} else {
}
}

private static String getCartCookie(HttpServletRequest request) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals("cart")) {
return Hash.decodeURIComponent(cookie.getValue());
}
}

return null;
}

private static long getTotalPrice(String cartData) {
long total = 0;

try {
JSONObject data = (JSONObject) new JSONParser().parse(cartData);

for (Object keyStr : data.keySet()) {
Object valueStr = data.get(keyStr);
int value = Integer.parseInt(valueStr.toString());
int key = Integer.parseInt(keyStr.toString());

Product product = Singleton.productDAO.get(key);
total += product.getPrice() * value;
}
} catch (ParseException ex) {
System.err.println(ex.getMessage());
}

return total;
}

private static void moveCartToOrderData(int id, String cartData, HttpServletResponse response) {
try {
JSONObject data = (JSONObject) new JSONParser().parse(cartData);

for (Object keyStr : data.keySet()) {
Object valueStr = data.get(keyStr);
int value = Integer.parseInt(valueStr.toString());
int key = Integer.parseInt(keyStr.toString());

Singleton.orderDataDAO.insert(new OrderData(id, key, value));
updateProductAmoutAferCheckout(key, value);
}

Cookie cookie = new Cookie("cart", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
} catch (ParseException ex) {
System.err.println(ex.getMessage());
}
}

private static void updateProductAmoutAferCheckout(int productID, int amount) {
Product product = Singleton.productDAO.get(productID);
product.setAmount(product.getAmount() - amount);

Singleton.productDAO.update(product);
}
}
16 changes: 16 additions & 0 deletions src/main/webapp/assets/js/web/checkoutHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const prices = document.getElementsByClassName("price");

for (let price of prices) {
const amount = parseFloat(price.textContent);

price.textContent = formatCurrencyVND(amount);
}

function formatCurrencyVND(amount) {
const formatter = new Intl.NumberFormat('vi-VN', {
style: 'currency',
currency: 'VND'
});

return formatter.format(amount);
}

0 comments on commit 3c7eec8

Please sign in to comment.