微信jsapi支付 退款介面

zuiaixiaoyadan發表於2024-11-03

微信官方退款介面文件地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4

引數按照文件構造 記錄出現錯誤:System.Net.WebException: The remote server returned an error: (400) Bad Request

這個是由於退款介面需要傳證書

證書的話是需要登入商戶號進行下載api證書 下載好匯入到伺服器中 呼叫支付介面的時候新增證書即可

public  string PostWebRequest(string postUrl, string paramData)
    {
        string ret = string.Empty;
        try
        {
       byte[] byteArray = Encoding.Default.GetBytes(paramData); //轉化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
       //初始化證書 fileName:證書存放的地址 password 證書的密碼
            X509Certificate2 certificate = new X509Certificate2(fileName,password); 
       //新增證書
            webReq.ClientCertificates.Add(certificate);  


webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); // newStream.Write(byteArray, 0, byteArray.Length);//寫入引數 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); //這裡報錯 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); newStream.Close(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { return ex.Message; } return ret; }

  

相關文章