.NET Core 微信小程式退款——(統一退款)

  • 2019 年 10 月 3 日
  • 筆記

繼上一篇".NET Core 微信小程式支付——(統一下單)後",本文將實現統一退款功能,能支付就應該能退款嘛,一般涉及到錢的東西都會比較敏感,所以在設計退款流程時一定要嚴謹,不能出一點差錯,否則你將會面臨自己掏腰包的可能,下面我們來講一講退款的實現步驟。

 

目錄

1、退款應該場景及規則

2、實現統一退款流程

3、退款統一回調處理

4、總結

退款應該場景及規則

當交易發生之後一段時間內,由於買家或者賣家的原因需要退款時,賣家可以通過退款介面將支付款退還給買家,微信支付將在收到退款請求並且驗證成功之後,按照退款規則將支付款按原路退到買家帳號上。

 

規則

1、交易時間超過一年的訂單無法提交退款;

2、微信支付退款支援單筆交易分多次退款,多次退款需要提交原支付訂單的商戶訂單號和設置不同的退款單號。申請退款總金額不能超過訂單金額。 一筆退款失敗後重新提交,請不要更換退款單號,請使用原商戶退款單號。

3、請求頻率限制:150qps,即每秒鐘正常的申請退款請求次數不超過150次

    錯誤或無效請求頻率限制:6qps,即每秒鐘異常或錯誤的退款申請請求不超過6次

4、每個支付訂單的部分退款次數不能超過50次

 

介面地址

介面鏈接:https://api.mch.weixin.qq.com/secapi/pay/refund

 

相關參數

 

官方退款文檔
文檔地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_4&index=6

實現統一退款流程

如果業務有多處退款流程,可以將退款流程進行封裝,方便多位置調用;如果公司有同主體下的不同小程式,都需要退款功能,也是可以進行封裝,針對不同的小程式進行退款。


去商戶後台先下載證書,退款時需要,下圖是商戶後台下載證書的介面:

 

引用包:

Senparc.Weixin.WxOpen

Senparc.Weixin.TenPay

 

註冊公眾號,小程式資訊

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");

 

統一退款程式碼實現

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 = "生成退款單出錯!" + 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;//商戶號          string Mch_key = PayInfo.TenPayV3_Key;//商戶密鑰          string notifyUrl = string.Format(PayInfo.TenPayV3_TenpayNotify, "RefundNotifyUrl");          var timeStamp = TenPayV3Util.GetTimestamp();          var nonceStr = TenPayV3Util.GetNoncestr();                //支付源單號          string outTradeNo = ordOrderPayItem.PayNo;//商戶訂單號/支付單號          refPay.PayNoSource = ordOrderPayItem.PayNo;            //退款單號          string outRefundNo = refPay.PayNo;//新退款單號            //支付時的總金額          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:projectsorderapi.trydou.comConfigapiclient_cert.p12";//根據自己的證書位置修改          //var password = Mch_id;//默認為商戶號,建議修改          //配置好證書地址,V3自動識別          var result = TenPayV3.Refund(dataInfo);            refPay.PayResult = result.SerializeObject();          //Logger.Info("提交退款申請:" + refPay.PayResult);          if (result.return_code.ToUpper() == "SUCCESS" && result.result_code.ToUpper() == "SUCCESS")          {              using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))              {                  //業務處理                  //提交事務                  scope.Complete();              }                  return true;          }          else          {              errMsg = result.err_code_des;              Logger.Error(string.Format("提交退款失敗,退款單號={0},關聯訂單號={1},關聯產品={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;      }

 

註:注意退款介面的參數,如:金額,退款地址等,確保參數正確,一般微信會收到退款的請求指令,微信處理成功後,會非同步回調退款的介面給伺服器。

退款統一回調處理

直接上程式碼如下:

/// <summary>  /// 退款回調  /// </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,//商戶訂單號                  PayPrice = ((float)refund_fee.ToInt() / 100).ToDecimal(),//退款金額                  Out_refund_no = out_refund_no,//商戶退款單號                    TransactionNo = transaction_id,//微信訂單號                  Refund_id = refund_id, //微信退款單號                 };                Logger.Info(string.Format("退款回調參數,return_code={0},return_msg={1},refundNotifyXml={2}", return_code, return_msg, refundNotifyXml));                result = Service.RefundNotifyUrl(param);              if (result.errno != 0)              {                  //回調處理邏輯失敗                  Logger.Error(string.Format("退款回調業務處理失敗:退款單號{0},{1}", param.Out_refund_no, result.errmsg));              }              else              {                  Logger.Info(string.Format("退款回調業務處理成功,退款單號:{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          {              //錯誤的訂單處理              Logger.Error(string.Format("退款回調失敗,return_code={0},return_msg={1}", return_code, return_msg));            }      }      catch (Exception ex)      {          Logger.Error(string.Format("退款回調異常:Message={0},StackTrace={1}", ex.Message, ex.StackTrace));      }        return Content("fail", "text/xml");  }

 

註:如果業務處理退款成功後,請返回結果告訴微信SUCCESS,否則微信也會按規則不停發送退款回調給伺服器,直到次數用完為止,具體查看上面規則文檔。

總結

以上就是微信退款的流程及相關知識的介紹,如有在實際中遇到問題的可與我聯繫,一起討論學習,下一篇會繼續推出公眾號與小程式的相關文章。

Tags: