shippingRate; $shippingMethod = $shippingRate->shippingMethod; $cart = $shippingOptionRequest->cart; if ($this->cartHasExcludedProducts($shippingRate, $cart)) { return null; } if (($shippingMethod->data['charge_by'] ?? 'cart_total') !== 'live') { return $this->resolveFixedPrice($shippingRate, $shippingMethod, $cart); } $postcode = $cart->shippingAddress?->postcode; if (! $postcode) { return null; } return $this->resolveLivePrice($shippingRate, $shippingMethod, $cart, $postcode); } /** * Wrapped in CachesLivePricing's cache so a live-pricing outage within * the cache window still serves the last successful quote instead of * immediately falling back. A cold cache during an outage falls back * to the rate's own configured static price (resolveFixedPrice()) — * see ManageShippingRates, which now allows a static price to be * configured on a "live" rate specifically for this fallback. */ private function resolveLivePrice(ShippingRate $shippingRate, $shippingMethod, $cart, string $postcode): ?ShippingOption { return $this->cached($shippingRate, $cart, function () use ($shippingRate, $shippingMethod, $cart, $postcode) { try { $destination = $this->areaResolver->resolve($postcode); $response = $this->client->call('ACS_Price_Calculation', [ 'Billing_Code' => config('acs.billing_code'), 'Acs_Station_Destination' => $destination->stationId, 'Weight' => $this->totalWeightInKg($cart), 'Pickup_Date' => now()->toDateString(), 'Charge_Type' => 2, ])->throwIfError(); } catch (AcsApiException $e) { report($e); return $this->resolveFixedPrice($shippingRate, $shippingMethod, $cart); } $amount = (int) round(($response->valueOutput['Total_Ammount'] ?? 0) * 100); return new ShippingOption( name: ShippingMethodName::resolve($shippingMethod) ?: $this->name(), description: $shippingMethod->description ?: $this->description(), identifier: $shippingRate->getIdentifier(), price: new Price($amount, $cart->currency, 1), taxClass: $shippingRate->getTaxClass(), taxReference: $shippingRate->getTaxReference(), meta: ['acs_station_destination' => $destination->stationId], ); }); } public function on(ShippingRate $shippingRate): self { $this->shippingRate = $shippingRate; return $this; } private function totalWeightInKg($cart): float { return max(WeightCalculator::totalKg($cart->lines), 0.5); // ACS minimum billable weight } }