public UnionPayClient(
            ILogger <UnionPayClient> logger,
            IHttpClientFactory clientFactory,
            IOptions <UnionPayOptions> optionsAccessor)
        {
            Logger        = logger;
            ClientFactory = clientFactory;
            Options       = optionsAccessor.Value;

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

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

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

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

            if (string.IsNullOrEmpty(Options.RootCert))
            {
                throw new ArgumentNullException(nameof(Options.RootCert));
            }
        }
Example #2
0
        public async Task <T> ExecuteAsync <T>(IUnionPayRequest <T> request, UnionPayOptions options) where T : UnionPayResponse
        {
            var version = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion();

            var merId = options.MerId;

            if (options.TestMode && (request is UnionPayGatewayPayFileTransferRequest || request is UnionPayNoRedirectPayFileTransferRequest || request is UnionPayQrCodePayFileTransferRequest || request is UnionPayWapPayFileTransferRequest))
            {
                merId = "700000000000001";
            }

            var txtParams = new UnionPayDictionary(request.GetParameters())
            {
                { VERSION, version },
                { ENCODING, options.Encoding },
                { SIGNMETHOD, options.SignMethod },
                { ACCESSTYPE, options.AccessType },
                { MERID, merId }
            };

            if (request.HasEncryptCertId() && options.EncryptCertificate?.key != null)
            {
                if (txtParams.TryGetValue(ACCNO, out var accNo))
                {
                    if (!string.IsNullOrEmpty(accNo))
                    {
                        // 对敏感信息加密
                        txtParams[ACCNO] = UnionPaySignature.EncryptData(accNo, options.EncryptCertificate.key);

                        txtParams.Add(ENCRYPTCERTID, options.EncryptCertificate.certId);
                    }
                }
            }

            UnionPaySignature.Sign(txtParams, options.SignCertificate.certId, options.SignCertificate.key, options.SecureKey);

            var query = UnionPayUtility.BuildQuery(txtParams);

            using (var client = _httpClientFactory.CreateClient(nameof(UnionPayClient)))
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(options.TestMode), query);

                var dictionary = ParseQueryString(body);

                var ifValidateCNName = !options.TestMode;
                if (!UnionPaySignature.Validate(dictionary, options.RootCertificate.cert, options.MiddleCertificate.cert, options.SecureKey, ifValidateCNName))
                {
                    throw new UnionPayException("sign check fail: check Sign and Data Fail!");
                }

                var parser = new UnionPayDictionaryParser <T>();
                var rsp    = parser.Parse(dictionary);
                rsp.Body = body;
                return(rsp);
            }
        }
Example #3
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request, UnionPayOptions options) where T : UnionPayNotify
        {
            var parameters = await GetParametersAsync(request);

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

            CheckNotifySign(parameters, options);
            return(rsp);
        }
        private void CheckNotifySign(UnionPayDictionary dictionary, UnionPayOptions options)
        {
            if (dictionary == null || dictionary.Count == 0)
            {
                throw new UnionPayException("sign check fail: sign is Empty!");
            }

            var ifValidateCNName = !options.TestMode;

            if (!UnionPaySignature.Validate(dictionary, options.RootCertificate.cert, options.MiddleCertificate.cert, options.SecureKey, ifValidateCNName))
            {
                throw new UnionPayException("sign check fail: check Sign and Data Fail!");
            }
        }
        public UnionPayNotifyClient(
            ILogger <UnionPayNotifyClient> logger,
            IOptions <UnionPayOptions> optionsAccessor)
        {
            Logger  = logger;
            Options = optionsAccessor.Value;

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

            if (string.IsNullOrEmpty(Options.RootCert))
            {
                throw new ArgumentNullException(nameof(Options.RootCert));
            }
        }
Example #6
0
        public Task <T> PageExecuteAsync <T>(IUnionPayRequest <T> request, UnionPayOptions options) where T : UnionPayResponse
        {
            var version = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion();
            var merId   = options.MerId;

            if (options.TestMode && (request is UnionPayGatewayPayFileTransferRequest || request is UnionPayNoRedirectPayFileTransferRequest || request is UnionPayQrCodePayFileTransferRequest || request is UnionPayWapPayFileTransferRequest))
            {
                merId = "700000000000001";
            }

            var txtParams = new UnionPayDictionary(request.GetParameters())
            {
                { VERSION, version },
                { ENCODING, options.Encoding },
                { SIGNMETHOD, options.SignMethod },
                { ACCESSTYPE, options.AccessType },
                { MERID, merId }
            };

            if (request.HasEncryptCertId() && options.EncryptCertificate?.key != null)
            {
                if (txtParams.TryGetValue(ACCNO, out var accNo))
                {
                    if (!string.IsNullOrEmpty(accNo))
                    {
                        // 对敏感信息加密
                        txtParams[ACCNO] = UnionPaySignature.EncryptData(accNo, options.EncryptCertificate.key);

                        txtParams.Add(ENCRYPTCERTID, options.EncryptCertificate.certId);
                    }
                }
            }

            UnionPaySignature.Sign(txtParams, options.SignCertificate.certId, options.SignCertificate.key, options.SecureKey);

            var rsp = Activator.CreateInstance <T>();

            var url = request.GetRequestUrl(options.TestMode);

            //输出post表单
            rsp.Body = BuildHtmlRequest(url, txtParams);
            return(Task.FromResult(rsp));
        }