返回顶部

收藏

微信移动支付V3开发详细教程服务端采用.net mvc webapi(C#)

更多
/// <summary>
        /// 生成微信支付签名,生成结果直接在可以端提交即可
        /// </summary>
        /// <returns></returns>
        public static string partnerid = ""; //PartnerID
        public static string partnerkey = ""; //PartnerKey
        public static string mchid = ""; //mchid
        public static string appId = ""; //appid
        public static string appsecret = ""; //appsecret
        public static string appkey = ""; //paysignkey(非appkey 在微信商户平台设置 (md5)111111111111) 
        public static string timeStamp = ""; //时间戳
        public static string nonceStr = ""; //随机字符串
        public static string notify_url = "http://api.kwstu.com/Pay/WxNotifyUrl"; //支付完成后的回调处理页面,*替换成notify_url.asp所在路径
        public static string prepayId = ""; //预支付ID
        public static string sign = "";     //为了获取预支付ID的签名
        public static string paySign = "";  //进行支付需要的签名
        public static string package = "";  //进行支付需要的包
        public HttpResponseMessage PostWxConfirmPay()
        {
                    //生成订单业务逻辑代码

                    //创建支付应答对象
                    var packageReqHandler = new RequestHandler();
                    //初始化
                    packageReqHandler.init();
                    timeStamp = TenpayUtil.getTimestamp();
                    nonceStr = TenpayUtil.getNoncestr();
                    //设置package订单参数
                    packageReqHandler.setParameter("appid", appId);
                    packageReqHandler.setParameter("body", unitBase.NAME + "于" + DateTime.Now.ToString("yyyy-MM-dd") + "在线预订“" + zphBase.ZPHNAME + "”的" + zphZwService.ZWID + "号展位,缴费" + price + "元。"); //商品信息 127字符
                    packageReqHandler.setParameter("mch_id", mchid);
                    packageReqHandler.setParameter("nonce_str", nonceStr.ToLower());
                    packageReqHandler.setParameter("notify_url", notify_url);
                    packageReqHandler.setParameter("out_trade_no", checkId); //商家订单号
                    packageReqHandler.setParameter("spbill_create_ip", HttpContext.Current.Request.UserHostAddress); //用户的公网ip,不是商户服务器IPHttpContext.Current.Request.UserHostAddress
                    packageReqHandler.setParameter("total_fee", (int.Parse(price)*100).ToString()); //商品金额,以分为单位(money * 100).ToString()
                    packageReqHandler.setParameter("trade_type", "APP");
                    //获取package包
                    sign = packageReqHandler.CreateMd5Sign("key", appkey);
                    packageReqHandler.setParameter("sign", sign);
                    string data = packageReqHandler.parseXML();
                    string prepayXml = HttpUtil.Send(data, "https://api.mch.weixin.qq.com/pay/unifiedorder");
                    //获取预支付ID
                    var xdoc = new XmlDocument();
                    xdoc.LoadXml(prepayXml);
                    XmlNode xn = xdoc.SelectSingleNode("xml");
                    XmlNodeList xnl = xn.ChildNodes;
                    if (xnl.Count > 7)
                    {
                        prepayId = xnl[7].InnerText;
                        package = string.Format("prepay_id={0}", prepayId);
                    }
                    //设置支付参数
                    var paySignReqHandler = new RequestHandler();
                    paySignReqHandler.setParameter("appid", appId);
                    paySignReqHandler.setParameter("noncestr", nonceStr.ToLower());
                    paySignReqHandler.setParameter("package", "Sign=WXPay");
                    paySignReqHandler.setParameter("partnerid", partnerid);
                    paySignReqHandler.setParameter("prepayid", prepayId);
                    paySignReqHandler.setParameter("timestamp", timeStamp);
                    paySign = paySignReqHandler.CreateMd5Sign("key", appkey);
                    var reList = new List<WxPayListModel>();
                    var reModel = new WxPayListModel();
                    reModel.appid = appId;
                    reModel.noncestr = nonceStr.ToLower();
                    reModel.package = "Sign=WXPay";
                    reModel.partnerid = partnerid;
                    reModel.prepayid = prepayId;
                    reModel.timestamp = timeStamp;
                    reModel.sign = paySign;
                    reList.Add(reModel);

        }

相关资料获取地址:https://item.taobao.com/item.htm?spm=686.1000925.0.0.AlZkml&amp;id=524281028471
原文地址:http://www.kwstu.com/ArticleView/netmvc_201511132050268716

标签:lua

收藏

0人收藏

支持

0

反对

0

发表评论