/// <summary>
/// 微支付退款申请
/// </summary>
/// <param name="context"></param>
/// <param name="returnMsg"></param>
/// <returns></returns>
public bool Refund(HttpContext context, ref string returnMsg)
{
int UID = 0;
string WXO_Remark = "";//退款说明
if (string.IsNullOrEmpty(context.Session["UID"] as string))
{
returnMsg = "请登录!";
return false;
}
UID = Convert.ToInt32(context.Session["UID"]); //当前用户

int Oid = Convert.ToInt32(context.Request["OID"].Trim()); //订单ID
model = modelBll.GetModel(Oid); //订单实体
Decimal refund;
if (context.Request["Refund"].Length > 0)
{
bool state = Decimal.TryParse(context.Request["Refund"].Trim(), out refund);
if(!state)
{
returnMsg = "请输入正确的退款金额!";
return false;
}
}
else
{
returnMsg = "请输入退款金额!";
return false;
}
if(refund > model.WXO_PRealPrice)
{
returnMsg = "输入错误,退款金额不得大于总金额!";
return false;
}

string Nonce = CreateRandomCode(15).ToLower(); //生成15个随机字符
string sign1 = "appid=" + CommonApi.AppID.ToString()+ //微信公众号的APPID
"&mch_id=" + CommonApi.MechID.ToString() + //商户号
"&nonce_str=" + Nonce + //随机字符串
"&op_user_id=" + CommonApi.MechID.ToString() + //用户(默认商户号)
"&out_refund_no=" + model.WXO_ORefundCode + //商户退款单号
"&out_trade_no=" + model.WXO_OCode + //商户单号
"&refund_fee=" + Convert.ToInt32(( refund*100 )) + //退款金额
"&total_fee=" + Convert.ToInt32((model.WXO_PRealPrice * 100)); //订单总金额
string sign2 = sign1 + "&key="+ CommonApi.PayKey.ToString(); //商户的Key
string sign = T9.Util.EncryptUtil.md5(sign2, 32).ToUpper(); //MD5加密

string URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
string RequstContext = "<xml>" +
"<appid><![CDATA["+CommonApi.AppID.ToString()+"]]></appid>" +
"<mch_id><![CDATA["+CommonApi.MechID.ToString()+"]]></mch_id>" +
"<nonce_str><![CDATA["+Nonce+"]]></nonce_str>" +
"<sign><![CDATA["+sign+"]]></sign>" +
"<out_trade_no><![CDATA["+model.WXO_OCode+"]]></out_trade_no>" +
"<out_refund_no><![CDATA["+model.WXO_ORefundCode+"]]></out_refund_no>" +
"<total_fee>"+ Convert.ToInt32((model.WXO_PRealPrice * 100 )) +"</total_fee>" +
"<refund_fee>"+ Convert.ToInt32((refund * 100 )) +"</refund_fee>" +
"<op_user_id><![CDATA["+CommonApi.MechID.ToString()+"]]></op_user_id>" +
"</xml>";
string WXRefund = SendInfoWithCa(URL, RequstContext); //调用发送方法

XmlDocument doc = new XmlDocument();
doc.LoadXml(WXRefund);
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")//业务级验证
{
return true;
}
else
{
returnMsg = "出现未知的错误,请联系管理员!";
return false;
}
}
else
{
returnMsg = "出现未知的错误,请联系管理员!";
return false;
}
}
/// <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;
}
#region 向微信服务器发送信息通过证书认证
/// <summary>
/// 向微信服务器发送信息通过证书认证
/// </summary>
/// <param name="posturl">请求路径</param>
/// <param name="postData">请求参数</param>
/// <returns>返回信息</returns>
public string SendInfoWithCa(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 = encoding.GetBytes(postData);
// 准备请求...
try
{
string cert = @"D:\***\**\apiclient_cert.p12"; //证书路径(此证书必须安装)
string password = "***"; //证书密码
ServicePointManager.ServerCertificateValidationCallback=new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert, password);
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
request.ClientCertificates.Add(cer);
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
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();
//发送请求并获取相应回应数据
request.KeepAlive = false;
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;
}
}
#region CheckValidationResult的定义
/// <summary>
/// CheckValidationResult的定义
/// </summary>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="chain"></param>
/// <param name="errors"></param>
/// <returns></returns>
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
if (errors == SslPolicyErrors.None)
{
return true;
}
return false;
}

05-11 23:01