Exemplo n.º 1
0
        private async Task <MoneyDTO> GetShippingResponseRatesAsync(RateShipmentResponse shipmentResponse, string shippingMethod, CommercePipelineExecutionContext context)
        {
            // Func<Rate, bool> shippingMethodMatchDelegate = rate => rate.ServiceCode?.IndexOf(shippingMethod, StringComparison.InvariantCultureIgnoreCase) >= 0;

            var shipmentDetail =
                shipmentResponse
                .RateResponse
                .Rates
                .FirstOrDefault(r => r.ServiceCode?.IndexOf(shippingMethod, StringComparison.InvariantCultureIgnoreCase) >= 0);

            if (shipmentDetail == null)
            {
                if (shipmentResponse.RateResponse.Errors.Count > 0)
                {
                    for (int i = 0; i < shipmentResponse.RateResponse.Errors.Count; i++)
                    {
                        context.Abort(await context.CommerceContext.AddMessage(
                                          context.GetPolicy <KnownResultCodes>().Error,
                                          "UnavailableServiceType",
                                          new object[1] {
                            shipmentResponse.RateResponse.Errors[i].Message
                        }
                                          ).ConfigureAwait(continueOnCapturedContext: false), context);
                    }
                }

                return(null);
            }


            return(shipmentDetail.ShippingAmount);
        }
Exemplo n.º 2
0
 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"));
 }