Пример #1
0
        public async Task <string> GetPublicKeyAsync()
        {
            string      url         = "https://fraud.mch.weixin.qq.com/risk/getpublickey";
            XmlDocument xmlDocument = new XmlDocument();

            var root      = xmlDocument.CreateElement("xml");
            var idElement = xmlDocument.CreateElement("mch_id");

            idElement.InnerText = _mchId;
            var nonceElement = xmlDocument.CreateElement("nonce_str");

            nonceElement.InnerText = Helper.GetNonceStr(32);
            var signTypeElement = xmlDocument.CreateElement("sign_type");

            signTypeElement.InnerText = "MD5";
            root.AppendChild(idElement);
            root.AppendChild(nonceElement);
            root.AppendChild(signTypeElement);
            xmlDocument.AppendChild(root);

            string sign = WeChatSignHelper.CreateMd5SignByXml(xmlDocument, _secretKey);

            var signElement = xmlDocument.CreateElement("sign");

            signElement.InnerText = sign;
            root.AppendChild(signElement);
            string body = XmlSerializeHelper.XmlToString(xmlDocument);

            var result =
                await _client.ExecutePostRequest(url, new Dictionary <string, string>(), body, _path, _password,
                                                 "xml/text");

            Check(XmlSerializeHelper.StringToObject <PayResponse>(result));

            XmlDocument res = new XmlDocument();

            res.LoadXml(result);


            return(res.FirstChild.SelectSingleNode("pub_key").InnerText);
        }