public async Task Handle(BuyUpsLabelCommand message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.Credentials == null)
            {
                throw new ArgumentNullException(nameof(message.Credentials));
            }

            await _upsClient.ConfirmShip(new ConfirmShipParameter
            {
                AlternateDeliveryAddress = message.AlternateDeliveryAddress,
                EmailAddress             = message.EmailAddress,
                Package            = message.Package,
                PaymentInformation = message.PaymentInformation,
                ShipFrom           = message.ShipFrom,
                Shipper            = message.Shipper,
                ShipTo             = message.ShipTo,
                Credentials        = message.Credentials
            });
        }
        public async Task <PurchaseOrderLabelValidationResult> Validate(string subject, PurchaseOrderLabelCommand parameter)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException(nameof(subject));
            }

            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (parameter.ParcelSize == null)
            {
                throw new ArgumentNullException(nameof(parameter.ParcelSize));
            }

            var order = await _orderRepository.Get(parameter.OrderId);

            if (order == null) // Check order exists.
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheOrderDoesntExist));
            }

            if (parameter.ParcelSize.Height < 0 || parameter.ParcelSize.Length < 0 || parameter.ParcelSize.Weight < 0 || parameter.ParcelSize.Width < 0)
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheParcelSizeIsInvalid));
            }

            if (order.SellerId != subject) // Only the seller can purchase the label.
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheOrderLabelCanBePurchasedOnlyBySeller));
            }

            if (order.Status != OrderAggregateStatus.Confirmed) // The order state should be confirmed.
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheOrderIsNotConfirmed));
            }

            if (order.OrderPayment == null || order.OrderPayment.Status != OrderPaymentStatus.Approved) // The order payment should be approved by the buyer.
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheOrderPaymentHasNotBeenConfirmed));
            }

            double shippingPrice = 0;
            var    payer         = await _openidClient.GetPublicClaims(_settingsProvider.GetBaseOpenidUrl(), subject); // Get the payer information.

            if (payer == null)
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.ThePayerPaypalAccountNotExist));
            }

            var seller = await _openidClient.GetPublicClaims(_settingsProvider.GetBaseOpenidUrl(), order.SellerId); // Get the seller information.

            if (seller == null)
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheSellerPaypalAccountNotExist));
            }

            var paypalBuyer  = payer.Claims.FirstOrDefault(c => c.Type == "paypal_email");
            var paypalSeller = seller.Claims.FirstOrDefault(c => c.Type == "paypal_email");

            if (paypalBuyer == null || string.IsNullOrWhiteSpace(paypalBuyer.Value))
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.ThePayerPaypalAccountNotExist));
            }

            if (paypalSeller == null || string.IsNullOrWhiteSpace(paypalSeller.Value))
            {
                return(new PurchaseOrderLabelValidationResult(ErrorDescriptions.TheSellerPaypalAccountNotExist));
            }

            string trackingNumber = null;
            string shipmentDigest = null;

            if (order.OrderParcel.Transporter == Transporters.Ups) // Retrieve UPS ratings.
            {
                var buyerName   = payer.Claims.First(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Name).Value;
                var sellerName  = seller.Claims.First(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Name).Value;
                var confirmShip = await _upsClient.ConfirmShip(new ConfirmShipParameter // 1. Confirm UPS shippment.
                {
                    Credentials = new UpsCredentials
                    {
                        LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                        Password      = _settingsProvider.GetUpsPassword(),
                        UserName      = _settingsProvider.GetUpsUsername()
                    },
                    AlternateDeliveryAddress = new UpsAlternateDeliveryAddressParameter
                    {
                        Name    = order.OrderParcel.ParcelShopName,
                        Address = new UpsAddressParameter
                        {
                            AddressLine = order.OrderParcel.ParcelShopAddressLine,
                            City        = order.OrderParcel.ParcelShopCity,
                            Country     = order.OrderParcel.ParcelShopCountryCode,
                            PostalCode  = order.OrderParcel.ParcelShopPostalCode.ToString()
                        }
                    },
                    Shipper = new UpsShipperParameter
                    {
                        Name    = buyerName,
                        Address = new UpsAddressParameter
                        {
                            AddressLine = order.OrderParcel.BuyerAddressLine,
                            City        = order.OrderParcel.BuyerCity,
                            Country     = order.OrderParcel.BuyerCountryCode,
                            PostalCode  = order.OrderParcel.BuyerPostalCode.ToString()
                        }
                    },
                    ShipFrom = new UpsShipParameter
                    {
                        Name          = buyerName,
                        AttentionName = buyerName,
                        CompanyName   = buyerName,
                        Address       = new UpsAddressParameter
                        {
                            AddressLine = order.OrderParcel.BuyerAddressLine,
                            City        = order.OrderParcel.BuyerCity,
                            Country     = order.OrderParcel.BuyerCountryCode,
                            PostalCode  = order.OrderParcel.BuyerPostalCode.ToString()
                        }
                    },
                    ShipTo = new UpsShipParameter
                    {
                        Name          = sellerName,
                        AttentionName = sellerName,
                        CompanyName   = sellerName,
                        Address       = new UpsAddressParameter
                        {
                            AddressLine = order.OrderParcel.SellerAddressLine,
                            City        = order.OrderParcel.SellerCity,
                            Country     = order.OrderParcel.SellerCountryCode,
                            PostalCode  = order.OrderParcel.SellerPostalCode.ToString()
                        }
                    },
                    Package = new UpsPackageParameter
                    {
                        Length = parameter.ParcelSize.Length,
                        Height = parameter.ParcelSize.Height,
                        Weight = parameter.ParcelSize.Weight,
                        Width  = parameter.ParcelSize.Width
                    },
                    EmailAddress = "*****@*****.**",
                    UpsService   = CommonBuilder.MappingBothUpsServices.First(kvp => kvp.Key == order.OrderParcel.UpsServiceCode).Value
                });

                if (confirmShip.Response.ResponseStatusCode == "0")
                {
                    return(new PurchaseOrderLabelValidationResult(confirmShip.Response.Error.ErrorDescription));
                }

                /*
                 * var acceptUpsShipment = await _upsClient.AcceptShip(new AcceptShipParameter // 2. Accept UPS shipment.
                 * {
                 *  Credentials = new UpsCredentials
                 *  {
                 *      LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                 *      Password = _settingsProvider.GetUpsPassword(),
                 *      UserName = _settingsProvider.GetUpsUsername()
                 *  },
                 *  ShipmentDigest = confirmShip.ShipmentDigest
                 * });
                 *
                 * if (acceptUpsShipment.Response.ResponseStatusCode == "0")
                 * {
                 *  return new PurchaseOrderLabelValidationResult(acceptUpsShipment.Response.Error.ErrorDescription);
                 * }
                 */


                shipmentDigest = confirmShip.ShipmentDigest;
                shippingPrice  = confirmShip.ShipmentCharges.TotalCharges.MonetaryValue;
            }

            return(new PurchaseOrderLabelValidationResult(order, paypalBuyer.Value, paypalSeller.Value, shippingPrice, shipmentDigest));
        }