private static async Task <GetCreditCardResponse> GetCreditCardAsync(ICartaoProtegidoClient client, string token)
        {
            var request = new GetCreditCardRequest
            {
                JustClickKey = token,
                RequestId    = Guid.NewGuid()
            };

            return(await client.GetCreditCardAsync(request));
        }
        public async Task GetCreditCardAsync_ForNullToken_ReturnsInternalServerError(CartaoProtegidoClient sut)
        {
            var request = new GetCreditCardRequest
            {
                JustClickKey = null
            };

            var response = await sut.GetCreditCardAsync(request, new MerchantCredentials { MerchantKey = "106c8a0c-89a4-4063-bf50-9e6c8530593b" });

            Assert.Equal(HttpStatusCode.InternalServerError, response.HttpStatus);
        }
        public async Task GetCreditCardAsync_ForInvalidToken_ReturnsErrorMessage(CartaoProtegidoClient sut)
        {
            var request = new GetCreditCardRequest
            {
                JustClickKey = "1ff03ed9-0000-0000-0000-23b700000000"
            };

            var response = await sut.GetCreditCardAsync(request, new MerchantCredentials { MerchantKey = "106c8a0c-89a4-4063-bf50-9e6c8530593b" });

            Assert.Equal(HttpStatusCode.OK, response.HttpStatus);
            Assert.NotEmpty(response.ErrorDataCollection);
        }
        public async Task GetCreditCardAsync_ForValidToken_ReturnsCardData(CartaoProtegidoClient sut)
        {
            var request = new GetCreditCardRequest
            {
                JustClickKey = "1ff03ed9-5f56-4ac6-bfb8-23b7a1aa55a7",
                RequestId    = Guid.NewGuid()
            };

            var response = await sut.GetCreditCardAsync(request, new MerchantCredentials { MerchantKey = "106c8a0c-89a4-4063-bf50-9e6c8530593b" });

            Assert.Equal(HttpStatusCode.OK, response.HttpStatus);
            Assert.Equal("4539321573193671", response.CardNumber);
            Assert.Equal("453932******3671", response.MaskedCardNumber);
            Assert.Equal("06/2020", response.CardExpiration);
            Assert.Equal("TESTE TESTETESTE", response.CardHolder);
            Assert.Equal(request.RequestId.Value, response.CorrelationId.Value);
        }
Ejemplo n.º 5
0
        public async Task <GetCreditCardResponse> GetCreditCardAsync(GetCreditCardRequest request, MerchantCredentials credentials = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_credentials == null && credentials == null)
            {
                throw new InvalidOperationException("Credentials are null");
            }

            var currentCredentials = credentials ?? _credentials;

            if (string.IsNullOrWhiteSpace(currentCredentials.MerchantKey))
            {
                throw new InvalidOperationException("Invalid credentials: MerchantKey is null");
            }

            var httpRequest = new RestRequest(@"v2/cartaoprotegido.asmx", Method.POST)
            {
                RequestFormat = DataFormat.Xml,
                XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer()
            };

            var sb = new StringBuilder();

            sb.AppendLine("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            sb.AppendLine("<soap:Body>");
            sb.AppendLine("<GetCreditCard xmlns=\"http://www.cartaoprotegido.com.br/WebService/\">");
            sb.AppendLine("<getCreditCardRequestWS>");
            sb.AppendLine(request.RequestId.HasValue
                ? $"<RequestId>{request.RequestId.Value}</RequestId>"
                : $"<RequestId>{Guid.NewGuid()}</RequestId>");
            sb.AppendLine($"<MerchantKey>{currentCredentials.MerchantKey}</MerchantKey>");
            sb.AppendLine($"<JustClickKey>{request.JustClickKey}</JustClickKey>");
            sb.AppendLine($"<JustClickAlias>{request.JustClickAlias}</JustClickAlias>");
            sb.AppendLine("</getCreditCardRequestWS>");
            sb.AppendLine("</GetCreditCard>");
            sb.AppendLine("</soap:Body>");
            sb.AppendLine("</soap:Envelope>");

            httpRequest.AddParameter("text/xml", sb.ToString(), ParameterType.RequestBody);

            var cancellationTokenSource = new CancellationTokenSource();

            var httpResponse = await RestClient.ExecuteTaskAsync(httpRequest, cancellationTokenSource.Token);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                return(new GetCreditCardResponse
                {
                    HttpStatus = httpResponse.StatusCode
                });
            }

            var jsonResponse = XmlDeserializer.Deserialize <GetCreditCardResponse>(httpResponse);

            jsonResponse.HttpStatus = httpResponse.StatusCode;
            return(jsonResponse);
        }