Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize WeChat Mini Programs's refund function by .NET Core

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of ".NET Core how to achieve WeChat Mini Programs's refund function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Refund should be based on scenarios and rules

Within a period of time after the transaction, when the buyer or seller needs a refund for reasons, the seller can refund the payment to the buyer through the refund interface. WeChat Pay will return the payment to the buyer's account according to the refund rules after receiving the refund request and verifying the success.

Rules

1. Orders that have been traded for more than one year cannot submit a refund

2. WeChat Pay's refund supports multiple refunds for a single transaction, and multiple refunds need to be submitted to the merchant's order number that originally paid the order and set different refund order numbers. The total amount of refund requested cannot exceed the amount of the order. Resubmit after a refund failure, please do not change the refund order number, please use the original merchant refund order number.

3. Limit the frequency of requests: 150qps, that is, the number of normal requests for refunds per second is no more than 150.

Error or invalid request frequency limit: 6qps, that is, no more than 6 abnormal or incorrect refund requests per second

4. The number of partial refunds for each payment order cannot exceed 50.

Interface address

Related parameters

Official refund document

Document address: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_4&index=6

Achieve a unified refund process

If the business has multiple refund processes, the refund process can be encapsulated to facilitate multiple locations to call; if the company has different Mini Program under the same subject, the refund function is required, and it can also be encapsulated for a refund for different Mini Program.

Download the certificate at the merchant backend first, which is required for a refund. The following figure shows the interface for downloading the certificate at the merchant backend:

Reference package:

Senparc.Weixin.WxOpen

Senparc.Weixin.TenPay

Registered official account, Mini Program information

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 = "error generating refund order!" + 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;// Merchant string Mch_key = PayInfo.TenPayV3_Key;// Merchant key string notifyUrl = string.Format (PayInfo.TenPayV3_TenpayNotify, "RefundNotifyUrl") Var timeStamp = TenPayV3Util.GetTimestamp (); var nonceStr = TenPayV3Util.GetNoncestr (); / / payment source order number string outTradeNo = ordOrderPayItem.PayNo;// merchant order number / payment order number refPay.PayNoSource = ordOrderPayItem.PayNo;// refund order number string outRefundNo = refPay.PayNo;// new refund order number / / Total amount at the time of payment int totalFee = (int) (ordOrderPayItem.PayPrice * 100); / / refund amount 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"; / / modify / / var password = Mch_id;// defaults to the merchant number according to your certificate location. It is recommended to modify / / configure the certificate address. V3 automatically recognizes var result = TenPayV3.Refund (dataInfo); refPay.PayResult = result.SerializeObject (); / / Logger.Info ("submit refund request:" + refPay.PayResult) If (result.return_code.ToUpper () = "SUCCESS" & & result.result_code.ToUpper () = = "SUCCESS") {using (TransactionScope scope = new TransactionScope (TransactionScopeAsyncFlowOption.Enabled)) {/ / Business Manager / / commit transaction scope.Complete ();} return true;} else {errMsg = result.err_code_des Logger.Error ("failed to submit refund, refund order number = {0}, related order number = {1}, related product = {2}, refund result= {3}", refPay.PayNo, refPay.RelationNo, refPay.RelationNos, refPay.PayResult));}} catch (Exception ex) {errMsg = ex.Message; Logger.Error (string.Format ("submit refund exception: Message= {0}, StackTrace= {1}", ex.Message, ex.StackTrace));} return false }

Note: pay attention to the parameters of the refund API, such as amount, refund address, etc., to ensure that the parameters are correct. Generally, Wechat will receive a refund request. After successful processing by Wechat, the refund API will be called back to the server asynchronously.

Unified callback processing of refund

Directly, the code is as follows:

/ [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 (); / / get the information needed in the interface 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,// merchant order number PayPrice = ((float) refund_fee.ToInt () / 100) .ToDecimal (), / / refund amount Out_refund_no = out_refund_no,// merchant refund order number TransactionNo = transaction_id,// Wechat order number Refund_id = refund_id, / / Wechat refund order number} Logger.Info (string.Format ("refund callback parameters, return_code= {0}, return_msg= {1}, refundNotifyXml= {2}", return_code, return_msg, refundNotifyXml); result = Service.RefundNotifyUrl (param); if (result.errno! = 0) {/ / callback processing logic failure Logger.Error (string.Format ("refund callback business processing failure: refund order number {0}, {1}", param.Out_refund_no, result.errmsg)) } else {Logger.Info (string.Format ("refund callback business processed successfully, refund order number: {0}", param.Out_refund_no)); string xml = string.Format (@ ", return_code, return_msg); return Content (xml," text/xml ") } else {/ / incorrect order processing Logger.Error (string.Format ("refund callback failed, return_code= {0}, return_msg= {1}", return_code, return_msg);}} catch (Exception ex) {Logger.Error ("refund callback exception: Message= {0}, StackTrace= {1}, ex.Message, ex.StackTrace));} return Content (" fail "," text/xml ");}

Note: if the business processes the refund successfully, please return the result and tell Wechat SUCCESS, otherwise Wechat will continue to send refund callback to the server according to the rules until the number of times is used up. For more information, please see the above rule document.

This is the end of the content of "how to achieve WeChat Mini Programs's refund function in .NET Core". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report