/** * 我的订单 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String myOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1 */ int pc = getPc(req); /* * 2. 得到url:... */ String url = getUrl(req); /* * 3. 从当前session中获取User */ User user = (User) req.getSession().getAttribute("sessionUser"); /* * 4. 使用pc和cid调用service#findByCategory得到PageBean */ PageBean<Order> pb = orderService.myOrders(user.getUid(), pc); /* * 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp */ pb.setUrl(url); req.setAttribute("pb", pb); return "f:/jsps/order/list.jsp"; }
/** * 确认收货 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String confirm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String oid = req.getParameter("oid"); /* * 校验订单状态 */ int status = orderService.findStatus(oid); if (status != 3) { req.setAttribute("code", "error"); req.setAttribute("msg", "状态不对,不能确认收货!"); return "f:/jsps/msg.jsp"; } orderService.updateStatus(oid, 4); // 设置状态为交易成功! req.setAttribute("code", "success"); req.setAttribute("msg", "恭喜,交易成功!"); return "f:/jsps/msg.jsp"; }
/** * 取消订单 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String oid = req.getParameter("oid"); /* * 校验订单状态 */ int status = orderService.findStatus(oid); if (status != 1) { req.setAttribute("code", "error"); req.setAttribute("msg", "状态不对,不能取消!"); return "f:/jsps/msg.jsp"; } orderService.updateStatus(oid, 5); // 设置状态为取消! req.setAttribute("code", "success"); req.setAttribute("msg", "您的订单已取消,您不后悔吗!"); return "f:/jsps/msg.jsp"; }
/** * 生成订单 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * 1. 获取所有购物车条目的id,查询之 */ String cartItemIds = req.getParameter("cartItemIds"); List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds); if (cartItemList.size() == 0) { req.setAttribute("code", "error"); req.setAttribute("msg", "您没有选择要购买的图书,不能下单!"); return "f:/jsps/msg.jsp"; } /* * 2. 创建Order */ Order order = new Order(); order.setOid(CommonUtils.uuid()); // 设置主键 order.setOrdertime(String.format("%tF %<tT", new Date())); // 下单时间 order.setStatus(1); // 设置状态,1表示未付款 order.setAddress(req.getParameter("address")); // 设置收货地址 User owner = (User) req.getSession().getAttribute("sessionUser"); order.setOwner(owner); // 设置订单所有者 BigDecimal total = new BigDecimal("0"); for (CartItem cartItem : cartItemList) { total = total.add(new BigDecimal(cartItem.getSubtotal() + "")); } order.setTotal(total.doubleValue()); // 设置总计 /* * 3. 创建List<OrderItem> * 一个CartItem对应一个OrderItem */ List<OrderItem> orderItemList = new ArrayList<OrderItem>(); for (CartItem cartItem : cartItemList) { OrderItem orderItem = new OrderItem(); orderItem.setOrderItemId(CommonUtils.uuid()); // 设置主键 orderItem.setQuantity(cartItem.getQuantity()); orderItem.setSubtotal(cartItem.getSubtotal()); orderItem.setBook(cartItem.getBook()); orderItem.setOrder(order); orderItemList.add(orderItem); } order.setOrderItemList(orderItemList); /* * 4. 调用service完成添加 */ orderService.createOrder(order); // 删除购物车条目 cartItemService.batchDelete(cartItemIds); /* * 5. 保存订单,转发到ordersucc.jsp */ req.setAttribute("order", order); return "f:/jsps/order/ordersucc.jsp"; }
/** * 加载订单 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String oid = req.getParameter("oid"); Order order = orderService.load(oid); req.setAttribute("order", order); String btn = req.getParameter("btn"); // btn说明了用户点击哪个超链接来访问本方法的 req.setAttribute("btn", btn); return "/jsps/order/desc.jsp"; }
/** * 回馈方法 当支付成功时,易宝会访问这里 用两种方法访问: 1. 引导用户的浏览器重定向(如果用户关闭了浏览器,就不能访问这里了) 2. * 易宝的服务器会使用点对点通讯的方法访问这个方法。(必须回馈success,不然易宝服务器会一直调用这个方法) * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String back(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * 1. 获取12个参数 */ String p1_MerId = req.getParameter("p1_MerId"); String r0_Cmd = req.getParameter("r0_Cmd"); String r1_Code = req.getParameter("r1_Code"); String r2_TrxId = req.getParameter("r2_TrxId"); String r3_Amt = req.getParameter("r3_Amt"); String r4_Cur = req.getParameter("r4_Cur"); String r5_Pid = req.getParameter("r5_Pid"); String r6_Order = req.getParameter("r6_Order"); String r7_Uid = req.getParameter("r7_Uid"); String r8_MP = req.getParameter("r8_MP"); String r9_BType = req.getParameter("r9_BType"); String hmac = req.getParameter("hmac"); /* * 2. 获取keyValue */ Properties props = new Properties(); props.load(this.getClass().getClassLoader().getResourceAsStream("payment.properties")); String keyValue = props.getProperty("keyValue"); /* * 3. 调用PaymentUtil的校验方法来校验调用者的身份 * >如果校验失败:保存错误信息,转发到msg.jsp * >如果校验通过: * * 判断访问的方法是重定向还是点对点,如果要是重定向 * 修改订单状态,保存成功信息,转发到msg.jsp * * 如果是点对点:修改订单状态,返回success */ boolean bool = PaymentUtil.verifyCallback( hmac, p1_MerId, r0_Cmd, r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid, r8_MP, r9_BType, keyValue); if (!bool) { req.setAttribute("code", "error"); req.setAttribute("msg", "无效的签名,支付失败!"); return "f:/jsps/msg.jsp"; } if (r1_Code.equals("1")) { orderService.updateStatus(r6_Order, 2); if (r9_BType.equals("1")) { req.setAttribute("code", "success"); req.setAttribute("msg", "恭喜,支付成功!"); return "f:/jsps/msg.jsp"; } else if (r9_BType.equals("2")) { resp.getWriter().print("success"); } } return null; }
/** * 支付准备 * * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String paymentPre(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("order", orderService.load(req.getParameter("oid"))); return "f:/jsps/order/pay.jsp"; }