public override BaseResponse Execute()
        {
            logger.Info("Executing...");
            ParamsVerification();
            BaseResponse response = null;
            SortedDictionary <string, string> paras = new SortedDictionary <string, string>();
            Type type = this.GetType();

            PropertyInfo[] properties = type.GetProperties();
            if (properties != null)
            {
                for (int i = 0; i < properties.Length; i++)
                {
                    string key = properties[i].Name;
                    if (key == "appid")
                    {
                        key = "mch_appid";
                    }
                    if (key == "mch_id")
                    {
                        key = "mchid";
                    }
                    if (key == "body" || key == "sign_type")
                    {
                        continue;
                    }
                    object value = properties[i].GetValue(this);

                    paras.Add(key, (value != null ? value.ToString() : ""));
                }
            }
            string sign = null;

            sign = HashWrapper.MD5_Hash(paras, this.shop_secret);
            logger.Info("sign:" + sign);
            paras.Add("sign", sign);
            NameValueCollection col = new NameValueCollection();

            foreach (KeyValuePair <string, string> param in paras)
            {
                if (param.Value != null && !string.IsNullOrEmpty(param.Value.ToString()))
                {
                    col.Add(param.Key, param.Value);
                }
            }
            string str = null;

            if (needCert)
            {
                str = HttpSercice.PostHttpRequest(this.url, col, RequestType.POST, "text/xml", true, config);
            }
            else
            {
                str = HttpSercice.PostHttpRequest(this.url, col, RequestType.POST, "text/xml");
            }
            logger.Info("response:" + str);
            response = ParseXML(str);
            logger.Info("Done.");
            return(response);
        }
        public override BaseResponse Execute()
        {
            if (string.IsNullOrEmpty(appid))
            {
                throw new Exception("invalid appid");
            }

            if (string.IsNullOrEmpty(secret))
            {
                throw new Exception("invalid secret");
            }

            AccessTokenResponse tokenRes = null;
            NameValueCollection col      = new NameValueCollection();

            col.Add("grant_type", "client_credential");
            col.Add("appid", this.appid);
            col.Add("secret", this.secret);
            DateTime now = DateTime.Now;
            string   res = HttpSercice.PostHttpRequest(this.url, col, RequestType.GET, null);

            if (!string.IsNullOrEmpty(res))
            {
                JObject json = null;
                try
                {
                    json = JObject.Parse(res);
                    if (json != null)
                    {
                        tokenRes = new AccessTokenResponse();
                        AccessToken accessToken = new AccessToken();
                        accessToken.Access_Token = json["access_token"] != null? json["access_token"].ToString():"";
                        if (!string.IsNullOrEmpty(accessToken.Access_Token))
                        {
                            accessToken.ExpiresIn = json["expires_in"] != null?int.Parse(json["expires_in"].ToString()) : 0;
                        }
                        accessToken.ExpiresTime = now.AddSeconds(accessToken.ExpiresIn);
                        if (!string.IsNullOrEmpty(accessToken.Access_Token) && accessToken.ExpiresIn > 0)
                        {
                            tokenRes.Access_Token = accessToken;
                        }

                        if (json["errcode"] != null)
                        {
                            tokenRes.err_code = json["errcode"].ToString();
                        }
                        if (json["errmsg"] != null)
                        {
                            tokenRes.err_code_des = json["errmsg"].ToString();
                        }
                    }
                }
                catch
                {
                }
            }
            return(tokenRes);
        }
Esempio n. 3
0
        public override BaseResponse Execute()
        {
            if (string.IsNullOrEmpty(this.url))
            {
                logger.Error("invalid url");
                throw new Exception("invalid url");
            }
            if (string.IsNullOrEmpty(appid))
            {
                logger.Error("invalid appid");
                throw new Exception("invalid appid");
            }
            if (string.IsNullOrEmpty(secret))
            {
                logger.Error("invalid secret");
                throw new Exception("invalid secret");
            }

            if (string.IsNullOrEmpty(AccessToken))
            {
                logger.Error("invalid AccessToken");
                throw new Exception("invalid AccessToken");
            }
            if (string.IsNullOrEmpty(OpenId))
            {
                logger.Error("invalid OpenId");
                throw new Exception("invalid OpenId");
            }

            UserInfoResponse    userRes = null;
            NameValueCollection col     = new NameValueCollection();

            col.Add("access_token", this.AccessToken);
            col.Add("openid", this.OpenId);
            string res = HttpSercice.PostHttpRequest(this.url, col, RequestType.GET, null);

            if (string.IsNullOrEmpty(res))
            {
                logger.Error("Nothing returned by wechat");
                return(null);
            }
            logger.Info(string.Format("UserInfoRequest response:{0}", res));
            try
            {
                JObject json = JObject.Parse(res);
                if (json != null)
                {
                    userRes = new UserInfoResponse(json);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(userRes);
        }
Esempio n. 4
0
        public override BaseResponse Execute()
        {
            PayOrderQueryResponse             response = null;
            SortedDictionary <string, object> paras    = new SortedDictionary <string, object>();
            Type type = this.GetType();

            PropertyInfo[] properties = type.GetProperties();
            if (properties != null)
            {
                for (int i = 0; i < properties.Length; i++)
                {
                    string key   = properties[i].Name;
                    object value = properties[i].GetValue(this);
                    paras.Add(key, value);
                }
            }
            string paraUrl = string.Empty;

            foreach (KeyValuePair <string, object> param in paras)
            {
                if (paraUrl == string.Empty)
                {
                    paraUrl = param.Key + "=" + (param.Value != null ? param.Value.ToString() : "");
                }
                else
                {
                    paraUrl += "&" + param.Key + "=" + (param.Value != null ? param.Value.ToString() : "");
                }
            }
            paraUrl += "&key=" + this.secret;
            string sign = null;

            if (signType.Trim().ToLower() == "md5")
            {
                sign = HashWrapper.MD5_Hash(paraUrl);
            }
            paras.Add("sign", sign);
            NameValueCollection col = new NameValueCollection();

            foreach (KeyValuePair <string, object> param in paras)
            {
                col.Add(param.Key, param.Value != null ? param.Value.ToString() : "");
            }
            string str = HttpSercice.PostHttpRequest(this.url, col, RequestType.POST, "xml");

            response = ParseXML(str);
            return(response);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns JSAPITicketResponse
        /// </summary>
        /// <returns>JSAPITicketResponse</returns>
        public override BaseResponse Execute()
        {
            JSAPITicketResponse ticketRes = null;

            if (Access_Token == null)
            {
                throw new Exception("Please input the valid accesstoken instance");
            }

            if (string.IsNullOrEmpty(Access_Token.Access_Token))
            {
                throw new Exception("invalid access token.");
            }

            if (DateTime.Now > Access_Token.ExpiresTime)
            {
                throw new Exception("access token is expired");
            }
            DateTime            now = DateTime.Now;
            NameValueCollection col = new NameValueCollection();

            col.Add("access_token", this.Access_Token.Access_Token);
            col.Add("type", "jsapi");
            string res = HttpSercice.PostHttpRequest(this.url, col, RequestType.GET, null);

            if (!string.IsNullOrEmpty(res))
            {
                JObject json = null;
                try
                {
                    json = JObject.Parse(res);
                    if (json != null)
                    {
                        ticketRes = new JSAPITicketResponse();
                        JSAPITicket ticket = new JSAPITicket();
                        if (json["ticket"] != null)
                        {
                            ticket.Ticket = json["ticket"].ToString();
                        }

                        if (json["expires_in"] != null)
                        {
                            ticket.ExipresIn   = int.Parse(json["expires_in"].ToString());
                            ticket.ExpiresTime = now.AddSeconds(ticket.ExipresIn);
                        }
                        if (json["errcode"] != null)
                        {
                            ticketRes.err_code = json["errcode"].ToString();
                        }
                        if (json["errmsg"] != null)
                        {
                            ticketRes.err_code_des = json["errmsg"].ToString();
                        }
                        ticketRes.Ticket = ticket;
                    }
                }
                catch
                {
                }
            }
            return(ticketRes);
        }
Esempio n. 6
0
        public override BaseResponse Execute()
        {
            if (string.IsNullOrEmpty(appid))
            {
                logger.Error("invalid appid");
                throw new Exception("invalid appid");
            }
            //logger.Info(string.Format("appid:{0}", this.appid));
            if (string.IsNullOrEmpty(secret))
            {
                logger.Error("invalid secret");
                throw new Exception("invalid secret");
            }
            //logger.Info(string.Format("app secret:{0}",this.secret));
            if (string.IsNullOrEmpty(code))
            {
                logger.Error("invalid authorization_code");
                throw new Exception("invalid authorization_code");
            }
            //logger.Info(string.Format("authorization_code:{0}", this.code));
            AccessTokenResponse tokenRes = null;
            NameValueCollection col      = new NameValueCollection();

            col.Add("appid", this.appid);
            col.Add("secret", this.secret);
            col.Add("code", this.code);
            col.Add("grant_type", "authorization_code");
            DateTime now = DateTime.Now;
            string   res = HttpSercice.PostHttpRequest(this.url, col, RequestType.GET, null);

            if (string.IsNullOrEmpty(res))
            {
                return(null);
            }
            logger.Info(string.Format("AccessTokenRequest response:{0}", res));
            try
            {
                JObject json = null;
                json = JObject.Parse(res);
                if (json != null)
                {
                    tokenRes = new AccessTokenResponse();
                    AccessToken accessToken = new AccessToken();
                    accessToken.Access_Token = json["access_token"] != null ? json["access_token"].ToString() : "";
                    if (!string.IsNullOrEmpty(accessToken.Access_Token))
                    {
                        accessToken.ExpiresIn = json["expires_in"] != null?int.Parse(json["expires_in"].ToString()) : 0;
                    }
                    accessToken.ExpiresTime = now.AddSeconds(accessToken.ExpiresIn);
                    if (!string.IsNullOrEmpty(accessToken.Access_Token) && accessToken.ExpiresIn > 0)
                    {
                        tokenRes.Access_Token = accessToken;
                    }
                    if (json["openid"] != null)
                    {
                        tokenRes.openid    = json["openid"].ToString();
                        accessToken.OpenId = tokenRes.openid;
                    }
                    if (json["scope"] != null)
                    {
                        accessToken.Scope = json["scope"].ToString();
                    }
                    if (json["errcode"] != null)
                    {
                        tokenRes.err_code = json["errcode"].ToString();
                    }
                    if (json["errmsg"] != null)
                    {
                        tokenRes.err_code_des = json["errmsg"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            if (tokenRes != null && !string.IsNullOrEmpty(tokenRes.err_code))
            {
                logger.Error(string.Format("Error code:{0}, Error message:{1}", tokenRes.err_code, tokenRes.err_code_des != null?tokenRes.err_code_des:""));
            }
            logger.Info("AccessTokenRequest completed.");
            return(tokenRes);
        }
Esempio n. 7
0
        public override BaseResponse Execute()
        {
            logger.Info(this.GetType().FullName + "................");
            logger.Info("url:" + this.url);
            if (string.IsNullOrEmpty(out_trade_no))
            {
                logger.Info("out_trade_no cannot be empty");
                throw new Exception("out_trade_no cannot be empty");
            }
            if (string.IsNullOrEmpty(notify_url))
            {
                logger.Info("notify_url cannot be empty");
                throw new Exception("notify_url cannot be empty");
            }
            if (string.IsNullOrEmpty(spbill_create_ip))
            {
                logger.Info("spbill_create_ip cannot be empty");
                throw new Exception("spbill_create_ip cannot be empty");
            }
            PreOrderResponse response = null;
            SortedDictionary <string, object> paras = new SortedDictionary <string, object>();
            Type type = this.GetType();

            PropertyInfo[] properties = type.GetProperties();
            if (properties != null)
            {
                for (int i = 0; i < properties.Length; i++)
                {
                    string key   = properties[i].Name;
                    object value = properties[i].GetValue(this);
                    paras.Add(key, value);
                }
            }
            string paraUrl = string.Empty;

            foreach (KeyValuePair <string, object> param in paras)
            {
                if (param.Value != null && !string.IsNullOrEmpty(param.Value.ToString()))
                {
                    if (paraUrl == string.Empty)
                    {
                        paraUrl = param.Key + "=" + (param.Value != null ? param.Value.ToString() : "");
                    }
                    else
                    {
                        paraUrl += "&" + param.Key + "=" + (param.Value != null ? param.Value.ToString() : "");
                    }
                }
            }
            paraUrl += "&key=" + this.shop_secret;
            logger.Info("parameters:" + paraUrl);
            string sign = null;

            if (sign_type.Trim().ToLower() == "md5")
            {
                sign = HashWrapper.MD5_Hash(paraUrl);
            }
            sign = sign.ToUpper();
            logger.Info("sign:" + sign);
            paras.Add("sign", sign);
            NameValueCollection col = new NameValueCollection();

            foreach (KeyValuePair <string, object> param in paras)
            {
                if (param.Value != null && !string.IsNullOrEmpty(param.Value.ToString()))
                {
                    col.Add(param.Key, param.Value.ToString());
                }
            }
            string str = HttpSercice.PostHttpRequest(this.url, col, RequestType.POST, "text/xml");

            logger.Info("response:" + str);
            response = ParseXML(str);
            logger.Info("Done.");
            return(response);
        }