123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package com.llisoft.service.pay.controller;
- import java.util.Objects;
- import javax.servlet.http.HttpServletRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.llisoft.common.exception.MtaException;
- import com.llisoft.common.util.JsonUtil;
- import com.llisoft.service.pay.entity.Order;
- import com.llisoft.service.pay.service.AppService;
- import com.llisoft.service.pay.service.OrderService;
- import com.llisoft.service.pay.service.PayService;
- import com.llisoft.service.pay.vo.OrderRequestVo;
- import com.llisoft.service.pay.vo.OrderResponseVo;
- /**
- * 订单
- */
- @Controller
- @RequestMapping("/order")
- public class OrderController{
-
- private Logger logger = LoggerFactory.getLogger(OrderController.class);
-
- @Autowired
- private OrderService orderService;
- @Autowired
- private PayService payService;
- @Autowired
- private AppService appService;
-
-
- /**
- * 添加订单
- * 暂时不加认证签名
- * @param request
- */
- @PostMapping
- public @ResponseBody OrderResponseVo add(OrderRequestVo vo) throws MtaException{
- logger.info("添加订单请求: {}", JsonUtil.toJson(vo));
- if(Objects.isNull(vo.getAppkey()) || vo.getAppkey().trim().isEmpty() || vo.getMoney()<=0) {
- throw new MtaException("添加订单: 非法入参: " + JsonUtil.toJson(vo));
- }
- String ordernum = orderService.add(vo.getAppkey(), vo.getMoney(), vo.getOrdernum(), vo.getTitle());
- return this.get(ordernum);
- }
-
- /**
- * 支付
- * @param orderId
- * @param type
- * @return
- */
- @GetMapping("/{orderId}/pay")
- public String pay(@PathVariable int orderId, // 默认支付宝
- @RequestParam(required=false,defaultValue="1") byte type) throws MtaException{
- if(orderId <= 0) {
- throw new MtaException("订单号非法: " + orderId);
- }
- Order order = orderService.get(orderId);
- if(order.getOrderStatus() == OrderService.STATUS_PAYED){
- logger.info("订单支付: 订单已经支付过: {}", orderId);
- return "redirect:/order/"+orderId+"/ok"; // 已经支付过
- }
- logger.info("订单支付: {}, 支付类型: {}", orderId, type==1 ? "支付宝" : "微信");
- String paynum = payService.add(orderId, type);
- return "redirect:/pay/" + paynum;
- }
-
- /**
- * 支付成功
- * @return
- * @throws MtaException
- */
- @GetMapping(value="/{orderId}/ok")
- public String ok(@PathVariable int orderId, HttpServletRequest request) throws MtaException{
- Order order = orderService.get(orderId);
- if (order.getOrderStatus() == OrderService.STATUS_PAYED) {
- // 重定向到业务回调地址
- String returnUrl = appService.getReturnUrl(order.getAppId(), order.getOrderNum());
- if (Objects.nonNull(returnUrl)) {
- return "redirect:" + returnUrl;
- }
- // 未配置成功地址的返回默认
- request.setAttribute("order", order);
- return "/payok.jsp";
- }
- return null;
- }
-
- /**
- * 获取订单
- * @param paynum
- * @return
- * @throws MtaException
- */
- @GetMapping("/{orderNum}")
- public @ResponseBody OrderResponseVo get(@PathVariable String orderNum) throws MtaException{
- OrderResponseVo responseBean = null;
- if(Objects.nonNull(orderNum) && !orderNum.trim().isEmpty()){
- Order order = orderService.getByOrderNum(orderNum);
- responseBean = new OrderResponseVo();
- BeanUtils.copyProperties(order, responseBean);
- responseBean.setPayed(order.getOrderStatus()==OrderService.STATUS_PAYED);
- responseBean.setPayUrl("/order/"+orderNum+"/pay");
- }
- logger.info("获取订单请求: {}, 返回结果: {}", orderNum, JsonUtil.toJson(responseBean));
- return responseBean;
- }
-
- }
|