Exemplo n.º 1
0
        private JDPayDictionary BuildEncryptDic <T>(IJDPayRequest <T> request, IDictionary <string, string> dic) where T : JDPayResponse
        {
            var signDic = new JDPayDictionary(dic)
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
            };

            var signContent = JDPaySecurity.GetSignContent(signDic);
            var sign        = JDPaySecurity.RSASign(signContent, PrivateKey);

            var encyptDic = new JDPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { SIGN, sign }
            };

            foreach (var item in dic)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    encyptDic.Add(item.Key, JDPaySecurity.EncryptECB(item.Value, DesKey));
                }
            }
            return(encyptDic);
        }
Exemplo n.º 2
0
        public async Task <T> ExecuteAsync <T>(IJDPayNPP10Request <T> request, JDPayOptions options) where T : JDPayResponse
        {
            var sortedTxtParams = new JDPayDictionary(request.GetParameters())
            {
                { JDPayContants.CUSTOMER_NO, options.CustomerNo },
                { JDPayContants.SIGN_TYPE, options.SignType }
            };

            var isEncrypt = false;

            if (request is JDPayDefrayPayRequest)
            {
                isEncrypt = true;
            }

            var encryptDic = JDPaySecurity.EncryptData(options.PrivateCret, options.Password, options.PublicCert, sortedTxtParams, options.SingKey, options.EncryptType, isEncrypt);

            var content = JDPayUtility.BuildQuery(encryptDic);

            using (var client = _httpClientFactory.CreateClient(nameof(JDPayClient)))
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content, "application/x-www-form-urlencoded");

                // 验签
                var dictionary = JsonConvert.DeserializeObject <JDPayDictionary>(body);
                if (!JDPaySecurity.VerifySign(dictionary, options.SingKey))
                {
                    throw new JDPayException("sign check fail: check Sign and Data Fail!");
                }

                var rsp = JsonConvert.DeserializeObject <T>(body);
                rsp.Body = body;
                return(rsp);
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public Task <T> PageExecuteAsync <T>(IJDPayRequest <T> request, string reqMethod) where T : JDPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JDPayDictionary(request.GetParameters());
            var encyptParams    = BuildEncryptDic(request, sortedTxtParams);
            var rsp             = Activator.CreateInstance <T>();

            var url = request.GetRequestUrl();

            if (reqMethod == "GET")
            {
                //拼接get请求的url
                var tmpUrl = url;
                if (encyptParams != null && encyptParams.Count > 0)
                {
                    if (tmpUrl.Contains("?"))
                    {
                        tmpUrl = tmpUrl + "&" + HttpClientEx.BuildQuery(encyptParams);
                    }
                    else
                    {
                        tmpUrl = tmpUrl + "?" + HttpClientEx.BuildQuery(encyptParams);
                    }
                }
                rsp.Body = tmpUrl;
            }
            else
            {
                //输出post表单
                rsp.Body = BuildHtmlRequest(url, encyptParams);
            }

            return(Task.FromResult(rsp));
        }
Exemplo n.º 5
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?.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);
            }
        }
Exemplo n.º 6
0
        public Task <T> PageExecuteAsync <T>(IJDPayRequest <T> request) where T : JDPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JDPayDictionary(request.GetParameters());
            var encyptParams    = BuildEncryptDic(request, sortedTxtParams);
            var rsp             = Activator.CreateInstance <T>();

            //输出post表单
            rsp.Body = BuildHtmlRequest(request, encyptParams);
            return(Task.FromResult(rsp));
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public Task <T> PageExecuteAsync <T>(IJDPayRequest <T> request, string optionsName) where T : JDPayResponse
        {
            var options = string.IsNullOrEmpty(optionsName) ? _optionsSnapshotAccessor.Value : _optionsSnapshotAccessor.Get(optionsName);
            // 字典排序
            var sortedTxtParams = new JDPayDictionary(request.GetParameters());
            var encyptParams    = BuildEncryptDic(request, sortedTxtParams, options);
            var rsp             = Activator.CreateInstance <T>();

            //输出post表单
            rsp.Body = BuildHtmlRequest(request, encyptParams);
            return(Task.FromResult(rsp));
        }
Exemplo n.º 9
0
        private string BuildHtmlRequest(string url, JDPayDictionary dicPara)
        {
            var sbHtml = new StringBuilder();

            sbHtml.Append("<form id='submit' name='submit' action='" + url + "' method='post' style='display:none;'>");
            foreach (var temp in dicPara)
            {
                sbHtml.Append("<input  name='" + temp.Key + "' value='" + temp.Value + "'/>");
            }
            sbHtml.Append("<input type='submit' style='display:none;'></form>");
            sbHtml.Append("<script>document.forms['submit'].submit();</script>");
            return(sbHtml.ToString());
        }
Exemplo n.º 10
0
        private void CheckNotifyDefrayPaySign(JDPayDictionary parameters)
        {
            if (parameters.Count == 0)
            {
                throw new Exception("sign check fail: parameters is Empty!");
            }

            if (!parameters.TryGetValue(JDPayContants.SIGN_DATA, out var sign_data))
            {
                throw new Exception("sign check fail: sign is Empty!");
            }

            if (!JDPaySecurity.VerifySign(parameters, Options.SingKey))
            {
                throw new Exception("sign check fail: check Sign and Data Fail!");
            }
        }
Exemplo n.º 11
0
        private void CheckNotifyDefrayPaySign(JDPayDictionary dictionary, JDPayOptions options)
        {
            if (dictionary.Count == 0)
            {
                throw new JDPayException("sign check fail: dictionary is Empty!");
            }

            if (!dictionary.TryGetValue(JDPayContants.SIGN_DATA, out var sign_data))
            {
                throw new JDPayException("sign check fail: sign is Empty!");
            }

            if (!JDPaySecurity.VerifySign(dictionary, options.SingKey))
            {
                throw new JDPayException("sign check fail: check Sign and Data Fail!");
            }
        }
Exemplo n.º 12
0
        private string BuildEncryptXml <T>(IJDPayRequest <T> request, JDPayDictionary dic) where T : JDPayResponse
        {
            var xmldoc = JDPayUtility.SortedDictionary2AllXml(dic);
            var smlStr = JDPayUtility.ConvertXmlToString(xmldoc);
            var sha256SourceSignString = SHA256.Compute(smlStr);
            var encyptBytes            = RSA_ECB_PKCS1Padding.Encrypt(Encoding.UTF8.GetBytes(sha256SourceSignString), PrivateKey);
            var sign    = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks);
            var data    = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>");
            var encrypt = JDPaySecurity.EncryptECB(data, DesKey);
            // 字典排序
            var reqdic = new JDPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JDPayUtility.SortedDictionary2XmlStr(reqdic));
        }
Exemplo n.º 13
0
        private void CheckNotifySign(JDPayDictionary parameters)
        {
            if (parameters.Count == 0)
            {
                throw new Exception("sign check fail: parameters is Empty!");
            }

            if (!parameters.TryGetValue(JDPayContants.SIGN, out var sign))
            {
                throw new Exception("sign check fail: sign is Empty!");
            }

            var signContent = JDPaySecurity.GetSignContent(parameters);

            if (!JDPaySecurity.RSACheckContent(signContent, sign, Options.PublicKey))
            {
                throw new Exception("sign check fail: check Sign and Data Fail");
            }
        }
Exemplo n.º 14
0
        private void CheckNotifySign(JDPayDictionary dictionary, JDPayOptions options)
        {
            if (dictionary.Count == 0)
            {
                throw new JDPayException("sign check fail: dictionary is Empty!");
            }

            if (!dictionary.TryGetValue(JDPayContants.SIGN, out var sign))
            {
                throw new JDPayException("sign check fail: sign is Empty!");
            }

            var signContent = JDPaySecurity.GetSignContent(dictionary);

            if (!JDPaySecurity.RSACheckContent(signContent, sign, options.PublicKey))
            {
                throw new JDPayException("sign check fail: check Sign and Data Fail");
            }
        }
Exemplo n.º 15
0
        public async Task <T> ExecuteAsync <T>(IJDPayNPP10Request <T> request) where T : JDPayResponse
        {
            var sortedTxtParams = new JDPayDictionary(request.GetParameters())
            {
                { Contants.CUSTOMER_NO, Options.CustomerNo },
                { Contants.SIGN_TYPE, Options.SignType }
            };

            var isEncrypt = false;

            if (request is JDPayDefrayPayRequest)
            {
                isEncrypt = true;
            }

            var encryptDic = JDPaySecurity.EncryptData(Options.PrivateCret, Options.Password, Options.PublicCert, sortedTxtParams, Options.SingKey, Options.EncryptType, isEncrypt);

            var content = HttpClientEx.BuildQuery(encryptDic);

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

            var body = await Client.DoPostAsync(request.GetRequestUrl(), content, "application/x-www-form-urlencoded");

            Logger?.LogTrace(1, "Response:{content}", body);

            var rsp = JsonConvert.DeserializeObject <T>(body);

            // 验签
            var dic = JsonConvert.DeserializeObject <JDPayDictionary>(body);

            if (!JDPaySecurity.VerifySign(dic, Options.SingKey))
            {
                throw new Exception("sign check fail: check Sign and Data Fail!");
            }

            rsp.Body = body;
            return(rsp);
        }
Exemplo n.º 16
0
        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 == JDPayContants.SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, Options.DesKeyBase64);
                        }
                        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 == JDPayContants.SIGN ? iter.Value.ToString() : JDPaySecurity.DecryptECB(iter.Value, Options.DesKeyBase64);
                        }
                        parameters.Add(iter.Key, value);
                    }
                }
            }
            return(parameters);
        }