Exemplo n.º 1
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JdPayNotifyResponse
        {
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                var key     = Convert.FromBase64String(Options.DesKey);
                var signDic = new JdPayDictionary();
                foreach (var item in request.Form)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                    {
                        if (item.Key == "sign")
                        {
                            signDic.Add(item.Key, item.Value);
                        }
                        else
                        {
                            signDic.Add(item.Key, DES3.DecryptECB(key, item.Value));
                        }
                    }
                }

                var parser = new JdPayDictionaryParser <T>();
                var rsp    = parser.Parse(signDic);

                CheckNotifySign(signDic, RSAPrivateParameters);
                return(rsp);
            }
            else
            {
                var body = await new StreamReader(request.Body).ReadToEndAsync();
                var rsp  = DecryptResponseXml <T>(JdPayUtil.FotmatXmlString(body));
                return(rsp);
            }
        }
Exemplo n.º 2
0
        public async Task <T> ExecuteAsync <T>(IJDPayRequest <T> request) where T : JDPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JDPayDictionary(request.GetParameters());

            var content = BuildEncryptXml(request, sortedTxtParams);

            Logger.LogInformation(0, "Request:{content}", content);

            var rspContent = await Client.DoPostAsync(request.GetRequestUrl(), content);

            Logger.LogInformation(1, "Response:{content}", rspContent);

            var parser = new JDPayXmlParser <T>();
            var rsp    = parser.Parse(JDPayUtility.FotmatXmlString(rspContent));

            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(2, "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);
        }
Exemplo n.º 3
0
        private T DecryptResponseXmlContent <T>(string xml) where T : JdPayResponse
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new Exception("Content is Empty!");
            }

            var entity = JdPayUtil.Deserialize <T>(xml);

            if (!string.IsNullOrEmpty(entity?.Encrypt))
            {
                var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(entity.Encrypt));
                var reqBody          = DES3.DecryptECB(DesKey, base64EncryptStr);

                var reqBodyDoc = new XmlDocument();
                reqBodyDoc.LoadXml(reqBody);

                var inputSign = JdPayUtil.GetValue(reqBodyDoc, "sign");
                var jdpayRoot = reqBodyDoc.SelectSingleNode("jdpay");
                var signNode  = jdpayRoot.SelectSingleNode("sign");
                jdpayRoot.RemoveChild(signNode);

                var reqBodyStr = JdPayUtil.ConvertXmlToString(reqBodyDoc);
                var xmlh       = xml.Substring(0, xml.IndexOf("<jdpay>"));
                if (!string.IsNullOrEmpty(xmlh))
                {
                    reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh);
                }
                if (JdPaySignature.RSACheckContent(reqBodyStr, inputSign, RSAPublicParameters))
                {
                    entity = JdPayUtil.Deserialize <T>(reqBody);
                }
                else
                {
                    throw new Exception("sign check fail: check Sign and Data Fail!");
                }

                entity.Body = reqBody;
            }
            else
            {
                entity.Body = xml;
            }
            return(entity);
        }
Exemplo n.º 4
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JdPayNotifyResponse
        {
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                var dic = new JdPayDictionary();
                foreach (var item in request.Form)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                    {
                        if (item.Key == "sign")
                        {
                            dic.Add(item.Key, item.Value);
                        }
                        else
                        {
                            dic.Add(item.Key, DES3.DecryptECB(DesKey, item.Value));
                        }
                    }
                }

                var query = HttpClientEx.BuildQuery(dic);
                Logger.LogInformation(1, "Request Content:{query}", query);

                var parser = new JdPayDictionaryParser <T>();
                var rsp    = parser.Parse(dic);

                CheckNotifySign(dic, RSAPrivateParameters);
                return(rsp);
            }
            else
            {
                var body = await new StreamReader(request.Body).ReadToEndAsync();
                Logger.LogInformation(1, "Request Content:{body}", body);

                var rsp = DecryptResponseXml <T>(JdPayUtil.FotmatXmlString(body));
                return(rsp);
            }
        }
Exemplo n.º 5
0
        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!");
            }
        }
Exemplo n.º 6
0
        private async Task <JDPayDictionary> GetParametersAsync(HttpRequest request)
        {
            var parameters = new JDPayDictionary();
            var form       = await request.ReadFormAsync();

            foreach (var item in form)
            {
                parameters.Add(item.Key, item.Key.Equals(SIGN) ? item.Value.ToString() : DES3.DecryptECB(item.Value, DesKey));
            }
            return(parameters);
        }