public async Task <T> ExecuteAsync <T>(IJDPayRequest <T> request) where T : JDPayResponse { // 字典排序 var sortedTxtParams = new JDPayDictionary(request.GetParameters()); var content = BuildEncryptXml(request, sortedTxtParams); Logger?.LogTrace(0, "Request:{content}", content); using (var client = ClientFactory.CreateClient(JDPayOptions.DefaultClientName)) { var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content); Logger?.LogTrace(1, "Response:{content}", 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(2, "Encrypt Content:{body}", 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; } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } return(rsp); } }
private JDPayDictionary GetParameters(HttpRequest request, bool isDecrypt = true) { var parameters = new JDPayDictionary(); if (request.Method == "POST") { foreach (var iter in request.Form) { if (!string.IsNullOrEmpty(iter.Value)) { var value = iter.Value.ToString(); if (isDecrypt) { value = iter.Key == SIGN?iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, DesKey); } parameters.Add(iter.Key, value); } } } else { foreach (var iter in request.Query) { if (!string.IsNullOrEmpty(iter.Value)) { var value = iter.Value.ToString(); if (isDecrypt) { value = iter.Key == SIGN?iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, DesKey); } parameters.Add(iter.Key, value); } } } return(parameters); }
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!"); } }
private async Task <JDPayDictionary> GetParametersAsync(HttpRequest request) { var parameters = new JDPayDictionary(); if (request.Method == "POST") { var form = await request.ReadFormAsync(); foreach (var iter in form) { if (!string.IsNullOrEmpty(iter.Value)) { parameters.Add(iter.Key, iter.Key == SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, DesKey)); } } } else { foreach (var iter in request.Query) { if (!string.IsNullOrEmpty(iter.Value)) { parameters.Add(iter.Key, iter.Key == SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, DesKey)); } } } return(parameters); }
private JDPayDictionary GetParameters(HttpRequest request, JDPayOptions options, bool isDecrypt = true) { var dictionary = new JDPayDictionary(); if (request.Method == "POST") { foreach (var iter in request.Form) { if (!string.IsNullOrEmpty(iter.Value)) { var value = iter.Value.ToString(); if (isDecrypt) { value = iter.Key == JDPayContants.SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, options.DesKeyBase64); } dictionary.Add(iter.Key, value); } } } else { foreach (var iter in request.Query) { if (!string.IsNullOrEmpty(iter.Value)) { var value = iter.Value.ToString(); if (isDecrypt) { value = iter.Key == JDPayContants.SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, options.DesKeyBase64); } dictionary.Add(iter.Key, value); } } } return(dictionary); }