微信小程序支付C#后端源碼
本文實例為大家分享了微信小程序支付C#后端源碼,供大家參考,具體內容如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.IO; using System.Security.Cryptography; using System.Text; using System.Xml; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Mvc_vue.Controllers { public class wxController : Controller { // // GET: /wx/ public ActionResult Index() { return View(); } //所需值 public static string _appid = "wxd930ea5d5a258f4f"; public static string _mch_id = "10000100"; public static string _key = "192006250b4c09247ec02edce69f6a2d"; //模擬wx統(tǒng)一下單 openid(前臺獲取) public string getda(string openid) { return Getprepay_id(_appid, "shanghaifendian", "monixiaofei", _mch_id, GetRandomString(30), "http://www.weixin.qq.com/wxpay/pay.php", openid, getRandomTime(), 1); } //微信統(tǒng)一下單獲取prepay_id & 再次簽名返回數(shù)據(jù) private static string Getprepay_id(string appid, string attach, string body, string mch_id, string nonce_str, string notify_url, string openid, string bookingNo, int total_fee) { var url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信統(tǒng)一下單請求地址 string strA = "appid=" + appid + "&attach=" + attach + "&body=" + body + "&mch_id=" + mch_id + "&nonce_str=" + nonce_str + "¬ify_url=" + notify_url + "&openid=" + openid + "&out_trade_no=" + bookingNo + "&spbill_create_ip=61.50.221.43&total_fee=" + total_fee + "&trade_type=JSAPI"; string strk = strA + "&key="+_key; //key為商戶平臺設置的密鑰key(假) string strMD5 = MD5(strk).ToUpper();//MD5簽名 //string strHash=HmacSHA256("sha256",strmd5).ToUpper(); //簽名方式只需一種(MD5 或 HmacSHA256 【支付文檔需仔細看】) //簽名 var formData = "<xml>"; formData += "<appid>" + appid + "</appid>";//appid formData += "<attach>" + attach + "</attach>"; //附加數(shù)據(jù)(描述) formData += "<body>" + body + "</body>";//商品描述 formData += "<mch_id>" + mch_id + "</mch_id>";//商戶號 formData += "<nonce_str>" + nonce_str + "</nonce_str>";//隨機字符串,不長于32位。 formData += "<notify_url>" + notify_url + "</notify_url>";//通知地址 formData += "<openid>" + openid + "</openid>";//openid formData += "<out_trade_no>" + bookingNo + "</out_trade_no>";//商戶訂單號 --待 formData += "<spbill_create_ip>61.50.221.43</spbill_create_ip>";//終端IP --用戶ip formData += "<total_fee>" + total_fee + "</total_fee>";//支付金額單位為(分) formData += "<trade_type>JSAPI</trade_type>";//交易類型(JSAPI--公眾號支付) formData += "<sign>" + strMD5 + "</sign>"; //簽名 formData += "</xml>"; //請求數(shù)據(jù) var getdata = sendPost(url, formData); //獲取xml數(shù)據(jù) XmlDocument doc = new XmlDocument(); doc.LoadXml(getdata); //xml格式轉json string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc); JObject jo = (JObject)JsonConvert.DeserializeObject(json); string prepay_id = jo["xml"]["prepay_id"]["#cdata-section"].ToString(); //時間戳 string _time = getTime().ToString(); //再次簽名返回數(shù)據(jù)至小程序 string strB = "appId=" + appid + "&nonceStr=" + nonce_str + "&package=prepay_id=" + prepay_id + "&signType=MD5&timeStamp=" + _time + "&key="_key; wx w = new wx(); w.timeStamp = _time; w.nonceStr = nonce_str; w.package = "prepay_id=" + prepay_id; w.paySign = MD5(strB).ToUpper(); ; w.signType = "MD5"; //向小程序發(fā)送json數(shù)據(jù) return JsonConvert.SerializeObject(w); } /// <summary> /// 生成隨機串 /// </summary> /// <param name="length">字符串長度</param> /// <returns></returns> private static string GetRandomString(int length) { const string key = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; if (length < 1) return string.Empty; Random rnd = new Random(); byte[] buffer = new byte[8]; ulong bit = 31; ulong result = 0; int index = 0; StringBuilder sb = new StringBuilder((length / 5 + 1) * 5); while (sb.Length < length) { rnd.NextBytes(buffer); buffer[5] = buffer[6] = buffer[7] = 0x00; result = BitConverter.ToUInt64(buffer, 0); while (result > 0 && sb.Length < length) { index = (int)(bit & result); sb.Append(key[index]); result = result >> 5; } } return sb.ToString(); } /// <summary> /// 獲取時間戳 /// </summary> /// <returns></returns> private static long getTime() { TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1))); long t = (long)cha.TotalSeconds; return t; } /// <summary> /// MD5簽名方法 /// </summary> /// <param name="inputText">加密參數(shù)</param> /// <returns></returns> private static string MD5(string inputText) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText); byte[] targetData = md5.ComputeHash(fromData); string byte2String = null; for (int i = 0; i < targetData.Length; i++) { byte2String += targetData[i].ToString("x2"); } return byte2String; } /// <summary> /// HMAC-SHA256簽名方式 /// </summary> /// <param name="message"></param> /// <param name="secret"></param> /// <returns></returns> private static string HmacSHA256(string message, string secret) { secret = secret ?? ""; var encoding = new System.Text.UTF8Encoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } } /// <summary> /// wx統(tǒng)一下單請求數(shù)據(jù) /// </summary> /// <param name="URL">請求地址</param> /// <param name="urlArgs">參數(shù)</param> /// <returns></returns> private static string sendPost(string URL, string urlArgs) { //context.Request["args"] System.Net.WebClient wCient = new System.Net.WebClient(); wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] postData = System.Text.Encoding.ASCII.GetBytes("body=" + urlArgs); byte[] responseData = wCient.UploadData(URL, "POST", postData); string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的數(shù)據(jù) return returnStr;202 } /// <summary> /// 生成訂單號 /// </summary> /// <returns></returns> private static string getRandomTime() { Random rd = new Random();//用于生成隨機數(shù) string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期 string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//帶日期的隨機數(shù) return str; } } }
使用的是MVC .NET Framework4
微信小程序支付前端源碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別
欄 目:C#教程
下一篇:C#利用VS中插件打包并發(fā)布winfrom程序
本文標題:微信小程序支付C#后端源碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5100.html
您可能感興趣的文章
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復原窗體的方
- 01-10WinForm實現(xiàn)程序一段時間不運行自動關閉的方法
- 01-10C#中DataGridView常用操作實例小結
- 01-10C#實現(xiàn)將程序鎖定到Win7任務欄的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10c# ArrayList的使用方法小總結
- 01-10C#一個簡單的定時小程序實現(xiàn)代碼
- 01-10C#實現(xiàn)程序等待延遲執(zhí)行的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設置