微信支付-公眾號支付 ASP.NET
阿新 • • 發佈:2019-01-30
protected void Page_Load(object sender, EventArgs e) { Total_price =100; //價格 //因為jsapi_ticket票據獲取次數有限,所以快取7200秒 if (HttpContext.Current.Session["jsapi"] == null) // {
//acess_token 同樣獲取次數有限,所以快取7200秒
string actoken = ""; if (HttpContext.Current.Session["token"] != null){ actoken = HttpContext.Current.Session["token"] as string; } else {
//獲取access_token
Access_token ac = au.GetAcess_token(AppID, AppSecret); if (ac != null)//獲取JSAPI的票據
WeiXinJsapi_ticket jsapi = au.GetTickect(actoken);
public string paySign = "";//paySign
public string RdCode = "";//隨機數
public string prepay_id = "";//package中prepay_id的值
public string key = "haobangren201691wuzhenghuitestpa";
/// <summary>
/// 獲取prepayId值方法
/// </summary>
public void GetprepayIdAndpaySign()
{
//公眾賬號ID
string appid = AppID;
//商品描述
string OrderCode = DateTime.Now.ToString("yyyyMMddHHmmss");
string body = "訂單號:" + OrderCode;
//商戶號
string mch_id = "1384081602";
//隨機字串
string nonce_str = wx.noncestr ;
//通知地址-接收微信支付成功通知 注意不能帶引數
string notify_url = "http://www.haobangren.com/WebChart/Member/Pays.aspx?action=add";
//使用者標識 -使用者在商戶appid下的唯一標識
string openid = userBll.GetUser(LoginUserID).OpenID;
//商戶訂單號
string out_trade_no = OrderCode;
out_trade_nos = OrderCode;
//下單IP
string spbill_create_ip = WebCommon.GetClientIP();
//總金額 分為單位
int total_fee = Total_price;
//交易型別 -JSAPI、NATIVE、APP 如果是生成二維碼請填寫NATIVE
string trade_type = "JSAPI";
//微信簽名
string tmpStr = "appid=" + appid + "&body=" + body + "&mch_id=" + mch_id + "&nonce_str=" + nonce_str + "¬ify_url=" + notify_url + "&openid=" + openid + "&out_trade_no=" + out_trade_no + "&spbill_create_ip=" + spbill_create_ip + "&total_fee=" + total_fee + "&trade_type=" + trade_type + "&key=" + key + "";
string Getprepay_idSign = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "MD5").ToUpper();
string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
string xml = "<xml>";
xml += "<openid>" + userBll.GetUser(LoginUserID).OpenID + "</openid>";
xml += "<body>" + body + "</body>";
xml += "<mch_id>" + mch_id + "</mch_id>";
xml += "<nonce_str>" + nonce_str + "</nonce_str>";
xml += "<notify_url>" + notify_url + "</notify_url>";
xml += "<openid>" + openid + "</openid>";
xml += "<out_trade_no>" + out_trade_no + "</out_trade_no>";
xml += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>";
xml += "<appid>" + AppID + "</appid>";
xml += "<total_fee>" + total_fee + "</total_fee>";
xml += "<trade_type>" + trade_type + "</trade_type>";
xml += "<sign>" + Getprepay_idSign + "</sign>";
xml += "</xml>";
DesignHawk.Common.LogHelper.WriteLog(null, "XML:" + xml);
string v = PostWebRequests(url, xml);
prepay_id = v;
paySign = "";
string v_tmpStr = "appId=" + appid + "&nonceStr=" + wx.noncestr + "&package=prepay_id=" + v + "&signType=MD5&timeStamp=" + wx.timestamp + "&key="+ key + "";
paySign = FormsAuthentication.HashPasswordForStoringInConfigFile(v_tmpStr, "MD5").ToUpper();
}
/// <summary>
/// 獲取prepay_id
/// </summary>
/// <param name="postUrl"></param>
/// <param name="menuInfo"></param>
/// <returns></returns>
public string PostWebRequests(string postUrl, string menuInfo)
{
string returnValue = string.Empty;
try
{
byte[] byteData = Encoding.UTF8.GetBytes(menuInfo);
Uri uri = new Uri(postUrl);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(uri);
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteData.Length;
//定義Stream資訊
Stream stream = webReq.GetRequestStream();
stream.Write(byteData, 0, byteData.Length);
stream.Close();
//獲取返回資訊
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
returnValue = streamReader.ReadToEnd();
//關閉資訊
streamReader.Close();
response.Close();
stream.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(returnValue);
XmlNodeList list = doc.GetElementsByTagName("xml");
XmlNode xn = list[0];
string prepay_ids = xn.SelectSingleNode("//prepay_id").InnerText;
return prepay_ids;
//如果是二維碼掃描,請返回下邊的code_url,然後自己再更具內容生成二維碼即可
//string code_url = xn.SelectSingleNode("//prepay_id").InnerText;
//return code_url;
}
catch (Exception ex)
{
return "";
}
}