WeChat payment for WeChat public account development

高洛峰
Release: 2017-02-22 16:02:59
Original
2049 people have browsed it

A few days ago, due to company project requirements, I wanted to create a function for online recharge using WeChat. This is to click on a web page in the WeChat browser to call up WeChat payment. Now let’s briefly talk about WeChat payment public account payment ’s development process:

First of all, your official account must be a certification service account, and must have the permission for WeChat payment; before developing and writing the code, we must make some payment-related The information is set up to facilitate subsequent operations. After is activated, WeChat will send you an email, which contains some information related to the background login of your official account merchant platform. After logging in to the merchant platform, go to -> ;Account Settings->API SecuritySet the key in it, which will be used later;

微信公众号开发之微信支付

##Account parameter description


Parameters in the emailAPI parameter nameDetailed descriptionAPPIDappidappid is the unique identifier of the WeChat public account or open platform APP. Apply for a public account on the public platform or in After applying for an APP account on the open platform, WeChat will automatically assign the corresponding appid to identify the application. This field value will also be included in the merchant's WeChat Payment approval email. WeChat Payment Merchant IDmch_idThe merchant’s payment account assigned by WeChat Pay after the merchant applies for WeChat Pay. API keykeyThe signature key generated during the transaction process is only retained in the merchant system and WeChat payment background, and will not be stored on the network spread in. The merchant must keep the key properly and do not transmit it on the network or store it in other clients to ensure that the key will not be leaked. Merchants can log in to the WeChat merchant platform according to the email prompts to set up. AppsecretsecretAppSecret is the interface password corresponding to the APPID, which is used to obtain the interface call credential access_token. In WeChat payment, the user's openid is first obtained through the OAuth2.0 interface. This openid is used by the order interface in the web payment mode within WeChat. Obtain the AppSecret in development mode (become a developer and the account has no abnormal status).


After completing these, we still need to understand a business process of official account payment:

微信公众号开发之微信支付

##Main interactions between merchant system and WeChat payment system:

1. The merchant server calls the unified ordering interface to request an order. For the API, please refer to the public api [Unified Ordering API]; before requesting a prepaid order, we need to call the WeChat OAuth2.0 web page to authorize the user to obtain the user's WeChat OpenId. This will not be explained in detail here. The following is the code implementation for prepaid orders:

string timeStamp = TenPayUtil.GetTimestamp();
                string nonceStr = TenPayUtil.GetNoncestr();
                string paySign = string.Empty;

                //创建支付应答对象
                var packageReqHandler = new RequestHandler(null);

                string spbill_create_ip = Request.UserHostAddress;

                //初始化
                //packageReqHandler.Init();
                //packageReqHandler.SetKey(TenPayInfo.Key);
                //设置package订单参数
                packageReqHandler.SetParameter("appid", appID); //公众账号ID
                packageReqHandler.SetParameter("body", StrUtil.GetCutString(productName, 100)); //不能超过127个字符
                packageReqHandler.SetParameter("mch_id", mchid); //商户号
                packageReqHandler.SetParameter("nonce_str", nonceStr.ToLower()); //随机字符串
                packageReqHandler.SetParameter("notify_url", notifyUrl); //接收财付通通知的URL
                packageReqHandler.SetParameter("openid", openId); //openid
                packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号
                // packageReqHandler.SetParameter("attach", "");          //附加数据  未来可用于区分不同微信支付业务
                packageReqHandler.SetParameter("spbill_create_ip", spbill_create_ip); //用户的公网ip,不是商户服务器IP
                packageReqHandler.SetParameter("total_fee", (onlinePayMoney * 100).ToString("0")); //商品金额,以分为单位(money * 100).ToString()
                packageReqHandler.SetParameter("trade_type", "JSAPI"); //交易类型

                //获取package包
                string sign = packageReqHandler.CreateMd5Sign("key", TenPayInfo.Key);
                packageReqHandler.SetParameter("sign", sign); //交易类型
                string data = packageReqHandler.ParseXML();
                LoggerHelper.Log(data);
                
                //调用统一下单接口请求订单
                var result = TenPayV3Service.Unifiedorder(data);
                LoggerHelper.Log(result);

                var res = XDocument.Parse(result);

                string prepayId = string.Empty;
                if (res.Element("xml").Element("return_code").Value == "SUCCESS")
                {
                    prepayId = res.Element("xml").Element("prepay_id").Value;
                }

                string package = string.Format("prepay_id={0}", prepayId);
                timeStamp = TenPayUtil.GetTimestamp();

                //设置支付参数
                var paySignReqHandler = new RequestHandler(null);
                paySignReqHandler.SetParameter("appId", appID);
                paySignReqHandler.SetParameter("timeStamp", timeStamp);
                paySignReqHandler.SetParameter("nonceStr", nonceStr);
                paySignReqHandler.SetParameter("package", package);
                paySignReqHandler.SetParameter("signType", "MD5");
                paySign = paySignReqHandler.CreateMd5Sign("key", TenPayInfo.Key);

                //将信息传递给支付页面
                ViewBag.appId = appID;
                ViewBag.timeStamp = timeStamp;
                ViewBag.nonceStr = nonceStr;
                ViewBag.package = package;
                ViewBag.paySign = paySign;
Copy after login


##The following is the page js related code:

<script type="text/javascript">        // 当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。
        document.addEventListener(&#39;WeixinJSBridgeReady&#39;, function onBridgeReady() {
            $(function () {                //公众号支付
                jQuery(&#39;#getBrandWCPayRequest&#39;).click(function (e) {
                    WeixinJSBridge.invoke(&#39;getBrandWCPayRequest&#39;, {                        "appId": "@ViewBag.appId", //公众号名称
                        "timeStamp": "@ViewBag.timeStamp", //时间戳
                        "nonceStr": "@ViewBag.nonceStr", //随机串
                        "package": "@Html.Raw(ViewBag.package.ToString())",//扩展包
                        "signType": "MD5", //微信签名方式
                        "paySign": "@ViewBag.paySign" //微信签名                    }, function (res) {                        if (res.err_msg == "get_brand_wcpay_request:ok") {                            //alert("微信支付成功!");
                            window.location.href = "@WxPaySettingConfig.WmallURL/Wmall/TradePay/Success/@ViewBag.ShopId/?orderNo=@orderNoMark";
                        }                        else if (res.err_msg == "get_brand_wcpay_request:cancel") {                            //alert("用户取消支付!");                        }                        else {
                            window.location.href = "/wxpay/jsapi/error/?isPayFail=1&csid=@ViewBag.ShopId&orderNo=@orderNoMark&biztype=1";    
                        }                        // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。                        //因此微信团队建议,当收到ok返回时,向商户后台询问是否收到交易成功的通知,若收到通知,前端展示交易成功的界面;若此时未收到通知,商户后台主动调用查询订单接口,查询订单的当前状态,并反馈给前端展示相应的界面。                    });
                });
            });            //WeixinJSBridge.log(&#39;yo~ ready.&#39;);
        }, false);    </script>
Copy after login



2. The merchant server receives the payment notification. For the API, please refer to the public API [

Payment Result Notification API

[HttpPost]
        public void NoticeUrl()
        {
            string xmlString = HttpClientHelper.GetPostString(Request);  5 
            //此处应记录日志
            LoggerHelper.Log(string.Format("【微支付】异步通知参数:{0}", xmlString));  8 
            var returnMsg = new ReturnMessage() { Return_Code = "SUCCESS", Return_Msg = string.Empty };
            //通知消息实体
            NotifyMessage message = null;
            //订单处理相关的方法内全局变量
            bool isNeedDeal = false; //标识订单是否需要处理
            string orderNo = string.Empty; //订单编号 (需要根据商家数据包字段判断所属订单)
            CorpSalesOrder saleOrder = null;
            try
            {
                message = HttpClientHelper.XmlDeserialize<NotifyMessage>(xmlString);
 
                //订单号 获得
                orderNo = message.Out_Trade_No;
                if (string.IsNullOrEmpty(orderNo))
                {
                    throw new InvalidOperationException("未找到该订单信息.");
                } 45                 var doc = new XmlDocument();
                doc.LoadXml(xmlString);
                var dic = new Dictionary<string, string>();
                string sign = string.Empty;
                foreach (XmlNode node in doc.FirstChild.ChildNodes)
                {
                    if (node.Name.ToLower() != "sign")
                        dic.Add(node.Name, node.InnerText);
                    else
                        sign = node.InnerText;
                }
                UnifiedWxPayModel model = UnifiedWxPayModel.CreateUnifiedModel(xddAppId, xddMchid, xddWxkey);
                if (model.ValidateMD5Signature(dic, sign))
                {
                    //处理通知 业务逻辑:
                    if (message.Return_Code == "SUCCESS")
                    {
                        if (message.Result_Code == "SUCCESS")
                        {
                   //此处处理支付成功后的业务逻辑
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("{0}:{1}", message.Err_Code, message.Err_Code_Des));
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(message.Return_Msg);
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                //此处记录异常日志
                returnMsg.Return_Code = "FAIL";
                returnMsg.Return_Msg = e.Message;
                LoggerHelper.Log("【微信支付异步通知】出错,订单编号:" + orderNo + ",错误原因:" + e.Message);
            }
            catch (Exception e)
            {
                //此处记录异常日志
                returnMsg.Return_Code = "FAIL";
                returnMsg.Return_Msg = e.Message;
                LoggerHelper.Log("【微信支付异步通知】出错,订单编号:" + orderNo + ",错误原因:" + (e.InnerException == null ? e.Message : e.InnerException.ToString()));
            }
            Response.Write(returnMsg.ToXmlString());
            Response.End();
        }
Copy after login
For more WeChat payment related articles on WeChat public account development, please pay attention to 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