コード例 #1
0
        private static decimal RateSinglePackage(FedExGlobalServiceSettings globalSettings, ILogger logger,
                                                 IShipment pak,
                                                 ServiceType service, PackageType packaging, CarrierCodeType carCode,
                                                 bool useNegotiatedRates)
        {
            var result = 0m;

            try
            {
                // Auth Header Data
                var req = new RateRequest
                {
                    WebAuthenticationDetail = new WebAuthenticationDetail
                    {
                        UserCredential = new WebAuthenticationCredential
                        {
                            Key      = globalSettings.UserKey,
                            Password = globalSettings.UserPassword
                        }
                    }
                };
                req.ClientDetail = new ClientDetail
                {
                    AccountNumber = globalSettings.AccountNumber,
                    MeterNumber   = globalSettings.MeterNumber,
                    IntegratorId  = "Hotcakes"
                };
                req.Version = new VersionId();

                // Basic Transaction Data
                req.TransactionDetail = new TransactionDetail
                {
                    CustomerTransactionId = Guid.NewGuid().ToString()
                };
                req.ReturnTransitAndCommit = false;
                req.CarrierCodes           = new CarrierCodeType[1] {
                    carCode
                };

                // Shipment Details
                req.RequestedShipment = new RequestedShipment
                {
                    LabelSpecification = new LabelSpecification
                    {
                        ImageType               = ShippingDocumentImageType.PDF,
                        LabelFormatType         = LabelFormatType.COMMON2D,
                        CustomerSpecifiedDetail = new CustomerSpecifiedLabelDetail()
                    },
                    RateRequestTypes = new[] { RateRequestType.LIST },
                    DropoffType      = GetDropOffType(globalSettings.DefaultDropOffType),
                    PackagingType    = GetPackageType(packaging),
                    TotalWeight      = new Weight
                    {
                        Value = Math.Round(pak.Items.Sum(y => y.BoxWeight), 1),
                        Units =
                            pak.Items[0].BoxWeightType == Shipping.WeightType.Kilograms
                                ? WeightUnits.KG
                                : WeightUnits.LB
                    },
                    PackageCount = pak.Items.Count.ToString(),
                    RequestedPackageLineItems = new RequestedPackageLineItem[pak.Items.Count]
                };

                /*
                 * Possible values for RateRequestType include: LIST and ACCOUNT.
                 * ACCOUNT will return negotiated rates, but LIST will return both regular and negotiated rates.
                 * http://www.fedex.com/us/developer/product/WebServices/MyWebHelp_March2010/Content/WS_Developer_Guide/Rate_Services.htm
                 */
                //req.RequestedShipment.RateRequestTypes = new[] { RateRequestType.ACCOUNT };

                // Uncomment these lines to get insured values passed in
                //
                //var totalValue = pak.Items.Sum(y => y.BoxValue);
                //req.RequestedShipment.TotalInsuredValue = new Money();
                //req.RequestedShipment.TotalInsuredValue.Amount = totalValue;
                //req.RequestedShipment.TotalInsuredValue.Currency = "USD";

                for (var i = 0; i < pak.Items.Count; i++)
                {
                    req.RequestedShipment.RequestedPackageLineItems[i] = new RequestedPackageLineItem
                    {
                        GroupNumber       = "1",
                        GroupPackageCount = (i + 1).ToString(),
                        Weight            = new Weight
                        {
                            Value = pak.Items[i].BoxWeight,
                            Units =
                                pak.Items[i].BoxWeightType == Shipping.WeightType.Kilograms
                                    ? WeightUnits.KG
                                    : WeightUnits.LB
                        }
                    };

                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions = new Dimensions
                    {
                        Height = pak.Items[i].BoxHeight.ToString(),
                        Length = pak.Items[i].BoxLength.ToString(),
                        Width  = pak.Items[i].BoxWidth.ToString(),
                        Units  = pak.Items[i].BoxLengthType == LengthType.Centimeters ? LinearUnits.CM : LinearUnits.IN
                    };
                }

                req.RequestedShipment.Recipient = new Party
                {
                    Address = new FedExRateServices.Address
                    {
                        City        = pak.DestinationAddress.City,
                        CountryCode = GetCountryCode(pak.DestinationAddress.CountryData),
                        PostalCode  = pak.DestinationAddress.PostalCode
                    }
                };


                if (pak.DestinationAddress.CountryData.IsoCode == "US" ||
                    pak.DestinationAddress.CountryData.IsoCode == "CA")
                {
                    req.RequestedShipment.Recipient.Address.StateOrProvinceCode = pak.DestinationAddress.RegionBvin;
                }
                else
                {
                    req.RequestedShipment.Recipient.Address.StateOrProvinceCode = string.Empty;
                }
                req.RequestedShipment.Recipient.Address.StreetLines = new string[2]
                {
                    pak.DestinationAddress.Street, pak.DestinationAddress.Street2
                };

                switch (service)
                {
                case ServiceType.GROUNDHOMEDELIVERY:
                    req.RequestedShipment.Recipient.Address.Residential          = true;
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = true;
                    break;

                case ServiceType.FEDEXGROUND:
                    req.RequestedShipment.Recipient.Address.Residential          = false;
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = true;
                    break;

                default:
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = false;
                    break;
                }

                req.RequestedShipment.Shipper = new Party
                {
                    AccountNumber = globalSettings.AccountNumber,
                    Address       = new FedExRateServices.Address
                    {
                        City        = pak.SourceAddress.City,
                        CountryCode = GetCountryCode(pak.SourceAddress.CountryData),
                        PostalCode  = pak.SourceAddress.PostalCode,
                        Residential = false
                    }
                };

                if (pak.SourceAddress.CountryData.IsoCode == "US" || // US or Canada
                    pak.SourceAddress.CountryData.IsoCode == "CA")
                {
                    req.RequestedShipment.Shipper.Address.StateOrProvinceCode = pak.SourceAddress.RegionBvin;
                }
                else
                {
                    req.RequestedShipment.Shipper.Address.StateOrProvinceCode = string.Empty;
                }
                req.RequestedShipment.Shipper.Address.StreetLines = new string[2]
                {
                    pak.SourceAddress.Street, pak.SourceAddress.Street2
                };

                var svc = new FedExRateServices.RateService
                {
                    Url =
                        globalSettings.UseDevelopmentServiceUrl
                            ? FedExConstants.DevRateServiceUrl
                            : FedExConstants.LiveRateServiceUrl
                };

                var res = svc.getRates(req);

                if (res.HighestSeverity == NotificationSeverityType.ERROR ||
                    res.HighestSeverity == NotificationSeverityType.FAILURE)
                {
                    if (globalSettings.DiagnosticsMode)
                    {
                        foreach (var err in res.Notifications)
                        {
                            logger.LogMessage("FEDEX", err.Message, EventLogSeverity.Debug);
                        }
                    }
                    result = 0m;
                }
                else
                {
                    result = 0m;

                    var lookingForService = GetServiceType(service);
                    var matchingResponse  = res.RateReplyDetails.FirstOrDefault(y => y.ServiceType == lookingForService);
                    if (matchingResponse != null)
                    {
                        if (useNegotiatedRates)
                        {
                            var matchedRate =
                                matchingResponse.RatedShipmentDetails.FirstOrDefault(
                                    y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_PACKAGE ||
                                    y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT);

                            if (matchedRate != null)
                            {
                                result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount;

                                if (globalSettings.DiagnosticsMode)
                                {
                                    logger.LogMessage("FEDEX SHIPMENT",
                                                      "Negotiated rates were found and are currently being used.",
                                                      EventLogSeverity.Information);
                                }
                            }
                            else
                            {
                                matchedRate =
                                    matchingResponse.RatedShipmentDetails.FirstOrDefault(
                                        y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_PACKAGE ||
                                        y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_SHIPMENT);

                                result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount;

                                if (globalSettings.DiagnosticsMode)
                                {
                                    logger.LogMessage("FEDEX SHIPMENT",
                                                      "No negotiated rates were found. Public rates are being shown. You should update your account information, or uncheck the ‘Use Negotiated Rates’ checkbox.",
                                                      EventLogSeverity.Information);
                                }
                                else
                                {
                                    logger.LogMessage("FEDEX SHIPMENT", "No negotiated rates were found.",
                                                      EventLogSeverity.Information);
                                }
                            }
                        }
                        else
                        {
                            var matchedRate =
                                matchingResponse.RatedShipmentDetails.FirstOrDefault(
                                    y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_PACKAGE ||
                                    y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_SHIPMENT);

                            if (matchedRate != null)
                            {
                                result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount;

                                if (
                                    matchingResponse.RatedShipmentDetails.Any(
                                        y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_PACKAGE ||
                                        y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT))
                                {
                                    if (globalSettings.DiagnosticsMode)
                                    {
                                        logger.LogMessage("FEDEX SHIPMENT",
                                                          "We also found negotiated rates for your account. You should consider checking the ‘Use Negotiated Rates’ checkbox.",
                                                          EventLogSeverity.Information);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = 0m;
                logger.LogException(ex);
            }

            return(result);
        }
コード例 #2
0
        private static decimal RateSinglePackage(FedExGlobalServiceSettings globalSettings,
                                                 MerchantTribe.Web.Logging.ILogger logger,
                                                 IShipment pak,
                                                 ServiceType service,
                                                 PackageType packaging,
                                                 CarrierCodeType carCode)
        {
            decimal result = 0m;



            try
            {
                // Auth Header Data
                var req = new FedExRateServices.RateRequest();
                req.WebAuthenticationDetail = new WebAuthenticationDetail();
                req.WebAuthenticationDetail.UserCredential          = new WebAuthenticationCredential();
                req.WebAuthenticationDetail.UserCredential.Key      = globalSettings.UserKey;
                req.WebAuthenticationDetail.UserCredential.Password = globalSettings.UserPassword;
                req.ClientDetail = new ClientDetail();
                req.ClientDetail.AccountNumber = globalSettings.AccountNumber;
                req.ClientDetail.MeterNumber   = globalSettings.MeterNumber;
                req.ClientDetail.IntegratorId  = "BVSoftware";
                req.Version = new VersionId();

                // Basic Transaction Data
                req.TransactionDetail = new TransactionDetail();
                req.TransactionDetail.CustomerTransactionId = System.Guid.NewGuid().ToString();
                req.ReturnTransitAndCommit = false;
                req.CarrierCodes           = new CarrierCodeType[1] {
                    carCode
                };

                // Shipment Details
                req.RequestedShipment = new RequestedShipment();

                req.RequestedShipment.LabelSpecification                         = new LabelSpecification();
                req.RequestedShipment.LabelSpecification.ImageType               = ShippingDocumentImageType.PDF;
                req.RequestedShipment.LabelSpecification.LabelFormatType         = LabelFormatType.COMMON2D;
                req.RequestedShipment.LabelSpecification.CustomerSpecifiedDetail = new CustomerSpecifiedLabelDetail();

                req.RequestedShipment.DropoffType       = GetDropOffType(globalSettings.DefaultDropOffType);
                req.RequestedShipment.PackagingType     = GetPackageType(packaging);
                req.RequestedShipment.TotalWeight       = new Weight();
                req.RequestedShipment.TotalWeight.Value = Math.Round(pak.Items.Sum(y => y.BoxWeight), 1);
                if (pak.Items[0].BoxWeightType == Shipping.WeightType.Kilograms)
                {
                    req.RequestedShipment.TotalWeight.Units = WeightUnits.KG;
                }
                else
                {
                    req.RequestedShipment.TotalWeight.Units = WeightUnits.LB;
                }

                // Uncomment these lines to get insured values passed in
                //
                //var totalValue = pak.Items.Sum(y => y.BoxValue);
                //req.RequestedShipment.TotalInsuredValue = new Money();
                //req.RequestedShipment.TotalInsuredValue.Amount = totalValue;
                //req.RequestedShipment.TotalInsuredValue.Currency = "USD";

                req.RequestedShipment.PackageCount = pak.Items.Count.ToString();

                req.RequestedShipment.RequestedPackageLineItems = new RequestedPackageLineItem[pak.Items.Count];
                for (int i = 0; i < pak.Items.Count; i++)
                {
                    req.RequestedShipment.RequestedPackageLineItems[i]                   = new RequestedPackageLineItem();
                    req.RequestedShipment.RequestedPackageLineItems[i].GroupNumber       = "1";
                    req.RequestedShipment.RequestedPackageLineItems[i].GroupPackageCount = (i + 1).ToString();
                    req.RequestedShipment.RequestedPackageLineItems[i].Weight            = new Weight();
                    req.RequestedShipment.RequestedPackageLineItems[i].Weight.Value      = pak.Items[i].BoxWeight;
                    req.RequestedShipment.RequestedPackageLineItems[i].Weight.Units      = pak.Items[i].BoxWeightType == Shipping.WeightType.Kilograms ? WeightUnits.KG : WeightUnits.LB;
                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions        = new Dimensions();
                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions.Height = pak.Items[i].BoxHeight.ToString();
                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions.Length = pak.Items[i].BoxLength.ToString();
                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions.Width  = pak.Items[i].BoxWidth.ToString();
                    req.RequestedShipment.RequestedPackageLineItems[i].Dimensions.Units  = pak.Items[i].BoxLengthType == LengthType.Centimeters ? LinearUnits.CM : LinearUnits.IN;
                }

                req.RequestedShipment.Recipient                     = new Party();
                req.RequestedShipment.Recipient.Address             = new FedExRateServices.Address();
                req.RequestedShipment.Recipient.Address.City        = pak.DestinationAddress.City;
                req.RequestedShipment.Recipient.Address.CountryCode = GetCountryCode(pak.DestinationAddress.CountryData);
                req.RequestedShipment.Recipient.Address.PostalCode  = pak.DestinationAddress.PostalCode;

                if (pak.DestinationAddress.CountryData.Bvin == "bf7389a2-9b21-4d33-b276-23c9c18ea0c0" || // US or Canada
                    pak.DestinationAddress.CountryData.Bvin == "94052dcf-1ac8-4b65-813b-b17b12a0491f")
                {
                    req.RequestedShipment.Recipient.Address.StateOrProvinceCode = pak.DestinationAddress.RegionData.Abbreviation; // GetStateCode(pak.DestinationAddress.RegionData);
                }
                else
                {
                    req.RequestedShipment.Recipient.Address.StateOrProvinceCode = string.Empty;
                }
                req.RequestedShipment.Recipient.Address.StreetLines = new string[2] {
                    pak.DestinationAddress.Street, pak.DestinationAddress.Street2
                };

                if (service == ServiceType.GROUNDHOMEDELIVERY)
                {
                    req.RequestedShipment.Recipient.Address.Residential          = true;
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = true;
                }
                else if (service == ServiceType.FEDEXGROUND)
                {
                    req.RequestedShipment.Recipient.Address.Residential          = false;
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = true;
                }
                else
                {
                    req.RequestedShipment.Recipient.Address.ResidentialSpecified = false;
                }

                req.RequestedShipment.Shipper = new Party();
                req.RequestedShipment.Shipper.AccountNumber       = globalSettings.AccountNumber;
                req.RequestedShipment.Shipper.Address             = new FedExRateServices.Address();
                req.RequestedShipment.Shipper.Address.City        = pak.SourceAddress.City;
                req.RequestedShipment.Shipper.Address.CountryCode = GetCountryCode(pak.SourceAddress.CountryData);
                req.RequestedShipment.Shipper.Address.PostalCode  = pak.SourceAddress.PostalCode;
                req.RequestedShipment.Shipper.Address.Residential = false;
                if (pak.SourceAddress.CountryData.Bvin == "bf7389a2-9b21-4d33-b276-23c9c18ea0c0" || // US or Canada
                    pak.SourceAddress.CountryData.Bvin == "94052dcf-1ac8-4b65-813b-b17b12a0491f")
                {
                    req.RequestedShipment.Shipper.Address.StateOrProvinceCode = pak.SourceAddress.RegionData.Abbreviation;
                }
                else
                {
                    req.RequestedShipment.Shipper.Address.StateOrProvinceCode = string.Empty;
                }
                req.RequestedShipment.Shipper.Address.StreetLines = new string[2] {
                    pak.SourceAddress.Street, pak.SourceAddress.Street2
                };


                var       svc = new FedExRateServices.RateService();
                RateReply res = svc.getRates(req);

                if (res.HighestSeverity == NotificationSeverityType.ERROR ||
                    res.HighestSeverity == NotificationSeverityType.FAILURE)
                {
                    if (globalSettings.DiagnosticsMode == true)
                    {
                        foreach (var err in res.Notifications)
                        {
                            logger.LogMessage("FEDEX", err.Message, Web.Logging.EventLogSeverity.Debug);
                        }
                    }
                    result = 0m;
                }
                else
                {
                    result = 0m;

                    var lookingForService = GetServiceType(service);
                    var matchingResponse  = res.RateReplyDetails.Where(y => y.ServiceType == lookingForService).FirstOrDefault();
                    if (matchingResponse != null)
                    {
                        var matchedRate = matchingResponse.RatedShipmentDetails.Where(
                            y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_PACKAGE ||
                            y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT).FirstOrDefault();
                        if (matchedRate != null)
                        {
                            result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = 0m;
                logger.LogException(ex);
            }

            return(result);
        }