/// <summary>
        ///     Parses the first reference number from the UPS Track API response.
        /// </summary>
        /// <param name="upsResponse"></param>
        /// <returns>First reference number or empty string.</returns>
        public static string GetReferenceNum(UPSResponse upsResponse)
        {
            try
            {
                var refNum = upsResponse.TrackResponse.Shipment.ReferenceNumber?.Value;

                return(refNum);
            }
            // Null reference will throw if reference # is not available
            catch (NullReferenceException ex)
            {
                Log.Warning($"No reference number found. Tracking Number: {upsResponse.TrackResponse.Shipment.InquiryNumber.Value}");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error in GetReferenceNum.");
                var title        = "Error in GetReferenceNum";
                var text         = $"Error message: {ex.Message}";
                var color        = "yellow";
                var teamsMessage = new TeamsMessage(title, text, color, teamsUrl);
                teamsMessage.LogToTeams(teamsMessage);
            }

            return("");
        }
Beispiel #2
0
        /// <summary>
        ///     Sets the receiving address on the tracking record based on the UPS Track API response.
        /// </summary>
        /// <param name="trackingRecord">Object containing tracking data that will be written to UPS Integration DB.</param>
        /// <param name="upsResponse">Response object from the UPS Track API.</param>
        public void SetDestinationAddress(UPSTracking trackingRecord, UPSResponse upsResponse)
        {
            var destinationAddress = TrackAPI.GetAddressByType(upsResponse, "ShipTo Address");

            trackingRecord.DestinationCity  = destinationAddress?.Address.City;
            trackingRecord.DestinationState = destinationAddress?.Address.StateProvinceCode;
            trackingRecord.DestinationZip   = destinationAddress?.Address.PostalCode;
        }
Beispiel #3
0
        /// <summary>
        ///     Parses the fault message from the UPS Track API response and logs error. If tracking number is invalid, it is added to the
        ///     invalid tracking numbers list that is used in exception reporting.
        /// </summary>
        /// <param name="upsResponse">Response object from the UPS Track API.</param>
        /// <param name="trackingNumber">Tracking number of the package being tracked.</param>
        public void HandleTrackAPIFault(UPSResponse upsResponse, string trackingNumber)
        {
            var errMessage = upsResponse.Fault.detail?.Errors.ErrorDetail.PrimaryErrorCode.Description;

            _logger.LogError($"{trackingNumber}: {errMessage}");

            // These will be written to an exception report in the invalid-tracking-numbers container
            invalidTrackingNumbers.Add(new InvalidTrackingNumber(trackingNumber, errMessage));
        }
Beispiel #4
0
        /// <summary>
        ///     Sets the package's current status, location, timestamp and exception (if applicable) on the tracking record based on the
        ///     UPS Track API response.
        /// </summary>
        /// <param name="trackingRecord">Object containing tracking data that will be written to UPS Integration DB.</param>
        /// <param name="upsResponse">Response object from the UPS Track API.</param>
        public void SetLatestActivity(UPSTracking trackingRecord, UPSResponse upsResponse)
        {
            var latestActivity = TrackAPI.GetLatestActivity(upsResponse);

            trackingRecord.Status          = latestActivity.Status;
            trackingRecord.Location        = latestActivity.Location;
            trackingRecord.TimeStamp       = latestActivity.TimeStamp;
            trackingRecord.ExceptionReason = latestActivity.ExceptionReason;
        }
Beispiel #5
0
        /// <summary>
        ///     Sets the sender address on the tracking record based on the UPS Track API response.
        /// </summary>
        /// <param name="trackingRecord">Object containing tracking data that will be written to UPS Integration DB.</param>
        /// <param name="upsResponse">Response object from the UPS Track API.</param>
        public void SetOriginAddress(UPSTracking trackingRecord, UPSResponse upsResponse)
        {
            var originAddress = TrackAPI.GetAddressByType(upsResponse, "Shipper Address");

            trackingRecord.OriginAddress = originAddress?.Address.AddressLine;
            trackingRecord.OriginCity    = originAddress?.Address.City;
            trackingRecord.OriginState   = originAddress?.Address.StateProvinceCode;
            var originZip = originAddress?.Address.PostalCode;

            // Format with hyphen after first 5 digits
            if (originZip.Length > 5)
            {
                originZip = originZip.Insert(5, "-");
            }

            trackingRecord.OriginZip = originZip;
        }
Beispiel #6
0
        private void AddShipmentActivity(UPSResponse upsResponse, Status status)
        {
            var package = new Package()
            {
                Activity = new Activity()
                {
                    Status           = status,
                    Date             = "20200907",
                    Time             = "183500",
                    ActivityLocation = new ActivityLocation()
                    {
                        Address = CreateOriginAddress()
                    }
                }
            };

            upsResponse.TrackResponse.Shipment.Package = package;
        }
Beispiel #7
0
        private UPSResponse CreateUPSResponse(string refNum)
        {
            var upsResponse = new UPSResponse()
            {
                TrackResponse = new TrackResponse()
                {
                    Shipment = new Shipment()
                    {
                        ReferenceNumber = new ReferenceNumber()
                        {
                            Code  = "01",
                            Value = refNum
                        }
                    }
                }
            };

            return(upsResponse);
        }
        /// <summary>
        ///     Parses the package's most recent status, location and timestamp from the UPS Track API response.
        /// </summary>
        /// <param name="upsResponse">The UPS Track API response.</param>
        /// <returns>Most recent status, location and timestamp of package.</returns>
        public static LatestActivity GetLatestActivity(UPSResponse upsResponse)
        {
            var activity = new LatestActivity();

            try
            {
                // The most recent activity will be at the first index
                var latestActivity = upsResponse.TrackResponse.Shipment.Package.Activity;

                var latestActivityCode = latestActivity.Status.Type;

                activity.Status = MapActivityCodeToStatus(latestActivityCode);

                if (activity.Status == "Exception")
                {
                    activity.ExceptionReason = latestActivity.Status.Description.Replace("'", "");;
                }

                // Remove seconds from timestamp
                var time = latestActivity.Time.Substring(0, 4);

                // Format time in hh:mm:ss for sql insert
                activity.TimeStamp = latestActivity.Date + " " + Regex.Replace(time, ".{2}", "$0:") + "00";

                // Get the location
                var city  = latestActivity.ActivityLocation?.Address.City;
                var state = latestActivity.ActivityLocation?.Address.StateProvinceCode;

                activity.Location = city != null ? city + ", " + state : "";
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error in ParseShipmentStatus.");
                var title        = $"Error in ParseShipmentStatus. Tracking Number: {upsResponse.TrackResponse.Shipment.InquiryNumber.Value}";
                var text         = $"Error message: {ex.Message}";
                var color        = "yellow";
                var teamsMessage = new TeamsMessage(title, text, color, teamsUrl);
                teamsMessage.LogToTeams(teamsMessage);
            }

            return(activity);
        }
Beispiel #9
0
        private UPSResponse CreateUPSResponse()
        {
            var upsResponse = new UPSResponse()
            {
                TrackResponse = new TrackResponse()
                {
                    Shipment = new Shipment()
                    {
                        ShipmentAddress = CreateAddresses(),
                        ReferenceNumber = new ReferenceNumber()
                        {
                            Code  = "01",
                            Value = "T3348-1122734"
                        }
                    }
                }
            };

            return(upsResponse);
        }
Beispiel #10
0
        /// <summary>
        /// Asynchronously requests verification from the UPS api of the address passed to it
        /// </summary>
        /// <param name="request">Object specifying details of the request to the UPS api</param>
        /// <returns></returns>
        public async Task <UPSResponse> Request(AddressKeyFormat address)
        {
            var request = new UPSRequest()
            {
                XAVRequest = new XAVRequest(RequestOption)
                {
                    AddressKeyFormat = address
                }
            };

            var requestJson = JsonConvert.SerializeObject(request);
            var content     = new StringContent(requestJson, Encoding.UTF8, "application/json");
            var reply       = await _httpClient.PostAsync(Url, content);

            var responseContent = await reply.Content.ReadAsStringAsync();

            UPSResponse response = JsonConvert.DeserializeObject <UPSResponse>(responseContent);

            return(response);
        }
        /// <summary>
        ///     Parses the origin (Shipper Address) or destination address (ShipTo Address) from the UPS Track API response.
        /// </summary>
        /// <param name="upsResponse">The UPS Track API response</param>
        /// <param name="addressType">Origin or Destination</param>
        /// <returns>Address of provided type (origin or destination)</returns>
        public static ShipmentAddress GetAddressByType(UPSResponse upsResponse, string addressType)
        {
            try
            {
                var address = upsResponse.TrackResponse.Shipment.ShipmentAddress
                              .Where(a => a.Type.Description == addressType)
                              .SingleOrDefault();

                return(address);
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error in GetAddressByType. Address type {addressType}.");
                var title        = "Error in GetAddressByType";
                var text         = $"Address type {addressType}. Error message: {ex.Message}";
                var color        = "red";
                var teamsMessage = new TeamsMessage(title, text, color, teamsUrl);
                teamsMessage.LogToTeams(teamsMessage);
            }

            Log.Warning($"No address was found for type {addressType}. Tracking Number: {upsResponse.TrackResponse.Shipment.InquiryNumber.Value}");
            return(new ShipmentAddress());
        }