首页 > 代码库 > C# 微支付退款查询接口 V3.3.6

C# 微支付退款查询接口 V3.3.6

#region 微支付退款查询

string Nonce = CreateRandomCode(15).ToLower(); //生成15个随机字符
string sign1 = "appid=" + PayHandle.AppID.ToString() + //微信公众号的APPID
"&mch_id=" + PayHandle.MechID.ToString() + //商户号
"&nonce_str=" + Nonce + //随机字符串
"&out_trade_no=" + row["WXO_OCode"].ToString(); //商户订单号
string sign2 = sign1 + "&key=" + PayHandle.PayKey.ToString(); //商户key
string sign = T9.Util.EncryptUtil.md5(sign2, 32).ToUpper(); //MD5加密
//调用退款查询接口
string URL = "https://api.mch.weixin.qq.com/pay/refundquery";
string RequstContext = "<xml>" +
"<appid><![CDATA[" + PayHandle.AppID.ToString() + "]]></appid>" +
"<mch_id><![CDATA[" + PayHandle.MechID.ToString() + "]]></mch_id>" +
"<nonce_str><![CDATA[" + Nonce + "]]></nonce_str>" +
"<sign><![CDATA[" + sign + "]]></sign>" +
"<out_trade_no><![CDATA[" + row["WXO_OCode"].ToString() + "]]></out_trade_no>" +
"</xml>";
string refundquery = SendInfo(URL, RequstContext); //调用向服务器发送请求的方法

XmlDocument doc = new XmlDocument();
doc.LoadXml(refundquery);
XmlElement rootElement = doc.DocumentElement;
string Code = rootElement.SelectSingleNode("return_code").InnerText; //协议级判断
if (Code == "SUCCESS") //协议级验证
{
string result_code = rootElement.SelectSingleNode("result_code").InnerText;
if (result_code == "SUCCESS")//业务级验证
{
int count = Convert.ToInt32(rootElement.SelectSingleNode("refund_count").InnerText);//退款记录数
if (count >= 2)
{
//数据处理
}
else
{
//数据处理
}
}
else
{
this.ClientScript.RegisterStartupScript(GetType(), "", "<script>ShowFW(‘出现未知的错误,请联系管理员!‘);</script>");
}
}
else
{
this.ClientScript.RegisterStartupScript(GetType(), "", "<script>ShowFW(‘出现未知的错误,请联系管理员!‘);</script>");
}
#endregion
#region 生成随机字符串
/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="codeCount"></param>
/// <param name="allChar"></param>
/// <returns></returns>
public static string CreateRandomCode(int codeCount)
{
//验证码中的出现的字符,避免了一些容易混淆的字符。
string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
string[] allCharArray = allChar.Split(‘,‘);
string randomCode = "";
int temp = -1;
bool breCreate = (codeCount < 6 && allCharArray.Length > 15);

Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Length);
if (temp == t && breCreate)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
#endregion
#region 向微信服务器发送信息
/// <summary>
/// 向微信服务器发送信息
/// </summary>
/// <param name="posturl"></param>
/// <param name="postData"></param>
/// <returns></returns>
public string SendInfo(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = http://www.mamicode.com/encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
T9.Util.LogUtil.WriteLog(ex.Message + "\r\n" + ex.StackTrace, "WebLog");
return string.Empty;
}
}
#endregion

C# 微支付退款查询接口 V3.3.6