Activation of enterprise transfer to user interface

php中世界最好的语言
Release: 2018-03-16 13:09:35
Original
4428 people have browsed it

This time I will bring you the activation of the enterprise transfer to user interface. What are the precautions 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 to

transfer money to the user, or allow the user to withdraw cash or send red 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

The certificate is downloaded in the Account Center-API Security. Now you need the mobile phone

verification code

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

The demo currently provided by WeChat does not include this part. Let’s make some modifications based on the official demo. Similar to the previous example, we all need to use the WxPayData object to manipulate our parameters. Define a TransfersPay object.

  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;
        }
    }
Copy after login

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;
        }
Copy after login

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");
                }
Copy after login

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());
        }
Copy after login

Get the result

In this way, the transfer/withdrawal function is realized.

Release

In a formal environment, we need to create our own order first, then request a transfer to WeChat, and process our order after success. CashTransfers method slightly adjusted.

       [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()));            
        }
Copy after login

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)
Copy after login

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 Manifest


The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!