public void ContainerTest() { //注册 AccessTokenContainer.Register(base._appId, base._appSecret); //获取Ticket完整结果(包括当前过期秒数) var ticketResult = JsApiTicketContainer.GetJsApiTicketResult(base._appId); Assert.IsNotNull(ticketResult); //只获取Ticket字符串 var ticket = JsApiTicketContainer.GetJsApiTicket(base._appId); Assert.AreEqual(ticketResult.ticket, ticket); Console.WriteLine(ticket); //getNewTicket { ticket = JsApiTicketContainer.TryGetJsApiTicket(base._appId, base._appSecret, false); Assert.AreEqual(ticketResult.ticket, ticket); ticket = JsApiTicketContainer.TryGetJsApiTicket(base._appId, base._appSecret, true); //Assert.AreNotEqual(ticketResult.ticket, ticket);//如果微信服务器缓存,此处会相同 Console.WriteLine(ticket); } }
public async Task <ActionResult> Test() { //注册 await AccessTokenContainer.RegisterAsync(TenPayV3Info.AppId, TenPayV3Info.Sub_AppSecret); var accessToken = AccessTokenContainer.TryGetAccessToken("wx4b58f9e96371d9ff", "43c5c4d9b8207634eb293a72ddad5bb1", true); var token = await AccessTokenContainer.TryGetAccessTokenAsync(TenPayV3Info.AppId, TenPayV3Info.Sub_AppSecret, false); //获取Ticket完整结果(包括当前过期秒数) var ticketResult = JsApiTicketContainer.GetJsApiTicketResult(TenPayV3Info.AppId); // //只获取Ticket字符串 var ticket = JsApiTicketContainer.GetJsApiTicket(TenPayV3Info.AppId); //getNewTicket { ticket = JsApiTicketContainer.TryGetJsApiTicket(TenPayV3Info.AppId, TenPayV3Info.Sub_AppSecret, false); ticket = JsApiTicketContainer.TryGetJsApiTicket(TenPayV3Info.AppId, TenPayV3Info.Sub_AppSecret, true); //Assert.AreNotEqual(ticketResult.ticket, ticket);//如果微信服务器缓存,此处会相同 } return(Content(token)); }
public ActionResult RichText(int id = 0) { //string token = AccessTokenContainer.GetAccessToken(AccessTokenContainer.GetFirstOrDefaultAppId()); string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, GetPageUrl()); ViewBag.pageUrl = GetPageUrl(); if (id == 0) { ViewBag.description = ""; } else { EntGoods good = EntGoodsBLL.SingleModel.GetModel(id); if (good == null) { ViewBag.description = ""; return(Content("产品不存在或已删除")); } ViewBag.description = good.description; } return(View()); }
/// <summary> /// 拼享惠产品描述编辑 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult PinRichText(int id = 0) { string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, Server.UrlDecode(GetPageUrl())); ViewBag.pageUrl = GetPageUrl(); if (id == 0) { //string temp = RedisUtil.Get<string>(PinGoodsBLL.key_new_pin_goods); ViewBag.description = ""; //temp ?? ""; } else { PinGoods good = PinGoodsBLL.SingleModel.GetModel(id); if (good == null) { ViewBag.description = ""; return(Content("产品不存在或已删除")); } ViewBag.description = good.description; } return(View("RichText")); }
public ActionResult Index() { var code = Request.Params["code"]; var appid = ConfigurationManager.AppSettings["AppID"]; var secret = ConfigurationManager.AppSettings["AppSecret"]; WxModel model = new WxModel(); var ticket = JsApiTicketContainer.GetJsApiTicket(appid); var noncestr = JSSDKHelper.GetNoncestr(); var timeSpan = JSSDKHelper.GetTimestamp(); var signature = JSSDKHelper.GetSignature(ticket, noncestr, timeSpan, Request.Url.AbsoluteUri); var accessToken = OAuthApi.GetAccessToken(appid, secret, code); if (OAuthApi.Auth(accessToken.access_token, accessToken.openid).errcode == ReturnCode.合法的OpenID) { accessToken = OAuthApi.RefreshToken(appid, accessToken.refresh_token); } var accessUserInfo = OAuthApi.GetUserInfo(accessToken.access_token, accessToken.openid); model.UnioId = accessUserInfo.openid; model.TimeStamp = timeSpan; model.Signature = signature; model.NonceStr = noncestr; return(View(model)); }
public ActionResult PlatStoreDescriptionRichText(int id = 0) { //string token = AccessTokenContainer.GetAccessToken(AccessTokenContainer.GetFirstOrDefaultAppId()); string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, GetPageUrl()); log4net.LogHelper.WriteInfo(this.GetType(), $"token={token};appid={ViewBag.appid};ticket={ViewBag.ticket};timestamp={ ViewBag.timestamp};nonceStr={ViewBag.nonceStr};signature={ViewBag.signature};pageUrl={GetPageUrl()}"); if (id == 0) { // string temp = RedisUtil.Get<string>("temp_psd_description_0"); ViewBag.description = "";//temp ?? ""; } else { PlatStore platStore = PlatStoreBLL.SingleModel.GetModel(id); if (platStore == null || platStore.State == -1) { ViewBag.description = ""; return(Content("店铺不存在或已删除")); } ViewBag.description = platStore.StoreDescription; } return(View()); }
protected void Page_Load(object sender, EventArgs e) { string ticket = string.Empty; timestamp = JSSDKHelper.GetTimestamp(); nonceStr = JSSDKHelper.GetNoncestr(); appID = "wx422044a7a4be9609"; ticket = JsApiTicketContainer.GetJsApiTicket(appID); signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, Request.Url.AbsoluteUri.ToString()); }
public ActionResult WxSignature(string url) { try { if (!AccessTokenContainer.CheckRegistered(appid)) //检查是否已经注册 { AccessTokenContainer.Register(appid, secret); //如果没有注册则进行注册 } var tokenResult = HttpRuntimeCache.GetCache("LocalAccessToken") as LocalAccessToken; var useCache = true; if (tokenResult == null || string.IsNullOrWhiteSpace(tokenResult?.access_token) || tokenResult.ExpiresTime == null || System.DateTime.Now > tokenResult.ExpiresTime) //过期 { useCache = false; var result = AccessTokenContainer.GetAccessTokenResult(appid, true); if (!string.IsNullOrWhiteSpace(result?.errmsg)) { return(Json(new { ErrMsg = JsonConvert.SerializeObject(result) }, JsonRequestBehavior.AllowGet)); //return Json(new { data = JsonConvert.SerializeObject(new { appid, secret }), ErrMsg = JsonConvert.SerializeObject(tokenResult) }, JsonRequestBehavior.AllowGet); } tokenResult = new LocalAccessToken(result) { }; HttpRuntimeCache.SetCache("LocalAccessToken", tokenResult, 7260); } var ticket = JsApiTicketContainer.GetJsApiTicket(appid); var model = new WXShare() { appId = appid, timestamp = JSSDKHelper.GetTimestamp(), nonce = JSSDKHelper.GetNoncestr(), url = Server.UrlDecode(url), ticket = ticket, //result = JsonConvert.SerializeObject(tokenResult), useCache = useCache }; model.signature = JSSDKHelper.GetSignature(ticket, model.nonce, model.timestamp, model.url); return(Json(model, JsonRequestBehavior.AllowGet)); } catch (System.Exception ex) { return(Json(new { ErrMsg = ex.Message, StackTrace = JsonConvert.SerializeObject(ex.StackTrace) }, JsonRequestBehavior.AllowGet)); } }
internal static Ticket GetTicket(string url) { var ticket = JsApiTicketContainer.GetJsApiTicket(WeixinSetting.WeixinAppId); var timestamp = JSSDKHelper.GetTimestamp(); var nonceStr = JSSDKHelper.GetNoncestr(); var signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, url); var model = new Ticket { ticket = ticket, nonceStr = nonceStr, timestamp = timestamp, signature = signature }; return(model); }
public ActionResult GetJSSDK(string url) { string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); string appId = AccessTokenContainer.GetFirstOrDefaultAppId(); string ticket = JsApiTicketContainer.GetJsApiTicket(webview_appid); string timestamp = JSSDKHelper.GetTimestamp(); string nonceStr = JSSDKHelper.GetNoncestr(); string signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, HttpUtility.UrlDecode(url)); //string accesstoken = AccessTokenContainer.TryGetAccessToken(webview_appid, webview_appsecret); object SDK = new { AppId = webview_appid, Timestamp = int.Parse(timestamp), NonceStr = nonceStr, Signature = signature, Ticket = ticket, Url = url, }; return(ApiModel(isok: true, message: "获取成功", data: SDK)); }
public ActionResult Index(string items, int orderType) { string ids = items; var lst = this._prodAppService.GetProductById(ids); ViewBag.OrderType = orderType; ViewBag.lst = lst; var address = _userAddressService.GetDefault(Common.UserHelper.Instance.getUserId()); if (address != null) { ViewBag.Address = address.Address; ViewBag.Phone = address.PhoneNo; ViewBag.Name = address.ContactName; string ticket = string.Empty; timestamp = JSSDKHelper.GetTimestamp(); nonceStr = JSSDKHelper.GetNoncestr(); JSSDKHelper jssdkhelper = new JSSDKHelper(); try { ticket = JsApiTicketContainer.GetJsApiTicket(Common.CommonConst.AppID); signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, Request.Url.AbsoluteUri.ToString()); ViewBag.timestamp = timestamp; ViewBag.nonceStr = nonceStr; ViewBag.AppId = Common.CommonConst.AppID; ViewBag.signature = signature; } catch (ErrorJsonResultException ex) { Logger.Error("errorcode:" + ex.JsonResult.errcode.ToString() + " errmsg:" + ex.JsonResult.errmsg); } } return(View()); }
public static JsApiToken GetJsApiToken(this IAccessControl ac, bool resetUrl) { var app = ac.Context.RequestServices.GetService <IWeChatContainer>().GetApp(); var timestamp = JSSDKHelper.GetTimestamp(); var noncestr = JSSDKHelper.GetNoncestr(); var jsApiTicket = JsApiTicketContainer.GetJsApiTicket(app.AppId); var url = ac.Context.Request.GetUrl(); if (resetUrl) { url = UrlHelper.Append(url, "t", timestamp); } var signature = JSSDKHelper.GetSignature(jsApiTicket, noncestr, timestamp, url); return(new JsApiToken { AppId = app.AppId, Ticket = jsApiTicket, Timestamp = timestamp, Nonce = noncestr, Signature = signature }); }
public static string Pay(string orderNumber, int totalFee, string openid) { //读取配置文件 var merchantId = string.Empty; var appId = string.Empty; var key = string.Empty; var appSecret = string.Empty; //接收微信支付异步通知回调地址 var notifyUrl = string.Empty; var payUrl = string.Empty; var xd = new XmlDocument(); xd.Load(HttpContext.Current.Server.MapPath("/app_data/xml/payment.xml")); var selectSingleNode = xd.SelectSingleNode("paymentMethod"); if (selectSingleNode != null) { var xnl = selectSingleNode.ChildNodes; foreach (XmlNode xn in xnl) { var xe = (XmlElement)xn; if (xe.GetAttribute("id") != "wechat") { continue; } merchantId = xe.GetAttribute("merchant_id"); appId = xe.GetAttribute("app_id"); appSecret = xe.GetAttribute("app_secret"); key = xe.GetAttribute("key"); notifyUrl = xe.GetAttribute("notify_url"); payUrl = xe.GetAttribute("pay_url") + orderNumber.ToString(); break; } } var timestamp = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetTimestamp(); var nonceStr = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetNoncestr(); JsApiTicketContainer.Register(appId, appSecret); var jsapiTicket = JsApiTicketContainer.GetJsApiTicket(appId); var signConfig = new Senparc.Weixin.MP.TenPayLib.RequestHandler(null); signConfig.SetParameter("jsapi_ticket", jsapiTicket); signConfig.SetParameter("noncestr", nonceStr); signConfig.SetParameter("timestamp", timestamp); signConfig.SetParameter("url", payUrl); var signature = signConfig.CreateSHA1Sign(); //支付 //商品或支付单简要描述 var body = "Billion智能仪表管理系统"; //商户系统内部的订单号,32个字符内,可包含字母,其他说明见商户订单号 //APP和网页支付提交用户端IP,Native支付填调用微信支付API的机器IP var spbillCreateIp = HttpContext.Current.Request.ServerVariables.Get("Remote_Addr"); //JSAPI,NATIVE,APP,WAP var trade_type = "JSAPI"; //创建支付应答对象 var panSigig = new RequestHandler(null); //初始化 panSigig.Init(); //设置package订单参数 panSigig.SetParameter("appid", appId); panSigig.SetParameter("mch_id", merchantId); panSigig.SetParameter("nonce_str", nonceStr); panSigig.SetParameter("body", body); panSigig.SetParameter("out_trade_no", orderNumber); panSigig.SetParameter("total_fee", totalFee.ToString()); panSigig.SetParameter("spbill_create_ip", spbillCreateIp); panSigig.SetParameter("notify_url", notifyUrl); panSigig.SetParameter("trade_type", trade_type); panSigig.SetParameter("openid", openid); var sign = panSigig.CreateMd5Sign("key", key); panSigig.SetParameter("sign", sign); var data = panSigig.ParseXML(); var result = TenPayV3.Unifiedorder(data); var res = System.Xml.Linq.XDocument.Parse(result); var xElement = res.Element("xml"); if (xElement == null) { return(""); } var element = xElement.Element("prepay_id"); if (element == null) { return(""); } var prepayId = element.Value; var paysignReqHandler = new RequestHandler(null); paysignReqHandler.Init(); //设置支付参数 paysignReqHandler.SetParameter("appId", appId); paysignReqHandler.SetParameter("timeStamp", timestamp); paysignReqHandler.SetParameter("nonceStr", nonceStr); paysignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId)); paysignReqHandler.SetParameter("signType", "MD5"); var paysign = paysignReqHandler.CreateMd5Sign("key", key); paysignReqHandler.SetParameter("paysign", paysign); NVelocityHelper nh = new NVelocityHelper(); nh.Put("app_id", appId); nh.Put("timestamp", timestamp); nh.Put("nonceStr", nonceStr); nh.Put("signature", signature); nh.Put("package", string.Format("prepay_id={0}", prepayId)); nh.Put("signType", "MD5"); nh.Put("paySign", paysign); nh.Put("orderid", orderNumber); return(nh.Write("app_data/template/wechat")); }
/// <summary> /// 获取可用JsApiTicket /// </summary> /// <param name="appId">公众号AppId</param> /// <param name="getNewTicket">是否强制重新获取新的Ticket</param> /// <returns></returns> public static string GetJsApiTicket(string appId, bool getNewTicket = false) { return(JsApiTicketContainer.GetJsApiTicket(appId, getNewTicket)); }