/// <summary> /// Used by Wechat Mini program to decode userinfo /// </summary> /// <param name="code"></param> /// <returns>Json str contains openid and session_key</returns> public static string GetSeeionKey(WeChatPayConfig config, string code) { string result = null; if (config == null) { throw new WeChatException("Configuration cannot be empty."); } if (string.IsNullOrEmpty(config.MiniAppId)) { throw new WeChatException("Appid cannot be empty of wechat mini app"); } if (string.IsNullOrEmpty(config.MiniAppSecret)) { throw new WeChatException("AppSecret cannot be empty of wechat mini app"); } if (string.IsNullOrEmpty(config.GetSeeionKeyApi)) { throw new WeChatException("Get seeion key api url cannot be empty of wechat mini app"); } string reqUrl = config.GetSeeionKeyApi; if (!reqUrl.EndsWith("?")) { reqUrl += "?"; } reqUrl += "appid=" + config.MiniAppId + "&secret=" + config.MiniAppSecret + "&js_code=" + code + "&grant_type=authorization_code"; result = HttpSercice.PostHttpRequest(reqUrl, null, null, RequestType.GET, false, null); return(result); }
public virtual void Send() { try { logger.Info("Template Id:" + template_id); logger.Info("Access token:" + token.Access_Token); string reqUrl = this.apiUrl + token.Access_Token; logger.Info("Template message url:" + reqUrl); string toJson = ToJson(); if (string.IsNullOrEmpty(toJson)) { logger.Error("message cannot be null"); throw new WeChatException("message cannot be null"); } logger.Info(string.Format("Json string {0} has been sent to wechat", toJson)); string res = HttpSercice.PostHttpRequest(reqUrl, toJson, null, RequestType.POST, false, null); if (string.IsNullOrEmpty(res)) { logger.Error("no response from wechat"); } else { logger.Info(res); } } catch (WeChatException wex) { logger.Error(wex); } catch (Exception ex) { logger.Fatal(ex); } }
private void GetToken(Resrouce_interface api) { lock (telcom) { Logger.Info("Gettoken..."); if (DateTimeUtil.ConvertDateTimeToInt(DateTime.Now) < expiredTime) { return; } //Logger.Info("id:"+api.Username); //Logger.Info("AppSecret:" + api.AppSecret); //Logger.Info("reqToken:" + UrlSignUtil.GetMD5(api.Username + api.AppSecret)); NameValueCollection col = new NameValueCollection(); col.Add("reqToken", UrlSignUtil.GetMD5(api.Username + api.AppSecret)); col.Add("id", api.Username); Logger.Info("URL:" + api.GetTokenUrl); string resStr = HttpSercice.PostHttpRequest(api.GetTokenUrl, col, WeChat.Adapter.Requests.RequestType.POST, null); if (!string.IsNullOrEmpty(resStr)) { try { Newtonsoft.Json.Linq.JObject jsonResult = Newtonsoft.Json.Linq.JObject.Parse(resStr); string result = jsonResult["result"].ToString(); string rtoken = jsonResult["token"] != null? jsonResult["token"].ToString():null; string message = jsonResult["resultMsg"].ToString(); Logger.Info("Request token result:" + resStr); if (!string.IsNullOrEmpty(result) && result.Trim() == "0") { token = rtoken; expiredTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now) + expiredInterval; } else { Logger.Error(message != null?message:"Failed to request token."); } } catch (Exception ex) { Logger.Fatal(ex); } } else { Logger.Info("Empty response."); } Logger.Info("Gettoken done!"); } }
public static void ProcessOrders() { logger.Info("Going to process orders..."); chargebitEntities db = null; try { List <Charge_Order> orders = null; db = new chargebitEntities(); db.Configuration.AutoDetectChangesEnabled = true; if (resources == null) { resources = (from r in db.Resource where r.Enabled == true select r).ToList <Resource>(); } if (resourceAPIs == null) { resourceAPIs = (from r in db.Resrouce_interface select r).ToList <Resrouce_interface>(); } lock (o) { logger.Info("Last Max Order Id:" + lastOrderId); orders = (from o in db.Charge_Order where o.Id > lastOrderId && ((o.Payed == true && o.Charge_type == 0 && o.Status == 10) || (o.Charge_type == 1 && o.Status != 3 && o.Status != 2) || (o.Charge_type == 2 && o.Status != 3 && o.Status != 2)) orderby o.Charge_type ascending orderby o.Id ascending select o).ToList <Charge_Order>(); logger.Info(string.Format("Get {0} unprocessed orders", orders.Count)); if (orders.Count == 0) { logger.Info("No unprocessed orders, thread will exit!"); return; } lastOrderId = (from o in orders select o.Id).Max(); logger.Info("Max Order Id updated to:" + lastOrderId); } List <int> agentIds = (from o in orders where o.Agent_Id > 0 select o.Agent_Id).ToList <int>(); List <Users> agents = new List <Users>(); if (agentIds != null && agentIds.Count > 0) { int[] ids = agentIds.ToArray <int>(); agents = (from u in db.Users where ids.Contains(u.Id) select u).ToList <Users>(); } List <Charge_Order> frontEndOrders = (from o in orders where o.Payed == true && o.Charge_type == 0 && o.Status == 10 select o).ToList <Charge_Order>(); List <Charge_Order> agentOrders = (from o in orders where o.Charge_type == 1 && o.Status != 3 && o.Status != 2 select o).ToList <Charge_Order>(); List <Charge_Order> backendOrders = (from o in orders where o.Charge_type == 2 && o.Status != 3 && o.Status != 2 select o).ToList <Charge_Order>(); //logger.Info(string.Format("{0} unprocessed frontend orders", frontEndOrders.Count)); //logger.Info(string.Format("{0} unprocessed agent orders", agentOrders.Count)); //logger.Info(string.Format("{0} unprocessed backend orders", backendOrders.Count)); ChargeResult result = null; logger.Info(""); foreach (Charge_Order corder in orders) { Resource resource = (from r in resources where r.Id == corder.Resource_id select r).FirstOrDefault <Resource>(); Resrouce_interface resourceAPI = null; if (resource != null) { resourceAPI = (from api in resourceAPIs where api.Resource_id == corder.Resource_id select api).FirstOrDefault <Resrouce_interface>(); } logger.Info(string.Format("Processing order Id-{0}, Phone-{1}, Agent-{2}, ChargeType-{3}", corder.Id.ToString(), corder.Phone_number, corder.Agent_Id, corder.Charge_type)); ChargeOrder order = new ChargeOrder() { Payed = corder.Payed, ChargeType = corder.Charge_type, AgencyId = corder.Agent_Id, OperateUserId = corder.Operate_User, Id = corder.Id, Province = corder.Province, City = corder.City, MobileSP = corder.MobileSP, MobileNumber = corder.Phone_number, OutOrderId = "", ResourceId = 0, ResourceTaocanId = corder.Resource_taocan_id, RouteId = corder.RuoteId, CreatedTime = corder.Created_time }; result = cb.Charge(order); Users agent = (from u in agents where corder.Agent_Id > 0 && corder.Agent_Id == u.Id select u).FirstOrDefault <Users>(); logger.Info("Order status - " + order.Status); //just for Synchronized resources if (!string.IsNullOrEmpty(corder.CallBackUrl) && corder.Agent_Id > 0 && agent != null && resourceAPI != null && (resourceAPI.Synchronized == true || result.Status == ChargeStatus.FAILED) ) { logger.Info("Synchronized resource api or failed resource api submission."); //send back the status to agent system NameValueCollection col = new NameValueCollection(); SortedDictionary <string, string> param = new SortedDictionary <string, string>(); param.Add("OrderId", corder.Id.ToString()); param.Add("ClientOrderId", corder.Client_Order_Id != null?corder.Client_Order_Id.ToString():""); param.Add("Message", result.Message); param.Add("Status", result.Status.ToString()); string querystr = ""; foreach (KeyValuePair <string, string> p in param) { col.Add(p.Key, p.Value != null ? p.Value : ""); if (querystr == "") { querystr = p.Key + "=" + (p.Value != null ? p.Value : ""); } else { querystr += "&" + p.Key + "=" + (p.Value != null ? p.Value : ""); } } logger.Info(string.Format("Post data to callback url - {0}", corder.CallBackUrl)); logger.Info(string.Format("Data - {0}", querystr)); querystr += "&key=" + agent.SecurityStamp; string sign = UrlSignUtil.GetMD5(querystr); col.Add("Sign", sign); //logger.Info("sign=" + sign); string resStr = HttpSercice.PostHttpRequest(corder.CallBackUrl, col, WeChat.Adapter.Requests.RequestType.POST, null); if (resStr == null) { corder.PushedTimes += 1; corder.Received = true; } else if (resStr.ToLower() == "fail") { corder.PushedTimes += 1; corder.Received = false; } db.SaveChanges(); } logger.Info(string.Format("Order ID:{2} - {0} - {1}", result.Status, result.Message, corder.Id)); logger.Info(""); } } catch (Exception ex) { logger.Error(ex); } finally { if (db != null) { db.Dispose(); } } }
public void SendStatusBackToAgentCallback(object oorder) { if (oorder.GetType() != typeof(Charge_Order)) { return; } Charge_Order order = oorder as Charge_Order; Logger.Info("SendStatusBackToAgentCallback..."); if (order == null || order.Agent_Id <= 0 || string.IsNullOrEmpty(order.CallBackUrl)) { Logger.Info("Not an agent order, no need to proceed anymore."); return; } Logger.Info(string.Format("Order Id {0}", order.Id.ToString())); if (!string.IsNullOrEmpty(order.Client_Order_Id)) { Logger.Info(string.Format("Client OrderId {0}", order.Client_Order_Id.ToString())); } if (!string.IsNullOrEmpty(order.Out_Order_Id)) { Logger.Info(string.Format("Out Order Id {0}", order.Out_Order_Id.ToString())); } Logger.Info(string.Format("Order Status {0}", order.Status.ToString())); Logger.Info(string.Format("Order Message {0}", order.Message != null?order.Message:"")); chargebitEntities db = null; try { if (string.IsNullOrEmpty(order.CallBackUrl)) { Logger.Info("Order callback URL is empty, no need to callback."); return; } db = new chargebitEntities(); NameValueCollection col = new NameValueCollection(); SortedDictionary <string, string> paras = new SortedDictionary <string, string>(); string orderId = order.Id.ToString(); string status = order.Status.ToString(); string message = order.Message != null ? order.Message : ""; Users agent = (from u in db.Users where u.Id == order.Agent_Id select u).FirstOrDefault <Users>(); Logger.Info(string.Format("Agent {0}", agent.Email)); if (agent == null) { status = "3"; message = "代理商账户没有找到"; } string token = agent.SecurityStamp; paras["OrderId"] = orderId; paras["Status"] = status; paras["ClientOrderId"] = order.Client_Order_Id != null? order.Client_Order_Id:""; paras["Message"] = message; string signStr = ""; foreach (KeyValuePair <string, string> p in paras) { if (signStr == string.Empty) { signStr += p.Key.ToLower() + "=" + p.Value; } else { signStr += "&" + p.Key.ToLower() + "=" + p.Value; } } Logger.Info(string.Format("Post data {1} to {0}", order.CallBackUrl, signStr)); signStr += "&key=" + token; Logger.Info(string.Format("String to be signed {0}", signStr)); Logger.Info(string.Format("Signature is {0}", UrlSignUtil.GetMD5(signStr))); paras["Sign"] = UrlSignUtil.GetMD5(signStr); foreach (KeyValuePair <string, string> p in paras) { col[p.Key] = p.Value; } Charge_Order dborder = (from o in db.Charge_Order where o.Id == order.Id select o).FirstOrDefault <Charge_Order>(); string resStr = HttpSercice.PostHttpRequest(order.CallBackUrl, col, WeChat.Adapter.Requests.RequestType.POST, null); if (resStr == null) { dborder.PushedTimes += 1; dborder.Received = true; } else if (resStr.ToLower() == "fail") { dborder.PushedTimes += 1; dborder.Received = false; } db.SaveChanges(); } catch (Exception ex) { Logger.Error(ex); } finally { if (db != null) { db.Dispose(); } Logger.Info("Leaving SendStatusBackToAgentCallback..."); } }