PayService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.ItemDao;
  9. import com.llisoft.pay.dao.OrderDao;
  10. import com.llisoft.pay.entity.Item;
  11. import com.llisoft.pay.entity.Order;
  12. import com.llisoft.pay.util.CodeUtil;
  13. @Service
  14. public class PayService {
  15. // 支付状态 未支付
  16. public static final byte STATUS_WAIT = 1;
  17. // 支付状态 已支付
  18. public static final byte STATUS_PAYED = 2;
  19. // 支付类型 支付宝
  20. public static final byte TYPE_ALI = 1;
  21. // 支付类型 微信
  22. public static final byte TYPE_WX = 2;
  23. // 支付标记 - 异步通知
  24. public static final byte FLAG_NOTIFY = 1;
  25. // 支付标记 - 同步回调
  26. public static final byte FLAG_RETURN = 2;
  27. // 支付标记 - 主动查询
  28. public static final byte FLAG_QUERY = 3;
  29. // 支付标记 - 对账
  30. public static final byte FLAG_CHECK = 4;
  31. private Logger logger = LoggerFactory.getLogger(PayService.class);
  32. @Autowired
  33. private ItemDao payDao;
  34. @Autowired
  35. private OrderDao orderDao;
  36. @Autowired
  37. private AliPayService aliPayService;
  38. @Autowired
  39. private WxPayService wxPayService;
  40. @Autowired
  41. private OrderService orderService;
  42. /**
  43. * 获取
  44. * @param payNum
  45. * @return
  46. * @throws Exception
  47. */
  48. public Item get(String payNum) throws Exception{
  49. Item pay = payDao.selectByPayNum(payNum);
  50. if (Objects.isNull(pay)) {
  51. throw new Exception("支付记录不存在: " + payNum);
  52. }
  53. return pay;
  54. }
  55. /**
  56. * 添加支付
  57. * @param payType 支付类型
  58. * @param payMoney 支付金额
  59. * @param orderId 订单号
  60. * @return 返回支付跳转地址
  61. */
  62. public String add(byte payType, int payMoney, int orderId){
  63. String payNum = CodeUtil.uuid();
  64. Item pay = new Item();
  65. pay.setPayNum(payNum);
  66. pay.setPayType(payType);
  67. pay.setPayMoney(payMoney);
  68. pay.setPayStatus(STATUS_WAIT);
  69. pay.setOrderId(orderId);
  70. payDao.insert(pay);
  71. logger.debug("添加支付成功: {}", payNum);
  72. return payNum;
  73. }
  74. /**
  75. * 发起支付
  76. * @param payNum 支付号
  77. * @param isMobile 是否移动端页面
  78. * @return
  79. * @throws Exception
  80. */
  81. public String pay(String payNum, boolean isMobile) throws Exception{
  82. Item item = this.get(payNum);
  83. if (Objects.isNull(item)) {
  84. throw new Exception("支付记录不存在: " + payNum);
  85. }
  86. Order order = orderDao.select(item.getOrderId());
  87. if (item.getPayType() == TYPE_ALI) { // 支付宝
  88. return isMobile ? aliPayService.paym(payNum, order.getOrderMoney(), order.getOrderTitle()) :
  89. aliPayService.pay(payNum, order.getOrderMoney(), order.getOrderTitle());
  90. }else if (item.getPayType() == TYPE_WX) { // 微信
  91. return wxPayService.pay(payNum, order.getOrderMoney(), order.getOrderTitle());
  92. }
  93. return null;
  94. }
  95. /**
  96. * 完成支付
  97. * 验证订单信息 + 更新支付状态 + 更新订单状态(包括支付方式)
  98. * @param payNum
  99. * @param tradeNum 第三方交易号(由第三方通知返回)
  100. * @param money 实际支付金额(分)
  101. * @param flag 支付成功标记(1异步通知/2同步通知/3主动查询/4对账)
  102. * @return
  103. * @throws Exception
  104. */
  105. @Transactional
  106. public boolean finish(String payNum, String tradeNum, int money, byte flag) throws Exception{
  107. Item item = this.get(payNum);
  108. if (item.getPayMoney() != money) { // 核对金额
  109. throw new Exception("支付金额异常: " + money);
  110. }
  111. // 更新支付状态
  112. payDao.updatePay(item.getPayId(), tradeNum, STATUS_PAYED, flag);
  113. logger.debug("支付处理成功, 状态更新为已支付: {}", payNum);
  114. return orderService.finish(item.getOrderId());
  115. }
  116. /**
  117. * 验证支付状态
  118. * @param payNum
  119. * @return
  120. * @throws Exception
  121. */
  122. public boolean check(int orderId) throws Exception {
  123. Item pay = payDao.selectByOrderIdLast(orderId);
  124. if(Objects.isNull(pay)) {
  125. return false;
  126. }
  127. // 未支付状态,去第三方验证
  128. if (pay.getPayStatus() == STATUS_WAIT) {
  129. if(pay.getPayType() == TYPE_ALI) {
  130. return aliPayService.query(pay.getPayNum());
  131. }else if (pay.getPayType() == TYPE_WX) {
  132. return wxPayService.query(pay.getPayNum());
  133. }
  134. }
  135. return false;
  136. }
  137. }