public decimal GetCostRO(int origin, int destination, int weight) { var roCityFrom = _rajaOngkirCityRepository.Get().Where(c => c.RegencyId == origin).FirstOrDefault(); var roCityTo = _rajaOngkirCityRepository.Get().Where(c => c.RegencyId == destination).FirstOrDefault(); var roCostResp = _rajaOngkirClient.GetPrice(roCityFrom.ROCityId.ToString(), roCityTo.ROCityId.ToString(), weight); foreach (JToken eachExp in roCostResp.SelectToken("rajaongkir.results")) { if ((string)eachExp["code"] == _rajaOngkirClient.DefaultCourier) { foreach (JToken eachServ in eachExp.SelectToken("costs")) { if ((string)eachServ["service"] == _rajaOngkirClient.DefaultService) { foreach (JToken eachCost in eachServ.SelectToken("cost")) { try { return((decimal)eachCost["value"]); } catch (Exception e) { } } } } } } throw new Exception("Could not fetch cost from Raja Ongkir"); }
private LogisticPacket UpdateCostBasedOnRO(LogisticPacket logisticPacket) { var roCityFrom = _rajaOngkirCityRepository.Get().Where(c => c.RegencyId == logisticPacket.AddressFrom.RegencyId).FirstOrDefault(); var roCityTo = _rajaOngkirCityRepository.Get().Where(c => c.RegencyId == logisticPacket.AddressTo.RegencyId).FirstOrDefault(); var roCostResp = _rajaOngkirClient.GetPrice(roCityFrom.ROCityId.ToString(), roCityTo.ROCityId.ToString(), logisticPacket.UnitNumber); bool found = false; foreach (JToken eachExp in roCostResp.SelectToken("rajaongkir.results")) { if ((string)eachExp["code"] == _rajaOngkirClient.DefaultCourier) { foreach (JToken eachServ in eachExp.SelectToken("costs")) { if ((string)eachServ["service"] == _rajaOngkirClient.DefaultService) { foreach (JToken eachCost in eachServ.SelectToken("cost")) { try { logisticPacket.CurrentNominal = (decimal)eachCost["value"]; found = true; break; } catch (Exception e) {} } if (found) { break; } } } } if (found) { break; } } if (!found) { throw new Exception("Could not fetct cost from Raja Ongkir"); } return(logisticPacket); }