又為大家帶來簡單的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[0].Rows[0]["fee_type"].ToString();
if (!string.IsNullOrEmpty(wx_fee_type))
{
signstr += "&fee_type=" + wx_fee_type;
}
}
//wx_is_subscribe
if (ds.Tables[0].Columns.Contains("is_subscribe"))
{
wx_is_subscribe = ds.Tables[0].Rows[0]["is_subscribe"].ToString();
if (!string.IsNullOrEmpty(wx_is_subscribe))
{
signstr += "&is_subscribe=" + wx_is_subscribe;
}
}
//wx_mch_id
if (ds.Tables[0].Columns.Contains("mch_id"))
{
wx_mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString();
if (!string.IsNullOrEmpty(wx_mch_id))
{
signstr += "&mch_id=" + wx_mch_id;
}
}
//wx_nonce_str
if (ds.Tables[0].Columns.Contains("nonce_str"))
{
wx_nonce_str = ds.Tables[0].Rows[0]["nonce_str"].ToString();
if (!string.IsNullOrEmpty(wx_nonce_str))
{
signstr += "&nonce_str=" + wx_nonce_str;
}
}
//wx_openid
if (ds.Tables[0].Columns.Contains("openid"))
{
wx_openid = ds.Tables[0].Rows[0]["openid"].ToString();
if (!string.IsNullOrEmpty(wx_openid))
{
signstr += "&openid=" + wx_openid;
}
}
//wx_out_trade_no
if (ds.Tables[0].Columns.Contains("out_trade_no"))
{
wx_out_trade_no = ds.Tables[0].Rows[0]["out_trade_no"].ToString();
if (!string.IsNullOrEmpty(wx_out_trade_no))
{
signstr += "&out_trade_no=" + wx_out_trade_no;
}
}
//wx_result_code
if (ds.Tables[0].Columns.Contains("result_code"))
{
wx_result_code = ds.Tables[0].Rows[0]["result_code"].ToString();
if (!string.IsNullOrEmpty(wx_result_code))
{
signstr += "&result_code=" + wx_result_code;
}
}
//wx_result_code
if (ds.Tables[0].Columns.Contains("return_code"))
{
wx_return_code = ds.Tables[0].Rows[0]["return_code"].ToString();
if (!string.IsNullOrEmpty(wx_return_code))
{
signstr += "&return_code=" + wx_return_code;
}
LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_return_code" + wx_return_code);
}
//wx_sign
if (ds.Tables[0].Columns.Contains("sign"))
{
wx_sign = ds.Tables[0].Rows[0]["sign"].ToString();
//if (!string.IsNullOrEmpty(wx_sign))
//{
// signstr += "&sign=" + wx_sign;
//}
}
//wx_time_end
if (ds.Tables[0].Columns.Contains("time_end"))
{
wx_time_end = ds.Tables[0].Rows[0]["time_end"].ToString();
if (!string.IsNullOrEmpty(wx_time_end))
{
signstr += "&time_end=" + wx_time_end;
}
LogHelper.WriteLog(typeof(NativeNotifyPage), "time_end" + wx_time_end);
}
//wx_total_fee
if (ds.Tables[0].Columns.Contains("total_fee"))
{
wx_total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString());
signstr += "&total_fee=" + wx_total_fee;
LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_total_fee" + wx_total_fee);
}
//wx_trade_type
if (ds.Tables[0].Columns.Contains("trade_type"))
{
wx_trade_type = ds.Tables[0].Rows[0]["trade_type"].ToString();
if (!string.IsNullOrEmpty(wx_trade_type))
{
signstr += "&trade_type=" + wx_trade_type;
}
}
//wx_transaction_id
if (ds.Tables[0].Columns.Contains("transaction_id"))
{
wx_transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString();
if (!string.IsNullOrEmpty(wx_transaction_id))
{
signstr += "&transaction_id=" + wx_transaction_id;
}
LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_transaction_id" + wx_transaction_id);
}
#endregion
//追加key 密鑰
signstr += "&key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();
//簽名正确
string orderStrwhere = "ordernumber='" + wx_out_trade_no + "'";
if (wx_sign == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(signstr, "MD5").ToUpper())
{
//簽名正确 處理訂單操作邏輯
}
else
{
//追加備注資訊
}
}
else
{
// 傳回資訊,如非空,為錯誤原因 簽名失敗 參數格式校驗錯誤
string return_msg = ds.Tables[0].Rows[0]["return_msg"].ToString();
}
return_result = sb.ToString();
}
}
public bool IsReusable
{
get
{
return false;
}
}
//獲得Post過來的資料
public 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);
}
}
備注:記得将方法位址加入到統一下單的中去如圖