/// <summary> /// 根据订单和用户生成支付记录 /// 生成的支付记录没有,支付方式,支付方式有前端返回来 /// </summary> /// <param name="singlePayInput">The order.</param> public Tuple <ServiceResult, Pay> AddSinglePay(SinglePayInput singlePayInput) { var webSite = Resolve <IAutoConfigService>().GetValue <WebSiteConfig>(); var pay = new Pay { EntityId = singlePayInput.Orders.Select(r => r.Id).ToJson(), Status = PayStatus.WaiPay, Type = singlePayInput.CheckoutType, Amount = singlePayInput.Orders.Sum(r => r.PaymentAmount), UserId = singlePayInput.User.Id }; // 是否为管理员代付 if (singlePayInput.IsAdminPay) { pay.PayType = PayType.AdminPay; } if (singlePayInput.Type == CheckoutType.Customer) { pay.Type = singlePayInput.Type; } var payExtension = new PayExtension { TradeNo = singlePayInput.Orders.FirstOrDefault() .Serial, // 使用第一个订单作为交易号 // Subject = order.OrderExtension?.Product?.Name, // 支付标题为商品名称 TotalAmount = singlePayInput.Orders.Sum(r => r.PaymentAmount), Body = $"您正在{webSite.WebSiteName}商城上购买商品,请认真核对订单信息", // ProductCode = order.OrderExtension?.Product?.Bn, UserName = singlePayInput.User.GetUserName(), IsFromOrder = singlePayInput.IsFromOrder }; if (singlePayInput.IsGroupBuy) { //是否为拼团购买 payExtension.IsGroupBuy = true; payExtension.BuyerCount = singlePayInput.BuyerCount; } if (singlePayInput.ExcecuteSqlList != null) { payExtension.ExcecuteSqlList = singlePayInput.ExcecuteSqlList; } if (singlePayInput.AfterSuccess != null) { payExtension.AfterSuccess = singlePayInput.AfterSuccess; } if (singlePayInput.OrderUser != null) { payExtension.OrderUser = singlePayInput.OrderUser; } payExtension.RedirectUrl = singlePayInput.RedirectUrl; if (Convert.ToInt16(singlePayInput.TriggerType) > 0) { payExtension.TriggerType = singlePayInput.TriggerType; } if (!singlePayInput.EntityId.IsNullOrEmpty()) { pay.EntityId = singlePayInput.EntityId; } IList <KeyValuePair <Guid, decimal> > acmountPay = new List <KeyValuePair <Guid, decimal> >(); if (singlePayInput.ReduceMoneys != null && singlePayInput.ReduceMoneys.Count > 0) { payExtension.Note = "扣除"; singlePayInput.ReduceMoneys.Foreach(r => { payExtension.Note += " " + r.Name + r.MaxPayPrice; acmountPay.Add(new KeyValuePair <Guid, decimal>(r.MoneyId, r.MaxPayPrice)); }); } pay.AccountPay = acmountPay.ToJson(); if (!singlePayInput.RedirectUrl.IsNullOrEmpty()) { payExtension.RedirectUrl = singlePayInput.RedirectUrl; } pay.Extensions = payExtension.ToJson(); if (Resolve <IPayService>().Add(pay)) { return(Tuple.Create(ServiceResult.Success, pay)); } return(Tuple.Create(ServiceResult.FailedWithMessage("支付订单创建失败"), new Pay())); }
public ApiResult Pay([FromBody] OrderEditPay orderEditPay) { var user = Resolve <IUserService>().GetUserDetail(orderEditPay.UserId); if (user == null) { return(ApiResult.Failure("用户已经不存在")); } if (user.Detail.PayPassword != orderEditPay.PayPassword.ToMd5HashString()) { return(ApiResult.Failure("支付密码不正确")); } var order = Resolve <IOrderService>().GetSingle(r => r.Id == orderEditPay.OrderId); if (order == null) { return(ApiResult.Failure("订单不存在")); } if (order.OrderStatus != OrderStatus.WaitingBuyerPay) { return(ApiResult.Failure("订单已付款或关闭,请刷新")); } IList <Order> orderList = new List <Order> { order }; var reduceMoneys = new List <OrderMoneyItem>(); order.OrderExtension.ReduceAmounts.Foreach(r => { reduceMoneys.Add(new OrderMoneyItem { MoneyId = r.MoneyTypeId, MaxPayPrice = r.ForCashAmount }); }); var singlePayInput = new SinglePayInput { Orders = orderList, User = user, ReduceMoneys = reduceMoneys, IsAdminPay = true }; var payResult = Resolve <IOrderAdminService>().AddSinglePay(singlePayInput); if (!payResult.Item1.Succeeded) { return(ApiResult.Failure(payResult.Item1.ToString())); } var payInput = AutoMapping.SetValue <PayInput>(payResult.Item2); payInput.LoginUserId = user.Id; payInput.PayId = payResult.Item2.Id; var result = Resolve <IPayService>().Pay(payInput); if (!result.Item1.Succeeded) { return(ApiResult.Failure(result.ToString())); } return(ApiResult.Success("支付成功!")); }
public async Task <Tuple <ServiceResult, OrderBuyOutput> > OpenSelfOrUpgrade(UserRightsOrderInput orderInput) { var result = ServiceResult.Success; var orderBuyOutput = new OrderBuyOutput(); //if (orderInput.GradeId == Guid.Parse("72be65e6-3000-414d-972e-1a3d4a366001")) //{ // result = ServiceResult.Failure("标准商家不能自己支付开通"); // new Tuple<ServiceResult, OrderBuyOutput>(result, orderBuyOutput); //} var payPrice = GetPayPrice(orderInput); if (!payPrice.Item1.Succeeded) { return(Tuple.Create(payPrice.Item1, orderBuyOutput)); } var context = Repository <IUserRightsRepository>().RepositoryContext; try { context.BeginTransaction(); var order = new Order { UserId = orderInput.BuyUser.Id, StoreId = string.Empty, OrderStatus = OrderStatus.WaitingBuyerPay, OrderType = OrderType.VirtualOrder, TotalAmount = payPrice.Item2, TotalCount = 1, PayId = 0, PaymentAmount = payPrice.Item2 }; // 订单扩展数据 var userRightOrder = AutoMapping.SetValue <UserRightsOrderInput>(orderInput); order.OrderExtension = new OrderExtension { // 价格信息 OrderAmount = new OrderAmount { TotalProductAmount = payPrice.Item2, // 商品总价 ExpressAmount = 0m, // 邮费 FeeAmount = 0m // 服务费 }, AttachContent = userRightOrder.ToJsons(), User = orderInput.BuyUser, // 下单用户 Store = null }; order.AccountPay = order.AccountPayPair.ToJsons(); order.Extension = order.OrderExtension.ToJsons(); // 如果发货人不为空 Resolve <IOrderService>().Add(order); var orderList = new List <Order> { order }; var singlePayInput = new SinglePayInput { Orders = orderList, User = orderInput.User, OrderUser = orderInput.BuyUser, ExcecuteSqlList = new BaseServiceMethod { Method = "ExcecuteSqlList", ServiceName = typeof(IUserRightsService).Name, Parameter = order.Id }, AfterSuccess = new BaseServiceMethod { Method = "AfterPaySuccess", ServiceName = typeof(IUserRightsService).Name, Parameter = order.Id }, TriggerType = TriggerType.Other, BuyerCount = 1, RedirectUrl = "/pages/index?path=successful_opening" }; var payResult = Resolve <IOrderAdminService>().AddSinglePay(singlePayInput); if (!payResult.Item1.Succeeded) { // 支付记录添加失败,回滚 context.RollbackTransaction(); return(Tuple.Create(payResult.Item1, new OrderBuyOutput())); } //更新人民币支付记录Id var orderIds = orderList.Select(e => e.Id).ToList(); Resolve <IOrderService>().Update(r => { r.PayId = payResult.Item2.Id; }, e => orderIds.Contains(e.Id)); // 输出赋值 orderBuyOutput.PayAmount = payResult.Item2.Amount; orderBuyOutput.PayId = payResult.Item2.Id; orderBuyOutput.OrderIds = orderList.Select(r => r.Id).ToList(); //开通成功修改UserDetail表地址 var buyUserDetail = Resolve <IUserDetailService>().GetSingle(u => u.UserId == orderInput.BuyUser.Id); if (buyUserDetail.RegionId <= 0) { buyUserDetail.RegionId = orderInput.RegionId.ToInt64(); Resolve <IUserDetailService>().Update(buyUserDetail); } context.SaveChanges(); context.CommitTransaction(); //var buyUser = Resolve<IUserService>().GetSingle(u => u.Mobile == orderInput.BuyUser.Mobile); orderBuyOutput.RegUser = orderInput.RegInfo; orderBuyOutput.BuyGrade = orderInput .BuyGrade; // Resolve<IAutoConfigService>().GetList<UserGradeConfig>().FirstOrDefault(s => s.Id == orderInput.BuyUser.GradeId); } catch (Exception ex) { context.RollbackTransaction(); result = ServiceResult.Failure(ex.Message); } finally { context.DisposeTransaction(); } // 删除缓存 Resolve <IUserService>().DeleteUserCache(orderInput.User.Id, orderInput.User.UserName); Resolve <IUserService>().DeleteUserCache(orderInput.BuyUser.Id, orderInput.BuyUser.UserName); return(new Tuple <ServiceResult, OrderBuyOutput>(result, orderBuyOutput)); }