public async Task <BaseApiResult <bool> > Excute(OrderPayReq input) { bool needPublishEvent = false; OrderEntity order = default; return(await HandleAsync(input, async() => { //获取订单状态 order = await orderRepository.GetAsync(x => x.UserId == currentUserInfo.UserId && x.Id == input.OrderId); if (order == null) { throw new ApplicationException("订单错误,无法进行支付!"); } //支付成功修改订单状态 order.PayOrder(); //创建订单记录 var log = new OrderHandleLogFactory().Create(order.OrderNo, order.Id, order.State, input.UserId, input.Account, "", true); //规约检查 if (await new PayOrderSpecification(input.UserId).SatisfiedBy(order)) { //rpc调用支付 var payResult = await accountRecharge.Excute(new AccountRechargeDto() { RechargeBalance = -order.TotalPrice, UserId = input.UserId, Account = input.Account }); if (payResult.IsError()) { throw new ApplicationException(payResult.ErrMessage); } needPublishEvent = true; //持久化 using (var tran = transaction.BeginTransaction()) { orderRepository.Update(order); orderHandleLogRepository.Add(log); await orderRepository.SaveAsync(); tran.Commit(); return true; } } else { return false; } }, async (e) => { //当支付成功且修改订单状态失败则发布事件回滚支付 if (needPublishEvent) { await eventBus.PublishAsync("EshopSample.PayOrderFailEvent", new PayOrderFailEvent() { TotalPrice = order.TotalPrice }); } })); }