支付回调:
获取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);
排序代码:
class="code_img_closed" src="/Upload/Images/2017112415/0015B68B3C38AA5B.gif" alt="">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); }logs_code_collapse">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>