支付回調: 獲取HttpRequest的body內容,之前使用Request.Form有時候數據請求不到(可能是跟.net core 版本有關?) 獲取到回調內容,接下來按照支付平臺來處理。 支付寶回調: 將返回的數據分割開來,decode後填裝到字典里後,獲取sign值等相關數據後,剔除sign及 ...
支付回調:
獲取HttpRequest的body內容,之前使用Request.Form有時候數據請求不到(可能是跟.net core 版本有關?)
var s = HttpRequest.Body; int count; var buffer = new byte[1024]; var builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Dispose();
獲取到回調內容,接下來按照支付平臺來處理。
支付寶回調:
var alipayReturnData = builder.ToString().Split('&').ToDictionary(a => a.Split('=')[0], a => System.Net.WebUtility.UrlDecode(a.Split('=')[1]));
將返回的數據分割開來,decode後填裝到字典里後,獲取sign值等相關數據後,剔除sign及signType,將剩餘值排序後調用密鑰驗簽後判斷結果
//request 接收的字元串含有urlencode,這裡需要decode一下 var alipayReturnData = builder.ToString().Split('&').ToDictionary(a => a.Split('=')[0], a => System.Net.WebUtility.UrlDecode(a.Split('=')[1])); //獲取sign var sign = alipayReturnData["sign"]; //去除sign及signtype alipayReturnData.Remove("sign"); alipayReturnData.Remove("sign_type"); //獲取支付寶訂單號及商戶交易訂單號 var tradeNo = alipayReturnData["trade_no"]; var tradeIds = alipayReturnData["out_trade_no"]; var dic = alipayReturnData.ToDictionary(d => d.Key, d => d.Value); var preSign = BuildData.BuildParamStr(dic); //驗簽 var result = GenerateRsaAssist.VerifySign(preSign, AliPayConfig.AliPublicKey, sign, SignType.Rsa2);
排序代碼:
public static string BuildParamStr(Dictionary<string, string> param) { if (param == null || param.Count == 0) { return ""; } var ascDic = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); var sb = new StringBuilder(); foreach (var item in ascDic) { if (!string.IsNullOrEmpty(item.Value)) { sb.Append(item.Key).Append("=").Append(item.Value).Append("&"); } } return sb.ToString().Substring(0, sb.ToString().Length - 1); }View Code
獲取結果後剩下的就交給業務代碼處理了。
微信回調:
創建WxPayData類,並將HttpRequest的body獲取到的stringbuilder內容轉換成xml
var notifyData = new WxPayData(); notifyData.FromXml(builder.ToString());
檢查支付結果中transaction_id是否存在
if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,則立即返回結果給微信支付後臺 var res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付結果中微信訂單號不存在"); return new WeChatNotifyRequest { IsVerify = false, PayNo = "", TradeIds = "", PayTime = "", Sign = "", Content = res.ToXml() }; }
查詢訂單,判斷訂單真實性
var transactionId = notifyData.GetValue("transaction_id").ToString(); var tradeIds = notifyData.GetValue("out_trade_no").ToString(); var payTime = notifyData.GetValue("time_end").ToString(); //查詢訂單,判斷訂單真實性 if (!await QueryOrder(transactionId)) { //若訂單查詢失敗,則立即返回結果給微信支付後臺 var res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "訂單查詢失敗"); return new WeChatNotifyRequest { IsVerify = false, PayNo = "", TradeIds = "", PayTime = "", Sign = "", Content = res.ToXml() }; } //查詢訂單成功 else { var res = new WxPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); return new WeChatNotifyRequest { IsVerify = true, PayNo = transactionId, TradeIds = tradeIds, PayTime = payTime, Sign = "", Content = res.ToXml() }; }
註意的是如果回調成功,要返回值給支付平臺,不然平臺沒有收到結果會一直調用回調內容
支付寶返回:success 或者fail
微信返回xml格式,
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>