Exemple #1
0
        public static bool SyncPaymentResult(decimal orderId)
        {
            IOnlineOrder factory = OnlineOrderFactory.GetFactory();
            OnlineOrder  order   = factory.QueryByOrderId(orderId);

            if (order.PaymentChannel != PaymentChannel.WeiXinPay && order.PaymentChannel == PaymentChannel.AliPay)
            {
                throw new MyException("目前只能同步微信或支付宝的支付结果");
            }

            if (order.Status != OnlineOrderStatus.WaitPay && order.Status != OnlineOrderStatus.Paying)
            {
                throw new MyException("只有待支付或支付中的订单才能同步");
            }

            switch (order.PaymentChannel)
            {
            case PaymentChannel.WeiXinPay: {
                UnifiedOrderQueryMessage result = PaymentServices.UnifiedOrderQuery(order.CompanyID, order.SerialNumber, order.OrderID.ToString());
                if (result.Success)
                {
                    DateTime payTime = GetConversionWeiXinPayTime(result.Tiem_End);
                    return(PaySuccess(orderId, result.TransactionId, payTime));
                }

                if (result.Return_Code.ToUpper() != "SUCCESS")
                {
                    throw new MyException(string.Format("同步失败:{0}", result.Return_Msg));
                }

                if (result.Result_Code.ToUpper() != "SUCCESS")
                {
                    throw new MyException(string.Format("同步失败:【{0}】{1}", result.Err_Code, result.Err_Code_Des));
                }

                if (result.Trade_State.ToUpper() != "SUCCESS")
                {
                    throw new MyException(string.Format("同步失败:{0}", GetWeiXinPayOrderErrorStateDes(result.Trade_State)));
                }
                break;
            }

            case PaymentChannel.AliPay: {
                DateTime payTime = DateTime.Now;
                bool     result  = AliPayApiServices.QueryPayResult(order.CompanyID, order.OrderID.ToString(), order.PrepayId, out payTime);
                if (result)
                {
                    return(PaySuccess(orderId, order.PrepayId, payTime));
                }
                else
                {
                    throw new MyException("该订单未支付");
                }
            }

            default: throw new MyException("支付通道不正确");
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        /// 手动退款(退款失败是调用)
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="certpath"></param>
        /// <returns></returns>
        public static bool ManualRefund(decimal orderId, string certpath)
        {
            TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "方法名:{0},操作类型:{1},订单编号:{2},备注:{3}", "ManualRefund", "订单退款处理", orderId, "开始订单退款处理");

            bool refundResult = false;

            lock (order_lock)
            {
                try
                {
                    IOnlineOrder factory = OnlineOrderFactory.GetFactory();
                    OnlineOrder  order   = factory.QueryByOrderId(orderId);
                    if (order.Status == OnlineOrderStatus.RefundFail)
                    {
                        refundResult = Refund(order, certpath);
                    }
                    if (refundResult)
                    {
                        OperateLogServices.AddOperateLog(OperateType.Other, string.Format("手动执行退款操作,退款订单号:{0}", orderId));
                    }
                }
                catch (Exception ex)
                {
                    ExceptionsServices.AddExceptionToDbAndTxt("OnlineOrderServices", "手动执行退款操作失败", ex, LogFrom.WeiXin);
                }
            }
            return(refundResult);
        }
Exemple #3
0
        /// <summary>
        /// 订单自动退款处理
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="certpath"></param>
        /// <returns></returns>
        public static bool AutoRefund(decimal orderId, string certpath)
        {
            TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "方法名:{0},操作类型:{1},订单编号:{2},备注:{3}", "AutoRefund", "订单退款处理", orderId, "开始订单退款处理");

            bool refundResult = false;

            lock (order_lock)
            {
                TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "进入lock,orderId:{0}", orderId);

                try
                {
                    IOnlineOrder factory = OnlineOrderFactory.GetFactory();
                    OnlineOrder  order   = factory.QueryByOrderId(orderId);
                    if (order.Status == OnlineOrderStatus.SyncPayResultFail && order.SyncResultTimes >= 3)
                    {
                        refundResult = Refund(order, certpath);
                    }
                }
                catch (Exception ex) {
                    TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "方法名:{0},操作类型:{1},订单编号:{2},Message:{3},StackTrace:{4}", "AutoRefund", "订单退款处理失败", orderId, ex.Message, ex.StackTrace);
                }
                TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "执行完成lock,orderId:{0}", orderId);
            }

            return(refundResult);
        }
Exemple #4
0
        public static OnlineOrder QueryByOrderId(decimal orderId)
        {
            if (orderId <= 0)
            {
                throw new ArgumentNullException("orderId");
            }

            IOnlineOrder factory = OnlineOrderFactory.GetFactory();

            return(factory.QueryByOrderId(orderId));
        }
Exemple #5
0
        public static bool PaySuccess(decimal orderId, string serialNumber, DateTime payTime, string payAccount = "")
        {
            bool result = false;

            lock (order_lock)
            {
                try
                {
                    IOnlineOrder factory = OnlineOrderFactory.GetFactory();

                    OnlineOrder order = factory.QueryByOrderId(orderId);
                    if (order == null)
                    {
                        throw new MyException("订单编号不存在");
                    }

                    if (order.Status != OnlineOrderStatus.WaitPay && order.Status != OnlineOrderStatus.Paying)
                    {
                        string message = string.Format("【{0}】,订单编号:{1},状态:{2}", order.PaymentChannel.GetDescription(), order.OrderID, order.Status.GetDescription());
                        TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "【更改订单状态为已支付失败】订单不是有效状态,备注:{0},支付账号:{1}", message, order.PayAccount);
                        return(false);
                    }

                    if (!string.IsNullOrWhiteSpace(payAccount))
                    {
                        result = factory.PaySuccess(order.OrderID, serialNumber, payAccount, payTime);
                    }
                    else
                    {
                        result = factory.PaySuccess(order.OrderID, serialNumber, payTime);
                    }

                    if (result)
                    {
                        order.Status       = OnlineOrderStatus.PaySuccess;
                        order.SerialNumber = serialNumber;
                        order.RealPayTime  = payTime;
                        TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "方法名:{0},操作类型:{1},订单编号:{2},备注:{3}", "PaySuccess", "更改订单状态为已支付", orderId, "更改订单状态为已支付成功");

                        bool   noticeResult = NoticePaymentResult(order);
                        string remark       = noticeResult ? "通知支付结果成功" : "通知支付结果失败";
                        TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "方法名:{0},操作类型:{1},订单编号:{2},备注:{3}", "PaySuccess", "更改订单状态为已支付", orderId, remark);
                    }
                    else
                    {
                        TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "【更改订单状态为已支付失败】【{0}】,订单编号:{1},状态:{2}", order.PaymentChannel.GetDescription(), order.OrderID, order.Status.GetDescription());
                    }
                }
                catch (Exception ex) {
                    TxtLogServices.WriteTxtLogEx("OnlineOrderServices", "【更改订单状态为已支付失败】订单编号:{0},Message:{1},StackTrace:{2}", orderId, ex.Message, ex.StackTrace);
                }
                return(result);
            }
        }
Exemple #6
0
        /// <summary>
        /// 再次同步支付结果
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="payTime"></param>
        /// <returns></returns>
        public static void AgainSyncPayResult(decimal orderId, DateTime payTime)
        {
            lock (order_lock)
            {
                IOnlineOrder factory = OnlineOrderFactory.GetFactory();

                OnlineOrder order = factory.QueryByOrderId(orderId);
                if (order.Status == OnlineOrderStatus.SyncPayResultFail)
                {
                    using (DbOperator dbOperator = ConnectionManager.CreateConnection())
                    {
                        try
                        {
                            dbOperator.BeginTransaction();
                            bool result = factory.SyncPayResultSuccess(orderId, dbOperator);
                            if (!result)
                            {
                                throw new MyException("更改同步成功状态失败");
                            }

                            string payDetailId     = string.Empty;
                            bool   noticePayResult = SyncNoticePayResult(orderId, payTime, order, out payDetailId);
                            if (!noticePayResult)
                            {
                                throw new MyException("同步支付结果失败");
                            }

                            dbOperator.CommitTransaction();


                            UpdateSyncResultTimes(order.OrderID, payDetailId);
                            SendSyncResultMessage(order);
                        }
                        catch (MyException ex)
                        {
                            dbOperator.RollbackTransaction();
                            factory.SyncPayResultFail(order.OrderID, ex.Message);
                        }
                        catch (Exception ex)
                        {
                            dbOperator.RollbackTransaction();
                            factory.SyncPayResultFail(orderId, "同步支付结果异常");

                            string message = string.Format("【{0}】再次同步支付结果,订单编号:{1},描述:{2}", order.PaymentChannel.GetDescription(), order.OrderTime, ex.Message);
                            ExceptionsServices.AddExceptionToDbAndTxt("OnlineOrderServices", message, ex, LogFrom.WeiXin);
                        }
                    }
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// 订单支付中
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static bool OrderPaying(decimal orderId)
        {
            lock (order_lock)
            {
                try
                {
                    IOnlineOrder factory = OnlineOrderFactory.GetFactory();

                    OnlineOrder order = factory.QueryByOrderId(orderId);
                    if (order.Status == OnlineOrderStatus.WaitPay)
                    {
                        return(factory.UpdateOrderStatusByOrderId(orderId, OnlineOrderStatus.Paying));
                    }
                    return(false);
                }
                catch (Exception ex)
                {
                    ExceptionsServices.AddExceptionToDbAndTxt("OnlineOrderServices", "更改订单支付中失败", ex, LogFrom.WeiXin);
                    return(false);
                }
            }
        }
Exemple #8
0
 /// <summary>
 /// 取消待支付订单
 /// </summary>
 /// <param name="orderId"></param>
 /// <returns></returns>
 public static bool CancelOrder(decimal orderId)
 {
     lock (order_lock)
     {
         try
         {
             IOnlineOrder factory = OnlineOrderFactory.GetFactory();
             OnlineOrder  order   = factory.QueryByOrderId(orderId);
             if (order.Status == OnlineOrderStatus.WaitPay)
             {
                 bool result = factory.UpdateOrderStatusByOrderId(orderId, OnlineOrderStatus.Cancel);
                 if (result && order.PaymentChannel == PaymentChannel.WeiXinPay)
                 {
                     PaymentServices.CloseOrderPay(order.CompanyID, orderId.ToString());
                 }
                 return(result);
             }
         }
         catch (Exception ex) {
             ExceptionsServices.AddExceptionToDbAndTxt("OnlineOrderServices", "取消订单失败", ex, LogFrom.WeiXin);
         }
     }
     return(false);
 }