一、獲取微信支付碼url (1)獲取微信支付碼url主方法 /// <summary> /// 獲取微信支付二維碼 /// </summary> /// <param name="log">日誌</param> /// <param name="orderId">訂單編號</param> /// < ...
一、獲取微信支付碼url
(1)獲取微信支付碼url主方法
/// <summary> /// 獲取微信支付二維碼 /// </summary> /// <param name="log">日誌</param> /// <param name="orderId">訂單編號</param> /// <returns></returns> public static string GetPayUrl(string orderId, decimal totalPrice) { //errMsg = ""; //Log4Net.Log4Net.Info(log, "訂單號:" + orderId + "發起Native的第二種支付方式"); WxPayData data = new WxPayData(); data.SetValue("body", "");//商品描述 data.SetValue("attach", "");//附加數據 data.SetValue("out_trade_no", orderId);//隨機字元串 data.SetValue("total_fee", Convert.ToInt32(totalPrice * 100));//總金額 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始時間 data.SetValue("time_expire", DateTime.Now.AddMinutes(30).ToString("yyyyMMddHHmmss"));//交易結束時間,前端二維碼有效期半小時 data.SetValue("goods_tag", "");//商品標記(可根據業務隨便填) data.SetValue("trade_type", "NATIVE");//交易類型 data.SetValue("product_id", orderId);//商品ID WxPayData result = WxPayApi.UnifiedOrder(data);//調用統一下單介面 string url = string.Empty; if (result.GetValue("return_code").ToString() == "SUCCESS") { if (result.GetValue("result_code").ToString() == "SUCCESS") { url = result.GetValue("code_url").ToString();//獲得統一下單介面返回的二維碼鏈接 } else { //errMsg = result.GetValue("err_code_des").ToString(); } } else { //errMsg = result.GetValue("return_msg").ToString(); } //Log4Net.Log4Net.Info(log, "訂單號:" + orderId + "發起Native的第二種支付方式,生成支付鏈接:" + url); return url; }(2)支付輔助類
/** * * 統一下單 * @param WxPaydata inputObj 提交給統一下單API的參數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //檢測必填參數 if (!inputObj.IsSet("out_trade_no")) { throw new Exception("缺少統一支付介面必填參數out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new Exception("缺少統一支付介面必填參數body!"); } else if (!inputObj.IsSet("total_fee")) { throw new Exception("缺少統一支付介面必填參數total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new Exception("缺少統一支付介面必填參數trade_type!"); } //關聯參數 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new Exception("統一支付介面中,缺少必填參數openid!trade_type為JSAPI時,openid為必填參數!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new Exception("統一支付介面中,缺少必填參數product_id!trade_type為JSAPI時,product_id為必填參數!"); } //非同步通知url未設置,則使用配置文件中的url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WxPayConfig.GetConfig().GetNotifyUrl());//非同步通知url } inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID());//appID inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID());//商戶號 inputObj.SetValue("spbill_create_ip", WxPayConfig.GetConfig().GetIp());//終端ip inputObj.SetValue("nonce_str", GenerateNonceStr());//隨機字元串 inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);//簽名類型 //簽名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; //Log4Net.Log4Net.Info(log, "WX UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); //Log4Net.Log4Net.Info(log, "WX UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; }
public class WxPayData { public const string SIGN_TYPE_MD5 = "MD5"; public const string SIGN_TYPE_HMAC_SHA256 = "HMAC-SHA256"; public WxPayData() { } //採用排序的Dictionary的好處是方便對數據包進行簽名,不用再簽名之前再做一次排序 private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>(); /** * 設置某個欄位的值 * @param key 欄位名 * @param value 欄位值 */ public void SetValue(string key, object value) { m_values[key] = value; } /** * 根據欄位名獲取某個欄位的值 * @param key 欄位名 * @return key對應的欄位值 */ public object GetValue(string key) { object o = null; m_values.TryGetValue(key, out o); return o; } /** * 判斷某個欄位是否已設置 * @param key 欄位名 * @return 若欄位key已被設置,則返回true,否則返回false */ public bool IsSet(string key) { object o = null; m_values.TryGetValue(key, out o); if (null != o) return true; else return false; } /** * @將Dictionary轉成xml * @return 經轉換得到的xml串 * @throws WxPayException **/ public string ToXml() { //數據為空時不能轉化為xml格式 if (0 == m_values.Count) { throw new Exception("WxPayData數據為空!"); } string xml = "<xml>"; foreach (KeyValuePair<string, object> pair in m_values) { //欄位值不能為null,會影響後續流程 if (pair.Value == null) { throw new Exception("WxPayData內部含有值為null的欄位!"); } if (pair.Value.GetType() == typeof(int)) { xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">"; } else if (pair.Value.GetType() == typeof(string)) { xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">"; } else//除了string和int類型不能含有其他數據類型 { throw new Exception("WxPayData欄位數據類型錯誤!"); } } xml += "</xml>"; return xml; } /** * @將xml轉為WxPayData對象並返回對象內部的數據 * @param string 待轉換的xml串 * @return 經轉換得到的Dictionary * @throws WxPayException */ public SortedDictionary<string, object> FromXml(string xml) { if (string.IsNullOrEmpty(xml)) { throw new Exception("將空的xml串轉換為WxPayData不合法!"); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.FirstChild;//獲取到根節點<xml> XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; m_values[xe.Name] = xe.InnerText;//獲取xml的鍵值對到WxPayData內部的數據中 } try { //2015-06-29 錯誤是沒有簽名 if (m_values["return_code"] != "SUCCESS") { return m_values; } CheckSign();//驗證簽名,不通過會拋異常 } catch (Exception ex) { throw new Exception(ex.Message); } return m_values; } /** * @Dictionary格式轉化成url參數格式 * @ return url格式串, 該串不包含sign欄位值 */ public string ToUrl() { string buff = ""; foreach (KeyValuePair<string, object> pair in m_values) { if (pair.Value == null) { throw new Exception("WxPayData內部含有值為null的欄位!"); } if (pair.Key != "sign" && pair.Value.ToString() != "") { buff += pair.Key + "=" + pair.Value + "&"; } } buff = buff.Trim('&'); return buff; } /** * @Dictionary格式化成Json * @return json串數據 */ public string ToJson() { //string jsonStr = JsonMapper.ToJson(m_values); //return jsonStr; return ""; } /** * @values格式化成能在Web頁面上顯示的結果(因為web頁面上不能直接輸出xml格式的字元串) */ public string ToPrintStr() { string str = ""; foreach (KeyValuePair<string, object> pair in m_values) { if (pair.Value == null) { throw new Exception("WxPayData內部含有值為null的欄位!"); } str += string.Format("{0}={1}\n", pair.Key, pair.Value.ToString()); } str = HttpUtility.HtmlEncode(str); return str; } /** * @生成簽名,詳見簽名生成演算法 * @return 簽名, sign欄位不參加簽名 */ public string MakeSign(string signType) { //轉url格式 string str = ToUrl(); //在string後加入API KEY str += "&key=" + WxPayConfig.GetConfig().GetKey(); if (signType == SIGN_TYPE_MD5) { var md5 = MD5.Create(); var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(b.ToString("x2")); } //所有字元轉為大寫 return sb.ToString().ToUpper(); } else if (signType == SIGN_TYPE_HMAC_SHA256) { return CalcHMACSHA256Hash(str, WxPayConfig.GetConfig().GetKey()); } else { throw new Exception("sign_type 不合法"); } } /** * @生成簽名,詳見簽名生成演算法 * @return 簽名, sign欄位不參加簽名 SHA256 */ public string MakeSign() { return MakeSign(SIGN_TYPE_HMAC_SHA256); } /** * * 檢測簽名是否正確 * 正確返回true,錯誤拋異常 */ public bool CheckSign(string signType) { //如果沒有設置簽名,則跳過檢測 if (!IsSet("sign")) { throw new Exception("WxPayData簽名存在但不合法!"); } //如果設置了簽名但是簽名為空,則拋異常 else if (GetValue("sign") == null || GetValue("sign").ToString() == "") { throw new Exception("WxPayData簽名存在但不合法!"); } //獲取接收到的簽名 string return_sign = GetValue("sign").ToString(); //在本地計算新的簽名 string cal_sign = MakeSign(signType); if (cal_sign == return_sign) { return true; } throw new Exception("WxPayData簽名驗證錯誤!"); } /** * * 檢測簽名是否正確 * 正確返回true,錯誤拋異常 */ public bool CheckSign() { return CheckSign(SIGN_TYPE_HMAC_SHA256); } /** * @獲取Dictionary */ public SortedDictionary<string, object> GetValues() { return m_values; } private string CalcHMACSHA256Hash(string plaintext, string salt) { string result = ""; var enc = Encoding.Default; byte[] baText2BeHashed = enc.GetBytes(plaintext), baSalt = enc.GetBytes(salt); System.Security.Cryptography.HMACSHA256 hasher = new HMACSHA256(baSalt); byte[] baHashedText = hasher.ComputeHash(baText2BeHashed); result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray()); return result; } }
/** * 生成隨機串,隨機串包含字母或數字 * @return 隨機串 */ public static string GenerateNonceStr() { RandomGenerator randomGenerator = new RandomGenerator(); return randomGenerator.GetRandomUInt().ToString(); }
public class RandomGenerator { readonly RNGCryptoServiceProvider csp; public RandomGenerator() { csp = new RNGCryptoServiceProvider(); } public int Next(int minValue, int maxExclusiveValue) { if (minValue >= maxExclusiveValue) throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue"); long diff = (long)maxExclusiveValue - minValue; long upperBound = uint.MaxValue / diff * diff; uint ui; do { ui = GetRandomUInt(); } while (ui >= upperBound); return (int)(minValue + (ui % diff)); } public uint GetRandomUInt() { var randomBytes = GenerateRandomBytes(sizeof(uint)); return BitConverter.ToUInt32(randomBytes, 0); } private byte[] GenerateRandomBytes(int bytesNumber) { byte[] buffer = new byte[bytesNumber]; csp.GetBytes(buffer); return buffer; } }
二、微信支付結果回調接收
(1)支付回調主接收方法
/// <summary> /// 微信支付回調函數 /// </summary> /// <returns></returns> [HttpPost] [AllowAnonymous] public async Task WxPayNotify() { HttpContext context = _httpContextAccessor.HttpContext; try { WxPayData notifyData = GetNotifyData(context); Log.Information("GetNotifyData finished"); //檢查支付結果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,則立即返回結果給微信支付後臺 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付結果中微信訂單號不存在"); await context.Response.WriteAsync(res.ToXml()); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); string out_trade_no = notifyData.GetValue("out_trade_no").ToString(); //查詢訂單,判斷訂單真實性 if (!QueryOrder(transaction_id)) { //若訂單查詢失敗,則立即返回結果給微信支付後臺 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "訂單查詢失敗"); await context.Response.WriteAsync(res.ToXml()); } //查詢訂單成功 else { //判斷訂單號和支付金額是否和資料庫中一致 //修改訂單狀態,插入支付payment信息 string result_code = notifyData.GetValue("result_code").ToString(); string openid = notifyData.GetValue("openid").ToString(); string trade_type = notifyData.GetValue("trade_type").ToString(); string bank_type = notifyData.GetValue("bank_type").ToString(); int total_fee = Convert.ToInt32(notifyData.GetValue("total_fee")); int cash_fee = Convert.ToInt32(notifyData.GetValue("cash_fee")); string time_end = notifyData.GetValue("time_end").ToString(); Log.Information($"out_trade_no is {out_trade_no}"); var orderInfo = await _orderRepository.FindAsync(item => item.OrderNumber.ToString() == out_trade_no); if (orderInfo == null) { //若訂單查詢失敗,則立即返回結果給微信支付後臺 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "商戶訂單不存在"); await context.Response.WriteAsync(res.ToXml()); } Log.Information($"total_fee is {total_fee}"); Log.Information($"DiscountPrice*100 is {orderInfo.DiscountPrice * 100}"