.NET开发微信小程序-接收微信支付回调数据
获取微信支付传回来的数据
1.MVC控制器
/// <summary> /// 付款返回的数据 /// </summary> /// <returns></returns> public string Notify_url() { String xmlData = getPostStr(); //保存付款成功过来的数据 Log4Helper.ErrorInfo("GXL", xmlData); WxAPIService.PayResult(xmlData); return "SUCCESS"; }
2.获取参数的方法
//获得Post过来的数据 public static string getPostStr() { Int32 intLen = Convert.ToInt32(System.Web.HttpContext.Current.Request.InputStream.Length); byte[] b = new byte[intLen]; System.Web.HttpContext.Current.Request.InputStream.Read(b, 0, intLen); return System.Text.Encoding.UTF8.GetString(b); }
3.处理付款结果
/// <summary> /// 付款结果处理 /// </summary> public static void PayResult(string ResultMsg) { if(!string.IsNullOrEmpty(ResultMsg)) { var xml = new XmlDocument(); xml.LoadXml(ResultMsg); //处理返回的值 DataSet ds = new DataSet(); StringReader stram = new StringReader(ResultMsg); XmlTextReader reader = new XmlTextReader(stram); ds.ReadXml(reader); string return_code = ds.Tables[0].Rows[0]["return_code"].ToString(); if (return_code.ToUpper() == "SUCCESS") { //通信成功 string result_code = ds.Tables[0].Rows[0]["result_code"].ToString();//业务结果 if (result_code.ToUpper() == "SUCCESS") { string appid = ds.Tables[0].Rows[0]["appid"].ToString(); string attach = ds.Tables[0].Rows[0]["attach"].ToString(); string mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString(); string openid = ds.Tables[0].Rows[0]["openid"].ToString(); Int32 total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString()); string transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString(); Log4Helper.ErrorInfo("GXL", "付款成功:attach:" + attach + ",小程序appID:" + appid + ",商户号:" + mch_id + ",支付人:" + openid + ",付款金额:" + total_fee + ",商户交易号:" + transaction_id); } else { Log4Helper.ErrorInfo("GXL", "支付失败:" + ResultMsg); } } else { Log4Helper.ErrorInfo("GXL", "支付失败:" + ResultMsg); } } }