gabrielbull/ups-api
PHP library wrapping UPS APIs (Quantum View, Tracking, Shipping, Rating, Time in Transit, Address Validation) with simple classes and examples. Helps fetch rates, create shipments, validate addresses, and track packages.
Tracking, Rate, AddressValidation). This aligns well with Laravel’s service-oriented architecture, where each API function can be abstracted into a dedicated service class.Ups\Tracking) as needed..env file, injected via the service container, or managed via a dedicated config file (e.g., config/ups.php).UpsShipmentEvent), enabling reactive workflows (e.g., notifications, inventory updates).App\Exceptions\Handler) may need customization to log UPS-specific errors (e.g., rate limits, invalid credentials).Ups\Tracking, Ups\Rate, etc., as singletons or context-bound instances.ShipmentTracked, RateCalculated)..env:
UPS_ACCESS_KEY=your_key
UPS_USER_ID=your_user_id
UPS_PASSWORD=your_password
AppServiceProvider:
$this->app->singleton(\Ups\Tracking::class, function ($app) {
return new \Ups\Tracking(
config('ups.access_key'),
config('ups.user_id'),
config('ups.password')
);
});
// app/Services/UpsTrackingService.php
class UpsTrackingService {
public function trackShipment(string $trackingNumber): array {
$tracking = app(\Ups\Tracking::class);
$shipment = $tracking->track($trackingNumber);
return $this->formatResponse($shipment);
}
}
// app/Listeners/UpsEventListener.php
class UpsEventListener {
public function handle(Ups\QuantumView $quantumView) {
foreach ($quantumView->getSubscription() as $event) {
event(new ShipmentTracked($event));
}
}
}
// config/logging.php
'channels' => [
'ups' => [
'driver' => 'single',
'path' => storage_path('logs/ups.log'),
'level' => 'debug',
],
];
// app/Exceptions/Handler.php
public function render($request, Throwable $exception) {
if ($exception instanceof \Ups\Exception\UpsException) {
return response()->json(['error' => 'UPS API Error'], 400);
}
return parent::render($request, $exception);
}
// app/Http/Middleware/ThrottleUpsApi.php
public function handle($request, Closure $next) {
return $next($request)->throttle('ups', 60); // 60 calls/minute
}
$rate = Cache::remember("ups_rate_{$shipmentId}", 300, function () {
return $this->upsRateService->getRate($shipment);
});
// Dispatch event to queue
dispatch(new ProcessUpsEvent($event));
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| UPS API downtime | Shipping operations halt | Implement retry logic with exponential backoff. |
| Invalid credentials | All API calls fail | Validate credentials on startup; alert admins. |
| Rate limit exceeded | Slow performance | Cache responses; implement queueing. |
| Ambiguous address validation | User confusion | Present candidate addresses to users. |
| Quantum View event delays | Real-time updates lag | Use Laravel Queues with priority processing. |
How can I help you explore Laravel packages today?