示例#1
0
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AppId) && !string.IsNullOrEmpty(AppKey) && !string.IsNullOrEmpty(MchId))
            {
                PayUtil.SaveLog("单次支付开始", "");
                PayUtil.SaveLog("传递支付参数", string.Format("OrderSN={0}、Body={1}、TotalFee={2}、Attach={3}、UserOpenId={4}", this.OrderSN, this.Body, this.TotalFee, this.Attach, this.UserOpenId));

                #region 基本参数===========================

                //设置package订单参数  具体参数列表请参考官方pdf文档,请勿随意设置
                Hashtable hsRequest = new Hashtable();
                hsRequest.Add("body", this.Body); //商品信息 127字符
                hsRequest.Add("appid", AppId);
                hsRequest.Add("mch_id", MchId);
                hsRequest.Add("nonce_str", NonceStr.ToLower());
                hsRequest.Add("notify_url", PayConfig.NotifyUrl);
                hsRequest.Add("openid", this.UserOpenId);
                hsRequest.Add("out_trade_no", this.OrderSN);                     //商家订单号
                hsRequest.Add("spbill_create_ip", Page.Request.UserHostAddress); //用户的公网ip,不是商户服务器IP
                hsRequest.Add("total_fee", this.TotalFee);                       //商品金额,以分为单位(money * 100).ToString()
                hsRequest.Add("trade_type", "JSAPI");
                if (!string.IsNullOrEmpty(this.Attach))
                {
                    hsRequest.Add("attach", this.Attach);//自定义参数 127字符
                }
                #endregion

                #region ===========生成签名==========
                PayUtil.CreateMd5Sign(hsRequest, AppKey, Request.ContentEncoding.BodyName);
                PayUtil.SaveLog("Pay页面Sign:", Sign);
                #endregion

                #region ===========获取package扩展包==========
                hsRequest.Add("sign", Sign);
                string data = PayUtil.ParseXML(hsRequest);
                PayUtil.SaveLog("Pay页面package(XML):", data);

                string prepayXml = PayUtil.Send(data, "https://api.mch.weixin.qq.com/pay/unifiedorder");
                PayUtil.SaveLog("Pay页面package(Back_XML):", prepayXml);

                //获取预支付ID
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(prepayXml);
                XmlNode     xn  = xmlDoc.SelectSingleNode("xml");
                XmlNodeList xnl = xn.ChildNodes;
                if (xnl.Count > 7)
                {
                    PrepayId = xnl[7].InnerText;
                    Package  = string.Format("prepay_id={0}", PrepayId);
                    PayUtil.SaveLog("Pay页面package:", Package);
                }
                #endregion

                #region =======生成【微信支付签名】=======
                Hashtable hsPaySign = new Hashtable();
                hsPaySign.Add("appId", AppId);
                hsPaySign.Add("timeStamp", TimeStamp);
                hsPaySign.Add("nonceStr", NonceStr);
                hsPaySign.Add("package", Package);
                hsPaySign.Add("signType", "MD5");
                PaySign = PayUtil.CreateMd5Sign(hsPaySign, AppKey, Request.ContentEncoding.BodyName);
                PayUtil.SaveLog("Pay页面paySign:", PaySign);
                #endregion
                //页面数据显示
                BindData();
            }
            else
            {
                Response.Write("参数不正确");
                Response.End();
            }
        }