|
|
@@ -100,65 +100,70 @@ public class Trade {
|
|
|
// return true;
|
|
|
// }
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public SuccessParams tradeForScanPay(Order order, long channelId) throws Exception {
|
|
|
- /**
|
|
|
- * 交易处理链,每个handle处理负责一个业务节点
|
|
|
- * 1、处理各种商家折扣,计算实际交易金额
|
|
|
- * 2、用户渠道积分用以抵消交易金额
|
|
|
- * 3、用户账户余额抵消交易金额
|
|
|
- * 4、保存订单信息
|
|
|
- * */
|
|
|
- List<BaseHandle> chain = new ArrayList() {{
|
|
|
- add(discountHandle);
|
|
|
- add(channelPointHandle);
|
|
|
- add(balanceHandle);
|
|
|
- add(wxPayHandle);
|
|
|
- }};
|
|
|
+ public SuccessParams tradeForScanPay(Order order, long channelId) {
|
|
|
+ SuccessParams successParams = SuccessParams.builder().build();
|
|
|
+ try {
|
|
|
+ /**
|
|
|
+ * 交易处理链,每个handle处理负责一个业务节点
|
|
|
+ * 1、处理各种商家折扣,计算实际交易金额
|
|
|
+ * 2、用户渠道积分用以抵消交易金额
|
|
|
+ * 3、用户账户余额抵消交易金额
|
|
|
+ * 4、保存订单信息
|
|
|
+ * */
|
|
|
+ List<BaseHandle> chain = new ArrayList() {{
|
|
|
+ add(discountHandle);
|
|
|
+ add(channelPointHandle);
|
|
|
+ add(balanceHandle);
|
|
|
+ add(wxPayHandle);
|
|
|
+ }};
|
|
|
|
|
|
- BigDecimal remain = order.getMoney();
|
|
|
+ BigDecimal remain = order.getMoney();
|
|
|
|
|
|
- //获取tenantId
|
|
|
- String tenantId = appShopService.getTenantId(order.getShopId());
|
|
|
- Bills bills = initBill(order, channelId, tenantId);
|
|
|
+ //获取tenantId
|
|
|
+ String tenantId = appShopService.getTenantId(order.getShopId());
|
|
|
+ Bills bills = initBill(order, channelId, tenantId);
|
|
|
|
|
|
- synchronized (order.getLoginUser().getId().toString().intern()) {
|
|
|
- //支付参数
|
|
|
- SuccessParams successParams = SuccessParams.builder()
|
|
|
- .orderType(OrderType.USER_PAY.name())
|
|
|
- .status(AppConstant.BillPayStatus.待付款.name())
|
|
|
- .userId(order.getLoginUser().getId())
|
|
|
- .totalPrice(order.getMoney())
|
|
|
- .bills(bills)
|
|
|
- .shopId(order.getShopId())
|
|
|
- .tenantId(tenantId)
|
|
|
- .channelId(channelId)
|
|
|
- .build();
|
|
|
+ synchronized (order.getLoginUser().getId().toString().intern()) {
|
|
|
+ //支付参数
|
|
|
+ successParams = SuccessParams.builder()
|
|
|
+ .orderType(OrderType.USER_PAY.name())
|
|
|
+ .status(AppConstant.BillPayStatus.待付款.name())
|
|
|
+ .userId(order.getLoginUser().getId())
|
|
|
+ .totalPrice(order.getMoney())
|
|
|
+ .bills(bills)
|
|
|
+ .shopId(order.getShopId())
|
|
|
+ .tenantId(tenantId)
|
|
|
+ .channelId(channelId)
|
|
|
+ .build();
|
|
|
|
|
|
- //处理各个节点
|
|
|
- for (BaseHandle node : chain) {
|
|
|
- HandleData res = node.handle(remain, order, successParams);
|
|
|
- Assert.isTrue(res.isSuccess(), () -> {
|
|
|
- throw new TradeException(ResCode.TRADE_ERROR, res.getSuccessParams());
|
|
|
- });
|
|
|
- remain = res.getRemain();
|
|
|
- successParams = res.getSuccessParams();
|
|
|
+ //处理各个节点
|
|
|
+ for (BaseHandle node : chain) {
|
|
|
+ HandleData res = node.handle(remain, order, successParams);
|
|
|
+ Assert.isTrue(res.isSuccess(), () -> {
|
|
|
+ throw new TradeException(ResCode.TRADE_ERROR, res.getSuccessParams());
|
|
|
+ });
|
|
|
+ remain = res.getRemain();
|
|
|
+ successParams = res.getSuccessParams();
|
|
|
|
|
|
- if (remain.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
- successParams.setStatus(AppConstant.BillPayStatus.付款成功.name());
|
|
|
- bills.setPayStatus(AppConstant.BillPayStatus.付款成功.name());
|
|
|
- dataHandle.saveOrUpdateEntity(billsService, successParams.getBills());
|
|
|
- applicationEventPublisher.publishEvent(new UserPayCsEvent(successParams));
|
|
|
- break;
|
|
|
+ if (remain.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ successParams.setStatus(AppConstant.BillPayStatus.付款成功.name());
|
|
|
+ bills.setPayStatus(AppConstant.BillPayStatus.付款成功.name());
|
|
|
+ dataHandle.saveOrUpdateEntity(billsService, successParams.getBills());
|
|
|
+ applicationEventPublisher.publishEvent(new UserPayCsEvent(successParams));
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- //新建事务,保存订单信息,
|
|
|
- SuccessParams finalSuccessParams = successParams;
|
|
|
- Assert.isTrue(dataHandle.saveOrUpdateEntity(billsService, successParams.getBills()), () -> {
|
|
|
- throw new TradeException(ResCode.TRADE_ERROR, finalSuccessParams);
|
|
|
- });
|
|
|
- PaymentCache.putSuccessParams(Convert.toStr(successParams.getBills().getId()), successParams);
|
|
|
- return successParams;
|
|
|
+ //新建事务,保存订单信息,
|
|
|
+ SuccessParams finalSuccessParams = successParams;
|
|
|
+ Assert.isTrue(dataHandle.saveOrUpdateEntity(billsService, successParams.getBills()), () -> {
|
|
|
+ throw new TradeException(ResCode.TRADE_ERROR, finalSuccessParams);
|
|
|
+ });
|
|
|
+ PaymentCache.putSuccessParams(Convert.toStr(successParams.getBills().getId()), successParams);
|
|
|
+ return successParams;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new TradeException(ResCode.TRADE_ERROR, successParams);
|
|
|
}
|
|
|
|
|
|
}
|