使用的是MVC .NET Framework4 微信小程式支付 小程式端源碼 MVC項目發佈前的配置 ...
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Web.Mvc; 7 using System.IO; 8 using System.Security.Cryptography; 9 using System.Text; 10 using System.Xml; 11 using Newtonsoft.Json; 12 using Newtonsoft.Json.Linq; 13 namespace Mvc_vue.Controllers 14 { 15 public class wxController : Controller 16 { 17 // 18 // GET: /wx/ 19 20 public ActionResult Index() 21 { 22 return View(); 23 } 24 //所需值 25 public static string _appid = "wxd930ea5d5a258f4f"; 26 public static string _mch_id = "10000100"; 27 public static string _key = "192006250b4c09247ec02edce69f6a2d"; 28 29 //模擬wx統一下單 openid(前臺獲取) 30 public string getda(string openid) 31 { 32 return Getprepay_id(_appid, "shanghaifendian", "monixiaofei", _mch_id, GetRandomString(30), "http://www.weixin.qq.com/wxpay/pay.php", openid, getRandomTime(), 1); 33 34 } 35 36 37 38 //微信統一下單獲取prepay_id & 再次簽名返回數據 39 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) 40 { 41 var url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信統一下單請求地址 42 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"; 43 string strk = strA + "&key="+_key; //key為商戶平臺設置的密鑰key(假) 44 string strMD5 = MD5(strk).ToUpper();//MD5簽名 45 46 //string strHash=HmacSHA256("sha256",strmd5).ToUpper(); //簽名方式只需一種(MD5 或 HmacSHA256 【支付文檔需仔細看】) 47 48 //簽名 49 var formData = "<xml>"; 50 formData += "<appid>" + appid + "</appid>";//appid 51 formData += "<attach>" + attach + "</attach>"; //附加數據(描述) 52 formData += "<body>" + body + "</body>";//商品描述 53 formData += "<mch_id>" + mch_id + "</mch_id>";//商戶號 54 formData += "<nonce_str>" + nonce_str + "</nonce_str>";//隨機字元串,不長於32位。 55 formData += "<notify_url>" + notify_url + "</notify_url>";//通知地址 56 formData += "<openid>" + openid + "</openid>";//openid 57 formData += "<out_trade_no>" + bookingNo + "</out_trade_no>";//商戶訂單號 --待 58 formData += "<spbill_create_ip>61.50.221.43</spbill_create_ip>";//終端IP --用戶ip 59 formData += "<total_fee>" + total_fee + "</total_fee>";//支付金額單位為(分) 60 formData += "<trade_type>JSAPI</trade_type>";//交易類型(JSAPI--公眾號支付) 61 formData += "<sign>" + strMD5 + "</sign>"; //簽名 62 formData += "</xml>"; 63 64 65 66 //請求數據 67 var getdata = sendPost(url, formData); 68 69 //獲取xml數據 70 XmlDocument doc = new XmlDocument(); 71 doc.LoadXml(getdata); 72 //xml格式轉json 73 string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc); 74 75 76 77 JObject jo = (JObject)JsonConvert.DeserializeObject(json); 78 string prepay_id = jo["xml"]["prepay_id"]["#cdata-section"].ToString(); 79 80 //時間戳 81 string _time = getTime().ToString(); 82 83 //再次簽名返回數據至小程式 84 string strB = "appId=" + appid + "&nonceStr=" + nonce_str + "&package=prepay_id=" + prepay_id + "&signType=MD5&timeStamp=" + _time + "&key="_key; 85 86 //wx自己寫的一個類 87 wx w = new wx(); 88 w.timeStamp = _time; 89 w.nonceStr = nonce_str; 90 w.package = "prepay_id=" + prepay_id; 91 w.paySign = MD5(strB).ToUpper(); ; 92 w.signType = "MD5"; 93 94 //向小程式返回json數據 95 return JsonConvert.SerializeObject(w); 96 } 97 98 /// <summary> 99 /// 生成隨機串 100 /// </summary> 101 /// <param name="length">字元串長度</param> 102 /// <returns></returns> 103 private static string GetRandomString(int length) 104 { 105 const string key = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; 106 if (length < 1) 107 return string.Empty; 108 109 Random rnd = new Random(); 110 byte[] buffer = new byte[8]; 111 112 ulong bit = 31; 113 ulong result = 0; 114 int index = 0; 115 StringBuilder sb = new StringBuilder((length / 5 + 1) * 5); 116 117 while (sb.Length < length) 118 { 119 rnd.NextBytes(buffer); 120 121 buffer[5] = buffer[6] = buffer[7] = 0x00; 122 result = BitConverter.ToUInt64(buffer, 0); 123 124 while (result > 0 && sb.Length < length) 125 { 126 index = (int)(bit & result); 127 sb.Append(key[index]); 128 result = result >> 5; 129 } 130 } 131 return sb.ToString(); 132 } 133 134 /// <summary> 135 /// 獲取時間戳 136 /// </summary> 137 /// <returns></returns> 138 private static long getTime() 139 { 140 TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1))); 141 long t = (long)cha.TotalSeconds; 142 return t; 143 } 144 145 146 /// <summary> 147 /// MD5簽名方法 148 /// </summary> 149 /// <param name="inputText">加密參數</param> 150 /// <returns></returns> 151 private static string MD5(string inputText) 152 { 153 MD5 md5 = new MD5CryptoServiceProvider(); 154 byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText); 155 byte[] targetData = md5.ComputeHash(fromData); 156 string byte2String = null; 157 158 for (int i = 0; i < targetData.Length; i++) 159 { 160 byte2String += targetData[i].ToString("x2"); 161 } 162 163 return byte2String; 164 } 165 /// <summary> 166 /// HMAC-SHA256簽名方式 167 /// </summary> 168 /// <param name="message"></param> 169 /// <param name="secret"></param> 170 /// <returns></returns> 171 private static string HmacSHA256(string message, string secret) 172 { 173 secret = secret ?? ""; 174 var encoding = new System.Text.UTF8Encoding(); 175 byte[] keyByte = encoding.GetBytes(secret); 176 byte[] messageBytes = encoding.GetBytes(message); 177 using (var hmacsha256 = new HMACSHA256(keyByte)) 178 { 179 byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); 180 return Convert.ToBase64String(hashmessage); 181 } 182 } 183 184 /// <summary> 185 /// wx統一下單請求數據 186 /// </summary> 187 /// <param name="URL">請求地址</param> 188 /// <param name="urlArgs">參數</param> 189 /// <returns></returns> 190 private static string sendPost(string URL, string urlArgs) 191 { 192 193 System.Net.WebClient wCient = new System.Net.WebClient(); 194 wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 195 //byte[] postData = System.Text.Encoding.ASCII.GetBytes(urlArgs); 如果微信簽名中有中文會簽名失敗 196 byte[] postData = System.Text.Encoding.UTF8.GetBytes(urlArgs); 197 byte[] responseData = wCient.UploadData(URL, "POST", postData); 198 199 string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的數據 200 return returnStr; 201 } 202 203 /// <summary> 204 /// 生成訂單號 205 /// </summary> 206 /// <returns></returns> 207 private static string getRandomTime() 208 { 209 Random rd = new Random();//用於生成隨機數 210 string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期 211 string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//帶日期的隨機數 212 return str; 213 } 214 215 } 216 }
使用的是MVC .NET Framework4