public virtual async Task <ActionResult> GetJsSdkConfigParameters([FromQuery] string jsUrl) { if (string.IsNullOrEmpty(jsUrl)) { throw new UserFriendlyException("需要计算的 JS URL 参数不能够为空。"); } var nonceStr = RandomStringHelper.GetRandomString(); var timeStamp = DateTimeHelper.GetNowTimeStamp(); var ticket = await _jsTicketAccessor.GetTicketAsync(); var @params = new WeChatParameters(); @params.AddParameter("noncestr", nonceStr); @params.AddParameter("jsapi_ticket", await _jsTicketAccessor.GetTicketAsync()); @params.AddParameter("url", HttpUtility.UrlDecode(jsUrl)); @params.AddParameter("timestamp", timeStamp); var signStr = _signatureGenerator.Generate(@params, SHA1.Create()).ToLower(); return(new JsonResult(new { appid = _optionsResolver.Resolve().AppId, noncestr = nonceStr, timestamp = timeStamp, signature = signStr, jsapi_ticket = ticket })); }
public virtual async Task <ActionResult> GetJsSdkWeChatPayParameters([FromQuery] string appId, string prepayId) { if (string.IsNullOrEmpty(prepayId)) { throw new UserFriendlyException("请传入有效的预支付订单 Id。"); } var nonceStr = RandomStringHelper.GetRandomString(); var timeStamp = DateTimeHelper.GetNowTimeStamp(); var package = $"prepay_id={prepayId}"; var signType = "MD5"; var option = await _optionsResolver.ResolveAsync(); var @params = new WeChatParameters(); @params.AddParameter("appId", appId); @params.AddParameter("nonceStr", nonceStr); @params.AddParameter("timeStamp", timeStamp); @params.AddParameter("package", package); @params.AddParameter("signType", signType); var paySignStr = _signatureGenerator.Generate(@params, MD5.Create(), option.ApiKey); return(new JsonResult(new { nonceStr, timeStamp, package, signType, paySign = paySignStr })); }
public async Task HandleAsync(WeChatPayHandlerContext context) { var parameters = new WeChatParameters(); var nodes = context.WeChatRequestXmlData.SelectSingleNode("/xml")?.ChildNodes; if (nodes == null) { return; } foreach (XmlNode node in nodes) { if (node.Name == "sign") { continue; } parameters.AddParameter(node.Name, node.InnerText); } var options = await WeChatPayOptionsResolver.ResolveAsync(); var responseSign = SignatureGenerator.Generate(parameters, MD5.Create(), options.ApiKey); if (responseSign != context.WeChatRequestXmlData.SelectSingleNode("/xml/sign")?.InnerText) { context.IsSuccess = false; context.FailedResponse = "订单签名验证没有通过"; Logger.LogWarning("订单签名验证没有通过。"); } }
public string Generate(WeChatParameters parameters, HashAlgorithm hashAlgorithm, string apiKey = null) { var signStr = $"{parameters.GetWaitForSignatureStr()}{(apiKey != null ? $"&key={apiKey}" : "")}"; var signBytes = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(signStr)); var sb = new StringBuilder(); foreach (var @byte in signBytes) { sb.Append($"{@byte:x2}"); } return(sb.ToString().ToUpper()); }