. net core to realize wechat applet payment function (unified order)


Recently, the company has developed several e-commerce applets and a core e-commerce live broadcast. As long as the e-commerce generally involves transaction information, it cannot be separated from the payment system. Here, we uniformly implement the payment process of the applets (the same as the service number implementation steps).

Payment ability of opening applet

It’s easy to open the payment function of small programs. Basically, it’s better to apply step by step according to wechat documents, as shown in the figure below

The above three steps are completed

1. Submit information to wechat

2. Wechat review and sign the agreement

3. Appid of the same subject bound to the merchant background

Merchant background binds appid of the same subject and authorizes

1. Log in merchant background https://pay.weixin.qq.com , enter the product center – appid authorization management, and enter the authorization page as shown in the figure below:

2. Fill in the relevant certified appid information, read and sign the wechat payment merchant number and appid authorization agreement, click “confirm” to submit, and initiate the authorization application, as shown in the following figure:

3. After the authorization application is initiated, the merchant shall go to the corresponding platform to confirm the authorization application.

Applet: go to the public platform, click “wechat payment – merchant number management”, view the relevant merchant number information, confirm the authorization application, or confirm the authorization information in the template message sent by the “public platform security assistant”;

The official account / subscription number: to the public platform, click “WeChat payment – merchant number management”, check the relevant merchant number information, confirm the authorization application;

App: go to the open platform, click “management center view wechat payment merchant number management” to view the relevant merchant number information and confirm the authorization application;

4. The above steps are bound successfully and wechat payment function can be realized.

Set the callback address in advance, and set the development configuration in the background of the merchant

After the callback address is set in advance, it is convenient to receive wechat notification to process business after the payment is successful. The setting is also relatively simple, needless to say, as shown in the figure:

Set different callback addresses according to your own needs.

Unified payment by code

1, unified payment, as long as official account of WeChat public platform can use the same payment, only need to bind APPID in the background of merchant.

Official picture of wechat app payment:

Wechat payment document description address: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

Reference package:



Official account number, applet information


The front end calls the unified single interface to obtain the prepayid

IRegisterService register = RegisterService.Start(env, senparcSetting.Value).UseSenparcGlobal(false, null);
register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value)
 .RegisterTenpayV3(senparcWeixinSetting.Value, "appid");

Some front-end codes are as follows:

Get the prepayid from the screenshot of the back-end part of the code. Please contact me for the code

After the unified payment is submitted successfully, the following figure will appear:

Note: normally, payment can be initiated after getting prepayid. Please make sure the given parameters are correct.

Wechat payment callback

Wechat payment callback rules are as follows:

Callback code implementation

///Wechat payment callback address
/// </summary>
/// <returns></returns>
public ActionResult PayNotifyUrl()
 ResponseResult result = new ResponseResult();
 ResponseHandler resHandler = new ResponseHandler(HttpContext);
 string return_code = resHandler.GetParameter("return_code");
 string return_msg = resHandler.GetParameter("return_msg");

  var mch_key = Senparc.Weixin.Config.SenparcWeixinSetting.TenPayV3_Key;

  var notifyXml = resHandler.ParseXML();
  bool signFlag = resHandler.IsTenpaySign();
  if (signFlag && return_code.ToUpper() == "SUCCESS")
   string attach = resHandler.GetParameter("attach");
   string device_info = resHandler.GetParameter("device_info");
   string out_trade_no = resHandler.GetParameter("out_trade_no");
   string transaction_id = resHandler.GetParameter("transaction_id");
   string total_fee = resHandler.GetParameter("total_fee");

   PayOrderNotifyParam param = new PayOrderNotifyParam()
    PayNo = out_trade_no,
    Attach = attach,
    PayPrice = decimal.Parse(total_fee) / 100,
    TransactionNo = transaction_id,

   //Callback Parameter Description: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_ 7&index=3
   // Logger.Info ( string.Format ("payment callback received parameters successfully, notifyxml = {0}, param = {1}", notifyxml, param.SerializeObject ()));

   result = Service.PayNotifyUrl(param);
   if (result.errno != 0)
    //Callback processing logic failed
    Logger.Error ( string.Format ("payment callback processing failed: payment doc No.: {0}, {1}", param.PayNo , result.errmsg );
    Logger.Info ( string.Format ("payment callback processing succeeded, payment No.: {0}", param.PayNo );
    string xml = string.Format(@"<xml>
       </xml>", return_code, return_msg);

    return Content(xml, "text/xml");

   //Wrong order processing
   Logger.Error ( string.Format ("failed to receive the payment callback, signflag = {0}, notifyxml = {1}", signflag, notifyxml));

 catch (Exception ex)
  Logger.Error ( string.Format ("exception of payment callback: Message = {0}, stacktrace = {1}", ex.Message , ex.StackTrace );

 return Content("fail", "text/xml");

The callback interface is better to verify whether the signature is correct. To verify the signature, please set the merchant key first. If the signature is successful and the wechat payment is successful, then the business data can be processed.

Note: if the business processing is successful, it is better to return success to tell wechat, otherwise wechat will call back once every other period of time until the number of calls is exhausted. Here, you need to process your own business.


The above process is the wechat payment process. Due to some privacy issues, some of the codes in the above steps adopt screenshots. If you don’t understand, please contact me. We will exchange and learn together. In the next article, we will realize the wechat refund function.

The above is the. Net core that Xiaobian introduced to you to realize the payment function of wechat applet. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]