tnblog
首页
视频
资源
登录

.NET接入微信支付(五)支付通知解析

3068人阅读 2023/2/23 17:12 总访问:25248 评论:0 收藏:0 手机
分类: 微信支付
        /// <summary>
        /// 读取通知请求流
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        private string ReadRequest(Stream stream)
        {
            System.IO.Stream s = stream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            return builder.ToString();
        }
        /// <summary>
        /// 支付通知处理
        /// </summary>
        public void ProcessNotify()
        {
            Logger loger = LogManager.GetCurrentClassLogger();
            //读取请求流
            string resultStr = ReadRequest(Request.InputStream);
            loger.Info("获取支付通知:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            loger.Info("获取支付通知:" + resultStr);
            //解析对象
            WcPayNotifyRequest req = resultStr.FromJsonObj<WcPayNotifyRequest>();
            WechatPayHeader payHeader = new WechatPayHeader(Request.Headers);
            if (!WxPayHelper.VerifySign(resultStr, payHeader))  //验签
            {
                loger.Info("平台证书验签失败:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                Response.StatusCode = 500;
                Response.Write("{\"code\": \"FAIL\",\"message\": \"验签失败\"}");
                Response.End();
                return;
            }
            loger.Info("平台证书验签成功:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            WcPaySuccessNotifyRequest wcPaySuccess = null;
            if (req.event_type.Contains("SUCCESS"))  //支付成功
            {
                string decryptText = WxPayHelper.GetAesGcmDecrypt(req.resource.associated_data, req.resource.nonce, req.resource.ciphertext);
                wcPaySuccess = decryptText.FromJsonObj<WcPaySuccessNotifyRequest>();
                loger.Info("获取支付通知资源对象解密:" + decryptText);
            }
            //查询订单,判断订单真实性
            if (!QueryOrder(wcPaySuccess))
            {
                Response.StatusCode = 500;
                Response.Write("{\"code\": \"FAIL\",\"message\": \"订单未支付成功\"}");
                Response.End();
                return;
            }
            else//查询订单支付成功
            {
                try
                {
                    //do ....
                    Response.StatusCode = 200;
                    Response.Write("{\"code\": \"SUCCESS\",\"message\": \"OK\"}");
                    Response.End();
                    return;
                }
                catch (Exception ex)
                {
                    loger.Info("处理支付回传结果失败:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    loger.Info("处理支付回传结果失败:" + ex.Message + ":" + ex.ToStringEx());
                    Response.StatusCode = 200;
                    Response.Write("{\"code\": \"SUCCESS\",\"message\": \"OK\"}");
                    Response.End();
                    return;
                }
            }
        }


评价
一木浮沉,吾与之。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术