支付通知APIhtml
netcore 中沒有Request.InputStreamjson
對於我來講,這個微信支付結果通知回調有兩個難點。api
一開始在想是怎麼在.NET Core 下接受微信支付回調傳遞給個人數據,從參考資料中獲得的解決方案就解決了這個難點。微信
如何驗證簽名。在我寫代碼的時候忽然想到我爲啥要驗證簽名,我直接解密微信支付回調的數據獲得訂單號,而後直接調用訂單查詢接口就能夠了,這樣就解決了難點二。app
/// <summary> /// 微信支付結果回調通知實體 /// </summary> public class WxPayNotifyModel { /// <summary> /// 通知的惟一ID /// </summary> public string id { set; get; } /// <summary> /// 通知建立時間,格式爲YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出如今字符串中,表示time元素的開頭,HH:mm:ss.表示時分秒,TIMEZONE表示時區(+08:00表示東八區時間,領先UTC 8小時,即北京時間)。例如:2015-05-20T13:29:35+08:00表示北京時間2015年05月20日13點29分35秒。 /// </summary> public string create_time { set; get; } /// <summary> /// 通知的類型,支付成功通知的類型爲TRANSACTION.SUCCESS /// </summary> public string event_type { set; get; } /// <summary> /// 通知的資源數據類型,支付成功通知爲encrypt-resource /// </summary> public string resource_type { set; get; } /// <summary> /// 通知資源數據,json格式 /// </summary> public WxPayResourceModel resource { set; get; } /// <summary> /// 回調摘要 /// </summary> public string summary { set; get; } } /// <summary> /// 微信支付回調通知結果resource實體 /// </summary> public class WxPayResourceModel { /// <summary> /// 對開啓結果數據進行加密的加密算法,目前只支持AEAD_AES_256_GCM /// </summary> public string algorithm { set; get; } /// <summary> /// Base64編碼後的開啓/停用結果數據密文 /// </summary> public string ciphertext { set; get; } /// <summary> /// 附加數據 /// </summary> public string associated_data { set; get; } /// <summary> /// 原始回調類型,爲transaction /// </summary> public string original_type { set; get; } /// <summary> /// 加密使用的隨機串 /// </summary> public string nonce { set; get; } } /// <summary> /// 微信支付回調通知結果解密實體 /// </summary> public class WxPayResourceDecryptModel { /// <summary> /// 直連商戶申請的公衆號或移動應用appid /// </summary> public string appid { set; get; } /// <summary> /// 商戶的商戶號,由微信支付生成並下發。 /// </summary> public string mchid { set; get; } /// <summary> /// 商戶系統內部訂單號,只能是數字、大小寫字母_-*且在同一個商戶號下惟一。特殊規則:最小字符長度爲6 /// </summary> public string out_trade_no { set; get; } /// <summary> /// 微信支付系統生成的訂單號。 /// </summary> public string transaction_id { set; get; } /// <summary> /// 交易狀態,枚舉值: /// SUCCESS:支付成功 /// REFUND:轉入退款 /// NOTPAY:未支付 /// CLOSED:已關閉 /// REVOKED:已撤銷(付款碼支付) /// USERPAYING:用戶支付中(付款碼支付) /// PAYERROR:支付失敗(其餘緣由,如銀行返回失敗) /// ACCEPT:已接收,等待扣款 /// </summary> public string trade_state { get; set; } /// <summary> /// 交易狀態描述 /// </summary> public string trade_state_desc { get; set; } /// <summary> /// 支付者信息 /// </summary> public WxPayerResourceDecryptModel payer { set; get; } } /// <summary> /// 支付用戶信息實體 /// </summary> public class WxPayerResourceDecryptModel { /// <summary> /// 用戶在直連商戶appid下的惟一標識。 /// </summary> public string openid { get; set; } }
public class WxPayCallbackViewModel { /// <summary> /// 返回狀態碼,錯誤碼,SUCCESS爲清算機構接收成功,其餘錯誤碼爲失敗。 /// </summary> public string code { set; get; } = "SUCCESS"; /// <summary> /// 返回信息,如非空,爲錯誤緣由。 /// </summary> public string message { set; get; } = string.Empty; }
public class AesGcm { private static string ALGORITHM = "AES/GCM/NoPadding"; private static int TAG_LENGTH_BIT = 128; private static int NONCE_LENGTH_BYTE = 12; private static string AES_KEY = "yourkeyhere";//換成你的API V3密鑰 public static string AesGcmDecrypt(string associatedData, string nonce, string ciphertext) { GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine()); AeadParameters aeadParameters = new AeadParameters( new KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)), 128, Encoding.UTF8.GetBytes(nonce), Encoding.UTF8.GetBytes(associatedData)); gcmBlockCipher.Init(false, aeadParameters); byte[] data = Convert.FromBase64String(ciphertext); byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)]; int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0); gcmBlockCipher.DoFinal(plaintext, length); return Encoding.UTF8.GetString(plaintext); } }
public async Task<WxPayCallbackViewModel> WxPayCallback() { var buffer = new MemoryStream(); Request.Body.CopyTo(buffer); var str = Encoding.UTF8.GetString(buffer.GetBuffer()); var wxPayNotifyModel = str.ToObject<WxPayNotifyModel>(); var resource = wxPayNotifyModel?.resource ?? new WxPayResourceModel(); var decryptStr = AesGcm.AesGcmDecrypt(resource.associated_data, resource.nonce, resource.ciphertext); var decryptModel = decryptStr.ToObject<WxPayResourceDecryptModel>(); var viewModel = new WxPayCallbackViewModel(); if (decryptModel.IsNotNull()) { var url = $"https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{decryptModel.out_trade_no}?mchid={WxPayConst.mchid}"; var client = new HttpClient(new WxPayHelper()); var resp = await client.GetAsync(url); var respStr = await resp.Content.ReadAsStringAsync(); var payModel = respStr.ToObject<WxPayStatusRespModel>(); return viewModel; } viewModel.code = "FAIL"; viewModel.message = "數據解密失敗"; return new WxPayCallbackViewModel(); }
.NET 版獲取回調數據流,除了獲取數據流的代碼這塊有區別,其餘的代碼.NET 與.NET Core一致,能夠互通使用。 async
System.IO.Stream s = HttpContext.Current.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); var str = builder.ToString();