This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handler for checkout with cod payment
- Loading branch information
1 parent
8a6a9f8
commit 3c7eec8
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |