示例#1
0
        public static string Pay(string orderNumber, int totalFee, string openid)
        {
            //读取配置文件
            var merchantId = string.Empty;
            var appId      = string.Empty;
            var key        = string.Empty;
            var appSecret  = string.Empty;
            //接收微信支付异步通知回调地址
            var notifyUrl = string.Empty;
            var payUrl    = string.Empty;

            var xd = new XmlDocument();

            xd.Load(HttpContext.Current.Server.MapPath("/app_data/xml/payment.xml"));
            var selectSingleNode = xd.SelectSingleNode("paymentMethod");

            if (selectSingleNode != null)
            {
                var xnl = selectSingleNode.ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    var xe = (XmlElement)xn;
                    if (xe.GetAttribute("id") != "wechat")
                    {
                        continue;
                    }
                    merchantId = xe.GetAttribute("merchant_id");
                    appId      = xe.GetAttribute("app_id");
                    appSecret  = xe.GetAttribute("app_secret");
                    key        = xe.GetAttribute("key");
                    notifyUrl  = xe.GetAttribute("notify_url");
                    payUrl     = xe.GetAttribute("pay_url") + orderNumber.ToString();
                    break;
                }
            }
            var timestamp = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetTimestamp();
            var nonceStr  = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetNoncestr();

            JsApiTicketContainer.Register(appId, appSecret);
            var jsapiTicket = JsApiTicketContainer.GetJsApiTicket(appId);
            var signConfig  = new Senparc.Weixin.MP.TenPayLib.RequestHandler(null);

            signConfig.SetParameter("jsapi_ticket", jsapiTicket);
            signConfig.SetParameter("noncestr", nonceStr);
            signConfig.SetParameter("timestamp", timestamp);
            signConfig.SetParameter("url", payUrl);
            var signature = signConfig.CreateSHA1Sign();
            //支付
            //商品或支付单简要描述
            var body = "Billion智能仪表管理系统";
            //商户系统内部的订单号,32个字符内,可包含字母,其他说明见商户订单号
            //APP和网页支付提交用户端IP,Native支付填调用微信支付API的机器IP
            var spbillCreateIp = HttpContext.Current.Request.ServerVariables.Get("Remote_Addr");

            //JSAPI,NATIVE,APP,WAP
            var trade_type = "JSAPI";
            //创建支付应答对象
            var panSigig = new RequestHandler(null);

            //初始化
            panSigig.Init();
            //设置package订单参数
            panSigig.SetParameter("appid", appId);
            panSigig.SetParameter("mch_id", merchantId);
            panSigig.SetParameter("nonce_str", nonceStr);
            panSigig.SetParameter("body", body);
            panSigig.SetParameter("out_trade_no", orderNumber);
            panSigig.SetParameter("total_fee", totalFee.ToString());
            panSigig.SetParameter("spbill_create_ip", spbillCreateIp);
            panSigig.SetParameter("notify_url", notifyUrl);
            panSigig.SetParameter("trade_type", trade_type);
            panSigig.SetParameter("openid", openid);

            var sign = panSigig.CreateMd5Sign("key", key);

            panSigig.SetParameter("sign", sign);

            var data = panSigig.ParseXML();

            var result = TenPayV3.Unifiedorder(data);

            var res      = System.Xml.Linq.XDocument.Parse(result);
            var xElement = res.Element("xml");

            if (xElement == null)
            {
                return("");
            }
            var element = xElement.Element("prepay_id");

            if (element == null)
            {
                return("");
            }
            var prepayId          = element.Value;
            var paysignReqHandler = new RequestHandler(null);

            paysignReqHandler.Init();

            //设置支付参数
            paysignReqHandler.SetParameter("appId", appId);
            paysignReqHandler.SetParameter("timeStamp", timestamp);
            paysignReqHandler.SetParameter("nonceStr", nonceStr);
            paysignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId));
            paysignReqHandler.SetParameter("signType", "MD5");
            var paysign = paysignReqHandler.CreateMd5Sign("key", key);

            paysignReqHandler.SetParameter("paysign", paysign);
            NVelocityHelper nh = new NVelocityHelper();

            nh.Put("app_id", appId);
            nh.Put("timestamp", timestamp);
            nh.Put("nonceStr", nonceStr);
            nh.Put("signature", signature);
            nh.Put("package", string.Format("prepay_id={0}", prepayId));
            nh.Put("signType", "MD5");
            nh.Put("paySign", paysign);
            nh.Put("orderid", orderNumber);
            return(nh.Write("app_data/template/wechat"));
        }