Пример #1
0
        public WeChatPayClient(
            IOptions <WeChatPayOptions> optionsAccessor,
            ILogger <WeChatPayClient> logger)
        {
            this.logger = logger;
            try
            {
                logger?.LogDebug($"{DateTime.Now} 微信支付初始化日志(1)");

                Options = optionsAccessor.Value;

                Client = new HttpClientEx();

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

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

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

                if (!string.IsNullOrEmpty(Options.Certificate))
                {
                    logger.LogInformation($"{DateTime.Now} 微信支付初始化日志(3) {Environment.CurrentDirectory}/{Options.Certificate}");

                    var certificate = $"{Environment.CurrentDirectory}/{Options.Certificate}";

                    logger.LogInformation($"{DateTime.Now} 微信支付初始化日志(4) {certificate}");

                    var clientHandler = new HttpClientHandler();

                    logger.LogInformation($"{DateTime.Now} 微信支付初始化日志(5) {Options.MchId}");

                    clientHandler.ClientCertificates.Add(
                        File.Exists(certificate) ? new X509Certificate2(certificate, Options.MchId, X509KeyStorageFlags.MachineKeySet) :
                        new X509Certificate2(Convert.FromBase64String(certificate), Options.MchId, X509KeyStorageFlags.MachineKeySet));

                    logger.LogInformation($"{DateTime.Now} 微信支付初始化日志(6)");

                    CertificateClient = new HttpClientEx(clientHandler);
                }

                logger.LogInformation($"{DateTime.Now} 微信支付初始化日志(7)");
                if (!string.IsNullOrEmpty(Options.RsaPublicKey))
                {
                    PublicKey = RSAUtilities.GetPublicKeyParameterFormAsn1PublicKey(Options.RsaPublicKey);
                }
            }
            catch (Exception ex)
            {
                logger.LogInformation($"{DateTime.Now} 微信支付初始化日志报错{ex.Message}", ex.Message);
            }
        }
Пример #2
0
        public WeChatPayClient(
            IOptions <WeChatPayOptions> optionsAccessor,
            ILogger <WeChatPayClient> logger)
        {
            Options = optionsAccessor.Value;
            Logger  = logger;
            Client  = new HttpClientEx();

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

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

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

            if (!string.IsNullOrEmpty(Options.Certificate))
            {
                var clientHandler = new HttpClientHandler();
                clientHandler.ClientCertificates.Add(
                    File.Exists(Options.Certificate) ? new X509Certificate2(Options.Certificate, Options.MchId) :
                    new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId, X509KeyStorageFlags.MachineKeySet));
                CertificateClient = new HttpClientEx(clientHandler);
            }

            if (!string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                PublicKey = RSAUtilities.GetPublicKeyParameterFormAsn1PublicKey(Options.RsaPublicKey);
            }
        }