Home>Article>WeChat Applet> Activation of enterprise transfer to user interface
This time I will bring you the activation of the enterprise transfer to user interface. What are theprecautions for the activation of enterprise transfer to user interface? The following is a practical case, let's take a look.
There is no such interface in the WeChat public account payment API. If the enterprise needs totransfer money to the user, or allow the user towithdraw cashor sendred envelope# to the user ## etc. need to be activated separately in the product center in the merchant platform.1. Activating the function
#Activating is just one click, very simple. However, it should be noted that the account that supports transfers to users and the account that receives payments from users are not the same. In order to meet this function, you need to recharge with Tenpay first (
Transaction Center--Fund Management--Recharge).2. Download the certificate
and the merchant platform login password. After downloading, install it on Windows. The password for installation is your merchant number.
#After installation, place the certificate in the website directory for verification in the code in the next step.
3. Transfer
public class TransfersPay { public string openid { get; set; } public int amount { get; set; } public string partner_trade_no { get; set; } public string re_user_name { get; set; } public string spbill_create_ip { get; set; } public WxPayData GetTransfersApiParameters() { WxPayData apiParam = new WxPayData(); apiParam.SetValue("partner_trade_no", partner_trade_no); apiParam.SetValue("openid", openid); apiParam.SetValue("check_name", "NO_CHECK"); apiParam.SetValue("amount", amount); apiParam.SetValue("desc", "提现"); apiParam.SetValue("spbill_create_ip", spbill_create_ip); apiParam.SetValue("re_user_name", re_user_name); return apiParam; } }
The WxpayApi in the official demo already contains methods related to official account payment. Add another Transfers method to transfer money:
public static WxPayData Transfers(WxPayData inputData, int timeOut = 6) { var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; inputData.SetValue("mch_appid", WxPayConfig.APPID);//公众账号ID inputData.SetValue("mchid", WxPayConfig.MCHID);//商户号 inputData.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串 inputData.SetValue("sign", inputData.MakeSign());//签名 string xml = inputData.ToXml(); var start = DateTime.Now; string response = HttpService.Post(xml, url, true, timeOut); // Portal.MVC.Logger.Info("WxPayApi"+ "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
Something that needs a little attention is that the names of several default parameters are different from other methods, such as appid and mch_id. In the transfer, they are mch_appid and mchid, and in the red envelope, they are also called wxappid and mch_id. Then notice that the third parameter of the httpService.post method is true. That is, the certificate will be used. Entering the post method, we can see:
//是否使用证书 if (isUseCert) { string path = HttpContext.Current.Request.PhysicalApplicationPath; X509Certificate2 cert = new X509Certificate2(path + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD); request.ClientCertificates.Add(cert); Log.Debug("WxPayApi", "PostXml used cert"); }
The path and password of the certificate are used here, and the password is the merchant number. After everything is ready, you can transfer money in the controller:
[LoginValid] public ActionResult CashTransfers(string orderNumber) { //var order = new Order(){Amount = 1}; // var openid = "oBSBmwQjqwjfzQlKsFNjxFLSixxx"; var user = _workContext.CurrentUser; var order = _paymentService.GetOrderByOrderNumber(orderNumber); var transfer = new TransfersPay { openid = user.OpenId, amount = (int) order.Amount*100, partner_trade_no = order.OrderNumber, re_user_name = "stoneniqiu", spbill_create_ip = _webHelper.GetCurrentIpAddress() }; var data = transfer.GetTransfersApiParameters(); var result = WxPayApi.Transfers(data); return Content(result.ToPrintStr()); }
Get the result
In this way, the transfer/withdrawal function is realized.
Release
[LoginValid] public ActionResult CashTransfers(string orderNumber) { var user = _workContext.CurrentUser; var order = _paymentService.GetOrderByOrderNumber(orderNumber); if (string.IsNullOrEmpty(user.OpenId)) { return Json(new PortalResult("请用微信登录!")); } if (order == null || order.OrderState != OrderState.Padding) { return Json(new PortalResult("订单有误!")); } var transfer = new TransfersPay { openid = user.OpenId, amount = (int) order.Amount*100, partner_trade_no = order.OrderNumber, re_user_name = "stoneniqiu", spbill_create_ip = _webHelper.GetCurrentIpAddress() }; var data = transfer.GetTransfersApiParameters(); var result = WxPayApi.Transfers(data); if (result.GetValue("result_code").ToString() == "SUCCESS") { return Json(new PortalResult(true, "提现成功")); } return Json(new PortalResult(false, result.GetValue("return_msg").ToString())); }
Another thing to note is that the operation timeout error always occurs after publishing. The suggestion is to change the timeout to 30 seconds. The default 6 seconds is prone to timeout. The same applies when placing orders together.
public static WxPayData Transfers(WxPayData inputData, int timeOut = 30)
If the money in the business account is gone, the following prompt will appear:
I believe you have mastered the method after reading the case in this article, please come for more exciting information Pay attention to other related articles on php Chinese website!
Recommended reading:
Usage of webpack automatic refresh and parsing Use of H5 cache ManifestThe above is the detailed content of Activation of enterprise transfer to user interface. For more information, please follow other related articles on the PHP Chinese website!