var AppId = "小程序appid";
var MchId = "商户号";
var MchKey = "商户密钥";
var zhengshu = "证书";
if (radioButton1.Checked)
{
AppId = "wx83e3fd2cffd8a402";
MchId = "1505386021";
MchKey = "0iCjINWd2CXYUnW3yl524K32Yw2Rt85m";
zhengshu = "D:\\zhengshu\\shuziyu\\apiclient_cert.p12";
}
else if (radioButton2.Checked)
{
AppId = "";
MchId = "";
MchKey = "";
zhengshu = "D:\\zhengshu\\ruien\\apiclient_cert.p12";
}
var dh = this.textBox1.Text;
var je = this.textBox2.Text;
string RefundNotifyUrl = "https://wxxcx001.shuziyu.net/api/wxapp/paynotify";
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var orderAmount = Math.Round(Convert.ToDecimal(je) * 100, 0, MidpointRounding.AwayFromZero); //退款金额
var orderAmount1 = Math.Round(Convert.ToDecimal(je) * 100, 0, MidpointRounding.AwayFromZero);//当前单号的总金额
var refundOrderNo = UniqueKeyHelper.GetUniqueKey(12);
var list = new SortedList<string, object>
{
{"appid", AppId},
{"mch_id", MchId},
{"nonce_str", Guid.NewGuid().ToString("N")},
{"out_trade_no", dh},
{"out_refund_no", refundOrderNo},
{"total_fee", orderAmount1},
{"refund_fee", orderAmount},
{"notify_url", "https://wxxcx001.shuziyu.net/api/wxapp/paynotify"}
};
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair<string, object> item in list)
{
var name = item.Key;
var value = item.Value.ToString();
if (value != "")
{
builder.Append(name + "=" + value + "&");
}
}
var stringSignTemp = builder.ToString().TrimEnd('&') + "&key=" + MchKey;
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] bt = md5.ComputeHash(Encoding.UTF8.GetBytes(stringSignTemp));
var singString = BitConverter.ToString(bt).Replace("-", "").ToUpper();
string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
string xml = @"<xml>
<appid>{0}</appid>
<mch_id>{1}</mch_id>
<nonce_str>{2}</nonce_str>
<out_trade_no>{3}</out_trade_no>
<out_refund_no>{4}</out_refund_no>
<total_fee>{5}</total_fee>
<refund_fee>{6}</refund_fee>
<sign>{7}</sign>
<notify_url>{8}</notify_url>
</xml>";
xml = string.Format(xml, AppId, MchId, list["nonce_str"], dh, refundOrderNo, orderAmount1, orderAmount, singString, RefundNotifyUrl);
var content = new StringContent(xml, Encoding.UTF8, "application/xml");
WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 cert = new X509Certificate2(zhengshu, MchId);
handler.ClientCertificates.Add(cert);
HttpClient client = new HttpClient(handler);
var response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
string msg = response.Content.ReadAsStringAsync().Result;
this.richTextBox1.Text = msg;
}
else
{
MessageBox.Show("退款失败");
}
12-21
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交