コード例 #1
0
        public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
        {
            var response = new GetShippingOptionResponse();

            if (_glsSettings.Tracing)
            {
                _traceMessages.AppendLine("\r\nReady to validate shipping input");
            }

            AOGLSCountry glsCountry = null;
            bool         validInput = ValidateShippingInfo(getShippingOptionRequest, ref response, ref glsCountry);

            if (validInput == false && response.Errors != null && response.Errors.Count > 0)
            {
                var test = _workContext;
                return(response);
            }

            try
            {
                if (_glsSettings.Tracing)
                {
                    _traceMessages.AppendLine("Ready to prapare GLS call");
                }

                List <PakkeshopData> parcelShops = null;

                if (glsCountry.SupportParcelShop)
                {
                    wsShopFinderSoapClient client = new wsShopFinderSoapClient(EndpointConfiguration.wsShopFinderSoap12, _glsSettings.EndpointAddress);
                    string zip    = getShippingOptionRequest.ShippingAddress.ZipPostalCode;
                    string street = getShippingOptionRequest.ShippingAddress.Address1;

                    if (_glsSettings.Tracing)
                    {
                        _traceMessages.AppendLine("Ready to call GLS at: '" + _glsSettings.EndpointAddress + "'");
                    }

                    try
                    {
                        // First try to find a number of shops near the address
                        parcelShops = client.GetParcelShopDropPointAsync(street, zip, glsCountry.TwoLetterGLSCode, _glsSettings.AmountNearestShops).Result.parcelshops.ToList();
                    }
                    catch (Exception ex)
                    {
                        if (_glsSettings.Tracing)
                        {
                            _traceMessages.AppendLine("Error finding parcelshops: " + ex.ToString());
                        }
                    }

                    if (parcelShops == null || parcelShops.Count == 0)
                    {
                        try
                        {
                            // If any errors or no shop found, try to find shops from only zip code
                            parcelShops = client.GetParcelShopsInZipcodeAsync(zip, glsCountry.TwoLetterGLSCode).Result.GetParcelShopsInZipcodeResult.ToList();
                        }
                        catch (Exception ex)
                        {
                            if (_glsSettings.Tracing)
                            {
                                _traceMessages.AppendLine("Error finding parcelshops: " + ex.ToString());
                            }
                        }
                    }

                    if (_glsSettings.Tracing && parcelShops != null && parcelShops.Count > 0)
                    {
                        _traceMessages.AppendLine(parcelShops.Count + " parcelshops found:");
                    }

                    if (parcelShops != null && parcelShops.Count > 0)
                    {
                        foreach (var parcelShop in parcelShops)
                        {
                            ShippingOption shippingOption = new ShippingOption()
                            {
                                Name        = BuildGLSName(parcelShop),
                                Description = "",
                                ShippingRateComputationMethodSystemName = "GLS",
                                Rate = GetRate(glsCountry.ShippingPrice_0_1)
                            };

                            response.ShippingOptions.Add(shippingOption);

                            if (_glsSettings.Tracing)
                            {
                                _traceMessages.AppendLine(" - " + shippingOption.Name);
                            }
                        }
                    }
                }

                if (parcelShops == null || parcelShops.Count == 0)
                {
                    Address        address        = getShippingOptionRequest.ShippingAddress;
                    ShippingOption shippingOption = new ShippingOption()
                    {
                        Name        = address.FirstName + " " + address.LastName,
                        Description = BuildDescription(address),
                        ShippingRateComputationMethodSystemName = "GLS",
                        Rate = GetRate(glsCountry.ShippingPrice_0_1)
                    };

                    response.ShippingOptions.Add(shippingOption);
                }

                if (response.ShippingOptions.Any())
                {
                    response.Errors.Clear();
                }
            }
            catch (Exception exc)
            {
                while (exc.InnerException != null)
                {
                    exc = exc.InnerException;
                }
                response.AddError($"GLS Service is currently unavailable, try again later. {exc.ToString()}");

                if (_glsSettings.Tracing)
                {
                    _traceMessages.AppendLine($"GLS Service is currently unavailable, try again later. {exc.ToString()}");
                }
            }
            finally
            {
                if (_glsSettings.Tracing && _traceMessages.Length > 0)
                {
                    var shortMessage =
                        $"GLS Shipping Options for customer {getShippingOptionRequest.Customer.Email}.  {getShippingOptionRequest.Items.Count} item(s) in cart (See more in full message)";
                    _logger.Information(shortMessage, new Exception(_traceMessages.ToString()), getShippingOptionRequest.Customer);
                }
            }

            return(response);
        }
コード例 #2
0
        private bool ValidateShippingInfo(GetShippingOptionRequest getShippingOptionRequest, ref GetShippingOptionResponse response, ref AOGLSCountry glsCountry)
        {
            if (getShippingOptionRequest == null)
            {
                throw new ArgumentNullException(nameof(getShippingOptionRequest));
            }

            if (getShippingOptionRequest.Items == null)
            {
                response.AddError("No shipment items");
                return(false);
            }

            if (getShippingOptionRequest.ShippingAddress == null)
            {
                response.AddError("Shipping address is not set");
                return(false);
            }

            if (getShippingOptionRequest.ShippingAddress.Country == null)
            {
                response.AddError("Shipping country is not set");
                return(false);
            }

            if (getShippingOptionRequest.CountryFrom == null)
            {
                response.AddError("From country is not set");
                return(false);
            }

            glsCountry = _glsService.GetCountryByThreeLetterCode(getShippingOptionRequest.ShippingAddress.Country.ThreeLetterIsoCode);
            if (glsCountry == null)
            {
                response.AddError("Not possible to send to " + getShippingOptionRequest.ShippingAddress.Country.Name + " (" + getShippingOptionRequest.ShippingAddress.Country.ThreeLetterIsoCode + ")");
                return(false);
            }

            if (string.IsNullOrEmpty(getShippingOptionRequest.ShippingAddress.ZipPostalCode))
            {
                response.AddError("Zipcode not set");
                return(false);
            }

            return(true);
        }