asp.net C# 微信小程序退款通知代码的坑

设置完回调后
req_info解密代码C#官方没提供!

string req_info = DecodeReqInfo(notifyData.GetValue(“req_info”).ToString());
if (req_info == null)
{
res.SetValue(“return_code”, “FAIL”);
res.SetValue(“return_msg”, “解密信息出错”);
LoginRecordDAL.Instance.AddLoginRecord(new loginRecord { msg = “解密信息出错!!”, userId = Guid.NewGuid() });
}
else
{ res.FromXml(req_info.ToString());

                    string transaction_id = res.GetValue("transaction_id").ToString();
                    decimal? refund_fee = Convert.ToDecimal(res.GetValue("refund_fee").ToString());
                    DateTime? refundtime = Convert.ToDateTime(res.GetValue("success_time").ToString());
                    string out_refund_no = res.GetValue("out_refund_no").ToString();
                    string refund_id = res.GetValue("refund_id").ToString();
                    string refund_statusStr = res.GetValue("refund_status").ToString();
                    short? refund_status = 0;
                    if (refund_statusStr == "SUCCESS")
                    {
                        refund_status = 1;
                    }
                    if (refund_statusStr == "CHANGE")
                    {
                        refund_status = 2;
                    }
                    if (refund_statusStr == "REFUNDCLOSE")
                    {
                        refund_status = 3;
                    }
                    int rowCount = OrdersDAL.Instance.UpdateRefund(new orders { transaction_id = transaction_id, refund_fee = refund_fee, refundtime = refundtime, out_refund_no = out_refund_no, refund_id = refund_id, refund_status = refund_status });
                    LoginRecordDAL.Instance.AddLoginRecord(new loginRecord { msg = "rowCount" + rowCount, userId = Guid.NewGuid() });

                    if (rowCount > 0)
                    {
                        LoginRecordDAL.Instance.AddLoginRecord(new loginRecord { msg = "退款成功!!", userId = Guid.NewGuid() });
                        res.SetValue("return_code", "SUCCESS");
                        res.SetValue("return_msg", "OK");
                        Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());

                    }
                    else
                    {

                        res.SetValue("return_code", "FAIL");
                        res.SetValue("return_msg", "订单状态更新失败");
                        Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());

                    }



                }

///
/// 解密微信支付退款结果通知
///
/// 要解密字符串
///
public static string DecodeReqInfo(string s)
{

        string r = null;
        string key = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(WxPayConfig.GetConfig().GetKey(), "md5").ToLower();
        r = DecodeAES256ECB(s, key);
        return r;
    }
    /// <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 (Exception e)
        {
            LoginRecordDAL.Instance.AddLoginRecord(new loginRecord { msg = "DecodeAES256ECB=" + e.Message, userId = Guid.NewGuid() });

        }
      
        return r;
    } 
  这个回调本机调式不了只有服务器上调式 验证的问题 每次发布测试发布测试 我之前犯了一个严重的问题。 res.FromXml(req_info.ToString());   这个FromXml源码中没有我截图选中这一行 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190524091031419.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dxczE1MTkyMDk1NjMz,size_16,color_FFFFFF,t_70) 可能之前设计的是必须return_code返回吧,但是我们解密的没有这个字段名,(不让我们用,不知道是不是坑)再就是C#的demo好像2.0的。源码中之前 异常之前没有  throw new WxPayException(ex.Message); 调式半天。很抑郁。特此记录一下。。
发布了55 篇原创文章 · 获赞 2 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wqs15192095633/article/details/90510466