本文转自:http://www.cnblogs.com/weizhiing/p/7700723.html
又为大家带来简单的c#后台支付结果回调方法,首先还是要去微信官网下载模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在打开WxPayAPI项目中的example文件下的
NativeNotifyPage.aspx打开网页中的代码页如图:
将以下代码加入进去就能完成:
public partial class NativeNotifyPage : System.Web.UI.Page { public static string wxJsApiParam { get; set; } //前段显示 public string return_result = ""; protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "text/plain"; Response.Write("Hello World"); LogHelper.WriteLog(typeof(NativeNotifyPage), "可以运行1-1"); String xmlData = getPostStr();//获取请求数据 if (xmlData == "") { } else { var dic = new Dictionary<string, string> { {"return_code", "SUCCESS"}, {"return_msg","OK"} }; var sb = new StringBuilder(); sb.Append("<xml>"); foreach (var d in dic) { sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">"); } sb.Append("</xml>"); //把数据重新返回给客户端 DataSet ds = new DataSet(); StringReader stram = new StringReader(xmlData); XmlTextReader datareader = new XmlTextReader(stram); ds.ReadXml(datareader); if (ds.Tables[0].Rows[0]["return_code"].ToString() == "SUCCESS") { LogHelper.WriteLog(typeof(NativeNotifyPage), "数据能返回"); string wx_appid = "";//微信开放平台审核通过的应用APPID string wx_mch_id = "";//微信支付分配的商户号 string wx_nonce_str = "";// 随机字符串,不长于32位 string wx_sign = "";//签名,详见签名算法 string wx_result_code = "";//SUCCESS/FAIL string wx_return_code = ""; string wx_openid = "";//用户在商户appid下的唯一标识 string wx_is_subscribe = "";//用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效 string wx_trade_type = "";// APP string wx_bank_type = "";// 银行类型,采用字符串类型的银行标识,银行类型见银行列表 string wx_fee_type = "";// 货币类型,符合ISO4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型 string wx_transaction_id = "";//微信支付订单号 string wx_out_trade_no = "";//商户系统的订单号,与请求一致。 string wx_time_end = "";// 支付完成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则 int wx_total_fee = 1;// 订单总金额,单位为分 int wx_cash_fee = 1;//现金支付金额订单现金支付金额,详见支付金额 #region 数据解析 //列 是否存在 string signstr = "";//需要前面的字符串 //wx_appid if (ds.Tables[0].Columns.Contains("appid")) { wx_appid = ds.Tables[0].Rows[0]["appid"].ToString(); if (!string.IsNullOrEmpty(wx_appid)) { signstr += "appid=" + wx_appid; } } //wx_bank_type if (ds.Tables[0].Columns.Contains("bank_type")) { wx_bank_type = ds.Tables[0].Rows[0]["bank_type"].ToString(); if (!string.IsNullOrEmpty(wx_bank_type)) { signstr += "&bank_type=" + wx_bank_type; } } //wx_cash_fee if (ds.Tables[0].Columns.Contains("cash_fee")) { wx_cash_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["cash_fee"].ToString()); signstr += "&cash_fee=" + wx_cash_fee; } //wx_fee_type if (ds.Tables[0].Columns.Contains("fee_type")) { wx_fee_type = ds.Tables[