【.net core】微信公众号JSAPI支付--退款成功回调

微信公众号JSAPI支付--退款成功回调

接上篇

Controller

    [HttpPost("TestRefundCallBack")]
    public void TestRefundCallBack()
    {
        _ticketRepository.TestRefundCallBack(HttpContext);
    }

Interface

    Task<(bool result, string msg)> TestRefundCallBack(HttpContext context);

业务仓储

    public async Task<(bool result,string msg)> TestRefundCallBack(HttpContext context)
    {
        _logger.LogWarning($"TestRefundCallBack comming");

        return (true, "success");
    }

帮助方法

    public string RefundCallBack(HttpContext context)
    {
        string out_refund_no = "";

        #region 
        //.net core 3.0以后需加下边这段,否则Stream会报错
        var syncIOFeature = context.Features.Get<IHttpBodyControlFeature>();
        if (syncIOFeature != null)
        {
            syncIOFeature.AllowSynchronousIO = true;
        }
        #endregion

        //接收从微信后台POST过来的数据
        System.IO.Stream s = context.Request.Body;
        int count = 0;
        byte[] buffer = new byte[1024];
        StringBuilder builder = new StringBuilder();
        while ((count = s.Read(buffer, 0, 1024)) > 0)
        {
            builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
        }
        s.Flush();
        s.Close();
        s.Dispose();
        _logger.LogWarning($"GetNotifyData Receive data from WeChat :{builder.ToString()}");

        //转换数据格式并验证签名
        WxPayData notifyData = new WxPayData();
        notifyData.NoSignFromXml(builder.ToString());

        if (notifyData.GetValue("return_code").ToString() == "SUCCESS")
        {
            WxPayData res = new WxPayData();
            res.SetValue("return_code", "SUCCESS");
            res.SetValue("return_msg", "OK");
            context.Response.WriteAsync(res.ToXml());

            string req_info = notifyData.GetValue("req_info").ToString();
            string key = Encrypt.GetStrMD5("xxx").ToLower();     //xxx是微信商户key
            req_info = WxPayData.DecodeAES256ECB(req_info, key);

            WxPayData data = new WxPayData();
            data.NoSignFromXml(req_info);

            if (data.GetValue("refund_status").ToString() == "SUCCESS")
            {
                out_refund_no = data.GetValue("out_refund_no").ToString();
            }
        }
        return out_refund_no;
    }

WxPayData 中的方法

    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;
    }

【.net core】微信公众号JSAPI支付--退款成功回调

上一篇:微信小程序性能优化技巧


下一篇:Android make sdk出错问题的解决