public IHttpActionResult Create([FromBody] OrderDTO OrderDto) { try { if (!ModelState.IsValid) { return(Ok(new ApiResult <System.Web.Http.ModelBinding.ModelStateDictionary>() { Code = 3, Data = ModelState, Info = "请仔细填写表单!" })); } if (!OrderDto.ShopCartIds.Any()) { return(Ok(new ApiResult <string>() { Code = 4, Info = "请选择商品然后支付!" })); } using (TransactionScope scope = new TransactionScope()) { var shopCartItems = ShoppingCartItemService.GetAll().Where(x => OrderDto.ShopCartIds.Contains(x.Id)).ToList(); decimal orderTotal = 0; foreach (var item in shopCartItems) { orderTotal += item.UnitPrice * item.Quantity; } WxPayData data = new WxPayData(); var orderNo = WxPayApi.GenerateOutTradeNo(); data.SetValue("body", "麦呗商城-微信收款"); data.SetValue("attach", "麦呗商城"); data.SetValue("out_trade_no", orderNo); data.SetValue("total_fee", 1); // data.SetValue("total_fee", Convert.ToInt32(entity.OrderTotal*100)); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("trade_type", "APP"); WxPayData result = WxPayApi.UnifiedOrder(data); WxPayData appData = new WxPayData(); appData.SetValue("prepayid", result.GetValue("prepay_id").ToString()); WxPayData appResult = WxPayApi.AppOrder(appData); var entity = OrderDto.ToEntity(); entity.PrePayId = result.GetValue("prepay_id").ToString(); entity.WeChatSign = appResult.GetValue("sign").ToString(); entity.NonceStr = appResult.GetValue("noncestr").ToString(); entity.TimeSpan = appResult.GetValue("timestamp").ToString(); entity.OrderGuid = Guid.NewGuid(); entity.OrderStatus = OrderStatus.NotPay; entity.OrderTotal = orderTotal; entity.OutTradeNo = orderNo; entity.CustomerId = User.Identity.GetUserId(); entity.CustomerIp = Request.GetOwinContext().Request.RemoteIpAddress; entity.CreateTime = DateTime.Now; OrderService.Insert(entity); //更新购物车ItemStatus foreach (var shopCart in shopCartItems) { shopCart.OrderId = entity.Id; shopCart.ShoppingCartStatus = ShoppingCartStatus.Order; } ShoppingCartItemService.Update(shopCartItems); scope.Complete(); return(Ok(new ApiResult <OrderDTO>() { Data = entity.ToModel(), Info = "添加订单成功" })); } } catch (Exception ex) { return(Ok(new ApiResult <string>() { Code = 1, Info = ex.Message })); } }