翼度科技»论坛 编程开发 .net 查看内容

微信支付之支付码支付

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
  1. <strong>一、获取微信支付码url</strong>
复制代码
(1)获取微信支付码url主方法
  1.         /// <summary>
  2.         /// 获取微信支付二维码
  3.         /// </summary>
  4.         /// <param name="log">日志</param>
  5.         /// <param name="orderId">订单编号</param>
  6.         /// <returns></returns>
  7.         public static string GetPayUrl(string orderId, decimal totalPrice)
  8.         {
  9.             //errMsg = "";
  10.             //Log4Net.Log4Net.Info(log, "订单号:" + orderId + "发起Native的第二种支付方式");
  11.             WxPayData data = new WxPayData();
  12.             data.SetValue("body", "");//商品描述
  13.             data.SetValue("attach", "");//附加数据
  14.             data.SetValue("out_trade_no", orderId);//随机字符串
  15.             data.SetValue("total_fee", Convert.ToInt32(totalPrice * 100));//总金额
  16.             data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
  17.             data.SetValue("time_expire", DateTime.Now.AddMinutes(30).ToString("yyyyMMddHHmmss"));//交易结束时间,前端二维码有效期半小时
  18.             data.SetValue("goods_tag", "");//商品标记(可根据业务随便填)
  19.             data.SetValue("trade_type", "NATIVE");//交易类型
  20.             data.SetValue("product_id", orderId);//商品ID
  21.             WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
  22.             string url = string.Empty;
  23.             if (result.GetValue("return_code").ToString() == "SUCCESS")
  24.             {
  25.                 if (result.GetValue("result_code").ToString() == "SUCCESS")
  26.                 {
  27.                     url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
  28.                 }
  29.                 else
  30.                 {
  31.                     //errMsg = result.GetValue("err_code_des").ToString();
  32.                 }
  33.             }
  34.             else
  35.             {
  36.                 //errMsg = result.GetValue("return_msg").ToString();
  37.             }
  38.             //Log4Net.Log4Net.Info(log, "订单号:" + orderId + "发起Native的第二种支付方式,生成支付链接:" + url);
  39.             return url;
  40.         }
复制代码
(2)支付辅助类
  1. /**
  2.         *
  3.         * 统一下单
  4.         * @param WxPaydata inputObj 提交给统一下单API的参数
  5.         * @param int timeOut 超时时间
  6.         * @throws WxPayException
  7.         * @return 成功时返回,其他抛异常
  8.         */
  9.         public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)
  10.         {
  11.             string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  12.             //检测必填参数
  13.             if (!inputObj.IsSet("out_trade_no"))
  14.             {
  15.                 throw new Exception("缺少统一支付接口必填参数out_trade_no!");
  16.             }
  17.             else if (!inputObj.IsSet("body"))
  18.             {
  19.                 throw new Exception("缺少统一支付接口必填参数body!");
  20.             }
  21.             else if (!inputObj.IsSet("total_fee"))
  22.             {
  23.                 throw new Exception("缺少统一支付接口必填参数total_fee!");
  24.             }
  25.             else if (!inputObj.IsSet("trade_type"))
  26.             {
  27.                 throw new Exception("缺少统一支付接口必填参数trade_type!");
  28.             }
  29.             //关联参数
  30.             if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
  31.             {
  32.                 throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  33.             }
  34.             if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
  35.             {
  36.                 throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  37.             }
  38.             //异步通知url未设置,则使用配置文件中的url
  39.             if (!inputObj.IsSet("notify_url"))
  40.             {
  41.                 inputObj.SetValue("notify_url", WxPayConfig.GetConfig().GetNotifyUrl());//异步通知url
  42.             }
  43.             inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID());//appID
  44.             inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID());//商户号
  45.             inputObj.SetValue("spbill_create_ip", WxPayConfig.GetConfig().GetIp());//终端ip              
  46.             inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  47.             inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);//签名类型
  48.             //签名
  49.             inputObj.SetValue("sign", inputObj.MakeSign());
  50.             string xml = inputObj.ToXml();
  51.             var start = DateTime.Now;
  52.             //Log4Net.Log4Net.Info(log, "WX UnfiedOrder request : " + xml);
  53.             string response = HttpService.Post(xml, url, false, timeOut);
  54.             //Log4Net.Log4Net.Info(log, "WX UnfiedOrder response : " + response);
  55.             var end = DateTime.Now;
  56.             int timeCost = (int)((end - start).TotalMilliseconds);
  57.             WxPayData result = new WxPayData();
  58.             result.FromXml(response);
  59.             ReportCostTime(url, timeCost, result);//测速上报
  60.             return result;
  61.         }
复制代码
 
  1. public class WxPayData
  2.     {
  3.         public const string SIGN_TYPE_MD5 = "MD5";
  4.         public const string SIGN_TYPE_HMAC_SHA256 = "HMAC-SHA256";
  5.         public WxPayData()
  6.         {
  7.         }
  8.         //采用排序的Dictionary的好处是方便对数据包进行签名,不用再签名之前再做一次排序
  9.         private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();
  10.         /**
  11.         * 设置某个字段的值
  12.         * @param key 字段名
  13.          * @param value 字段值
  14.         */
  15.         public void SetValue(string key, object value)
  16.         {
  17.             m_values[key] = value;
  18.         }
  19.         /**
  20.         * 根据字段名获取某个字段的值
  21.         * @param key 字段名
  22.          * @return key对应的字段值
  23.         */
  24.         public object GetValue(string key)
  25.         {
  26.             object o = null;
  27.             m_values.TryGetValue(key, out o);
  28.             return o;
  29.         }
  30.         /**
  31.          * 判断某个字段是否已设置
  32.          * @param key 字段名
  33.          * @return 若字段key已被设置,则返回true,否则返回false
  34.          */
  35.         public bool IsSet(string key)
  36.         {
  37.             object o = null;
  38.             m_values.TryGetValue(key, out o);
  39.             if (null != o)
  40.                 return true;
  41.             else
  42.                 return false;
  43.         }
  44.         /**
  45.         * @将Dictionary转成xml
  46.         * @return 经转换得到的xml串
  47.         * @throws WxPayException
  48.         **/
  49.         public string ToXml()
  50.         {
  51.             //数据为空时不能转化为xml格式
  52.             if (0 == m_values.Count)
  53.             {
  54.                 throw new Exception("WxPayData数据为空!");
  55.             }
  56.             string xml = "<xml>";
  57.             foreach (KeyValuePair<string, object> pair in m_values)
  58.             {
  59.                 //字段值不能为null,会影响后续流程
  60.                 if (pair.Value == null)
  61.                 {
  62.                     throw new Exception("WxPayData内部含有值为null的字段!");
  63.                 }
  64.                 if (pair.Value.GetType() == typeof(int))
  65.                 {
  66.                     xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
  67.                 }
  68.                 else if (pair.Value.GetType() == typeof(string))
  69.                 {
  70.                     xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
  71.                 }
  72.                 else//除了string和int类型不能含有其他数据类型
  73.                 {
  74.                     throw new Exception("WxPayData字段数据类型错误!");
  75.                 }
  76.             }
  77.             xml += "</xml>";
  78.             return xml;
  79.         }
  80.         /**
  81.         * @将xml转为WxPayData对象并返回对象内部的数据
  82.         * @param string 待转换的xml串
  83.         * @return 经转换得到的Dictionary
  84.         * @throws WxPayException
  85.         */
  86.         public SortedDictionary<string, object> FromXml(string xml)
  87.         {
  88.             if (string.IsNullOrEmpty(xml))
  89.             {
  90.                 throw new Exception("将空的xml串转换为WxPayData不合法!");
  91.             }
  92.             XmlDocument xmlDoc = new XmlDocument();
  93.             xmlDoc.LoadXml(xml);
  94.             XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
  95.             XmlNodeList nodes = xmlNode.ChildNodes;
  96.             foreach (XmlNode xn in nodes)
  97.             {
  98.                 XmlElement xe = (XmlElement)xn;
  99.                 m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
  100.             }
  101.             try
  102.             {
  103.                 //2015-06-29 错误是没有签名
  104.                 if (m_values["return_code"] != "SUCCESS")
  105.                 {
  106.                     return m_values;
  107.                 }
  108.                 CheckSign();//验证签名,不通过会抛异常
  109.             }
  110.             catch (Exception ex)
  111.             {
  112.                 throw new Exception(ex.Message);
  113.             }
  114.             return m_values;
  115.         }
  116.         /**
  117.         * @Dictionary格式转化成url参数格式
  118.         * @ return url格式串, 该串不包含sign字段值
  119.         */
  120.         public string ToUrl()
  121.         {
  122.             string buff = "";
  123.             foreach (KeyValuePair<string, object> pair in m_values)
  124.             {
  125.                 if (pair.Value == null)
  126.                 {
  127.                     throw new Exception("WxPayData内部含有值为null的字段!");
  128.                 }
  129.                 if (pair.Key != "sign" && pair.Value.ToString() != "")
  130.                 {
  131.                     buff += pair.Key + "=" + pair.Value + "&";
  132.                 }
  133.             }
  134.             buff = buff.Trim('&');
  135.             return buff;
  136.         }
  137.         /**
  138.         * @Dictionary格式化成Json
  139.          * @return json串数据
  140.         */
  141.         public string ToJson()
  142.         {
  143.             //string jsonStr = JsonMapper.ToJson(m_values);
  144.             //return jsonStr;
  145.             return "";
  146.         }
  147.         /**
  148.         * @values格式化成能在Web页面上显示的结果(因为web页面上不能直接输出xml格式的字符串)
  149.         */
  150.         public string ToPrintStr()
  151.         {
  152.             string str = "";
  153.             foreach (KeyValuePair<string, object> pair in m_values)
  154.             {
  155.                 if (pair.Value == null)
  156.                 {
  157.                     throw new Exception("WxPayData内部含有值为null的字段!");
  158.                 }
  159.                 str += string.Format("{0}={1}\n", pair.Key, pair.Value.ToString());
  160.             }
  161.             str = HttpUtility.HtmlEncode(str);
  162.             return str;
  163.         }
  164.         /**
  165.         * @生成签名,详见签名生成算法
  166.         * @return 签名, sign字段不参加签名
  167.         */
  168.         public string MakeSign(string signType)
  169.         {
  170.             //转url格式
  171.             string str = ToUrl();
  172.             //在string后加入API KEY
  173.             str += "&key=" + WxPayConfig.GetConfig().GetKey();
  174.             if (signType == SIGN_TYPE_MD5)
  175.             {
  176.                 var md5 = MD5.Create();
  177.                 var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
  178.                 var sb = new StringBuilder();
  179.                 foreach (byte b in bs)
  180.                 {
  181.                     sb.Append(b.ToString("x2"));
  182.                 }
  183.                 //所有字符转为大写
  184.                 return sb.ToString().ToUpper();
  185.             }
  186.             else if (signType == SIGN_TYPE_HMAC_SHA256)
  187.             {
  188.                 return CalcHMACSHA256Hash(str, WxPayConfig.GetConfig().GetKey());
  189.             }
  190.             else
  191.             {
  192.                 throw new Exception("sign_type 不合法");
  193.             }
  194.         }
  195.         /**
  196.         * @生成签名,详见签名生成算法
  197.         * @return 签名, sign字段不参加签名 SHA256
  198.         */
  199.         public string MakeSign()
  200.         {
  201.             return MakeSign(SIGN_TYPE_HMAC_SHA256);
  202.         }
  203.         /**
  204.         *
  205.         * 检测签名是否正确
  206.         * 正确返回true,错误抛异常
  207.         */
  208.         public bool CheckSign(string signType)
  209.         {
  210.             //如果没有设置签名,则跳过检测
  211.             if (!IsSet("sign"))
  212.             {
  213.                 throw new Exception("WxPayData签名存在但不合法!");
  214.             }
  215.             //如果设置了签名但是签名为空,则抛异常
  216.             else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
  217.             {
  218.                 throw new Exception("WxPayData签名存在但不合法!");
  219.             }
  220.             //获取接收到的签名
  221.             string return_sign = GetValue("sign").ToString();
  222.             //在本地计算新的签名
  223.             string cal_sign = MakeSign(signType);
  224.             if (cal_sign == return_sign)
  225.             {
  226.                 return true;
  227.             }
  228.             throw new Exception("WxPayData签名验证错误!");
  229.         }
  230.         /**
  231.         *
  232.         * 检测签名是否正确
  233.         * 正确返回true,错误抛异常
  234.         */
  235.         public bool CheckSign()
  236.         {
  237.             return CheckSign(SIGN_TYPE_HMAC_SHA256);
  238.         }
  239.         /**
  240.         * @获取Dictionary
  241.         */
  242.         public SortedDictionary<string, object> GetValues()
  243.         {
  244.             return m_values;
  245.         }
  246.         private string CalcHMACSHA256Hash(string plaintext, string salt)
  247.         {
  248.             string result = "";
  249.             var enc = Encoding.Default;
  250.             byte[]
  251.             baText2BeHashed = enc.GetBytes(plaintext),
  252.             baSalt = enc.GetBytes(salt);
  253.             System.Security.Cryptography.HMACSHA256 hasher = new HMACSHA256(baSalt);
  254.             byte[] baHashedText = hasher.ComputeHash(baText2BeHashed);
  255.             result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());
  256.             return result;
  257.         }
  258.     }
复制代码
 
  1. /**
  2.         * 生成随机串,随机串包含字母或数字
  3.         * @return 随机串
  4.         */
  5.         public static string GenerateNonceStr()
  6.         {
  7.             RandomGenerator randomGenerator = new RandomGenerator();
  8.             return randomGenerator.GetRandomUInt().ToString();
  9.         }
复制代码
  1. public class RandomGenerator
  2.     {
  3.         readonly RNGCryptoServiceProvider csp;
  4.         public RandomGenerator()
  5.         {
  6.             csp = new RNGCryptoServiceProvider();
  7.         }
  8.         public int Next(int minValue, int maxExclusiveValue)
  9.         {
  10.             if (minValue >= maxExclusiveValue)
  11.                 throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue");
  12.             long diff = (long)maxExclusiveValue - minValue;
  13.             long upperBound = uint.MaxValue / diff * diff;
  14.             uint ui;
  15.             do
  16.             {
  17.                 ui = GetRandomUInt();
  18.             } while (ui >= upperBound);
  19.             return (int)(minValue + (ui % diff));
  20.         }
  21.         public uint GetRandomUInt()
  22.         {
  23.             var randomBytes = GenerateRandomBytes(sizeof(uint));
  24.             return BitConverter.ToUInt32(randomBytes, 0);
  25.         }
  26.         private byte[] GenerateRandomBytes(int bytesNumber)
  27.         {
  28.             byte[] buffer = new byte[bytesNumber];
  29.             csp.GetBytes(buffer);
  30.             return buffer;
  31.         }
  32.     }
复制代码
 二、微信支付结果回调接收
(1)支付回调主接收方法
  1. /// <summary>
  2.         /// 微信支付回调函数
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         [HttpPost]
  6.         [AllowAnonymous]
  7.         public async Task WxPayNotify()
  8.         {
  9.             HttpContext context = _httpContextAccessor.HttpContext;
  10.             try
  11.             {
  12.                 WxPayData notifyData = GetNotifyData(context);
  13.                 Log.Information("GetNotifyData finished");
  14.                 //检查支付结果中transaction_id是否存在
  15.                 if (!notifyData.IsSet("transaction_id"))
  16.                 {
  17.                     //若transaction_id不存在,则立即返回结果给微信支付后台
  18.                     WxPayData res = new WxPayData();
  19.                     res.SetValue("return_code", "FAIL");
  20.                     res.SetValue("return_msg", "支付结果中微信订单号不存在");
  21.                     await context.Response.WriteAsync(res.ToXml());
  22.                 }
  23.                 string transaction_id = notifyData.GetValue("transaction_id").ToString();
  24.                 string out_trade_no = notifyData.GetValue("out_trade_no").ToString();
  25.                 //查询订单,判断订单真实性
  26.                 if (!QueryOrder(transaction_id))
  27.                 {
  28.                     //若订单查询失败,则立即返回结果给微信支付后台
  29.                     WxPayData res = new WxPayData();
  30.                     res.SetValue("return_code", "FAIL");
  31.                     res.SetValue("return_msg", "订单查询失败");
  32.                     await context.Response.WriteAsync(res.ToXml());
  33.                 }
  34.                 //查询订单成功
  35.                 else
  36.                 {
  37.                     //判断订单号和支付金额是否和数据库中一致
  38.                     //修改订单状态,插入支付payment信息
  39.                     string result_code = notifyData.GetValue("result_code").ToString();
  40.                     string openid = notifyData.GetValue("openid").ToString();
  41.                     string trade_type = notifyData.GetValue("trade_type").ToString();
  42.                     string bank_type = notifyData.GetValue("bank_type").ToString();
  43.                     int total_fee = Convert.ToInt32(notifyData.GetValue("total_fee"));
  44.                     int cash_fee = Convert.ToInt32(notifyData.GetValue("cash_fee"));
  45.                     string time_end = notifyData.GetValue("time_end").ToString();
  46.                     Log.Information($"out_trade_no is {out_trade_no}");
  47.                     var orderInfo = await _orderRepository.FindAsync(item => item.OrderNumber.ToString() == out_trade_no);
  48.                     if (orderInfo == null)
  49.                     {
  50.                         //若订单查询失败,则立即返回结果给微信支付后台
  51.                         WxPayData res = new WxPayData();
  52.                         res.SetValue("return_code", "FAIL");
  53.                         res.SetValue("return_msg", "商户订单不存在");
  54.                         await context.Response.WriteAsync(res.ToXml());
  55.                     }
  56.                     Log.Information($"total_fee is {total_fee}");
  57.                     Log.Information($"DiscountPrice*100 is {orderInfo.DiscountPrice * 100}");
  58.                     if (orderInfo.DiscountPrice * 100 != total_fee)//订单支付金额不一致
  59.                     {
  60.                         WxPayData res = new WxPayData();
  61.                         res.SetValue("return_code", "FAIL");
  62.                         res.SetValue("return_msg", "订单支付金额不一致");
  63.                         await context.Response.WriteAsync(res.ToXml());
  64.                     }
  65.                     if (orderInfo.PayStatus == PayStatus.Success && orderInfo.DiscountPrice * 100 == total_fee)//订单已支付
  66.                     {
  67.                         WxPayData res = new WxPayData();
  68.                         res.SetValue("return_code", "SUCCESS");
  69.                         res.SetValue("return_msg", "OK");
  70.                         await context.Response.WriteAsync(res.ToXml());
  71.                     }
  72.                     else
  73.                     {<br>                        //该部分主要是一些自定义逻辑了,可以根据自己的业务需求设置;
  74.                         WXPayResult wPay = new WXPayResult();
  75.                         wPay.OpenId = openid;
  76.                         wPay.TradeState = result_code;
  77.                         wPay.BankType = bank_type;
  78.                         wPay.TotalFee = total_fee;
  79.                         wPay.CashFee = cash_fee;
  80.                         wPay.TransactionId = transaction_id;
  81.                         wPay.TimeEnd = time_end;
  82.                         wPay = await _wXPayResultRepository.InsertAsync(wPay, true);
  83.                         if (null != wPay)
  84.                         {
  85.                             OrderWXPaymentMapping mapping = new OrderWXPaymentMapping();
  86.                             mapping.OrderNumber = orderInfo.OrderNumber;
  87.                             mapping.WXPayResultId = wPay.Id;
  88.                             mapping = await _orderWXPaymentRepository.InsertAsync(mapping, true);
  89.                             if (mapping != null)
  90.                             {
  91.                                 Order orderEntity = null;
  92.                                 if (result_code == "SUCCESS")
  93.                                 {
  94.                                     orderInfo.PayStatus = PayStatus.Success;
  95.                                     orderEntity = await _orderRepository.UpdateAsync(orderInfo, true);//10001支付成功
  96.                                 }
  97.                                 else
  98.                                 {
  99.                                     orderInfo.PayStatus = PayStatus.Fail;
  100.                                     orderEntity = await _orderRepository.UpdateAsync(orderInfo, true); ;//10002支付失败
  101.                                 }
  102.                                 if (orderEntity != null)
  103.                                 {
  104.                                     WxPayData res = new WxPayData();
  105.                                     res.SetValue("return_code", "SUCCESS");
  106.                                     res.SetValue("return_msg", "OK");
  107.                                     await context.Response.WriteAsync(res.ToXml());
  108.                                 }
  109.                                 else
  110.                                 {
  111.                                     //若订单查询失败,则立即返回结果给微信支付后台
  112.                                     WxPayData res = new WxPayData();
  113.                                     res.SetValue("return_code", "FAIL");
  114.                                     res.SetValue("return_msg", "商户订单处理失败");
  115.                                     await context.Response.WriteAsync(res.ToXml());
  116.                                 }
  117.                             }
  118.                             else
  119.                             {
  120.                                 //若订单查询失败,则立即返回结果给微信支付后台
  121.                                 WxPayData res = new WxPayData();
  122.                                 res.SetValue("return_code", "FAIL");
  123.                                 res.SetValue("return_msg", "商户订单处理失败");
  124.                                 await context.Response.WriteAsync(res.ToXml());
  125.                             }
  126.                         }
  127.                         else
  128.                         {
  129.                             //若订单查询失败,则立即返回结果给微信支付后台
  130.                             WxPayData res = new WxPayData();
  131.                             res.SetValue("return_code", "FAIL");
  132.                             res.SetValue("return_msg", "商户订单处理失败");
  133.                             await context.Response.WriteAsync(res.ToXml());
  134.                         }
  135.                     }
  136.                 }
  137.             }
  138.             catch (Exception ex)
  139.             {
  140.                 Log.Information($"WxPayNotify error message is {ex.Message}");
  141.                 //若订单查询失败,则立即返回结果给微信支付后台
  142.                 WxPayData res = new WxPayData();
  143.                 res.SetValue("return_code", "FAIL");
  144.                 res.SetValue("return_msg", "商户订单处理失败");
  145.                 await context.Response.WriteAsync(res.ToXml());
  146.                 var currentInfo = MethodBase.GetCurrentMethod();
  147.                 //Log4Net.Error(log, ex.Message, currentInfo.ReflectedType.FullName + "-" + currentInfo.Name);
  148.             }
  149.         }
复制代码
(2)辅助方法
  1.         /// <summary>
  2.         /// 接收从微信支付后台发送过来的数据并验证签名
  3.         /// </summary>
  4.         /// <returns>微信支付后台返回的数据</returns>
  5.         public WxPayData GetNotifyData(HttpContext context)
  6.         {
  7.             string message = string.Empty;
  8.             context.Request.EnableBuffering();
  9.             context.Request.Body.Seek(0, SeekOrigin.Begin);
  10.             Log.Information(context.Request.Method);
  11.             using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
  12.             {
  13.                 message = reader.ReadToEndAsync().Result;
  14.             }
  15.             Log.Information($"context result is {message}");
  16.             //Log4Net.Info(log, $"GetNotifyData Receive data from WeChat :{message}");
  17.             //转换数据格式并验证签名
  18.             WxPayData data = new WxPayData();
  19.             try
  20.             {
  21.                 data.FromXml(message);
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 //若签名错误,则立即返回结果给微信支付后台
  26.                 WxPayData res = new WxPayData();
  27.                 res.SetValue("return_code", "FAIL");
  28.                 res.SetValue("return_msg", ex.Message);
  29.                 context.Response.WriteAsync(res.ToXml());
  30.             }
  31.             //Log4Net.Info(log, $"GetNotifyData Check sign success");
  32.             return data;
  33.         }
复制代码
 提示:
* APPID:绑定支付的APPID(必须配置)
            * MCHID:商户号(必须配置)
            * KEY:商户支付密钥,参考开户邮件设置(必须配置),请妥善保管,避免密钥泄露
 
出处:https://www.cnblogs.com/cby-love本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利.如果您觉得文章对您有帮助,可以点击文章右下角"推荐".您的鼓励是作者坚持原创和持续写作的最大动力!
来源:https://www.cnblogs.com/cby-love/archive/2023/01/02/17020194.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具