public async Task <ShipEstimateResponse> GetShippingRates(OrderCalculatePayload <CheckoutConfig> payload) { var response = new ShipEstimateResponse(); // Group lineItems into shipments var shipments = payload.OrderWorksheet.LineItems.GroupBy(li => li.ShipFromAddress.ID); foreach (var shipment in shipments) { // Use a shipping estimator (EasyPost, Fedex, UPS, ect) to get shipMethods var shipMethods = new List <ShipMethod>() { new ShipMethod() { Name = "Fedex 2 Day Priority", Cost = 12.45M, EstimatedTransitDays = 2 } }; response.ShipEstimates.Add(new ShipEstimate() { ShipMethods = shipMethods }); } return(response); }
public async Task <ShipEstimateResponse> GetRates(IEnumerable <IGrouping <AddressPair, LineItem> > groupedLineItems, EasyPostShippingProfiles profiles) { // First, filter out any line items that are set to have free shipping var filteredGroupedList = new List <Grouping <AddressPair, LineItem> >(); foreach (IGrouping <AddressPair, LineItem> group in groupedLineItems) { var filteredLineItems = group.ToList().Where(li => li.Product.xp.FreeShipping == false); filteredGroupedList.Add(new Grouping <AddressPair, LineItem>(group.Key, filteredLineItems)); } var easyPostShipments = filteredGroupedList.Select(li => EasyPostMappers.MapShipment(li, profiles)).ToList(); var easyPostResponses = new List <EasyPostShipment[]>(); var postShipments = easyPostShipments; foreach (var shipment in postShipments) { var response = await Throttler.RunAsync(shipment, 200, 10, PostShipment); easyPostResponses.Add(response.ToArray()); } var shipEstimateResponse = new ShipEstimateResponse { ShipEstimates = groupedLineItems.Select((lineItems, index) => { // If all line items in the list have FreeShipping, then Mock rates if (lineItems.ToList().All(li => li.Product?.xp?.FreeShipping)) { return(MockRatesForFreeShipping(lineItems.ToList())); } var firstLi = lineItems.First(); var shipMethods = EasyPostMappers.MapRates(easyPostResponses[index]); return(new ShipEstimate() { ID = easyPostResponses[index][0].id, ShipMethods = shipMethods, // This will get filtered down based on carrierAccounts ShipEstimateItems = lineItems.Select(li => new ShipEstimateItem() { LineItemID = li.ID, Quantity = li.Quantity }).ToList(), xp = { AllShipMethods = shipMethods, // This is being saved so we have all data to compare rates across carrierAccounts SupplierID = firstLi.SupplierID, // This will help with forwarding the supplier order ShipFromAddressID = firstLi.ShipFromAddressID // This will help with forwarding the supplier order } }); }).ToList(), }; return(shipEstimateResponse); }