.NET Core 微信小程序退款步驟——(統(tǒng)一退款)
繼上一篇".NET Core 微信小程序支付——(統(tǒng)一下單)后",本文將實(shí)現(xiàn)統(tǒng)一退款功能,能支付就應(yīng)該能退款嘛,一般涉及到錢(qián)的東西都會(huì)比較敏感,所以在設(shè)計(jì)退款流程時(shí)一定要嚴(yán)謹(jǐn),不能出一點(diǎn)差錯(cuò),否則你將會(huì)面臨自己掏腰包的可能,下面我們來(lái)講一講退款的實(shí)現(xiàn)步驟。
退款應(yīng)該場(chǎng)景及規(guī)則
當(dāng)交易發(fā)生之后一段時(shí)間內(nèi),由于買(mǎi)家或者賣(mài)家的原因需要退款時(shí),賣(mài)家可以通過(guò)退款接口將支付款退還給買(mǎi)家,微信支付將在收到退款請(qǐng)求并且驗(yàn)證成功之后,按照退款規(guī)則將支付款按原路退到買(mǎi)家?guī)ぬ?hào)上。
規(guī)則
1、交易時(shí)間超過(guò)一年的訂單無(wú)法提交退款;
2、微信支付退款支持單筆交易分多次退款,多次退款需要提交原支付訂單的商戶訂單號(hào)和設(shè)置不同的退款單號(hào)。申請(qǐng)退款總金額不能超過(guò)訂單金額。 一筆退款失敗后重新提交,請(qǐng)不要更換退款單號(hào),請(qǐng)使用原商戶退款單號(hào)。
3、請(qǐng)求頻率限制:150qps,即每秒鐘正常的申請(qǐng)退款請(qǐng)求次數(shù)不超過(guò)150次
錯(cuò)誤或無(wú)效請(qǐng)求頻率限制:6qps,即每秒鐘異常或錯(cuò)誤的退款申請(qǐng)請(qǐng)求不超過(guò)6次
4、每個(gè)支付訂單的部分退款次數(shù)不能超過(guò)50次
接口地址
接口鏈接:https://api.mch.weixin.qq.com/secapi/pay/refund
相關(guān)參數(shù)


官方退款文檔
文檔地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_4&index=6
實(shí)現(xiàn)統(tǒng)一退款流程
如果業(yè)務(wù)有多處退款流程,可以將退款流程進(jìn)行封裝,方便多位置調(diào)用;如果公司有同主體下的不同小程序,都需要退款功能,也是可以進(jìn)行封裝,針對(duì)不同的小程序進(jìn)行退款。
去商戶后臺(tái)先下載證書(shū),退款時(shí)需要,下圖是商戶后臺(tái)下載證書(shū)的界面:
引用包:
Senparc.Weixin.WxOpen
Senparc.Weixin.TenPay
注冊(cè)公眾號(hào),小程序信息
services.AddSenparcGlobalServices(Configuration) .AddSenparcWeixinServices(Configuration); IRegisterService register = RegisterService.Start(env, senparcSetting.Value).UseSenparcGlobal(false, null); register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value) .RegisterTenpayV3(senparcWeixinSetting.Value, "appid");
統(tǒng)一退款代碼實(shí)現(xiàn)
publicbool RefundProduct(OrdOrderProduct ordOrderProduct, PayOrderMstParam payOrderMstParam, OrdOrderPayItem ordOrderPayItem, string appID, DateTime thisTime, ref string errMsg)
{
try
{
OrdOrderPayMst refPay = null;
if (!PayOrderManager.CreatePayOrderMST(payOrderMstParam, thisTime, ref refPay, ref errMsg))
{
errMsg = "生成退款單出錯(cuò)!" + errMsg;
return false;
}
var PayInfo = Senparc.Weixin.Config.SenparcWeixinSetting.Items[appID];
string AppID = PayInfo.WxOpenAppId;
string AppSecret = PayInfo.WxOpenAppSecret;
string Mch_id = PayInfo.TenPayV3_MchId;//商戶號(hào)
string Mch_key = PayInfo.TenPayV3_Key;//商戶密鑰
string notifyUrl = string.Format(PayInfo.TenPayV3_TenpayNotify, "RefundNotifyUrl");
var timeStamp = TenPayV3Util.GetTimestamp();
var nonceStr = TenPayV3Util.GetNoncestr();
//支付源單號(hào)
string outTradeNo = ordOrderPayItem.PayNo;//商戶訂單號(hào)/支付單號(hào)
refPay.PayNoSource = ordOrderPayItem.PayNo;
//退款單號(hào)
string outRefundNo = refPay.PayNo;//新退款單號(hào)
//支付時(shí)的總金額
int totalFee = (int)(ordOrderPayItem.PayPrice * 100);
//退款金額
int refundFee = (int)(refPay.PayPrice * 100);
string opUserId = PayInfo.TenPayV3_MchId;
var dataInfo = new TenPayV3RefundRequestData(AppID, Mch_id, Mch_key,
null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null, notifyUrl: notifyUrl);
//Logger.Info($"PayInfo={PayInfo.SerializeObject()}");
//Logger.Info($"dataInfo={dataInfo.SerializeObject()}");
//var basePath = AppContext.BaseDirectory;
//var certPath = Path.Combine(basePath, "Config/apiclient_cert.p12");
//var cert = @"D:\projects\orderapi.trydou.com\Config\apiclient_cert.p12";//根據(jù)自己的證書(shū)位置修改
//var password = Mch_id;//默認(rèn)為商戶號(hào),建議修改
//配置好證書(shū)地址,V3自動(dòng)識(shí)別
var result = TenPayV3.Refund(dataInfo);
refPay.PayResult = result.SerializeObject();
//Logger.Info("提交退款申請(qǐng):" + refPay.PayResult);
if (result.return_code.ToUpper() == "SUCCESS" && result.result_code.ToUpper() == "SUCCESS")
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
//業(yè)務(wù)處理
//提交事務(wù)
scope.Complete();
}
return true;
}
else
{
errMsg = result.err_code_des;
Logger.Error(string.Format("提交退款失敗,退款單號(hào)={0},關(guān)聯(lián)訂單號(hào)={1},關(guān)聯(lián)產(chǎn)品={2},退款result={3}",
refPay.PayNo, refPay.RelationNo, refPay.RelationNos, refPay.PayResult));
}
}
catch (Exception ex)
{
errMsg = ex.Message;
Logger.Error(string.Format("提交退款異常:Message={0},StackTrace={1}", ex.Message, ex.StackTrace));
}
return false;
}
注:注意退款接口的參數(shù),如:金額,退款地址等,確保參數(shù)正確,一般微信會(huì)收到退款的請(qǐng)求指令,微信處理成功后,會(huì)異步回調(diào)退款的接口給服務(wù)器。
退款統(tǒng)一回調(diào)處理
直接上代碼如下:
/// <summary>
/// 退款回調(diào)
/// </summary>
[HttpPost("RefundNotifyUrl")]
public ActionResult RefundNotifyUrl()
{
ResponseResult result = new ResponseResult();
ResponseHandler resHandler = new ResponseHandler(HttpContext);
string return_code = resHandler.GetParameter("return_code");
string return_msg = resHandler.GetParameter("return_msg");
try
{
var mch_key = Senparc.Weixin.Config.SenparcWeixinSetting.TenPayV3_Key;
if (return_code.ToUpper() == "SUCCESS")
{
//string result_code = resHandler.GetParameter("result_code");
//string appId = resHandler.GetParameter("appid");
//string mch_id = resHandler.GetParameter("mch_id");
//string nonce_str = resHandler.GetParameter("nonce_str");
string req_info = resHandler.GetParameter("req_info");
var decodeReqInfo = TenPayV3Util.DecodeRefundReqInfo(req_info, mch_key);
var decodeDoc = XDocument.Parse(decodeReqInfo);
var refundNotifyXml = decodeDoc.SerializeObject();
//獲取接口中需要用到的信息
string out_trade_no = decodeDoc.Root.Element("out_trade_no").Value;
string out_refund_no = decodeDoc.Root.Element("out_refund_no").Value;
string transaction_id = decodeDoc.Root.Element("transaction_id").Value;
string refund_id = decodeDoc.Root.Element("refund_id").Value;
int total_fee = int.Parse(decodeDoc.Root.Element("total_fee").Value);
int refund_fee = int.Parse(decodeDoc.Root.Element("refund_fee").Value);
RefundNotifyParam param = new RefundNotifyParam()
{
PayNo = out_trade_no,//商戶訂單號(hào)
PayPrice = ((float)refund_fee.ToInt() / 100).ToDecimal(),//退款金額
Out_refund_no = out_refund_no,//商戶退款單號(hào)
TransactionNo = transaction_id,//微信訂單號(hào)
Refund_id = refund_id, //微信退款單號(hào)
};
Logger.Info(string.Format("退款回調(diào)參數(shù),return_code={0},return_msg={1},refundNotifyXml={2}", return_code, return_msg, refundNotifyXml));
result = Service.RefundNotifyUrl(param);
if (result.errno != 0)
{
//回調(diào)處理邏輯失敗
Logger.Error(string.Format("退款回調(diào)業(yè)務(wù)處理失?。和丝顔翁?hào){0},{1}", param.Out_refund_no, result.errmsg));
}
else
{
Logger.Info(string.Format("退款回調(diào)業(yè)務(wù)處理成功,退款單號(hào):{0}", param.Out_refund_no));
string xml = string.Format(@"<xml>
<return_code><![CDATA[{0}]]></return_code>
<return_msg><![CDATA[{1}]]></return_msg>
</xml>", return_code, return_msg);
return Content(xml, "text/xml");
}
}
else
{
//錯(cuò)誤的訂單處理
Logger.Error(string.Format("退款回調(diào)失敗,return_code={0},return_msg={1}", return_code, return_msg));
}
}
catch (Exception ex)
{
Logger.Error(string.Format("退款回調(diào)異常:Message={0},StackTrace={1}", ex.Message, ex.StackTrace));
}
return Content("fail", "text/xml");
}
注:如果業(yè)務(wù)處理退款成功后,請(qǐng)返回結(jié)果告訴微信SUCCESS,否則微信也會(huì)按規(guī)則不停發(fā)送退款回調(diào)給服務(wù)器,直到次數(shù)用完為止,具體查看上面規(guī)則文檔。
總結(jié)
以上所述是小編給大家介紹的.NET Core 微信小程序退款步驟——(統(tǒng)一退款),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
ASP.NET數(shù)據(jù)庫(kù)緩存依賴實(shí)例分析
這篇文章主要介紹了ASP.NET數(shù)據(jù)庫(kù)緩存依賴,以實(shí)例的形式分析總結(jié)了數(shù)據(jù)庫(kù)緩存依賴的原理與用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
基于自定義Unity生存期模型PerCallContextLifeTimeManager的問(wèn)題
本篇文章小編將為大家介紹,基于自定義Unity生存期模型PerCallContextLifeTimeManager的問(wèn)題。需要的朋友參考下2013-04-04
.Net Winform 實(shí)現(xiàn)CSS3.0 潑墨畫(huà)效果(示例代碼)
這篇文章主要介紹了.Net Winform 實(shí)現(xiàn)CSS3.0 潑墨畫(huà)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Asp.Net 網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫(kù)優(yōu)化措施 使用主從庫(kù)(全)
網(wǎng)站規(guī)模到了一定程度之后,該分的也分了,該優(yōu)化的也做了優(yōu)化,但是還是不能滿足業(yè)務(wù)上對(duì)性能的要求;這時(shí)候我們可以考慮使用主從庫(kù)。2010-06-06
ASP.NET中畫(huà)圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章給大家介紹了asp.net中畫(huà)圖形驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Asp.NET MVC中使用SignalR實(shí)現(xiàn)推送功能
這篇文章主要為大家詳細(xì)介紹了Asp.NET MVC 中使用 SignalR 實(shí)現(xiàn)推送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

