Example #1
0
        public JDPayClient(
            IOptions <JDPayOptions> optionsAccessor,
            ILogger <JDPayClient> logger,
            IHttpClientFactory clientFactory)
        {
            Logger        = logger;
            ClientFactory = clientFactory;
            Options       = optionsAccessor.Value;

            if (string.IsNullOrEmpty(Options.Merchant))
            {
                throw new ArgumentNullException(nameof(Options.Merchant));
            }

            if (string.IsNullOrEmpty(Options.RsaPrivateKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPrivateKey));
            }

            if (string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPublicKey));
            }

            if (string.IsNullOrEmpty(Options.DesKey))
            {
                throw new ArgumentNullException(nameof(Options.DesKey));
            }
        }
Example #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);
            }
        }
Example #3
0
        public Task <T> PageExecuteAsync <T>(IJDPayRequest <T> request, JDPayOptions options) where T : JDPayResponse
        {
            // 字典排序
            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));
        }
Example #4
0
        public async Task <T> ExecuteAsync <T>(IJDPayRequest <T> request, JDPayOptions options) where T : JDPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JDPayDictionary(request.GetParameters());

            var content = BuildEncryptXml(request, sortedTxtParams, options);

            using (var client = _httpClientFactory.CreateClient(nameof(JDPayClient)))
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                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);

                    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), options.PublicKey);
                    var decryptStr             = JDPaySecurity.BytesToString(decryptByte);
                    if (sha256SourceSignString == decryptStr)
                    {
                        rsp         = parser.Parse(reqBody);
                        rsp.Encrypt = encrypt;
                    }
                    else
                    {
                        throw new JDPayException("sign check fail: check Sign and Data Fail!");
                    }
                }
                return(rsp);
            }
        }
Example #5
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!");
            }
        }
Example #6
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");
            }
        }
Example #7
0
        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);
        }
Example #8
0
        private JDPayDictionary BuildEncryptDic <T>(IJDPayRequest <T> request, IDictionary <string, string> dictionary, JDPayOptions options) where T : JDPayResponse
        {
            var signDic = new JDPayDictionary(dictionary)
            {
                { JDPayContants.VERSION, request.GetApiVersion() },
                { JDPayContants.MERCHANT, options.Merchant }
            };

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

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

            foreach (var iter in dictionary)
            {
                if (!string.IsNullOrEmpty(iter.Value))
                {
                    encyptDic.Add(iter.Key, JDPaySecurity.EncryptECB(iter.Value, options.DesKeyBase64));
                }
            }
            return(encyptDic);
        }
Example #9
0
        private string BuildEncryptXml <T>(IJDPayRequest <T> request, JDPayDictionary dictionary, JDPayOptions options) where T : JDPayResponse
        {
            var xmldoc = JDPayUtility.SortedDictionary2AllXml(dictionary);
            var smlStr = JDPayUtility.ConvertXmlToString(xmldoc);
            var sha256SourceSignString = SHA256.Compute(smlStr);
            var encyptBytes            = RSA_ECB_PKCS1Padding.Encrypt(Encoding.UTF8.GetBytes(sha256SourceSignString), options.PrivateKey);
            var sign    = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks);
            var data    = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>");
            var encrypt = JDPaySecurity.EncryptECB(data, options.DesKeyBase64);
            // 字典排序
            var reqdic = new JDPayDictionary
            {
                { JDPayContants.VERSION, request.GetApiVersion() },
                { JDPayContants.MERCHANT, options.Merchant },
                { JDPayContants.ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JDPayUtility.SortedDictionary2XmlStr(reqdic));
        }
Example #10
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request, JDPayOptions options) where T : JDPayNotify
        {
            if (request.HasFormContentType || request.Method == "GET")
            {
                var rspInstance = Activator.CreateInstance <T>();

                var parameters = GetParameters(request, options, !(rspInstance is JDPayDefrayPayNotify));

                var parser = new JDPayDictionaryParser <T>();
                var rsp    = parser.Parse(parameters);

                if (rsp is JDPayDefrayPayNotify)
                {
                    CheckNotifyDefrayPaySign(rsp.Parameters, options);
                }
                else
                {
                    CheckNotifySign(rsp.Parameters, options);
                }

                return(rsp);
            }

            if (request.HasTextXmlContentType())
            {
                var body   = await new StreamReader(request.Body).ReadToEndAsync();
                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);

                    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), options.PublicKey);
                    var decryptStr             = JDPaySecurity.BytesToString(decryptByte);
                    if (sha256SourceSignString == decryptStr)
                    {
                        rsp         = parser.Parse(reqBody);
                        rsp.Encrypt = encrypt;
                        return(rsp);
                    }

                    throw new JDPayException("sign check fail: check Sign and Data Fail!");
                }

                throw new JDPayException("encrypt is Empty!");
            }

            throw new JDPayException("content type is not supported!");
        }