Exemple #1
0
        private static double ToGrams(Weight.UnitEnum weightUnit, double mass)
        {
            switch (weightUnit)
            {
            case Weight.UnitEnum.Gram: return(mass);;

            case Weight.UnitEnum.Kilogram: return(mass / 1000.0);

            case Weight.UnitEnum.Ounce: return(mass * 28.3495);

            case Weight.UnitEnum.Pound: return(mass * 453.592);

            default: return(mass);
            }
        }
 public IActionResult CalculateRates(Request model)
 {
     HttpContext.Session.Clear();
     if (ModelState.IsValid)
     {
         //create dimensions object
         Dimensions.UnitEnum dimensionsUnit = new Dimensions.UnitEnum();
         foreach (KeyValuePair <string, Dimensions.UnitEnum> item in DimensionUnitsConfig.DimensionsUnitsList)
         {
             if (item.Key == model.shipment.package.dimensions.unit)
             {
                 dimensionsUnit = item.Value;
             }
         }
         Dimensions dimensions = new Dimensions
         {
             Length = model.shipment.package.dimensions.length,
             Width  = model.shipment.package.dimensions.width,
             Height = model.shipment.package.dimensions.height,
             Unit   = dimensionsUnit
         };
         //create weight object
         Weight.UnitEnum weightUnit = new Weight.UnitEnum();
         foreach (KeyValuePair <string, Weight.UnitEnum> item in WeightUnitsConfig.WeightUnitsList)
         {
             if (item.Key == model.shipment.package.weight.unit)
             {
                 weightUnit = item.Value;
             }
         }
         Weight weight = new Weight
         {
             Value = model.shipment.package.weight.value,
             Unit  = weightUnit
         };
         //create package list
         List <ShipmentPackage> packageList = new List <ShipmentPackage>();
         //create package and push to package list
         ShipmentPackage package = new ShipmentPackage
         {
             Weight     = weight,
             Dimensions = dimensions
         };
         packageList.Add(package);
         // create ship to address
         AddressDTO ship_to = new AddressDTO
         {
             Name          = model.shipment.ship_to.name,
             Phone         = model.shipment.ship_to.phone,
             PostalCode    = model.shipment.ship_to.postal_code,
             StateProvince = model.shipment.ship_to.state_province,
             CityLocality  = model.shipment.ship_to.city_locality,
             AddressLine1  = model.shipment.ship_to.address_line1,
             AddressLine2  = model.shipment.ship_to.address_line2,
             CompanyName   = model.shipment.ship_to.company_name,
             CountryCode   = model.shipment.ship_to.country_code
         };
         // create ship from address
         AddressDTO ship_from = new AddressDTO
         {
             Name          = model.shipment.ship_from.name,
             Phone         = model.shipment.ship_from.phone,
             PostalCode    = model.shipment.ship_from.postal_code,
             StateProvince = model.shipment.ship_from.state_province,
             CityLocality  = model.shipment.ship_from.city_locality,
             AddressLine1  = model.shipment.ship_from.address_line1,
             AddressLine2  = model.shipment.ship_from.address_line2,
             CompanyName   = model.shipment.ship_from.company_name,
             CountryCode   = model.shipment.ship_from.country_code
         };
         // create shipment
         AddressValidatingShipment shipment = new AddressValidatingShipment
         {
             Packages = packageList,
             ShipFrom = ship_from,
             ShipTo   = ship_to
         };
         // create rateoptions
         var           apiKey     = "2zWOj6zfmwc98DLwos4B9U5Jg9wOxkAjlX20vgEYtDs";
         var           carrierApi = new CarriersApi();
         List <string> carrierIds = new List <string>();
         try
         {
             List <Carrier> carrierList = carrierApi.CarriersList(apiKey).Carriers;
             foreach (Carrier carrier in carrierList)
             {
                 carrierIds.Add(carrier.CarrierId);
             }
         }
         catch (Exception e)
         {
             Debug.Print("Exception when calling CarriersApi.CarriersList: " + e.Message);
         }
         RateRequest rateOptions = new RateRequest
         {
             CarrierIds = carrierIds
         };
         // create request
         RateShipmentRequest request = new RateShipmentRequest
         {
             Shipment    = shipment,
             RateOptions = rateOptions
         };
         //send rate results to action through tempdata
         var rateApi = new RatesApi();
         try
         {
             RateShipmentResponse result = rateApi.RatesRateShipment(request, apiKey);
             RateResponse         rates  = result.RateResponse;
             HttpContext.Session.SetString("rates", JsonConvert.SerializeObject(rates));
             return(RedirectToAction("RateResults"));
         }
         catch (Exception e)
         {
             Debug.Print("Exception when calling RatesApi.RatesRateShipment: " + e.Message);
         }
     }
     return(RedirectToAction("Rates"));
 }