Exemplo n.º 1
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        private static string GetAccessToken()
        {
            var appId             = BLLConfig.Get("wx_appid");
            var appSecret         = BLLConfig.Get("wx_appsecret");
            var accessToken       = BLLConfig.Get("wx_access_token");
            var accessTokenExpire = BLLConfig.Get("wx_access_token_expire");

            if ((!string.IsNullOrEmpty(accessTokenExpire)) && (!string.IsNullOrEmpty(accessToken)) && DateTime.Now < DateTime.Parse(accessTokenExpire))
            {
                return(accessToken);
            }
            string        parm          = string.Format("grant_type=client_credential&appid={0}&secret={1}", appId, appSecret);
            string        result        = RequestUtil.Get("https://api.weixin.qq.com/cgi-bin/token?" + parm);
            WXAccessToken wxAccessToken = JSONHelper.JsonToObject <WXAccessToken>(result);

            if (wxAccessToken.errcode != 0)
            {
                return(string.Empty);
            }
            using (Entities db = new Entities())
            {
                db.ht_sys_config.FirstOrDefault(p => p.xkey == "wx_access_token").xvalue        = wxAccessToken.access_token;
                db.ht_sys_config.FirstOrDefault(p => p.xkey == "wx_access_token_expire").xvalue = DateTime.Now.AddSeconds(wxAccessToken.expires_in - 60).ToString();
                if (db.SaveChanges() > 0)
                {
                }
                else
                {
                }
            }
            return(wxAccessToken.access_token);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 微信支付
        /// </summary>
        /// <param name="orderId">订单号</param>
        /// <param name="totalAmount">订单金额</param>
        /// <param name="openId">用户OpenId</param>
        /// <param name="ip">用户IP</param>
        /// <param name="notiUrl">通知Url</param>
        /// <param name="body">订单内容</param>
        /// <param name="tradeType">交易类型</param>
        /// <returns></returns>
        public static WXPayRequest WXPay(string orderId, decimal totalAmount, string openId, string ip, string notiUrl, out bool isSuccess, string body = "", string tradeType = "")
        {
            string appId  = BLLConfig.Get("wx_appid");
            string mchId  = BLLConfig.Get("wx_mchid");
            string mchKey = BLLConfig.Get("wx_mchsecret");

            return(WXApi.GetBrandWcPayRequest(orderId, totalAmount, appId, mchId, mchKey, openId, ip, notiUrl, out isSuccess, body, tradeType));
        }