public async Task GetMaskedCreditCardAsync_ForNullToken_ReturnsInternalServerError(CartaoProtegidoClient sut)
        {
            var request = new GetMaskedCreditCardRequest
            {
                JustClickKey = null
            };

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

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

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

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

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

            Assert.Equal(HttpStatusCode.OK, response.HttpStatus);
            Assert.Equal("453932******3671", response.MaskedCardNumber);
            Assert.Equal("06/2020", response.CardExpiration);
            Assert.Equal("TESTE TESTETESTE", response.CardHolder);
        }
Beispiel #4
0
        public async Task <GetMaskedCreditCardResponse> GetMaskedCreditCardAsync(GetMaskedCreditCardRequest 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("<GetMaskedCreditCard xmlns=\"http://www.cartaoprotegido.com.br/WebService/\">");
            sb.AppendLine("<getMaskedCreditCardRequestWS>");
            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("</getMaskedCreditCardRequestWS>");
            sb.AppendLine("</GetMaskedCreditCard>");
            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 GetMaskedCreditCardResponse
                {
                    HttpStatus = httpResponse.StatusCode
                });
            }

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

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