Esempio n. 1
0
        public async void PaySuccessNotificationTest()
        {
            var         config      = new Helper.WeChatConfig();
            string      xml         = @"<xml>
                <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
                <attach><![CDATA[支付测试]]></attach>
                <bank_type><![CDATA[CFT]]></bank_type>
                <fee_type><![CDATA[CNY]]></fee_type>
                <is_subscribe><![CDATA[Y]]></is_subscribe>
                <mch_id><![CDATA[10000100]]></mch_id>
                <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
                <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
                <out_trade_no><![CDATA[1409811653]]></out_trade_no>
                <result_code><![CDATA[SUCCESS]]></result_code>
                <return_code><![CDATA[SUCCESS]]></return_code>
                <err_code><![CDATA[SUCCESS]]></err_code>
                <err_code_des><![CDATA[SUCCESS]]></err_code_des>
                <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
                <time_end><![CDATA[20140903131540]]></time_end>
                <total_fee>1</total_fee>
                <coupon_fee><![CDATA[10]]></coupon_fee>
                <coupon_count><![CDATA[1]]></coupon_count>
                <coupon_type><![CDATA[CASH]]></coupon_type>
                <coupon_id><![CDATA[10000]]></coupon_id>
                <trade_type><![CDATA[JSAPI]]></trade_type>
                <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
                </xml>";
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(xml);
            var signNode = xmlDocument.FirstChild.SelectSingleNode("sign");

            xmlDocument.FirstChild.RemoveChild(signNode);
            string sign = WeChatSignHelper.CreateMd5SignByXml(xmlDocument, config.Value.MerchantSecret);

            xml = xml.Replace("B552ED6B279343CB493C5DD0D78AB241", sign);
            byte[] buffer = Encoding.UTF8.GetBytes(xml);
            Stream stream = new MemoryStream(buffer);

            await new PayNotificationHandler(config.Value.MerchantId, config.Value.MerchantSecret)
            .AddHandler(new UnifiedOrderNotificationHandlerMock())
            .ExecutedAsync(stream);
        }
Esempio n. 2
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);
        }