shopper/shipping
Laravel package for managing shipping in Shopper-based apps: define shipping methods and zones, configure rates and rules, and integrate checkout shipping selection. Built to fit neatly into the Shopper ecosystem and typical Laravel e-commerce workflows.
Installation
composer require shopper/shipping
Publish the config file (if available):
php artisan vendor:publish --provider="Shopper\Shipping\ShippingServiceProvider"
Basic Configuration
Edit config/shipping.php (if published) to define your shipping providers (e.g., FedEx, UPS, USPS). Example:
'providers' => [
'fedex' => [
'key' => env('FEDEX_API_KEY'),
'account' => env('FEDEX_ACCOUNT'),
'test_mode' => env('APP_ENV') === 'local',
],
],
First Use Case: Fetch Rates
use Shopper\Shipping\Facades\Shipping;
$rates = Shipping::getRates([
'origin' => ['zip' => '90210'],
'destination' => ['zip' => '10001'],
'package' => ['weight' => 2.5, 'dimensions' => [10, 10, 5]],
]);
Provider Agnostic Rate Requests Use the facade to abstract provider logic:
$rates = Shipping::getRates($requestData);
Internally, the package routes requests to configured providers (e.g., FedEx, UPS).
Dynamic Provider Selection Override the default provider per request:
$rates = Shipping::getRates($requestData, 'ups');
Shipping Label Generation
$label = Shipping::createLabel($shipmentData, 'fedex');
Tracking Integration
$tracking = Shipping::track('FED123456789', 'fedex');
Laravel Services
Bind the Shipping facade to your service container for dependency injection:
public function __construct(private Shipping $shipping) {}
Request Validation Validate shipping data before passing to the package:
$validated = $request->validate([
'origin.zip' => 'required|string',
'destination.zip' => 'required|string',
'package.weight' => 'required|numeric',
]);
Caching Rates Cache provider responses to reduce API calls:
$rates = Cache::remember("shipping_rates_{$cacheKey}", now()->addHours(1), function() use ($requestData) {
return Shipping::getRates($requestData);
});
Event Listeners
Dispatch events for shipping actions (e.g., ShippingRateFetched):
Shipping::getRates($data)->each(function($rate) {
event(new ShippingRateFetched($rate));
});
Missing Config
shipping.php file.php artisan vendor:publish and verify provider keys are set in .env.Provider-Specific Quirks
shipperNumber).Rate Limiting
test_mode in config and mock responses:
'providers' => [
'fedex' => [
'test_mode' => true,
'mock_response' => file_get_contents(base_path('tests/mocks/fedex_rates.json')),
],
],
Error Handling
try {
$rates = Shipping::getRates($data);
} catch (\Shopper\Shipping\Exceptions\ProviderException $e) {
Log::error("Shipping error: " . $e->getMessage());
abort(500, "Shipping service unavailable");
}
Enable Logging
Add to config/shipping.php:
'debug' => env('APP_ENV') === 'local',
Check storage/logs/laravel.log for provider API responses.
Mock Providers Use Laravel’s HTTP testing to mock provider APIs:
$this->get('api/fedex/rates')
->json(['rates' => [...]]);
Custom Providers
Extend the base Provider class:
namespace App\Providers;
use Shopper\Shipping\Contracts\Provider;
class CustomProvider implements Provider {
public function getRates(array $data) { ... }
public function createLabel(array $data) { ... }
}
Register in config/shipping.php:
'providers' => [
'custom' => \App\Providers\CustomProvider::class,
],
Request/Response Transformers
Override the Shipping facade to modify data before/after API calls:
Shipping::extend(function($app) {
$app->resolving('shipping', function($shipping) {
$shipping->beforeGetRates(function($data) {
$data['custom_field'] = 'value';
return $data;
});
});
});
Webhook Handling Listen for provider webhooks (e.g., shipping updates) via Laravel events:
event(new ShippingWebhookReceived($payload));
How can I help you explore Laravel packages today?