/// <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; }
public static JSAPITicket GetWeChatJsApiTicket() { if (WeChatJsApiTicket == null || WeChatJsApiTicket.ExpiresTime < DateTime.Now) { WeChatJsApiTicket = RequestWeChatJsApiTicket(); } return(WeChatJsApiTicket); }
static PersistentValueManager() { KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatPayConfig.xml")); KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatAccessToken.xml")); KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatJSAPITicket.xml")); config = XMLUtil.DeserializeXML <WeChatPayConfig>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatPayConfig.xml")); WeChatAccessToken = XMLUtil.DeserializeXML <AccessToken>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatAccessToken.xml")); WeChatJsApiTicket = XMLUtil.DeserializeXML <JSAPITicket>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatJSAPITicket.xml")); }
private static JSAPITicket RequestWeChatJsApiTicket() { JSAPITicket ticket = null; JSAPITicketRequest request = null; request = new JSAPITicketRequest(config); request.Access_Token = GetWeChatAccessToken(); BaseResponse res = request.Execute(); if (res != null) { JSAPITicketResponse jsRes = (JSAPITicketResponse)res; ticket = jsRes.Ticket; } if (ticket != null) { XMLUtil.SerializeObject(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatJSAPITicket.xml"), ticket); } return(ticket); }
/// <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); }
public JsonResult PreCharge(WeChatChargeModel model) { logger.Info("WeChatController.PreCharge......................................................"); ApiMessage message = new ApiMessage(); ChargeOrder order = null; if (ModelState.IsValid) { try { if (string.IsNullOrEmpty(model.OpenId)) { message.Status = "ERROR"; message.Message = "请从公众号菜单打开此页面"; return(Json(message, JsonRequestBehavior.AllowGet)); } //ChargeBridge cb = new ChargeBridge(); order = new ChargeOrder() { ChargeType = 0, AgencyId = 0, Id = 0, Province = model.Province, City = model.City, MobileSP = model.SPName, MobileNumber = model.Mobile, OutOrderId = "", ResourceId = 0, ResourceTaocanId = model.ResourceTaocanId, RouteId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), Payed = false, OpenId = model.OpenId, OpenAccountType = 1 }; // OrderManagement orderMgt = new OrderManagement(); ResourceManagement resourceMgr = new ResourceManagement(0); string msg = string.Empty; if (orderMgt.IsThisMonthCharged(order.MobileNumber, order.ResourceTaocanId, out msg)) { message.Status = "ERROR"; message.Message = msg; return(Json(message, JsonRequestBehavior.AllowGet)); } order = orderMgt.GenerateOrder(order); int total = 0; List <BResourceTaocan> taocans = resourceMgr.FindResourceTaocans(order.ResourceTaocanId, 0, 0, out total); if (taocans == null || taocans.Count == 0) { message.Message = "当前套餐不可用"; message.Status = "ERROR"; return(Json(message, JsonRequestBehavior.AllowGet)); } logger.Info(string.Format("Order is generated, Id - {0}, mobile - {1}", order.Id, order.MobileNumber)); BResourceTaocan taocan = taocans[0]; message.Status = "OK"; message.Message = "预充值订单已经生成"; message.Item = null; // string ip = Request.ServerVariables["REMOTE_ADDR"]; if (ip != null && ip.IndexOf("::") > -1) { ip = "127.0.0.1"; } string prepayId = WeChatPaymentWrapper.GetPrepayId(PersistentValueManager.config, Session["wechat_openid"] != null ? Session["wechat_openid"].ToString() : "", order.PaymentId.ToString(), "TEST WECHATPAY", ip, (int)taocan.Taocan.Sale_price * 100, TradeType.JSAPI); logger.Info(string.Format("Prepay Id - {0}", prepayId)); WeChatOrder weOrder = new WeChatOrder(); weOrder.Order = new ChargeOrder { Id = order.Id, Payed = order.Payed, PaymentId = order.PaymentId, MobileNumber = order.MobileNumber, MobileSP = order.MobileSP, Province = order.Province }; weOrder.PrepayId = prepayId; weOrder.PaySign = ""; message.Item = weOrder; AccessToken token = PersistentValueManager.GetWeChatAccessToken(); JSAPITicket ticket = PersistentValueManager.GetWeChatJsApiTicket(); SortedDictionary <string, string> parameters = new SortedDictionary <string, string>(); parameters.Add("appId", PersistentValueManager.config.APPID); parameters.Add("timeStamp", model.timestamp); parameters.Add("nonceStr", model.nancestr); parameters.Add("package", "prepay_id=" + prepayId); parameters.Add("signType", "MD5"); logger.Info(string.Format("timeStamp:{0}", model.timestamp)); logger.Info(string.Format("nonceStr:{0}", model.nancestr)); logger.Info(string.Format("package:{0}", "prepay_id=" + prepayId)); string querystr = null; foreach (KeyValuePair <string, string> para in parameters) { if (querystr == null) { querystr = para.Key + "=" + para.Value; } else { querystr += "&" + para.Key + "=" + para.Value; } } querystr += "&key=" + PersistentValueManager.config.ShopSecret; logger.Info(querystr); string sign = UrlSignUtil.GetMD5(querystr); model.paySign = sign.ToUpper(); model.prepay_id = prepayId; logger.Info(string.Format("paySign:{0}", sign.ToUpper())); message.Item = model; } catch (KMBitException kex) { logger.Error(kex); message.Message = kex.Message; message.Status = "ERROR"; } catch (Exception ex) { message.Message = "未知错误,请联系我们"; message.Status = "ERROR"; logger.Fatal(ex); } finally { } } logger.Info("Done."); return(Json(message, JsonRequestBehavior.AllowGet)); }
// GET: WeChat public ActionResult Index() { logger.Info(this.GetType().FullName + " - Index...."); WeChatChargeModel model = new WeChatChargeModel(); //get weichat account openid from redirect URL parameters if (!string.IsNullOrEmpty(Request["openid"])) { model.OpenId = Request["openid"].ToString(); Session["wechat_openid"] = model.OpenId; } else { if (Session["wechat_openid"] != null) { logger.Info("already has wechat openid stored."); model.OpenId = Session["wechat_openid"] != null ? Session["wechat_openid"].ToString() : ""; } else { string code = Request["code"]; if (!string.IsNullOrEmpty(code)) { logger.Info("new request from wechat public account, code is " + code); AccessToken weChatAccessToken = AuthHelper.GetAccessToken(PersistentValueManager.config, code); if (weChatAccessToken != null) { logger.Info("get wechat account openid by code " + code); model.OpenId = weChatAccessToken.OpenId; Session["wechat_openid"] = model.OpenId; } } } } if (!string.IsNullOrEmpty(model.OpenId)) { logger.Info("Wechat OpenId:" + model.OpenId); } else { logger.Error("Wechat OpenId is empty, cannot processing any more."); } model.nancestr = Guid.NewGuid().ToString().Replace("-", ""); if (model.nancestr.Length > 32) { model.nancestr = model.nancestr.Substring(0, 32); } model.appid = PersistentValueManager.config.APPID; model.timestamp = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now).ToString(); SortedDictionary <string, string> param = new SortedDictionary <string, string>(); JSAPITicket ticket = PersistentValueManager.GetWeChatJsApiTicket(); if (ticket != null) { param.Add("jsapi_ticket", ticket.Ticket); logger.Info("jsapi_ticket:" + ticket.Ticket); } //param.Add("appId", model.appid); param.Add("timestamp", model.timestamp); param.Add("noncestr", model.nancestr); param.Add("url", Request.Url.AbsoluteUri.ToString()); logger.Info(Request.Url.AbsoluteUri.ToString()); //param.Add("jsApiList", "[chooseWXPay]"); string sign = UrlSignUtil.SHA1_Hash(param); model.signature = sign; logger.Info("Done Index...."); return(View(model)); }