微信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>"; //回調失敗返回給微信 } }