public int Insert(Order_Payment orderPayment, SqlTransaction transaction) { /* Create Procedure [dbo].[sp_Order_Payment_Insert] @OrderID Int ,@PaymentOrgID Int ,@PaymentMoney Float ,@IsUseCoupon bit ,@IsUseIntegral bit ,@IsUseAccount bit ,@CreateTime datetime ,@ReferenceID int output As */ var paras = new List<SqlParameter>() { this.SqlServer.CreateSqlParameter( "OrderID", SqlDbType.Int, orderPayment.OrderID, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "PaymentOrgID", SqlDbType.Int, orderPayment.PaymentOrgID, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "PaymentMoney", SqlDbType.Float, orderPayment.PaymentMoney, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "TradeNo", SqlDbType.VarChar, orderPayment.TradeNo, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "IsUseCoupon", SqlDbType.Bit, orderPayment.IsUseCoupon, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "IsUseIntegral", SqlDbType.Bit, orderPayment.IsUseIntegral, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "IsUseAccount", SqlDbType.Bit, orderPayment.IsUseAccount, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "CreateTime", SqlDbType.DateTime, DateTime.Now, ParameterDirection.Input), this.SqlServer.CreateSqlParameter( "ReferenceID", SqlDbType.Int, null, ParameterDirection.Output) }; this.SqlServer.ExecuteNonQuery(CommandType.StoredProcedure, "sp_Order_Payment_Insert", paras, transaction); return (int)paras.Find(p => p.ParameterName == "ReferenceID").Value; }
/// <summary> /// 订单在线支付 /// </summary> /// <param name="order">订单对象</param> /// <param name="totalFee">支付金额</param> /// <param name="orderOrgID">支付方式(支付宝,财富通,工行,农行等)</param> /// <param name="tradeNo">支付交易号</param> /// <returns>返回修改订单成功与否</returns> public bool OrderOnLinePayment(Order order, double totalFee, int orderOrgID, string tradeNo) { //支付成功,改写数据库订单信息 //添加支付记录信息 //添加订单状态跟踪信息 LogUtils.Log( string.Format("系统更新在线支付订单信息,订单编码:{0},支付平台编码:{1},支付交易号:{2}", order.ID, orderOrgID, tradeNo), "OrderOnLinePayment", Category.Info, null, this.userID); order.Status = order.Status == 100 ? 0 : order.Status; this.orderDA.SqlServer.BeginTransaction(); var transaction = this.orderDA.SqlServer.Transaction; try { this.Edit(order, transaction); var orderPaymentDa = new DAFactoryTransact().CreateOrderPaymentDA(); var payment = new Order_Payment { OrderID = order.ID, IsDelete = 0, IsUseAccount = false, IsUseCoupon = false, IsUseIntegral = false, PaymentMoney = totalFee, TradeNo = tradeNo, PaymentOrgID = orderOrgID, CreateTime = DateTime.Now }; orderPaymentDa.Insert(payment, transaction); new OrderStatusTrackingService().Add( new Order_Status_Tracking { OrderID = order.ID, EmployeeID = isBackage ? this.userID : 0, UserID = isBackage?0:this.userID, Remark = "订单已支付", Status = order.Status }, transaction); transaction.Commit(); //检查是否可以由系统自动确认订单 try { if (!this.ConfirmOrderBySystem(order,payment)) { LogUtils.Log( string.Format("在线支付成功后,订单自动确认失败,订单编码:{0},支付平台编码:{1},支付交易号:{2}", order.ID, orderOrgID, tradeNo), "在线支付订单状态更新", Category.Warn, null, this.userID); } else { LogUtils.Log( string.Format("在线支付成功后,订单自动确认成功,订单编码:{0},支付平台编码:{1},支付交易号:{2}", order.ID, orderOrgID, tradeNo), "在线支付订单状态更新", Category.Info, null, this.userID); } } catch (Exception ex) { LogUtils.Log( string.Format("在线支付成功后,订单自动确认时出错,订单编码:{0},支付平台编码:{1},支付交易号:{2},错误信息:{3},堆栈:{4}", order.ID, orderOrgID, tradeNo,ex.Message,ex.StackTrace), "在线支付订单状态更新", Category.Error, null, this.userID); } return true; } catch(Exception exception) { if (transaction != null) { transaction.Rollback(); if (transaction.Connection!=null&&transaction.Connection.State != ConnectionState.Closed) { transaction.Connection.Close(); } } TextLogger.Instance.Log("更新订单支付信息出错~~", Category.Error, exception); LogUtils.Log( string.Format( "用户,UserID={0}已支付订单{1},处理订单状态是出错。错误信息:{2}", this.userID, order.OrderCode, exception.Message), "UpdatePayment", Category.Error); return false; } }
/// <summary> /// 确认订单--修改订单信息 /// </summary> /// <param name="order"> /// 订单对象 /// </param> /// <param name="transaction"> /// 事务对象 /// </param> /// <param name="orderProducts"></param> public void ConfirmByUpdateOrder(Order order, SqlTransaction transaction, Order_Payment orderPayment, List<Order_Product> orderProducts = null) { this.orderDA.UpdateForConfirmOrder(order, transaction); if (Utils.IsPushERP) //判断是否推送ERP { string errorMsg; if (!this.PushOrderToHwErp(order, orderProducts, transaction, out errorMsg, orderPayment)) { throw new Exception("订单推送失败,原因:" + errorMsg); //推送失败,主动抛出异常,让外边的程序进行回滚 } } }
/// <summary> /// 系统自动确认订单 /// </summary> /// <param name="order"> /// 订单对象 /// </param> /// <returns> /// 系统确认是否成功 /// </returns> public bool ConfirmOrderBySystem(Order order, Order_Payment payment = null) { /** * 系统自动审核订单步骤 * 1. 检查是否符合自动审核条件 * 2. 改变订单状态至已确认(1) * 3. 添加订单跟踪信息 * **/ if (!this.ValidateConfirmOrderBySystem(order)) { LogUtils.Log( string.Format("系统自动确认订单失败,原因:不符合条件。订单编码:{0}", order.ID), "检查订单自动确认是否符合条件:ConfirmOrderBySystem", Category.Info, null, this.userID); TextLogger.Instance.Log("系统自动确认订单失败,原因:不符合条件", Category.Warn, null); return false; } LogUtils.Log( string.Format("检查订单符合系统自动确认条件。订单编码:{0}", order.ID), "检查订单自动确认是否符合条件:ConfirmOrderBySystem", Category.Info, null, this.userID); SqlTransaction transaction = null; try { this.orderDA.SqlServer.BeginTransaction(); transaction = this.orderDA.SqlServer.Transaction; this.ConfirmByUpdateOrder(order, transaction, payment); new OrderStatusTrackingService().Add( new Order_Status_Tracking { OrderID = order.ID, EmployeeID = 0, Remark = "订单已经确认,等待出库", Status = 1, // 已确认的订单状态为码 1 UserID = 0 }, transaction); transaction.Commit(); LogUtils.Log( string.Format("系统自动确认订单成功。订单编码:{0}", order.ID), "系统自动确认订单:ConfirmOrderBySystem", Category.Info, null, this.userID); return true; } catch (Exception exception) { if (transaction != null) { transaction.Rollback(); } LogUtils.Log( string.Format( "系统自动确认订单发生错误,需客服手动确认。订单编码:{0},错误信息:{1},堆栈:{2}", order.ID, exception.Message, exception.StackTrace), "系统自动确认订单:ConfirmOrderBySystem", Category.Error, null, this.userID); TextLogger.Instance.Log("系统自动确认订单失败,需客服手动确认", Category.Error, exception); return false; } }
private bool PushOrderToHwErp(Order order, List<Order_Product> orderProducts, SqlTransaction transaction, out string errorMsg, Order_Payment payment = null) { #region 订单推送ERP #region 订单基本信息 errorMsg = string.Empty; var hi = new HwRest.HwOrderInfo(); hi.orderNumber = order.OrderCode; hi.orderDate = order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"); if (order.PaymentStatus == 1) { //var orderPayment = new OrderPaymentService().QueryByOrderID(order.ID); //if (orderPayment != null) hi.payTime = Convert.ToDateTime(orderPayment.CreateTime).ToString(); //todo:若订单已在线支付,则认为是当前时间支付,因为在线支付订单是有系统在支付时自动确认的。当时不够准确。 hi.payTime = payment == null ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : payment.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"); } if (string.IsNullOrWhiteSpace(hi.payTime)) { hi.payTime = ""; } var orderReceiveAddress = new UserReceiveAddressService().QueryByID(order.RecieveAddressID); hi.buyerNick = string.IsNullOrWhiteSpace(order.UserName) ? orderReceiveAddress.Consignee : order.UserName; hi.totalAmount = Math.Round(order.TotalMoney + order.DeliveryCost, 2).ToString(); hi.payment = payment == null ? (order.TotalMoney + order.DeliveryCost).ToString() : Math.Round(payment.PaymentMoney, 2).ToString(); hi.postAmount = order.DeliveryCost.ToString(); hi.discount = order.Discount.ToString(); hi.points = ""; hi.pointsAmount = ""; hi.couponsAmount = ""; hi.virtualAmount = ""; hi.fullMinus = ""; var orderInvoice = new OrderInvoiceService().SelectByOrderID(order.ID); hi.invoiceTitle = orderInvoice != null ? orderInvoice.InvoiceTitle : ""; hi.invoiceContent = orderInvoice != null ? orderInvoice.InvoiceContent : ""; hi.invoiceAmount = orderInvoice != null ? orderInvoice.InvoiceCost.ToString() : ""; hi.tradeFrom = "官网订单"; if (order.PaymentMethodID == 0) { if (payment != null) { hi.paymentType = payment.PaymentOrgName; } else { hi.paymentType = "网上支付"; } hi.sellerMemo = ""; } else { hi.paymentType = "货到付款"; hi.sellerMemo = ""; } hi.consignee = orderReceiveAddress != null ? orderReceiveAddress.Consignee : order.UserName; if (orderReceiveAddress != null) { if (!string.IsNullOrWhiteSpace(orderReceiveAddress.CountyName(","))) { var strs = orderReceiveAddress.CountyName(",").Split(','); if (strs.Length == 3) { hi.province = strs[0]; hi.city = strs[1]; hi.cityarea = strs[2]; } else { hi.cityarea = orderReceiveAddress.CountyName(","); hi.province = ""; hi.city = ""; } } } else { hi.cityarea = ""; hi.province = ""; hi.city = ""; } hi.address = orderReceiveAddress != null ? orderReceiveAddress.Address : ""; hi.mobilePhone = orderReceiveAddress != null ? orderReceiveAddress.Mobile : ""; hi.telephone = orderReceiveAddress != null ? orderReceiveAddress.Tel : ""; hi.zip = orderReceiveAddress != null ? orderReceiveAddress.ZipCode : ""; hi.buyerMessage ="客户备注:"+ order.Remark + ";客服备注:" + order.Description; #endregion #region 商品列表 List<HwRest.HwProductList> hplist = new List<HwRest.HwProductList>(); if (orderProducts == null || orderProducts.Count < 1) { orderProducts = new OrderProductService().QueryByOrderId(order.ID); } foreach (var orderProduct in orderProducts) { var hwProduct = hplist.FirstOrDefault(p => p.productNumber == orderProduct.Barcode && orderProduct.TransactPrice <= 0); //赠品与正常商品合并 if (hwProduct != null) { hwProduct.orderCount = (int.Parse(hwProduct.orderCount) + orderProduct.Quantity).ToString(); hwProduct.giftCount = (int.Parse(hwProduct.giftCount) + orderProduct.Quantity).ToString(); //将商品价格当做优惠金额进行处理 hwProduct.discountFee = (int.Parse(hwProduct.discountFee) + int.Parse(hwProduct.price) * orderProduct.Quantity).ToString(); } else { HwRest.HwProductList hpl = new HwRest.HwProductList(); hpl.productNumber = orderProduct.Barcode; //新系统中没有商品编号,此处设为商品条形码 hpl.productName = orderProduct.ProductName; hpl.skuNumber = orderProduct.Barcode; hpl.skuName = ""; hpl.price = orderProduct.TransactPrice.ToString(); hpl.orderCount = orderProduct.Quantity.ToString(); hpl.giftCount = (orderProduct.TransactPrice > 0 ? 0 : orderProduct.Quantity).ToString(); hpl.amount = (orderProduct.TransactPrice * orderProduct.Quantity).ToString(); hpl.memo = orderProduct.TransactPrice > 0 ? "" : "【赠品】"; hpl.discountFee = "0"; hpl.barcode = orderProduct.Barcode; hplist.Add(hpl); } } #endregion HwRest.OWebOrderItems OWebOrderItems = new HwRest.OWebOrderItems(); OWebOrderItems.OWebOrderItem = hplist.ToArray(); HwRest.HwOrderAdd_Info hai = new HwRest.HwOrderAdd_Info(); hi.OWebOrderItems = OWebOrderItems; hai.OWebOrder = hi; HwRest.HwClient HwClient = new HwRest.HwClient(); HwClient.XmlValues = hai.ToXmlParameter(); HwClient.OrderNumber = hi.orderNumber; string HwBackXml = HwClient.Execute(); XmlDocument doc = new XmlDocument(); doc.LoadXml(HwBackXml); XmlNode root = doc.SelectSingleNode("//Response"); var log = new Order_Erp_Log { ERP = "HW_ERP", OrderID = order.ID, OperateType = 1, ReqContent = HwClient.XmlValues, ResContent = HwBackXml, UserID = order.UserID, Operator = isBackage ? this.userID : 0, CreateTime = DateTime.Now }; if (payment != null) { log.ExtField = "在线支付订单,支付时间:" + payment.CreateTime; } var result = false; if (HwBackXml.Contains("推单异常") || root.FirstChild.InnerText.ToLower() != "true") { log.IsSuccess = false; //获取退单错误信息 var errorNode = root.SelectSingleNode("//value"); if (errorNode != null) { errorMsg = errorNode.InnerText; } result = false; LogUtils.Log("订单推送失败,订单编码:" + order.ID + ",错误消息:" + HwBackXml, "订单推送ERP", Category.Error); } else { log.IsSuccess = true; result = true; LogUtils.Log("订单推送成功,订单编码:" + order.ID, "订单推送ERP"); } try { var orderErpLogService = new OrderERPLogService(); orderErpLogService.Add(log, null); //添加日志,无需事务,防止回滚 } catch(Exception exception) { LogUtils.Log( string.Format( "[Order_ERP]订单推送成功,但是写入日志信息发生错误。订单编号:{0},错误信息:{1}/{2}", order.OrderCode, exception.Message, exception.InnerException), "[Order_ERP]订单推送", Category.Error); } return result; #endregion }