接上篇,微信公众号支付申请退款
Controller
[HttpGet("TestRefund")]
public void TestRefund()
{
_ticketRepository.TestRefund();
}
Interface
(bool result, string msg) TestRefund();
业务仓储
public (bool result,string msg) TestRefund()
{
ApplyRefund apply = new ApplyRefund()
{
notify_url = $"xxx", //退款成功的回调地址
transaction_id = "", //微信订单号
refund_fee = "1", //单位:分
total_fee = "1"
};
var result = _wxHelper.ApplyRefund(apply);
//处理业务逻辑
return (true,"success");
}
用到的实体
public class ApplyRefund
{
/// <summary>
/// 微信订单号(和out_trade_no二传一,优先级transaction_id>out_trade_no)
/// </summary>
public string transaction_id { get; set; }
/// <summary>
/// 商户订单号(和transaction_id二传一,优先级transaction_id>out_trade_no)
/// </summary>
public string out_trade_no { get; set; }
/// <summary>
/// 订单总金额(分)
/// </summary>
public string total_fee { get; set; }
/// <summary>
/// 退款金额(分)
/// </summary>
public string refund_fee { get; set; }
/// <summary>
/// 退款回调地址
/// </summary>
public string notify_url { get; set; }
}
帮助方法
/// <summary>
/// 退款
/// </summary>
/// <param name="transaction_id"></param>
/// <param name="out_trade_no"></param>
/// <param name="total_fee"></param>
/// <param name="refund_fee"></param>
/// <returns></returns>
public string ApplyRefund(ApplyRefund dto)
{
_logger.LogWarning("Refund", "Refund is processing...");
WxPayData data = new WxPayData();
if (!string.IsNullOrEmpty(dto.transaction_id))//微信订单号存在的条件下,则已微信订单号为准
{
data.SetValue("transaction_id", dto.transaction_id);
}
else//微信订单号不存在,才根据商户订单号去退款
{
data.SetValue("out_trade_no", dto.out_trade_no);
}
data.SetValue("total_fee", int.Parse(dto.total_fee));//订单总金额
data.SetValue("refund_fee", int.Parse(dto.refund_fee));//退款金额
data.SetValue("out_refund_no", GenerateOutTradeNo());//随机生成商户退款单号
data.SetValue("op_user_id", "xxx");//操作员,默认为商户号
data.SetValue("notify_url", dto.notify_url);//操作员,默认为商户号
WxPayData result = Refund(data);//提交退款申请给API,接收返回数据
return result.ToPrintStr();
}
/// <summary>
///
/// </summary>
/// <param name="inputObj"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public WxPayData Refund(WxPayData inputObj, int timeOut = 6)
{
string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
//检测必填参数
if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
{
throw new Exception("退款申请接口中,out_trade_no、transaction_id至少填一个!");
}
else if (!inputObj.IsSet("out_refund_no"))
{
throw new Exception("退款申请接口中,缺少必填参数out_refund_no!");
}
else if (!inputObj.IsSet("total_fee"))
{
throw new Exception("退款申请接口中,缺少必填参数total_fee!");
}
else if (!inputObj.IsSet("refund_fee"))
{
throw new Exception("退款申请接口中,缺少必填参数refund_fee!");
}
else if (!inputObj.IsSet("op_user_id"))
{
throw new Exception("退款申请接口中,缺少必填参数op_user_id!");
}
inputObj.SetValue("appid", "xxx");//公众账号ID
inputObj.SetValue("mch_id", "xxx");//商户号
inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_MD5);//签名类型
inputObj.SetValue("sign", inputObj.MakeSign());//签名
string xml = inputObj.ToXml();
var start = DateTime.Now;
string response = _webHelper.Post(xml, url, true, timeOut);//调用HTTP通信接口提交数据到API
var end = DateTime.Now;
//int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
//将xml格式的结果转换为对象以返回
WxPayData result = new WxPayData();
result.FromXml(response);
//ReportCostTime(url, timeCost, result);//测速上报
return result;
}
public string RefundQuery(string refund_id, string out_refund_no, string transaction_id, string out_trade_no)
{
_logger.LogWarning("RefundQuery is processing...");
WxPayData data = new WxPayData();
if (!string.IsNullOrEmpty(refund_id))
{
data.SetValue("refund_id", refund_id);//微信退款单号,优先级最高
}
else if (!string.IsNullOrEmpty(out_refund_no))
{
data.SetValue("out_refund_no", out_refund_no);//商户退款单号,优先级第二
}
else if (!string.IsNullOrEmpty(transaction_id))
{
data.SetValue("transaction_id", transaction_id);//微信订单号,优先级第三
}
else
{
data.SetValue("out_trade_no", out_trade_no);//商户订单号,优先级最低
}
WxPayData result = RefundQuery(data);//提交退款查询给API,接收返回数据
return result.ToPrintStr();
}