C#解密退款req_info结果通知

微信支付退款结果通知API地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_16&index=10

解密方式

解密步骤如下:

(1)对加密串A做base64解码,得到加密串B

(2)对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 )

(3)用key*对加密串B做AES-256-ECB解密

  返回的XML字符格式如下,使用上面的方法解密有个大坑,就是返回的已经是base64解码的字符串,不需要再base解码,要不用AES256解码会报错,你大爷的。。

<xml>
<return_code>SUCCESS</return_code>
<appid><![CDATA[....]]></appid>
<mch_id><![CDATA[.....]]></mch_id>
<nonce_str><![CDATA[.....]]></nonce_str>
<req_info><![CDATA[base64字符串]]></req_info></xml

C#解密退款req_info结果通知源代码如下

using System;
using System.Data;
using System.Web.UI;
using System.Web;
using System.Text;
using System.Security.Cryptography;  
namespace WxPayAPI
{
    /// <summary>
    /// 退款结果通知回调处理类
    /// </summary>
    public class RefundNotify : Notify
    {
        /// <summary>
        /// AES-256-ECB字符解密
        /// </summary>
        /// <param name="s">要解密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string DecodeAES256ECB(string s, string key)
        {
            string r = null;
            try
            {
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(s);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                r = UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch { }
            return r;
        }
        /// <summary>
        /// 解密微信支付退款结果通知
        /// </summary>
        /// <param name="s">要解密字符串</param>
        /// <returns></returns>
        public static string DecodeReqInfo(string s)
        {
            string r = null;
            string key = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(WxPayConfig.KEY, "md5").ToLower();
            r = DecodeAES256ECB(s, key);
            return r;
        }
        /// <summary>
        /// 微信通知数据
        /// </summary>
        private WxPayData notifyData;
        /// <summary>
        /// 查询订单信息
        /// </summary>
        private WxPayData orderData;
        public RefundNotify(Page page) : base(page) { }
        public RefundNotify(HttpContext context) : base(context) { }
        /// <summary>
        /// 处理微信通知
        /// </summary>
        public override void ProcessNotify()
        {
            string cType = this.GetType().ToString();
            notifyData = GetNotifyData();
            if (!notifyData.IsSet("return_code") || notifyData.GetValue("return_code").ToString() != "SUCCESS" || !notifyData.IsSet("req_info"))
            {
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "未返回正确微信支付退款解密信息");
                HttpResponse Response = HttpContext.Current.Response;
                Response.Charset = "utf-8";
                Response.ContentEncoding = Encoding.UTF8;
                Response.Write(res.ToXml());
            }
            //信息解密
            string req_info = DecodeReqInfo(notifyData.GetValue("notifyData").ToString());
            if (req_info == null)
            {
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "解密信息出错");
                HttpResponse Response = HttpContext.Current.Response;
                Response.Charset = "utf-8";
                Response.ContentEncoding = Encoding.UTF8;
                Response.Write(res.ToXml());
                Response.End();
            }
            //req_info为xml字符串,注意不能用WxPayData实例的FromXml方法,因为会验证签名,req_info不包含签名,自己重载一个FromXml方法,不需要验证签名
            //...其他逻辑
        }
    }
}

 


原创文章,转载请注明出处:C#解密退款req_info结果通知

评论(0)Web开发网
阅读(753)喜欢(2)Asp.Net/C#