Пример #1
0
        public ActionResult Notify()
        {
            Log.Error(this.GetType().ToString(), "transaction_id===>微信回调开始");
            ResultNotify result       = new ResultNotify(Request.InputStream);
            WxPayData    handlerRes   = result.ProcessNotify();
            string       out_trade_no = handlerRes.GetValue("out_trade_no").ToString();
            string       resXml       = string.Empty;

            Log.Error(this.GetType().ToString(), "transaction_id===>111111");
            //支付成功后处理
            if (handlerRes.GetValue("return_code").ToString() == "SUCCESS")
            {
                resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
                         + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
                string transaction_id = handlerRes.GetValue("transaction_id").ToString();
                Log.Error(this.GetType().ToString(), "transaction_id===>" + transaction_id);
                WxPayData orderCheck = null; //CheckTransactionidPayState(transaction_id, out_trade_no);
                if (true)
                {
                    var status = _rechargeOrderContract.RechargeOrders.Where(x => x.Prepay_Id == transaction_id && x.order_Uid == out_trade_no)
                                 .Select(x => x.pay_status).ToList().FirstOrDefault();
                    if (status != 1)
                    {
                        using (var a = _rechargeOrderContract.GetTransaction())
                        {
                            //status 0 订单生成 1 支付成功 2 支付失败
                            var res = _rechargeOrderContract.PaymentSuccess(transaction_id, 1, out_trade_no);
                            if (res.ResultType == OperationResultType.Error)
                            {
                                a.Rollback();
                            }
                            else
                            {
                                var b = _rechargeOrderContract.RechargeOrders.Where(x => x.order_Uid == out_trade_no)
                                        .Select(x => new MemberDepositDto()
                                {
                                    MemberId           = x.UserId,
                                    Price              = x.RechargeType == 0 ? x.TureAmount : 0,
                                    Score              = x.RechargeType == 1 ? x.TureAmount : 0,
                                    Cash               = x.Amount,
                                    order_Uid          = x.order_Uid,
                                    MemberDepositType  = 2,
                                    MemberActivityType = x.RechargeType
                                }).FirstOrDefault();
                                var updateMember = _memberdepositContract.InsertWx(b);
                                if (updateMember.ResultType == OperationResultType.Error)
                                {
                                    a.Rollback();
                                }
                                else
                                {
                                    //更新成功
                                    a.Commit();
                                }
                            }
                        }
                    }
                }
                else
                {
                    resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
                             + "<return_msg><![CDATA[支付失败]]></return_msg>" + "</xml> ";
                }
            }
            else
            {
                resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
                         + "<return_msg><![CDATA[支付失败]]></return_msg>" + "</xml> ";
                //支付失败处理
                if (out_trade_no != "")
                {
                    var res = _rechargeOrderContract.PaymentSuccess(null, 2, out_trade_no);
                }
            }
            Response.Write(resXml);
            Response.End();
            return(View());
        }
        //获取订单支付状态
        //public string GetWxOrderPayStatus(string out_trade_no)
        //{
        //    var a = _rechargeOrderContract.RechargeOrders.Where(x => x.order_Uid == out_trade_no).FirstOrDefault();
        //    var b = (from c in _rechargeOrderContract.RechargeOrders
        //             where c.order_Uid == out_trade_no
        //             select c.pay_status).FirstOrDefault();
        //    return b.ToString();
        //}

        //查询微信支付订单状态
        public string GetWxOrderPayStatus(string out_trade_no)
        {
            WxPayData data = new WxPayData();

            data.SetValue("appid", PaymentConfigHelper.Wx_XD_AppId);
            data.SetValue("mch_id", PaymentConfigHelper.Wx_XD_MCHID);//商户号
            data.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
            data.SetValue("transaction_id", "");
            data.SetValue("out_trade_no", out_trade_no);
            WxPayData result      = WxPayApi.OrderQuery(data, 6, 2);
            string    return_code = result.GetValue("return_code").ToString();
            string    result_code = result.GetValue("result_code").ToString();

            string trade_state = result.GetValue("trade_state").ToString();//订单交易状态

            if (return_code == "SUCCESS" && result_code == "SUCCESS")
            {
                switch (trade_state)
                {
                case "SUCCESS":
                    #region 支付成功处理
                    string transaction_id = result.GetValue("transaction_id").ToString();
                    var    status         = _rechargeOrderContract.RechargeOrders.Where(x => x.Prepay_Id == transaction_id && x.order_Uid == out_trade_no)
                                            .Select(x => x.pay_status).ToList().FirstOrDefault();
                    if (status != 1)
                    {
                        using (var a = _rechargeOrderContract.GetTransaction())
                        {
                            //status 0 订单生成 1 支付成功 2 支付失败
                            var res = _rechargeOrderContract.PaymentSuccess(transaction_id, 1, out_trade_no);
                            if (res.ResultType == OperationResultType.Error)
                            {
                                a.Rollback();
                            }
                            else
                            {
                                var b = _rechargeOrderContract.RechargeOrders.Where(x => x.order_Uid == out_trade_no)
                                        .Select(x => new MemberDepositDto()
                                {
                                    MemberId           = x.UserId,
                                    Price              = x.RechargeType == MemberActivityFlag.Recharge ? x.TureAmount : 0,
                                    Score              = x.RechargeType == MemberActivityFlag.Score ? x.TureAmount : 0,
                                    Card               = x.Amount,
                                    order_Uid          = x.order_Uid,
                                    MemberDepositType  = MemberDepositFlag.System,
                                    MemberActivityType = x.RechargeType
                                }).FirstOrDefault();
                                var updateMember = _memberdepositContract.InsertWx(b);
                                if (updateMember.ResultType == OperationResultType.Error)
                                {
                                    a.Rollback();
                                }
                                else
                                {
                                    //更新成功
                                    a.Commit();
                                }
                            }
                        }
                    }
                    #endregion
                    break;

                case "PAYERROR":
                    #region 支付失败处理
                    var resFaile = _rechargeOrderContract.PaymentSuccess(null, 2, out_trade_no);
                    #endregion
                    break;

                case "REFUND":
                    #region 转入退款
                    #endregion
                    break;

                case "NOTPAY":
                    #region 未支付
                    #endregion
                    break;

                case "CLOSED":
                    #region 已关闭
                    #endregion
                    break;

                case "REVOKED":
                    #region 已撤销(刷卡支付)
                    #endregion
                    break;

                case "USERPAYING":
                    #region 用户支付中
                    #endregion
                    break;
                }
            }
            var order      = _rechargeOrderContract.RechargeOrders.Where(x => x.order_Uid == out_trade_no).FirstOrDefault();
            var orderState = (from c in _rechargeOrderContract.RechargeOrders
                              where c.order_Uid == out_trade_no
                              select c.pay_status).FirstOrDefault();
            return(orderState.ToString());
        }