quitenoisemaker/shipping-tracker
Composer 安装命令:
composer require quitenoisemaker/shipping-tracker
包简介
An extendable Laravel package for tracking shipments from Nigerian and European carriers.
README 文档
README
A Laravel package to simplify shipment tracking and webhook handling for Africa and Europe logistics. Supports providers like Cargoplug, Sendbox, and DHL, with an open-source spirit welcoming contributions.
Features
- Track shipments with a unified interface via DTOs.
- Handle webhooks from couriers effortlessly.
- Store tracking history in a
shippingstable. - Queue webhook processing with the
databasedriver. - Extensible for custom providers.
- Normalize provider-specific statuses (e.g., "In transit" ->
in_transit) - [New] CLI commands for tracking and health checks.
- [New]
FakeShippingProviderfor easy testing.
Requirements
- PHP 8.1, 8.2, or 8.3
- Laravel 10 or 11
- Composer
Installation
-
Install via Composer:
composer require quitenoisemaker/shipping-tracker
-
Publish the configuration and migrations:
php artisan vendor:publish --provider="Quitenoisemaker\ShippingTracker\ShippingTrackerServiceProvider" -
Run migrations to create the
shipping_webhooksandjobstables:php artisan migrate
-
Add Environment Variables
Update your
.envfile:SENDBOX_API_URL=https://live.sendbox.co SENDBOX_APP_ID=your_app_id SENDBOX_APP_CLIENT_KEY=your_client_key CARGOPLUG_API_URL=https://api.getcargoplug.com/api/v1 CARGOPLUG_SECRET_KEY=your_secret_key CARGOPLUG_CLIENT_KEY=your_client_key DHL_BASE_URL=https://api-eu.dhl.com DHL_API_KEY=your_api_key
Configuration
The config/shipping-tracker.php file allows you to:
- Set the default provider (e.g.,
dhl). - Define provider classes and their API credentials.
- Add new providers by mapping keys to their implementation classes.
Note: Providers like (Sendbox and Cargoplug) do not use tracking number prefixes or webhook signatures. The package caches successful provider matches for known tracking numbers to optimize tracking. New tracking numbers trigger a full provider search, and explicit provider selection via use() is respected without affecting the cache.
Example:
return [ 'default' => 'sendbox', 'providers' => [ 'sendbox' => \Quitenoisemaker\ShippingTracker\Providers\SendboxShippingProvider::class, 'cargoplug' => \Quitenoisemaker\ShippingTracker\Providers\CargoplugShippingProvider::class, ], 'sendbox' => [ 'base_url' => env('SENDBOX_API_URL', 'https://live.sendbox.co'), 'app_id' => env('SENDBOX_APP_ID'), 'client_key' => env('SENDBOX_APP_CLIENT_KEY'), ], 'cargoplug' => [ 'base_url' => env('CARGOPLUG_API_URL', 'https://api.getcargoplug.com/api/v1'), 'secret_key' => env('CARGOPLUG_SECRET_KEY'), 'client_key' => env('CARGOPLUG_CLIENT_KEY'), ], 'dhl' => [ 'base_url' => env('DHL_BASE_URL', 'https://api-eu.dhl.com'), 'api_key' => env('DHL_API_KEY'), ], ];
Usage
Tracking a Shipment
Track a single shipment. If no provider is specified, the package tries all configured providers, caching the successful provider for the tracking number. New tracking numbers trigger a full provider search.
use Quitenoisemaker\ShippingTracker\facades\ShippingTracker; try { $result = ShippingTracker::track('SB123456789'); dd($result); } catch (\Quitenoisemaker\ShippingTracker\Exceptions\ShippingException $e) { \Log::error('Tracking failed: ' . $e->getMessage()); }
Explicitly specify a provider to bypass automatic resolution and caching:
$result = ShippingTracker::use('cargoplug')->track('CP987654321');
The response is a strict TrackingResult object, not an array.
echo $result->trackingNumber; echo $result->status; // 'in_transit' echo $result->provider; // 'sendbox' // $result->events is a Collection of TrackingEvent objects foreach ($result->events as $event) { echo $event->status; echo $event->timestamp; }
Tracking Multiple Shipments
Track multiple shipments in one call (client-side, as providers do not support batch API calls):
try { $results = ShippingTracker::trackMultiple(['SB123456789', 'CP987654321']); dd($results); } catch (\Quitenoisemaker\ShippingTracker\Exceptions\ShippingException $e) { \Log::error('Batch tracking failed: ' . $e->getMessage()); }
The response is an array keyed by tracking number, with results or error messages.
CLI Commands (New in v2.0)
Track a shipment directly from your terminal:
php artisan shipping:track SB123456789 --provider=sendbox
Check API Health: Validate your configuration and API keys for all providers:
php artisan shipping:check-status
Testing with Fakes
You can use the FakeShippingProvider to test your application without making real API calls.
// In your test or local dev ShippingTracker::use('fake')->track('ANY-NUMBER');
Tracking History
Shipment updates from webhooks are stored in the shipments table, including tracking_number, provider, status, location, estimated_delivery, and history.
Retrieve a shipment’s history:
use Quitenoisemaker\ShippingTracker\Models\Shipment; $shipment = Shipment::where('tracking_number', 'SB123456789')->first(); dd($shipment->history);
Or use the convenience method:
$history = ShippingTracker::getHistory('SB123456789'); // Returns a Shipment model. The 'history' attribute is cast to an array/json. dd($history->history);
Handling Webhooks
Webhooks are validated for required fields. *
- Default required fields for Cargoplug and Sendbox:
-
- tracking_number
-
- status
- DHL specific required field:
-
- self (the subscription URL, always starting with https://api-eu.dhl.com)
-
- scope
- The package is extensible for signature-based validation for providers that support webhook signing.
Route::prefix('api/shipping')->group(function () { Route::post('webhooks/{provider}', [\Quitenoisemaker\ShippingTracker\Http\Controllers\WebhookController::class, 'handle'])->name('shipping.webhook'); });
Register the webhook URL (e.g., https://your-app.com/api/shipping/webhooks/sendbox) with your provider.
Status Normalization
ShippingTracker normalizes provider statuses using StatusMapper in both webhooks and API tracking (track method). It handles case and space variations (e.g., Cargoplug's "In Transit" → in_transit, "PAID" → paid). Examples:
- Sendbox:
delivery_started→in_transit,delivered→delivered - Cargoplug:
received_abroad→in_transit,In Transit→in_transit,delivered→delivered - DHL:
CUSTOMER PICKUP→delivered,PACKAGE SCREENED SUCCESSFULLY→in_transit,ATTEMPTED DELIVERY→on_hold
Extending the Package
Create a custom provider:
- Create a class implementing
Quitenoisemaker\ShippingTracker\Contracts\ShippingProvider:namespace App\ShippingProviders; use Quitenoisemaker\ShippingTracker\Contracts\ShippingProvider; class CustomProvider implements ShippingProvider { public function track(string $trackingNumber): array { // API call to your provider return ['tracking_number' => $trackingNumber, 'status' => 'in_transit']; } public function handleWebhook(array $payload): void { // Process webhook data } }
- Register it in
config/shipping-tracker.php:'providers' => [ 'custom' => [ 'class' => \App\ShippingProviders\CustomProvider::class, ], ];
- Use it:
$tracker->use('custom')->track('CUSTOM123');
Troubleshooting
- API Credential Errors:
- Ensure
CARGOPLUG_API_KEYandSENDBOX_API_KEYare set in.env. - Verify keys with your provider’s dashboard.
- Test with:
php artisan tinkerand$tracker->use('cargoplug')->track('test_number').
- Ensure
- Webhook Not Received:
- Confirm the webhook URL is correct (e.g.,
your-app.com/shipping/webhook). - Check
shipping_webhookstable for entries. - Ensure the queue worker is running:
php artisan queue:work. - Verify your server allows POST requests (e.g., no firewall blocks).
- Confirm the webhook URL is correct (e.g.,
- Queue Issues:
- Ensure
QUEUE_CONNECTION=databasein.env. - Check
jobstable exists:php artisan migrate. - Clear cache:
php artisan config:cache.
- Ensure
- Test Failures:
- Run
composer installto ensure dependencies. - Check
phpunit.xmlfor correct database settings. - Share errors with the community (see Contributing).
- Run
Testing
Run tests with:
php artisan test
Changelog
v2.0.0 (Latest)
- [Breaking]
track()now returnsTrackingResultDTO instead of an array. - Added
shipping:trackandshipping:check-statusArtisan commands. - Added
FakeShippingProviderfor testing. - Added
checkHealth()toShippingProviderInterface. - Implemented lazy loading for providers to improve robustness.
v1.2.0
- Added DHL integration for tracking and webhook handling.
- Normalized DHL statuses.
v1.0.0
- Initial release.
- Supports Cargoplug and Sendbox providers.
- Features tracking, webhook handling, and history storage.
Contributing
We love open source! Join us by contributing at github.com/quitenoisemaker/shipping-tracker. See CONTRIBUTING.md for how to submit issues or pull requests.
License
MIT License. See LICENSE for details.
quitenoisemaker/shipping-tracker 适用场景与选型建议
quitenoisemaker/shipping-tracker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 15, 最近一次更新时间为 2025 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「shipping」 「tracking」 「webhooks」 「sendbox」 「cargoplug」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 quitenoisemaker/shipping-tracker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 quitenoisemaker/shipping-tracker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 quitenoisemaker/shipping-tracker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP SDK for the Parcel Monkey UK API
PHP wrapper for DPD Germany SOAP API
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
A PHP (and Laravel) package to interface with the Snipcart api.
Audit changes of your Eloquent models in Laravel/Lumen
Alfabank REST API integration
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-30