隐藏

C# 微信v3退款

发布:2020/4/26 17:32:51作者:管理员 来源:本站 浏览次数:1226

1.退款需要退款证书。C#都是用p12的证书,双击证书导入,密码是mch_id(商户号)

2.调用微信退款接口进行退款操作


string  resp=string.Empty  
string cert = Server.MapPath("apiclient_cert.p12");
   string password = mch_id;(商户号)
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

  //这两句特别注意

  // X509Certificate cer = new X509Certificate(cert, password);//线下发布需要添加的
     X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);//线上发        布需要添加

   HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
        webrequest.ClientCertificates.Add(cer);
        webrequest.Method = "post";
        webrequest.ContentType = "application/x-www-form-urlencoded";
        webrequest.ContentLength = xml.Length;
        HttpWebResponse response = null;
   try
        {
            StreamWriter swRequestWriter = new StreamWriter(webrequest.GetRequestStream());
            swRequestWriter.Write(xml);

            if (swRequestWriter != null)
                swRequestWriter.Close();

            response = (HttpWebResponse)webrequest.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
            {
                resp = reader.ReadToEnd();
            }
        }
 catch (Exception exp)
        {
            throw exp;
        }
        finally
        {
            if (response != null)
                response.Close();
        }

分享几个退款时遇到的问题
1、退款提示:订单号非法
  原因:退款的订单号错误导致的
2、退款时提示:基础连接已关闭,或被意外关闭(验证证书的问题)
  原因: 首先要通过证书请求退款的接口,其次要把下面的这行语句
 // X509Certificate cer = new X509Certificate(cert, password);//线下发布需要添加的
 修改成这样的才可以
 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);//线上发        布需要添加
3、 Miss Parameters  
  原因:传参数时,参数的名字和文档大小写不一致
4、每次退款,退款的订单号不能相同,否则无法退款