Trade.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package org.springblade.payment.handle;
  2. import cn.hutool.core.convert.Convert;
  3. import org.springblade.common.enums.AppConstant;
  4. import org.springblade.common.enums.OrderType;
  5. import org.springblade.common.enums.ResCode;
  6. import org.springblade.core.log.exception.ServiceException;
  7. import org.springblade.ldt.bills.entity.Bills;
  8. import org.springblade.ldt.user.service.IUserChannelPointService;
  9. import org.springblade.payment.handle.entity.HandleData;
  10. import org.springblade.payment.handle.entity.Order;
  11. import org.springblade.payment.handle.handler.*;
  12. import org.springblade.ldt.activity.service.IActivityService;
  13. import org.springblade.ldt.activity.service.IJoinRecordService;
  14. import org.springblade.ldt.bills.service.IBalanceBillsService;
  15. import org.springblade.ldt.bills.service.IBillsService;
  16. import org.springblade.ldt.bills.service.IPointBillsService;
  17. import org.springblade.ldt.shop.service.IShopService;
  18. import org.springblade.payment.entity.SuccessParams;
  19. import org.springblade.payment.service.IPaymentService;
  20. import org.springblade.webSocket.WebSocketServer;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.util.Assert;
  25. import java.math.BigDecimal;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * @author: lianghanqiang
  30. * @description: 交易处理,总控交易处理链
  31. * @since: 8/26/21 -- 5:53 PM
  32. */
  33. @Component
  34. public class Trade {
  35. @Autowired
  36. private IBillsService billsService;
  37. @Autowired
  38. private IPointBillsService pointBillsService;
  39. @Autowired
  40. private IBalanceBillsService balanceBillsService;
  41. @Autowired
  42. private WebSocketServer webSocketServer;
  43. @Autowired
  44. private IPaymentService paymentService;
  45. @Autowired
  46. private IShopService shopService;
  47. @Autowired
  48. private IActivityService activityService;
  49. @Autowired
  50. private IJoinRecordService joinRecordService;
  51. @Autowired
  52. private IUserChannelPointService userChannelPointService ;
  53. @Autowired
  54. private DataHandle dataHandle ;
  55. @Transactional
  56. public boolean tradeForPayCode(Order order, long channelId) throws Exception {
  57. /**
  58. * 交易处理链,每个handle处理负责一个业务节点
  59. * 1、处理各种商家折扣,计算实际交易金额
  60. * 2、用户渠道积分用以抵消交易金额
  61. * 3、用户账户余额抵消交易金额
  62. * 4、剩余金额用户微信支付
  63. * */
  64. List<BaseHandle> chain = new ArrayList(){{
  65. add(new DiscountHandle(shopService,activityService,joinRecordService));
  66. add(new ChannelPointHandle(pointBillsService,userChannelPointService));
  67. add(new BalanceHandle(balanceBillsService));
  68. add(new WxPayHandle(billsService,webSocketServer,dataHandle));
  69. }} ;
  70. BigDecimal remain = order.getMoney();
  71. Bills bills = initBill(order,channelId);
  72. //支付参数
  73. SuccessParams successParams = SuccessParams.builder()
  74. .orderType(OrderType.USER_PAY.name())
  75. .status(AppConstant.BillPayStatus.待付款.name())
  76. .userId(order.getLoginUser().getId())
  77. .totalPrice(order.getMoney())
  78. .bills(bills)
  79. .shopId(order.getShopId())
  80. .channelId(channelId)
  81. .build();
  82. //处理各个节点
  83. for (BaseHandle node: chain) {
  84. HandleData res = node.handle(remain, order,successParams);
  85. Assert.isTrue(res.isSuccess(),()->{throw new ServiceException(ResCode.TRADE_ERROR);});
  86. remain = res.getRemain();
  87. successParams = res.getSuccessParams();
  88. if(remain.compareTo(BigDecimal.ZERO)==0){
  89. //处理积分余额数据变更
  90. paymentService.success(successParams);
  91. break;
  92. }
  93. }
  94. return true;
  95. }
  96. @Transactional
  97. public SuccessParams tradeForScanPay(Order order, long channelId) throws Exception {
  98. /**
  99. * 交易处理链,每个handle处理负责一个业务节点
  100. * 1、处理各种商家折扣,计算实际交易金额
  101. * 2、用户渠道积分用以抵消交易金额
  102. * 3、用户账户余额抵消交易金额
  103. * 4、保存订单信息
  104. * */
  105. List<BaseHandle> chain = new ArrayList(){{
  106. add(new DiscountHandle(shopService,activityService,joinRecordService));
  107. add(new ChannelPointHandle(pointBillsService,userChannelPointService));
  108. add(new BalanceHandle(balanceBillsService));
  109. }} ;
  110. BigDecimal remain = order.getMoney();
  111. Bills bills = initBill(order,channelId);
  112. //支付参数
  113. SuccessParams successParams = SuccessParams.builder()
  114. .orderType(OrderType.USER_PAY.name())
  115. .status(AppConstant.BillPayStatus.待付款.name())
  116. .userId(order.getLoginUser().getId())
  117. .totalPrice(order.getMoney())
  118. .bills(bills)
  119. .shopId(order.getShopId())
  120. .channelId(channelId)
  121. .build();
  122. //处理各个节点
  123. for (BaseHandle node: chain) {
  124. HandleData res = node.handle(remain, order,successParams);
  125. Assert.isTrue(res.isSuccess(),()->{throw new ServiceException(ResCode.TRADE_ERROR);});
  126. remain = res.getRemain();
  127. successParams = res.getSuccessParams();
  128. if(remain.compareTo(BigDecimal.ZERO)==0){
  129. //处理积分余额数据变更
  130. paymentService.success(successParams);
  131. break;
  132. }
  133. }
  134. if(remain.compareTo(BigDecimal.ZERO)>0){
  135. bills.setPrice(remain);
  136. Assert.isTrue(billsService.saveOrUpdate(successParams.getBills()),()->{throw new ServiceException(ResCode.TRADE_ERROR);});
  137. }
  138. return successParams;
  139. }
  140. /**
  141. * 初始化交易账单
  142. * */
  143. private Bills initBill(Order order, long channelId){
  144. Bills bills = new Bills();
  145. bills.setPayStatus(AppConstant.BillPayStatus.待付款.name());
  146. bills.setAppid(order.getAppId());
  147. bills.setCost(order.getMoney());
  148. bills.setOpenid(order.getOpenId());
  149. bills.setChannelId(Convert.toStr(channelId));
  150. bills.setType(OrderType.USER_PAY.name());
  151. bills.setPrice(BigDecimal.ZERO);
  152. bills.setOpenid(order.getLoginUser().getOpenid());
  153. bills.setFee(BigDecimal.ZERO);
  154. bills.setPointFee(BigDecimal.ZERO);
  155. bills.setTitle(order.getBillsTitle());
  156. bills.setPayId(order.getLoginUser().getId());
  157. bills.setReceiveId(order.getShopId());
  158. bills.setMallId(order.getMallId());
  159. return bills;
  160. }
  161. }