예제 #1
0
        /// <summary>
        /// 支付宝Notify & Return回写操作
        /// Notify使用Post方式,Return使用Get方法并完成URL的重定向
        /// </summary>
        /// <param name="httpMethod">Http请求方式,0为Get,1为Post</param>
        public override void Notify_Return(HttpMethod httpMethod)
        {
            var    Request  = HttpContext.Current.Request;
            var    Response = HttpContext.Current.Response;
            var    get_post = httpMethod == HttpMethod.Get ? Request.QueryString : Request.Form;
            string body     = Utility.DecryptString(Request.Params["body"].ToString());
            int    userID   = Convert.ToInt32(body.Split('|')[0]);
            string orderID  = "0";
            string orderIDs = string.Empty;

            orderID = body.Split('|')[1].Split(',')[0];
            if (body.Split('|')[1].Split(',').Length > 1)
            {
                orderIDs = Utility.DecryptString(body.Split('|')[1]);
            }
            int     types      = Convert.ToInt32(body.Split('|')[2]);
            decimal totalFee   = Convert.ToDecimal(body.Split('|')[3]);
            int     sourceType = Convert.ToInt32(body.Split('|')[4]);
            string  exchangeID = Request.Params["trade_no"].ToString();//支付宝的交易号
            string  exception  = null;
            bool    ret        = false;

            try
            {
                StringBuilder result = new StringBuilder();
                foreach (var item in get_post.AllKeys)
                {
                    result.Append(item + get_post[item].ToString() + ",");
                }
                LoggerFactory.Instance.Logger_Info("支付宝回调参数集合:" + result.ToString());
                ret = AlipayReturnParam(userID,
                                        orderID,
                                        exchangeID,
                                        get_post["notify_id"],
                                        get_post["sign"],
                                        get_post,
                                        System.Web.HttpContext.Current);
            }
            catch (Exception ex)
            {
                exception = "sign信息验证状态:" + ret + ",异常信息:" + ex.Message;
                LoggerFactory.Instance.Logger_Info(exception);
            }

            var model = new OrderEventArgs
            {
                OrderID          = orderID,//商家订单号
                UserID           = userID,
                UseType          = (UseType)types,
                TotalFee         = totalFee,
                SourceType       = sourceType,
                ExceptionMessage = exception,
                ExchangeCode     = exchangeID//支付宝交易号
            };

            try
            {
                LoggerFactory.Instance.Logger_Info(model.ToString());
            }
            catch (Exception ex)
            {
                LoggerFactory.Instance.Logger_Info(ex.Message);
            }

            if (ret)
            {
                try
                {
                    //成功
                    OnSuccess(model);
                    if (httpMethod == HttpMethod.Post)
                    {
                        Response.Write("success");
                    }
                }
                catch (Exception)//业务层出现问题,向支付宝也输出fail
                {
                    Response.Write("fail");
                }
            }
            else
            {
                try
                {
                    OnFail(model);
                    if (httpMethod == HttpMethod.Post)
                    {
                        Response.Write("fail");
                    }
                }
                catch (Exception)
                {
                    Response.Write("fail");
                }
            }
        }