Exemple #1
0
        private bool RemoveTracking(string trackingNumber)
        {
            var result = false;

            try
            {
                var track    = new Aftership.Tracking(trackingNumber);
                var couriers = _connection.DetectCouriers(trackingNumber);
                foreach (var courier in couriers)
                {
                    track.Slug = courier.Slug;
                    result     = _connection.DeleteTracking(track);
                    if (result)
                    {
                        break;
                    }
                }
            }
            catch (WebException ex)
            {
                _logger.Warning(
                    string.Format("Cannot delete tracking with number - {0}", trackingNumber),
                    new Exception(ex.Message));
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Gets all events for a tracking number.
        /// </summary>
        /// <param name="trackingNumber">The tracking number to track.</param>
        /// <returns>List of Shipment Events.</returns>
        public IList <ShipmentStatusEvent> GetShipmentEvents(string trackingNumber)
        {
            if (string.IsNullOrWhiteSpace(_settings.ApiKey) || _settings.ApiKey.Equals("MyAfterShipAPIKey"))
            {
                return(new List <ShipmentStatusEvent>());
            }

            if (string.IsNullOrEmpty(trackingNumber))
            {
                return(new List <ShipmentStatusEvent>());
            }

            var tracker      = new Infrastructure.Aftership.Tracking(trackingNumber);
            var genericAttrs = _genericAttributeService.GetAttributesForEntity(_shipment.Id, "Shipment");
            var trackingInfo = genericAttrs.FirstOrDefault(a => a.Key.Equals(Constants.SHIPMENT_TRACK_ID_ATTRIBUTE_NAME));
            IList <ShipmentStatusEvent> shipmentStatusList = new List <ShipmentStatusEvent>();
            IList <Courier>             couriers           = null;

            if (trackingInfo != null)
            {
                try
                {
                    tracker.Id         = trackingInfo.Value;
                    shipmentStatusList = GetShipmentStatusEvents(tracker);
                }
                catch (WebException)
                {
                    _logger.Error(
                        string.Format("Error getting tracking information on Aftership events - {0}", trackingNumber));
                }
            }
            else
            {
                try
                {
                    // use try-catch to ensure exception won't be thrown is web service is not available
                    couriers = _connection.DetectCouriers(trackingNumber, null, null, null, null);
                }
                catch (Exception ex)
                {
                    _logger.Error("Error getting couriers information on Aftership", ex);
                }

                if (couriers == null)
                {
                    return(shipmentStatusList);
                }

                foreach (var courier in couriers)
                {
                    try
                    {
                        tracker.Slug       = courier.Slug;
                        tracker            = _connection.GetTrackingByNumber(tracker);
                        shipmentStatusList = GetShipmentStatusEvents(tracker);
                        if (shipmentStatusList.Any())
                        {
                            break;
                        }
                    }
                    catch (WebException)
                    {
                        _logger.Error(
                            string.Format("Error getting tracking information on Aftership events - {0}", trackingNumber));
                    }
                }
            }

            return(shipmentStatusList);
        }