使用Senparc.Weixin.TenPay
string appid= appid;
string openid = "微信用户的openid";
string mrch_id = "商户ID";
string mrchKey ="商户密钥";
string nonce_str = TenPayUtil.GetNoncestr();//随机码
string outradeno= data.outradeno; // 商户订单号 或者微信支付单号
Random rand = new Random();
int rannum = rand.Next(100000, 999999);
string strtime = DateTime.Now.ToString("yyMMddHHmmss");
string outrefundno = "b" + rannum + strtime; // 新退款单号
data.outrefundno = outrefundno ;
int totalFee = 100;// 支付时金额
int refundFee = 100;// 退款金额
var dataInfo = new TenPayV3RefundRequestData(appid, mrch_id, mrchKey , "", nonce_str, null, outradeno, outrefundno , totalFee, refundFee, openid, null, null, null, "CNY", "MD5");
// 证书绝对路径,如@"F:\apiclient_cert.p12"
string cert = ConfigurationManager.AppSettings["CertPath"]; // 证书位置
string pws = "商户ID";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
//调用证书
X509Certificate2 cer = new X509Certificate2(cert.ToString(), pws.ToString(), X509KeyStorageFlags.MachineKeySet);
var result = TenPayV3.Refund(dataInfo, cert, pws, 10000);
if (result.return_code.ToUpper() == "SUCCESS" && result.result_code.ToUpper() == "SUCCESS")
{
string SUCCESS= true;
string MSG = string.Format("退款成功,退款单号={0},退款result={3}",outRefundNo, refundFee);
}
else
{
string errMsg = result.err_code_des;
string SUCCESS= false;
string MSG = string.Format("提交退款失败,退款单号={0},退款result={3}",outRefundNo, refundFee);
}
出现错误:指定的网络密码不正确
微信下载证书后,仔细阅读证书使用说明。
参考连接:
https://www.cnblogs.com/hailang8/p/11508627.html
https://www.cnblogs.com/rockchen/p/4479029.html
https://blog.csdn.net/shan1774965666/article/details/51690519