OrderService.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.llisoft.pay.service;
  2. import java.util.Objects;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import com.llisoft.pay.dao.OrderDao;
  9. import com.llisoft.pay.entity.App;
  10. import com.llisoft.pay.entity.Order;
  11. import com.llisoft.pay.util.CodeUtil;
  12. import com.llisoft.pay.util.JsonUtil;
  13. @Service
  14. public class OrderService {
  15. // 支付状态 未支付
  16. public static final byte STATUS_WAIT = 1;
  17. // 支付状态 已支付
  18. public static final byte STATUS_PAYED = 2;
  19. private Logger logger = LoggerFactory.getLogger(OrderService.class);
  20. @Autowired
  21. private OrderDao orderDao;
  22. @Autowired
  23. private PayService payService;
  24. @Autowired
  25. private AppService appService;
  26. /**
  27. * 添加
  28. * @param appKey 支付key(由支付服务为业务单独分配)
  29. * @param orderMoney 订单金额(分)
  30. * @param orderTitle 标题(如果客户端没有传会使用app的name)
  31. * @return
  32. * @throws Exception
  33. */
  34. public String add(String appKey, int orderMoney, String orderTitle) throws Exception{
  35. // 验证appKey合法性
  36. App app = appService.get(appKey);
  37. if (Objects.isNull(app)) {
  38. logger.error("appKey不存在: {}", appKey);
  39. return null;
  40. }
  41. // 生成订单号, 当前时间+5位随机数
  42. String orderNum = CodeUtil.time();
  43. if(Objects.isNull(orderTitle) || orderTitle.trim().isEmpty()) {
  44. orderTitle = app.getAppName(); // 没有传title时使用业务名称
  45. }
  46. // 创建订单
  47. Order order = new Order();
  48. order.setOrderNum(orderNum);
  49. if(app.getDebug()) {
  50. orderMoney = 1; // 一分钱测试
  51. }
  52. order.setOrderMoney(orderMoney);
  53. order.setOrderTitle(orderTitle); // 订单描述
  54. order.setOrderStatus(STATUS_WAIT);
  55. order.setAppId(app.getAppId());
  56. orderDao.insert(order);
  57. logger.debug("添加订单成功: {}", orderNum);
  58. return orderNum;
  59. }
  60. /**
  61. * 订单支付
  62. * @param orderNum
  63. * @param payType
  64. * @return
  65. * @throws Exception
  66. */
  67. public String pay(String orderNum, byte payType, boolean isMobile) throws Exception{
  68. Order order = orderDao.selectByOrderNum(orderNum);
  69. if(order.getOrderStatus() == STATUS_PAYED){
  70. logger.error("订单已经支付过: {}", orderNum);
  71. return null;
  72. }
  73. // 添加支付记录
  74. String payNum = payService.add(payType, order.getOrderMoney(), order.getOrderId());
  75. return payService.pay(payNum, isMobile);
  76. }
  77. /**
  78. * 订单核对
  79. * @param orderNum
  80. * @return
  81. * @throws Exception
  82. */
  83. public boolean check(String orderNum) throws Exception {
  84. Order order = orderDao.selectByOrderNum(orderNum);
  85. if(order.getOrderStatus() == STATUS_PAYED) {
  86. logger.debug("当前订单状态为已支付,直接返回: {}", orderNum);
  87. return true;
  88. }
  89. logger.debug("当前订单状态为未支付,调用接口验证: {}", orderNum);
  90. return payService.check(order.getOrderId());
  91. }
  92. /**
  93. * 完成订单
  94. * 更新订单状态 + 通知业务服务
  95. * @param orderId
  96. * @return 返回同步回调地址
  97. * @throws Exception
  98. */
  99. @Transactional
  100. public boolean finish(int orderId) throws Exception{
  101. // 更新订单状态
  102. orderDao.updatePay(orderId, STATUS_PAYED);
  103. logger.debug("订单处理成功, 状态更新为已支付: {}", orderId);
  104. // 发送支付成功异步通知
  105. Order order = orderDao.select(orderId);
  106. appService.notify(order.getAppId(), order.getOrderNum(), JsonUtil.toJson(order));
  107. return true;
  108. }
  109. }