C# 微信v3退款

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

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

  

 1 string  resp=string.Empty  
 2 string cert = Server.MapPath("apiclient_cert.p12"); 
 3    string password = mch_id;(商户号)
 4     ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
 5 
 6   //这两句特别注意
 7 
 8   // X509Certificate cer = new X509Certificate(cert, password);//线下发布需要添加的
 9      X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);//线上发        布需要添加
10 
11    HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
12         webrequest.ClientCertificates.Add(cer);
13         webrequest.Method = "post";
14         webrequest.ContentType = "application/x-www-form-urlencoded";
15         webrequest.ContentLength = xml.Length;
16         HttpWebResponse response = null;
17    try
18         {
19             StreamWriter swRequestWriter = new StreamWriter(webrequest.GetRequestStream());
20             swRequestWriter.Write(xml);
21 
22             if (swRequestWriter != null)
23                 swRequestWriter.Close();
24 
25             response = (HttpWebResponse)webrequest.GetResponse();
26             using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
27             {
28                 resp = reader.ReadToEnd();
29             }
30         }
31  catch (Exception exp)
32         {
33             throw exp;
34         }
35         finally
36         {
37             if (response != null)
38                 response.Close();
39         }

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

 

C# 微信v3退款

上一篇:C#开发微信门户及应用(20)-微信企业号的菜单管理


下一篇:微信分享功能详解