OrderController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.llisoft.service.pay.controller;
  2. import java.util.Objects;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.BeanUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import com.llisoft.common.exception.MtaException;
  16. import com.llisoft.common.util.JsonUtil;
  17. import com.llisoft.service.pay.entity.Order;
  18. import com.llisoft.service.pay.service.AppService;
  19. import com.llisoft.service.pay.service.OrderService;
  20. import com.llisoft.service.pay.service.PayService;
  21. import com.llisoft.service.pay.vo.OrderRequestVo;
  22. import com.llisoft.service.pay.vo.OrderResponseVo;
  23. /**
  24. * 订单
  25. */
  26. @Controller
  27. @RequestMapping("/order")
  28. public class OrderController{
  29. private Logger logger = LoggerFactory.getLogger(OrderController.class);
  30. @Autowired
  31. private OrderService orderService;
  32. @Autowired
  33. private PayService payService;
  34. @Autowired
  35. private AppService appService;
  36. /**
  37. * 添加订单
  38. * 暂时不加认证签名
  39. * @param request
  40. */
  41. @PostMapping
  42. public @ResponseBody OrderResponseVo add(OrderRequestVo vo) throws MtaException{
  43. logger.info("添加订单请求: {}", JsonUtil.toJson(vo));
  44. if(Objects.isNull(vo.getAppkey()) || vo.getAppkey().trim().isEmpty() || vo.getMoney()<=0) {
  45. throw new MtaException("添加订单: 非法入参: " + JsonUtil.toJson(vo));
  46. }
  47. String ordernum = orderService.add(vo.getAppkey(), vo.getMoney(), vo.getOrdernum(), vo.getTitle());
  48. return this.get(ordernum);
  49. }
  50. /**
  51. * 支付
  52. * @param orderId
  53. * @param type
  54. * @return
  55. */
  56. @GetMapping("/{orderId}/pay")
  57. public String pay(@PathVariable int orderId, // 默认支付宝
  58. @RequestParam(required=false,defaultValue="1") byte type) throws MtaException{
  59. if(orderId <= 0) {
  60. throw new MtaException("订单号非法: " + orderId);
  61. }
  62. Order order = orderService.get(orderId);
  63. if(order.getOrderStatus() == OrderService.STATUS_PAYED){
  64. logger.info("订单支付: 订单已经支付过: {}", orderId);
  65. return "redirect:/order/"+orderId+"/ok"; // 已经支付过
  66. }
  67. logger.info("订单支付: {}, 支付类型: {}", orderId, type==1 ? "支付宝" : "微信");
  68. String paynum = payService.add(orderId, type);
  69. return "redirect:/pay/" + paynum;
  70. }
  71. /**
  72. * 支付成功
  73. * @return
  74. * @throws MtaException
  75. */
  76. @GetMapping(value="/{orderId}/ok")
  77. public String ok(@PathVariable int orderId, HttpServletRequest request) throws MtaException{
  78. Order order = orderService.get(orderId);
  79. if (order.getOrderStatus() == OrderService.STATUS_PAYED) {
  80. // 重定向到业务回调地址
  81. String returnUrl = appService.getReturnUrl(order.getAppId(), order.getOrderNum());
  82. if (Objects.nonNull(returnUrl)) {
  83. return "redirect:" + returnUrl;
  84. }
  85. // 未配置成功地址的返回默认
  86. request.setAttribute("order", order);
  87. return "/payok.jsp";
  88. }
  89. return null;
  90. }
  91. /**
  92. * 获取订单
  93. * @param paynum
  94. * @return
  95. * @throws MtaException
  96. */
  97. @GetMapping("/{orderNum}")
  98. public @ResponseBody OrderResponseVo get(@PathVariable String orderNum) throws MtaException{
  99. OrderResponseVo responseBean = null;
  100. if(Objects.nonNull(orderNum) && !orderNum.trim().isEmpty()){
  101. Order order = orderService.getByOrderNum(orderNum);
  102. responseBean = new OrderResponseVo();
  103. BeanUtils.copyProperties(order, responseBean);
  104. responseBean.setPayed(order.getOrderStatus()==OrderService.STATUS_PAYED);
  105. responseBean.setPayUrl("/order/"+orderNum+"/pay");
  106. }
  107. logger.info("获取订单请求: {}, 返回结果: {}", orderNum, JsonUtil.toJson(responseBean));
  108. return responseBean;
  109. }
  110. }