Пример #1
0
            /// <summary>
            /// Validates the address for shipping.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="shippingAddress">The address to validate for shipping.</param>
            /// <param name="deliveryModeId">The delivery mode identifier.</param>
            private static void ValidateShippingAddress(
                RequestContext context,
                Address shippingAddress,
                string deliveryModeId)
            {
                ThrowIf.Null(context, "context");
                ThrowIf.Null(context.Runtime, "context.Runtime");

                if (shippingAddress == null)
                {
                    return;
                }

                var request = new ValidateShippingAddressServiceRequest(shippingAddress, false, deliveryModeId);

                var response = context.Runtime.Execute <ValidateShippingAddressServiceResponse>(request, context);

                if (!response.IsAddressValid)
                {
                    // invalid address anomaly encountered and handle it
                    InvalidShippingAddressNotification notification = new InvalidShippingAddressNotification(shippingAddress);
                    request.RequestContext.Notify(notification);
                }
            }
Пример #2
0
            /// <summary>
            /// Validates the address.
            /// </summary>
            /// <param name="shippingRequest">The shipping request.</param>
            /// <returns>The response.</returns>
            /// <remarks>Throws an exception if the address field in the request is null.</remarks>
            private static ValidateShippingAddressServiceResponse ValidateShippingAddress(ValidateShippingAddressServiceRequest shippingRequest)
            {
                if (shippingRequest.AddressToValidate == null)
                {
                    throw new ArgumentNullException("shippingRequest", "shippingRequest.AddressToValidate");
                }

                if (shippingRequest.DeliveryModeId == null)
                {
                    throw new ArgumentNullException("shippingRequest", "shippingRequest.DeliveryModeId");
                }

                var dataServiceRequest  = new GetShippingAdapterConfigurationDataRequest(new[] { shippingRequest.DeliveryModeId }, QueryResultSettings.AllRecords);
                var dataServiceResponse = shippingRequest.RequestContext.Execute <EntityDataServiceResponse <ShippingAdapterConfig> >(dataServiceRequest);

                var shippingAdapterConfigLines = dataServiceResponse.PagedEntityCollection.Results;

                ParameterSet adapterConfig = ExtractAdapterConfigForSpecificDeliveryMode(shippingRequest.DeliveryModeId, shippingAdapterConfigLines);

                var             carrierServiceRequest = new ValidateShippingAddressCarrierServiceRequest(adapterConfig, shippingRequest.AddressToValidate, shippingRequest.SuggestAddress);
                IRequestHandler carrierAdapterHandler = GetCarrierAdapterService(shippingRequest.RequestContext, carrierServiceRequest.GetType(), adapterConfig);

                bool isAddressValid = true;
                IEnumerable <Address> addressSuggestions = null;

                if (carrierAdapterHandler != null)
                {
                    // Call the adapter's validation method
                    var carrierServiceResponse = shippingRequest.RequestContext.Runtime.Execute <ValidateShippingAddressCarrierServiceResponse>(carrierServiceRequest, shippingRequest.RequestContext, carrierAdapterHandler);
                    isAddressValid     = carrierServiceResponse.IsAddressValid;
                    addressSuggestions = carrierServiceResponse.RecommendedAddresses;
                }
                else
                {
                    NetTracer.Warning("No registered shipping carrier is found.");
                }

                return(new ValidateShippingAddressServiceResponse(isAddressValid, addressSuggestions));
            }