Ejemplo n.º 1
0
        /// <summary>
        /// 根据返回的XML创建WeXinPayAPIResponseEntity
        /// </summary>
        /// <param name="orderInfoXmlStr">订单XML</param>
        /// <returns></returns>
        public static WeXinPayAPIResponseEntity Create(string orderInfoXmlStr)
        {
            WeXinPayAPIResponseEntity entity = null;

            if (string.IsNullOrEmpty(orderInfoXmlStr) == false)
            {
                //加载返回的XML
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(orderInfoXmlStr);
                XmlElement root = doc.DocumentElement;

                //获取WeXinPayAPIResponseEntity所有的属性
                var props    = typeof(WeXinPayAPIResponseEntity).GetProperties();
                var propDict = new Dictionary <string, System.Reflection.PropertyInfo>();
                foreach (var pop in props)
                {
                    propDict.Add(pop.Name, pop);
                }

                entity = new WeXinPayAPIResponseEntity();
                //遍历设置WeXinPayAPIResponseEntity的值
                foreach (XmlElement cNode in root.ChildNodes)
                {
                    string nodeName = cNode.Name;
                    if (propDict.ContainsKey(nodeName) == true)
                    {
                        propDict[nodeName].SetValue(entity, cNode.InnerText, null);
                    }
                }
            }
            return(entity);
        }
Ejemplo n.º 2
0
        //调用支付接口
        private void Pay(WeXinPayAPIResponseEntity resultEntity)
        {
            if (resultEntity.return_code == "SUCCESS")
            {
                if (resultEntity.result_code == "SUCCESS")
                {
                    //参数说明详见 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12
                    Smobiler.Plugins.WeiXinPayEntity appPayEntity = new Smobiler.Plugins.WeiXinPayEntity()
                    {
                        nonceStr  = resultEntity.nonce_str,
                        package   = "Sign=WXPay",
                        partnerId = resultEntity.mch_id,
                        prepayId  = resultEntity.prepay_id,
                        timeStamp = ((int)((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000)).ToString()
                    };

                    //按签名规范重新生成签名,注意package的值格式为Sign=WXPay
                    //https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3
                    string signTempStr = string.Format("appid={0}&noncestr={1}&package={2}&partnerid={3}&prepayid={4}&timestamp={5}&key={6}", resultEntity.appid, appPayEntity.nonceStr, appPayEntity.package, appPayEntity.partnerId, appPayEntity.prepayId, appPayEntity.timeStamp, key);
                    var    md5         = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    appPayEntity.sign = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(signTempStr))).Replace("-", "");

                    //调用支付功能
                    this.weiXin1.pay(appPayEntity, (obj, args) =>
                    {
                        if (args.isError == true)
                        {
                            MessageBox.Show(string.Format("APP Pay Error: {0}", args.error));
                        }
                        else
                        {
                            //如果没有返回错误,代表支付成功,那么这里直接去查询订单状态
                            //一定要以具体查询的结果为准,不要以APP的返回状态为准
                            //https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_2
                            Toast("支付成功!");
                        }
                    });
                }
                else
                {
                    throw new Exception("WeXinPayAPIResponseError " + resultEntity.err_code + "/" + resultEntity.err_code_des);
                }
            }
            else
            {
                throw new Exception("WeXinPayAPIResponseError " + resultEntity.return_msg);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
        /// 调起支付时,需要先调用微信接口生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付
        /// </summary>
        private WeXinPayAPIResponseEntity UnifiedOrder()
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(unifieOrderUrl);

            myHttpWebRequest.Method      = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            SortedDictionary <string, string> payDict = new SortedDictionary <string, string>();

            payDict.Add("appid", appid);                                                //应用ID
            payDict.Add("mch_id", mch_id);                                              //商户号
            payDict.Add("device_info", this.Client.DeviceID);                           //设备号
            payDict.Add("nonce_str", System.Guid.NewGuid().ToString("N"));              //随机字符串
            payDict.Add("body", "请支持0.01元");                                            //商品描述
            payDict.Add("out_trade_no", out_trade_no);                                  //外部订单编号
            payDict.Add("total_fee", "1");                                              //金额,单位是分,100代表为1元
            payDict.Add("spbill_create_ip", this.Client.IPEndPoint.Address.ToString()); //获取客户端的IP地址
            payDict.Add("notify_url", "http://www.weixin.qq.com/wxpay/pay.php");        //由于notify_url为必须的字段,如果有通知地址就填写一个,如果没有的话,可随意填写一个
            payDict.Add("trade_type", "APP");                                           //这里固定为APP
            string payXmlStr = CreateRequestXML(payDict);

            byte[] encodedBytes = Encoding.UTF8.GetBytes(payXmlStr);
            myHttpWebRequest.ContentLength = encodedBytes.Length;
            Stream requestStream = myHttpWebRequest.GetRequestStream();

            requestStream.Write(encodedBytes, 0, encodedBytes.Length);
            requestStream.Close();

            //获取结果
            HttpWebResponse result    = (HttpWebResponse)myHttpWebRequest.GetResponse();
            string          resultStr = "";

            if (result.StatusCode == HttpStatusCode.OK)
            {
                using (Stream mystream = result.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(mystream))
                    {
                        resultStr = reader.ReadToEnd();
                    }
                }
            }

            return(WeXinPayAPIResponseEntity.Create(resultStr));
        }