public async Task InvalidateCreditCardAsync_ForValidToken_ReturnsOK(CartaoProtegidoClient sut)
        {
            var saveRequest = new SaveCreditCardRequest
            {
                CustomerName           = "Bjorn Ironside",
                CustomerIdentification = "762.502.520-96",
                CardHolder             = "BJORN IRONSIDE",
                CardExpiration         = "10/2025",
                CardNumber             = "1000100010001000"
            };

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

            Assert.Equal(HttpStatusCode.OK, saveResponse.HttpStatus);
            Assert.Empty(saveResponse.ErrorDataCollection);
            Assert.NotNull(saveResponse.JustClickKey);

            var invalidateRequest = new InvalidateCreditCardRequest
            {
                JustClickKey = saveResponse.JustClickKey
            };

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

            Assert.Equal(HttpStatusCode.OK, invalidateResponse.HttpStatus);
        }
        public async Task InvalidateCreditCardAsync_ForInvalidToken_ReturnsErrorMessage(CartaoProtegidoClient sut)
        {
            var request = new InvalidateCreditCardRequest
            {
                JustClickKey = Guid.NewGuid().ToString()
            };

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

            Assert.Equal(HttpStatusCode.OK, response.HttpStatus);
            Assert.NotEmpty(response.ErrorDataCollection);
        }
Esempio n. 3
0
        public async Task <InvalidateCreditCardResponse> InvalidateCreditCardAsync(InvalidateCreditCardRequest 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("<InvalidateCreditCard xmlns=\"http://www.cartaoprotegido.com.br/WebService/\">");
            sb.AppendLine("<invalidateCreditCardRequestWS>");
            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("</invalidateCreditCardRequestWS>");
            sb.AppendLine("</InvalidateCreditCard>");
            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 InvalidateCreditCardResponse
                {
                    HttpStatus = httpResponse.StatusCode
                });
            }

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

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