隐藏

.Net Core微信充值成功回调

发布:2022/12/27 17:08:48作者:管理员 来源:本站 浏览次数:500

微信APP支付成功后,回调(C# .netcore)

        [HttpPost]
        public async Task<string> GetNotify()
        {
            //获取回调POST过来的xml数据的代码
            Stream stream = HttpContext.Request.Body;
            byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
            await stream.ReadAsync(buffer, 0, buffer.Length); //.net core 3.1需要用ReadAsync才不会出错
            string xml = System.Text.Encoding.UTF8.GetString(buffer);

            //System.IO.File.WriteAllText(@"D:\notify.txt", xml);//将POST过来的xml数据写入TXT文件,用于postman测试

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            if (xmlDoc.DocumentElement.GetElementsByTagName("return_code")[0].InnerText == "SUCCESS")
            {
                string RechargeMoney = xmlDoc.DocumentElement.GetElementsByTagName("total_fee")[0].InnerText;  //充值金额
                string RechargeTime = xmlDoc.DocumentElement.GetElementsByTagName("time_end")[0].InnerText;  //充值时间
                string RechargeNo = xmlDoc.DocumentElement.GetElementsByTagName("out_trade_no")[0].InnerText;  //订单号
                string Result_Code = xmlDoc.DocumentElement.GetElementsByTagName("result_code")[0].InnerText;  //result_code
                string Return_Code = xmlDoc.DocumentElement.GetElementsByTagName("return_code")[0].InnerText;  //return_code
                string TransactionId = xmlDoc.DocumentElement.GetElementsByTagName("transaction_id")[0].InnerText;  //商户平台支付单号
               
                //这里是进行各种逻辑代码,最好对sign进行下验证

                return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"; //回调成功返回给微信,避免重复回调
                return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>"; //回调失败返回给微信
            }
        }