/**
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\Response
  * @throws \Exception
  */
 public function getAllRates(Request $request)
 {
     $shipment = new Shipment();
     //Destination data
     $shipment->setToPostalCode($request->input('to_zip', ''))->setToName($request->input('to_name', ''))->setToAddress1($request->input('to_address', ''))->setToCity($request->input('to_city', ''))->setToState($request->input('to_state', ''))->setToCountryCode($request->input('to_country', ''));
     //sender data
     $shipment->setFromPostalCode($request->input('from_zip', env('SHIPMENT_FROM_POSTAL_CODE')))->setFromName($request->input('from_name', env('SHIPMENT_FROM_NAME')))->setFromAddress1($request->input('from_address', env('SHIPMENT_FROM_ADDRESS1')))->setFromCity($request->input('from_city', env('SHIPMENT_FROM_CITY')))->setFromState($request->input('from_state', env('SHIPMENT_FROM_STATE')))->setFromCountryCode($request->input('from_country', env('SHIPMENT_FROM_COUNTRY_CODE')));
     $country = $this->countryRepository->findBy('code', $shipment->getToCountryCode());
     if (!$country) {
         throw new StoreResourceFailedException('Invalid country. Code "' . $shipment->getToCountryCode() . '" not found');
     }
     $carriers = $this->repository->allWithCountry($country);
     foreach ($carriers as $carrier) {
         $shipment->addCarrier($carrier);
     }
     try {
         $package = new Package();
         $package->setWeight(floatval($request->input('parcel_weight', '')))->setHeight(floatval($request->input('parcel_height', '')))->setLength(floatval($request->input('parcel_length', '')))->setWidth(floatval($request->input('parcel_width', '')));
         $shipment->setPackage($package);
         $rates = $shipment->getRates();
     } catch (ShipmentException $e) {
         throw new StoreResourceFailedException($e->getMessage(), $e->getFields());
     }
     return response()->json(['data' => $rates]);
 }
 /**
  * @param $idCarrier
  * @return mixed
  */
 protected function validCarrier($idCarrier)
 {
     $carrier = $this->carrierRepository->find($idCarrier);
     if (!$carrier) {
         throw new UpdateResourceFailedException("Carrier '{$idCarrier}' not found");
     }
     return $carrier;
 }
 /**
  * @param $idCarrier
  * @param $idService
  *
  * @return CarrierService
  */
 protected function getService($idCarrier, $idService)
 {
     $carrier = $this->carrierRepository->find($idCarrier);
     if (!$carrier) {
         throw new UpdateResourceFailedException('Carrier not found');
     }
     $service = $this->repository->findByIdAndCarrier($idService, $carrier);
     if (!$service) {
         throw new UpdateResourceFailedException('Service not found');
     }
     return $service;
 }
 public function getRates()
 {
     $carrierRates = [];
     foreach ($this->carriers as $carrier) {
         $this->carrierRepository->setModel($carrier);
         $response = [];
         $response['code'] = $carrier->code;
         $response['name'] = $carrier->name;
         if (class_exists($carrier->model_reference)) {
             $shipper = new $carrier->model_reference($carrier, $this);
             $shipper->setServices($this->carrierRepository->getServices());
             $result = $shipper->getRate();
             if (isset($result['errors'])) {
                 $response['errors'] = $result['errors'];
             } else {
                 $response['rates'] = $result;
             }
         }
         $carrierRates[] = $response;
     }
     return $carrierRates;
 }