public async Task <ValidateAddress> ValidateAddressAsync(ValidateAddress request, CancellationToken cancellationToken = default)
    {
        try
        {
            // don't waste resources
            if (!AddressAttributes.SupportedCountries.ContainsKey(request.OriginalAddress.CountryCode))
            {
                return(request);
            }

            var client = new v4.AddressValidationPortTypeClient(
                v4.AddressValidationPortTypeClient.EndpointConfiguration.AddressValidationServicePort,
                _options.Url);

            var wrap = CreateRequest(request);

            var serviceRequest = new v4.addressValidationRequest1(wrap);

            var reply = await client.addressValidationAsync(serviceRequest);

            if (reply.AddressValidationReply.HighestSeverity == v4.NotificationSeverityType.SUCCESS ||
                reply.AddressValidationReply.HighestSeverity == v4.NotificationSeverityType.NOTE ||
                reply.AddressValidationReply.HighestSeverity == v4.NotificationSeverityType.WARNING)
            {
                var result           = reply.AddressValidationReply;
                var addressResults   = result.AddressResults[0];
                var effectiveAddress = addressResults.EffectiveAddress;
                var parsedAddress    = addressResults.ParsedAddressPartsDetail;
                var lines            = effectiveAddress?.StreetLines ?? new string[1] {
                    string.Empty
                };

                request.ProposedAddress = new Address(
                    lines[0],
                    effectiveAddress?.City ?? string.Empty,
                    effectiveAddress?.StateOrProvinceCode ?? string.Empty,
                    effectiveAddress?.PostalCode ?? string.Empty,
                    effectiveAddress?.CountryCode ?? string.Empty,
                    effectiveAddress?.Residential ?? true);

                if (lines.Length == 2)
                {
                    request.ProposedAddress.StreetLine2 = lines[1];
                }

                request.ProposedAddress.IsResidential = addressResults.Classification switch
                {
                    v4.FedExAddressClassificationType.MIXED => false,
                    v4.FedExAddressClassificationType.BUSINESS => false,
                    _ => true,
                };

                if (!request.ValidationBag.ContainsKey("Classification"))
                {
                    request.ValidationBag.Add("Classification", addressResults.Classification.ToString());
                }

                if (!request.ValidationBag.ContainsKey("State"))
                {
                    request.ValidationBag.Add("State", addressResults.State.ToString());
                }

                foreach (var a in addressResults.Attributes)
                {
                    if (!request.ValidationBag.ContainsKey(a.Name))
                    {
                        request.ValidationBag.Add(a.Name, a.Value);
                    }
                }
            }
            else
            {
                var exMsg = string.Empty;
                foreach (var notification in reply.AddressValidationReply.Notifications)
                {
                    exMsg += notification.Message;
                    request.Errors.Add(new Error
                    {
                        Source      = notification.Source,
                        Number      = notification.Code,
                        Description = notification.Message
                    });
                }
            }
        }
        catch (Exception ex)
        {
            var exMsg = "FedExValidation client failed";
            _logger.LogError(ex, exMsg);
            request.InternalErrors.Add(ex?.Message ?? exMsg);
        }

        return(request);
    }
Example #2
0
 private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
 {
     return(AddressValidationPortTypeClient.GetEndpointAddress(EndpointConfiguration.AddressValidationServicePort));
 }
Example #3
0
 private static System.ServiceModel.Channels.Binding GetDefaultBinding()
 {
     return(AddressValidationPortTypeClient.GetBindingForEndpoint(EndpointConfiguration.AddressValidationServicePort));
 }
Example #4
0
 public AddressValidationPortTypeClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
     base(AddressValidationPortTypeClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
Example #5
0
 public AddressValidationPortTypeClient(EndpointConfiguration endpointConfiguration) :
     base(AddressValidationPortTypeClient.GetBindingForEndpoint(endpointConfiguration), AddressValidationPortTypeClient.GetEndpointAddress(endpointConfiguration))
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
Example #6
0
 public AddressValidationPortTypeClient() :
     base(AddressValidationPortTypeClient.GetDefaultBinding(), AddressValidationPortTypeClient.GetDefaultEndpointAddress())
 {
     this.Endpoint.Name = EndpointConfiguration.AddressValidationServicePort.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }