Exemplo n.º 1
0
        public async Task <AvailableInstallmentPlansResponse> AvailableInstallmentPlansAsync(string authorizationKey, AvailableInstallmentPlansRequest request)
        {
            HttpRequestMessage requestMessage = BuildRequest(authorizationKey, "api/v3/lookup/installment-plans", HttpMethod.Post);
            var requestDto = _mapper.Map <AvailableInstallmentPlansRequestDto>(request);

            string content = await SerializeDto(requestDto);

            requestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");

            HttpResponseMessage responseMessage = await CallAsync(() => Client.SendAsync(requestMessage), true);

            if (responseMessage == null)
            {
                _log.Error(new { message = "AvailableInstallmentPlans query failed. Service response was NULL or not OK", request });
                return(null);
            }

            if (responseMessage.StatusCode != HttpStatusCode.OK)
            {
                ResponseMessageDto[] errorList = await ParseJsonArrayAsync <ResponseMessageDto>(responseMessage);

                _log.Warning(new { message = "AvailableInstallmentPlans query faild. ", request, responseMessage.StatusCode, errorList });
                ResponseMessageDto firstError = errorList.FirstOrDefault();
                return(new AvailableInstallmentPlansResponse
                {
                    HasError = true,
                    ErrorCode = firstError?.Code,
                    ErrorMessage = firstError?.CustomerFacingMessage
                });
            }

            AvailableInstallmentPlansResponseDto responseDto = await ParseJsonAsync <AvailableInstallmentPlansResponseDto>(responseMessage);

            if (responseDto.HasError)
            {
                _log.Error(new { message = "AvailableInstallmentPlans query failed", request, responseDto.ErrorMessage });
                return(new AvailableInstallmentPlansResponse
                {
                    HasError = true,
                    ErrorMessage = responseDto.ErrorMessage
                });
            }

            var result = _mapper.Map <AvailableInstallmentPlansResponse>(responseDto);

            _log.Debug(new { message = "AvailableInstallmentPlans query succeeded", request.Amount, result });

            return(result);
        }
Exemplo n.º 2
0
        private string GetErrorMessage(ResponseMessageDto[] errorList, out string errorCode)
        {
            if (!errorList.Any())
            {
                errorCode = string.Empty;
                return(string.Empty);
            }

            ResponseMessageDto userError = errorList.FirstOrDefault(e => IsUserError(e.Code));

            if (userError != null)
            {
                errorCode = userError.Code;
                return(userError.CustomerFacingMessage);
            }

            ResponseMessageDto firstError = errorList.First();

            errorCode = firstError.Code;
            return(firstError.CustomerFacingMessage);
        }