public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JDPayNotifyResponse { if (request.HasFormContentType || request.Method == "GET") { var rspInstance = Activator.CreateInstance <T>(); var parameters = GetParameters(request, !(rspInstance is JDPayDefrayPayNotifyResponse)); var query = JDPayUtility.BuildQuery(parameters); Logger?.LogTrace(0, "Request:{query}", query); var parser = new JDPayDictionaryParser <T>(); var rsp = parser.Parse(parameters); if (rsp is JDPayDefrayPayNotifyResponse) { CheckNotifyDefrayPaySign(rsp.Parameters); } else { CheckNotifySign(rsp.Parameters); } return(rsp); } else if (request.HasTextXmlContentType()) { var body = await new StreamReader(request.Body).ReadToEndAsync(); Logger?.LogTrace(0, "Request:{body}", body); var parser = new JDPayXmlParser <T>(); var rsp = parser.Parse(JDPayUtility.FotmatXmlString(body)); if (!string.IsNullOrEmpty(rsp.Encrypt)) { var encrypt = rsp.Encrypt; var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encrypt)); var reqBody = JDPaySecurity.DecryptECB(base64EncryptStr, Options.DesKeyBase64); Logger?.LogTrace(1, "Encrypt Content:{reqBody}", reqBody); var reqBodyDoc = new XmlDocument() { XmlResolver = null }; reqBodyDoc.LoadXml(reqBody); var sign = JDPayUtility.GetValue(reqBodyDoc, "sign"); var rootNode = reqBodyDoc.SelectSingleNode("jdpay"); var signNode = rootNode.SelectSingleNode("sign"); rootNode.RemoveChild(signNode); var reqBodyStr = JDPayUtility.ConvertXmlToString(reqBodyDoc); var xmlh = rsp.Body.Substring(0, rsp.Body.IndexOf("<jdpay>")); if (!string.IsNullOrEmpty(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } var sha256SourceSignString = SHA256.Compute(reqBodyStr); var decryptByte = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), Options.PublicKey); var decryptStr = JDPaySecurity.BytesToString(decryptByte); if (sha256SourceSignString == decryptStr) { rsp = parser.Parse(reqBody); rsp.Encrypt = encrypt; return(rsp); } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } else { throw new Exception("encrypt is Empty!"); } } else { throw new Exception("content type is not supported!"); } }
public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JDPayNotifyResponse { if (request.HasFormContentType) { var parameters = await GetParametersAsync(request); var query = HttpClientEx.BuildQuery(parameters); Logger.LogInformation(0, "Request:{query}", query); var parser = new JDPayDictionaryParser <T>(); var rsp = parser.Parse(parameters); CheckNotifySign(rsp.Parameters, PrivateKey); return(rsp); } else if (request.HasTextXmlContentType()) { var body = await new StreamReader(request.Body).ReadToEndAsync(); Logger.LogInformation(0, "Request:{body}", body); var parser = new JDPayXmlParser <T>(); var rsp = parser.Parse(JDPayUtility.FotmatXmlString(body)); if (!string.IsNullOrEmpty(rsp.Encrypt)) { var encrypt = rsp.Encrypt; var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encrypt)); var reqBody = DES3.DecryptECB(base64EncryptStr, DesKey); Logger.LogInformation(1, "Encrypt Content:{body}", reqBody); var reqBodyDoc = new XmlDocument(); reqBodyDoc.LoadXml(reqBody); var sign = JDPayUtility.GetValue(reqBodyDoc, "sign"); var rootNode = reqBodyDoc.SelectSingleNode("jdpay"); var signNode = rootNode.SelectSingleNode("sign"); rootNode.RemoveChild(signNode); var reqBodyStr = JDPayUtility.ConvertXmlToString(reqBodyDoc); var xmlh = rsp.Body.Substring(0, rsp.Body.IndexOf("<jdpay>")); if (!string.IsNullOrEmpty(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } var sha256SourceSignString = SHA256.Compute(reqBodyStr); var decryptByte = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), PublicKey); var decryptStr = DES3.BytesToString(decryptByte); if (sha256SourceSignString == decryptStr) { rsp = parser.Parse(reqBody); rsp.Encrypt = encrypt; } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } return(rsp); } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } }