Esempio n. 1
0
        /// <summary>
        /// Get rates
        /// </summary>
        /// <param name="request">Request details</param>
        /// <returns>The asynchronous task whose result contains the rates info</returns>
        private async Task <UPSRate.RateResponse> GetRatesAsync(UPSRate.RateRequest request)
        {
            try
            {
                //create client
                var ratePort = _upsSettings.UseSandbox
                    ? UPSRate.RatePortTypeClient.EndpointConfiguration.RatePort
                    : UPSRate.RatePortTypeClient.EndpointConfiguration.ProductionRatePort;
                using (var client = new UPSRate.RatePortTypeClient(ratePort))
                {
                    //create object to authenticate request
                    var security = new UPSRate.UPSSecurity
                    {
                        ServiceAccessToken = new UPSRate.UPSSecurityServiceAccessToken
                        {
                            AccessLicenseNumber = _upsSettings.AccessKey
                        },
                        UsernameToken = new UPSRate.UPSSecurityUsernameToken
                        {
                            Username = _upsSettings.Username,
                            Password = _upsSettings.Password
                        }
                    };

                    //save debug info
                    if (_upsSettings.Tracing)
                    {
                        _logger.Information($"UPS rates. Request: {ToXml(new UPSRate.RateRequest1(security, request))}");
                    }

                    //try to get response details
                    var response = await client.ProcessRateAsync(security, request);

                    //save debug info
                    if (_upsSettings.Tracing)
                    {
                        _logger.Information($"UPS rates. Response: {ToXml(response)}");
                    }

                    return(response.RateResponse);
                }
            }
            catch (FaultException <UPSRate.ErrorDetailType[]> ex)
            {
                //get error details
                var message = ex.Message;
                if (ex.Detail.Any())
                {
                    message = ex.Detail.Aggregate(message, (details, detail) =>
                                                  $"{details}{Environment.NewLine}{detail.Severity} error: {detail.PrimaryErrorCode?.Description}");
                }

                //rethrow exception
                throw new Exception(message, ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create request details to get shipping rates
        /// </summary>
        /// <param name="shippingOptionRequest">Shipping option request</param>
        /// <param name="saturdayDelivery">Whether to get rates for Saturday Delivery</param>
        /// <returns>Rate request details</returns>
        private UPSRate.RateRequest CreateRateRequest(GetShippingOptionRequest shippingOptionRequest, bool saturdayDelivery = false)
        {
            //set request details
            var request = new UPSRate.RateRequest
            {
                Request = new UPSRate.RequestType
                {
                    //used to define the request type
                    //Shop - the server validates the shipment, and returns rates for all UPS products from the ShipFrom to the ShipTo addresses
                    RequestOption = new[] { "Shop" }
                }
            };

            //prepare addresses details
            var stateCodeTo     = shippingOptionRequest.ShippingAddress.StateProvince?.Abbreviation;
            var stateCodeFrom   = shippingOptionRequest.StateProvinceFrom?.Abbreviation;
            var countryCodeFrom = (shippingOptionRequest.CountryFrom ?? _countryService.GetAllCountries().FirstOrDefault())
                                  .TwoLetterIsoCode ?? string.Empty;

            var addressFromDetails = new UPSRate.ShipAddressType
            {
                AddressLine       = new[] { shippingOptionRequest.AddressFrom },
                City              = shippingOptionRequest.CityFrom,
                StateProvinceCode = stateCodeFrom,
                CountryCode       = countryCodeFrom,
                PostalCode        = shippingOptionRequest.ZipPostalCodeFrom
            };
            var addressToDetails = new UPSRate.ShipToAddressType
            {
                AddressLine                 = new[] { shippingOptionRequest.ShippingAddress.Address1, shippingOptionRequest.ShippingAddress.Address2 },
                City                        = shippingOptionRequest.ShippingAddress.City,
                StateProvinceCode           = stateCodeTo,
                CountryCode                 = shippingOptionRequest.ShippingAddress.Country.TwoLetterIsoCode,
                PostalCode                  = shippingOptionRequest.ShippingAddress.ZipPostalCode,
                ResidentialAddressIndicator = string.Empty
            };

            //set shipment details
            request.Shipment = new UPSRate.ShipmentType
            {
                Shipper = new UPSRate.ShipperType
                {
                    ShipperNumber = _upsSettings.AccountNumber,
                    Address       = addressFromDetails
                },
                ShipFrom = new UPSRate.ShipFromType
                {
                    Address = addressFromDetails
                },
                ShipTo = new UPSRate.ShipToType
                {
                    Address = addressToDetails
                }
            };

            //set pickup options and customer classification for US shipments
            if (countryCodeFrom.Equals("US", StringComparison.InvariantCultureIgnoreCase))
            {
                request.PickupType = new UPSRate.CodeDescriptionType
                {
                    Code = GetUpsCode(_upsSettings.PickupType)
                };
                request.CustomerClassification = new UPSRate.CodeDescriptionType
                {
                    Code = GetUpsCode(_upsSettings.CustomerClassification)
                };
            }

            //set negotiated rates details
            if (!string.IsNullOrEmpty(_upsSettings.AccountNumber) && !string.IsNullOrEmpty(stateCodeFrom) && !string.IsNullOrEmpty(stateCodeTo))
            {
                request.Shipment.ShipmentRatingOptions = new UPSRate.ShipmentRatingOptionsType
                {
                    NegotiatedRatesIndicator   = string.Empty,
                    UserLevelDiscountIndicator = string.Empty
                };
            }

            //set saturday delivery details
            if (saturdayDelivery)
            {
                request.Shipment.ShipmentServiceOptions = new UPSRate.ShipmentServiceOptionsType
                {
                    SaturdayDeliveryIndicator = string.Empty
                };
            }

            //set packages details
            switch (_upsSettings.PackingType)
            {
            case PackingType.PackByOneItemPerPackage:
                request.Shipment.Package = GetPackagesForOneItemPerPackage(shippingOptionRequest).ToArray();
                break;

            case PackingType.PackByVolume:
                request.Shipment.Package = GetPackagesByCubicRoot(shippingOptionRequest).ToArray();
                break;

            case PackingType.PackByDimensions:
            default:
                request.Shipment.Package = GetPackagesByDimensions(shippingOptionRequest).ToArray();
                break;
            }

            return(request);
        }