public async Task <BaseResultModel> RefundAsync(string outTradeNo, string outRefundNo, int totalFee, int refundFee, SystemConfigs tencentparam, string msg = null) { BaseResultModel model = new BaseResultModel(); try { TenPayV3Info TenPayV3Info = new TenPayV3Info(tencentparam.AppID, tencentparam.AppSecret, tencentparam.TenPayMchId, tencentparam.TenPayKey, tencentparam.TenPayNotify, tencentparam.TenPayWxOpenNotify); string nonceStr = TenPayV3Util.GetNoncestr(); //string outTradeNo = Order.WXPayBillno; //string outRefundNo = result.RefundNumber; //var totalFee = (int)(ServiceResult.result.item.ServiceExpense * 100);//单位:分 //var totalFee = totalFee;//单位:分 //int refundFee = totalFee; string opUserId = TenPayV3Info.MchId; var notifyUrl = "http://admin/integraltel.com/api/RefundPayNotify"; var dataInfo = new TenPayV3RefundRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, TenPayV3Info.Key, null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null, refundDescription: msg, notifyUrl: notifyUrl); var cert = @"C:\cert\apiclient_cert.p12"; //根据自己的证书位置修改 var password = TenPayV3Info.MchId; //默认为商户号,建议修改 Thread.Sleep(50); var wxpayresult = await TenPayV3.RefundAsync(dataInfo, cert, password); Thread.Sleep(50); model.code = wxpayresult.result_code; model.msg = wxpayresult.return_msg; } catch (Exception ex) { model.code = "FAIL"; model.msg = ex.Message.ToString(); } return(model); }
/// <summary> /// 退款 /// </summary> /// <param name="appId"></param> /// <param name="mchId"></param> /// <param name="mchKey"></param> /// <param name="localTranNo"></param> /// <param name="localRefundNo"></param> /// <param name="orderAmount"></param> /// <param name="refundAmount"></param> /// <param name="refundDesc"></param> /// <param name="notifyUrl"></param> /// <returns></returns> public async Task <(bool success, string originXml)> Refund(string appId, string mchId, string mchKey, string localTranNo, string localRefundNo, int orderAmount, int refundAmount, string refundDesc, string notifyUrl) { string nonceStr = TenPayV3Util.GetNoncestr(); var result = await TenPayV3.RefundAsync(Shashlik.Kernel.GlobalKernelServiceProvider.KernelServiceProvider, new TenPayV3RefundRequestData( appId, mchId, mchKey, null, nonceStr, null, localTranNo, localRefundNo, (int)orderAmount, (int)refundAmount, null, null, refundDescription : refundDesc, notifyUrl : notifyUrl)); Logger.LogInformation($"微信支付申请退款,out_trade_no:{localTranNo},result:{result.ResultXml}"); return(result.IsReturnCodeSuccess() && result.IsResultCodeSuccess(), result.ResultXml); }
public async Task <IActionResult> RefundGet([FromQuery] RefundModel refund) { try { // WeixinTrace.SendCustomLog("进入退款流程", "1"); if (string.IsNullOrWhiteSpace(refund.NonceStr)) { refund.NonceStr = TenPayV3Util.GetNoncestr(); } if (string.IsNullOrWhiteSpace(refund.OpUserId)) { refund.OpUserId = TenPyConfigRead.MchId; } if (string.IsNullOrWhiteSpace(refund.OutTradeNo) && string.IsNullOrWhiteSpace(refund.TransactionId)) { return(BadRequest("需要OutTradeNo,TransactionId之一")); } // string outTradeNo = HttpContext.Session.GetString("BillNo"); // WeixinTrace.SendCustomLog("进入退款流程", "2 outTradeNo:" + refund.OutTradeNo); refund.SignType = "MD5"; var dataInfo = new TenPayV3RefundRequestData( appId: TenPyConfigRead.AppId, mchId: TenPyConfigRead.MchId, key: TenPyConfigRead.Key, deviceInfo: refund.DeviceInfo, nonceStr: refund.NonceStr, transactionId: refund.TransactionId, outTradeNo: refund.OutTradeNo, outRefundNo: refund.OutRefundNo, totalFee: refund.TotalFee, refundFee: refund.RefundFee, opUserId: refund.OpUserId, refundAccount: refund.RefundAccount, notifyUrl: refund.NotifyUrl); //#region 旧方法 //var cert = @"D:\cert\apiclient_cert_SenparcRobot.p12";//根据自己的证书位置修改 //var password = TenPayV3Info.MchId;//默认为商户号,建议修改 //var result = TenPayV3.Refund(dataInfo, TenPyConfigRead.CertPath, Int32.Parse(TenPyConfigRead.CertSecret)); //#endregion #region 新方法(Senparc.Weixin v6.4.4+) var result = await TenPayV3.RefundAsync(_serviceProvider, dataInfo);//证书地址、密码,在配置文件中设置,并在注册微信支付信息时自动记录 #endregion var log = _logger.CreateLogger("申请退款"); if (result.return_code == "FAIL") { log.LogError($"退款单号(out_refund_no):{refund.OutRefundNo} 通讯标记(return_code):{result.return_code} {result.return_msg}"); } if (result.result_code == "FAIL") { log.LogError($"退款单号(out_refund_no):{refund.OutRefundNo} 业务结果(result_code):{result.result_code}\n{result.err_code}:{result.err_code_des}"); } else if (result.result_code == "SUCCESS") { log.LogInformation($"退款单号(out_refund_no):{refund.OutRefundNo} 业务结果(result_code):{result.result_code}"); } // WeixinTrace.SendCustomLog("进入退款流程", "3 Result:" + result.ToJson()); // ViewData["Message"] = $"退款结果:{result.result_code} {result.err_code_des}。您可以刷新当前页面查看最新结果。"; return(Ok(new { respond = result, request = refund })); //return Json(result, JsonRequestBehavior.AllowGet); } catch (Exception ex) { WeixinTrace.WeixinExceptionLog(new WeixinException(ex.Message, ex)); throw; } #region 原始方法 //RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 //packageReqHandler.SetParameter("appid", TenPayV3Info.AppId); //公众账号ID //packageReqHandler.SetParameter("mch_id", TenPayV3Info.MchId); //商户号 //packageReqHandler.SetParameter("out_trade_no", "124138540220170502163706139412"); //填入商家订单号 ////packageReqHandler.SetParameter("out_refund_no", ""); //填入退款订单号 //packageReqHandler.SetParameter("total_fee", ""); //填入总金额 //packageReqHandler.SetParameter("refund_fee", "100"); //填入退款金额 //packageReqHandler.SetParameter("op_user_id", TenPayV3Info.MchId); //操作员Id,默认就是商户号 //packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 //string sign = packageReqHandler.CreateMd5Sign("key", TenPayV3Info.Key); //packageReqHandler.SetParameter("sign", sign); //签名 ////退款需要post的数据 //string data = packageReqHandler.ParseXML(); ////退款接口地址 //string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; ////本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) //string cert = @"D:\cert\apiclient_cert_SenparcRobot.p12"; ////私钥(在安装证书时设置) //string password = TenPayV3Info.MchId; //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); ////调用证书 //X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); //#region 发起post请求 //HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); //webrequest.ClientCertificates.Add(cer); //webrequest.Method = "post"; //byte[] postdatabyte = Encoding.UTF8.GetBytes(data); //webrequest.ContentLength = postdatabyte.Length; //Stream stream; //stream = webrequest.GetRequestStream(); //stream.Write(postdatabyte, 0, postdatabyte.Length); //stream.Close(); //HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); //StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); //string responseContent = streamReader.ReadToEnd(); //#endregion //// var res = XDocument.Parse(responseContent); ////string openid = res.Element("xml").Element("out_refund_no").Value; //return Content("申请成功:<br>" + HttpUtility.RequestUtility.HtmlEncode(responseContent)); #endregion }