smart-dato/fedex-rest-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

smart-dato/fedex-rest-sdk

Composer 安装命令:

composer require smart-dato/fedex-rest-sdk

包简介

FedEx REST SDK

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel package for integrating with the FedEx REST APIs. Supports shipment creation, cancellation, validation, tag management, and trade document uploads. Built on Saloon 3.x for HTTP and Spatie Laravel Data 4.x for DTOs.

Requirements

  • PHP 8.4+
  • Laravel 11 or 12

Installation

Install the package via Composer:

composer require smart-dato/fedex-rest-sdk

Publish the config file:

php artisan vendor:publish --tag="fedex-rest-sdk-config"

Configuration

Add the following environment variables to your .env file:

FEDEX_CLIENT_ID=your-client-id
FEDEX_CLIENT_SECRET=your-client-secret
FEDEX_ACCOUNT_NUMBER=your-account-number

The published config file (config/fedex-rest-sdk.php) contains all available options:

return [
    'client_id' => env('FEDEX_CLIENT_ID'),
    'client_secret' => env('FEDEX_CLIENT_SECRET'),
    'account_number' => env('FEDEX_ACCOUNT_NUMBER'),

    'grant_type' => env('FEDEX_GRANT_TYPE', 'client_credentials'),
    'child_key' => env('FEDEX_CHILD_KEY'),
    'child_secret' => env('FEDEX_CHILD_SECRET'),

    'base_url' => env('FEDEX_BASE_URL', 'https://apis.fedex.com'),
    'document_base_url' => env('FEDEX_DOCUMENT_BASE_URL', 'https://documentapi.prod.fedex.com'),

    'verify_ssl' => env('FEDEX_VERIFY_SSL', true),
];

For the sandbox environment, override the URLs:

FEDEX_BASE_URL=https://apis-sandbox.fedex.com
FEDEX_DOCUMENT_BASE_URL=https://documentapitest.prod.fedex.com/sandbox

Authentication Grant Types

The SDK supports three FedEx authentication modes:

  • client_credentials — Standard B2B (default)
  • csp_credentials — Compatible/Integrator customers with child accounts
  • client_pc_credentials — Proprietary Parent-Child customers

For CSP or Parent-Child authentication, also set:

FEDEX_GRANT_TYPE=csp_credentials
FEDEX_CHILD_KEY=your-child-key
FEDEX_CHILD_SECRET=your-child-secret

Usage

Resolve the SDK from the container (or use the FedEx facade):

use SmartDato\FedEx\FedEx;

$fedex = app(FedEx::class);

Create a Shipment

use SmartDato\FedEx\Data\Ship\CreateShipmentData;

$request = CreateShipmentData::from([
    'accountNumber' => ['value' => '123456789'],
    'labelResponseOptions' => 'URL_ONLY',
    'requestedShipment' => [
        'shipper' => [
            'address' => [
                'streetLines' => ['123 Ship Street'],
                'city' => 'Memphis',
                'stateOrProvinceCode' => 'TN',
                'postalCode' => '38116',
                'countryCode' => 'US',
            ],
            'contact' => [
                'personName' => 'John Shipper',
                'phoneNumber' => '1234567890',
                'companyName' => 'Shipper Co',
            ],
        ],
        'recipients' => [
            [
                'address' => [
                    'streetLines' => ['456 Recipient Ave'],
                    'city' => 'Los Angeles',
                    'stateOrProvinceCode' => 'CA',
                    'postalCode' => '90001',
                    'countryCode' => 'US',
                ],
                'contact' => [
                    'personName' => 'Jane Recipient',
                    'phoneNumber' => '0987654321',
                    'companyName' => 'Recipient Inc',
                ],
            ],
        ],
        'pickupType' => 'DROPOFF_AT_FEDEX_LOCATION',
        'serviceType' => 'STANDARD_OVERNIGHT',
        'packagingType' => 'YOUR_PACKAGING',
        'totalWeight' => 10.0,
        'shippingChargesPayment' => [
            'paymentType' => 'SENDER',
        ],
        'labelSpecification' => [
            'imageType' => 'PDF',
            'labelStockType' => 'PAPER_4X6',
        ],
        'requestedPackageLineItems' => [
            [
                'weight' => [
                    'units' => 'LB',
                    'value' => 10.0,
                ],
            ],
        ],
    ],
]);

$response = $fedex->ship()->createShipment($request);

$response->transactionId;
$response->output->transactionShipments[0]->masterTrackingNumber;
$response->output->transactionShipments[0]->pieceResponses[0]->trackingNumber;

Validate a Shipment

use SmartDato\FedEx\Data\Ship\ValidateShipmentData;

$request = ValidateShipmentData::from([
    'accountNumber' => ['value' => '123456789'],
    'requestedShipment' => [
        // Same structure as create shipment
    ],
]);

$response = $fedex->ship()->validateShipment($request);

foreach ($response->output->alerts as $alert) {
    $alert->code;       // "SHIPMENT.VALIDATION.SUCCESS"
    $alert->alertType;  // "NOTE"
    $alert->message;
}

Cancel a Shipment

use SmartDato\FedEx\Data\Ship\CancelShipmentData;

$request = CancelShipmentData::from([
    'accountNumber' => ['value' => '123456789'],
    'trackingNumber' => '794644790138',
]);

$response = $fedex->ship()->cancelShipment($request);

$response->output->cancelledShipment;  // true

Create and Cancel Tags

use SmartDato\FedEx\Data\Ship\CreateTagData;
use SmartDato\FedEx\Data\Ship\CancelTagData;

// Create a tag
$response = $fedex->ship()->createTag(CreateTagData::from([
    'accountNumber' => ['value' => '123456789'],
    'requestedShipment' => [/* ... */],
]));

$response->output->masterTrackingNumber;
$response->output->completedTagDetail->confirmationNumber;

// Cancel a tag
$fedex->ship()->cancelTag(CancelTagData::from([
    'accountNumber' => ['value' => '123456789'],
    'serviceType' => 'PRIORITY_OVERNIGHT',
    'completedTagDetail' => [
        'confirmationNumber' => 'CONF123',
    ],
]), shipmentId: 'SHIP123');

Retrieve Async Shipment Results

For shipments with 40+ packages processed asynchronously:

use SmartDato\FedEx\Data\Ship\AsyncResultsData;

$response = $fedex->ship()->retrieveAsyncResults(AsyncResultsData::from([
    'accountNumber' => ['value' => '123456789'],
    'jobId' => '624deea6-b709-470c-8c39-4b5511281492',
]));

Upload Trade Documents

use SmartDato\FedEx\Data\Document\UploadDocumentData;

$data = UploadDocumentData::from([
    'workflowName' => 'ETDPreshipment',
    'name' => 'commercial_invoice.pdf',
    'contentType' => 'application/pdf',
    'meta' => [
        'shipDocumentType' => 'COMMERCIAL_INVOICE',
        'originCountryCode' => 'US',
        'destinationCountryCode' => 'CA',
    ],
]);

$response = $fedex->documents()->uploadDocument($data, '/path/to/commercial_invoice.pdf');

$response->output->meta->docId;        // Use this in your shipment request
$response->output->meta->documentType;  // "CI"

Upload Multiple Documents

use SmartDato\FedEx\Data\Document\MultiUploadDocumentData;

$data = MultiUploadDocumentData::from([
    'workflowName' => 'ETDPreshipment',
    'carrierCode' => 'FDXE',
    'originCountryCode' => 'US',
    'destinationCountryCode' => 'CA',
    'metaData' => [
        [
            'fileName' => 'invoice.pdf',
            'contentType' => 'application/pdf',
            'shipDocumentType' => 'COMMERCIAL_INVOICE',
        ],
        [
            'fileName' => 'certificate.pdf',
            'contentType' => 'application/pdf',
            'shipDocumentType' => 'CERTIFICATE_OF_ORIGIN',
        ],
    ],
]);

$response = $fedex->documents()->multiUploadDocuments($data, [
    '/path/to/invoice.pdf',
    '/path/to/certificate.pdf',
]);

foreach ($response->output->documentResponses as $doc) {
    $doc->docId;
    $doc->documentType;
}

Upload Letterhead/Signature Images

use SmartDato\FedEx\Data\Document\ImageDocumentData;

$data = ImageDocumentData::from([
    'referenceId' => '1234',
    'name' => 'signature.png',
    'contentType' => 'image/png',
    'workflowName' => 'LetterheadSignature',
    'meta' => [
        'imageType' => 'SIGNATURE',
        'imageIndex' => 'IMAGE_1',
    ],
]);

$response = $fedex->documents()->uploadImage($data, '/path/to/signature.png');

$response->output->status;               // "SUCCESS"
$response->output->documentReferenceId;   // "1234"

Using the Facade

use SmartDato\FedEx\Facades\FedEx;

$response = FedEx::ship()->createShipment($request);
$labels = FedEx::documents()->uploadDocument($data, $filePath);

On-the-fly Instantiation

Create instances without the Laravel container, useful for multiple accounts or non-Laravel usage:

use SmartDato\FedEx\FedEx;

$fedex = FedEx::make([
    'client_id' => 'your-client-id',
    'client_secret' => 'your-client-secret',
    'base_url' => 'https://apis-sandbox.fedex.com',
    'document_base_url' => 'https://documentapitest.prod.fedex.com/sandbox',
]);

$response = $fedex->ship()->createShipment($request);

Accessing Raw Request/Response

After any API call, you can inspect the raw Saloon request and response for debugging or logging:

$response = $fedex->ship()->createShipment($request);

$fedex->ship()->lastRequest();    // Saloon\Http\Request
$fedex->ship()->lastResponse();   // Saloon\Http\Response

// Same for documents
$fedex->documents()->lastRequest();
$fedex->documents()->lastResponse();

Available Enums

The package provides typed enums for API constants:

use SmartDato\FedEx\Enums\PaymentType;       // SENDER, RECIPIENT, THIRD_PARTY, COLLECT
use SmartDato\FedEx\Enums\PickupType;        // CONTACT_FEDEX_TO_SCHEDULE, DROPOFF_AT_FEDEX_LOCATION, USE_SCHEDULED_PICKUP
use SmartDato\FedEx\Enums\WeightUnit;        // KG, LB
use SmartDato\FedEx\Enums\DimensionUnit;     // CM, IN
use SmartDato\FedEx\Enums\ImageType;         // ZPLII, EPL2, PDF, PNG
use SmartDato\FedEx\Enums\LabelStockType;    // PAPER_4X6, STOCK_4X675, PAPER_4X675, PAPER_4X8, PAPER_4X9, PAPER_7X475
use SmartDato\FedEx\Enums\LabelFormatType;   // COMMON2D, LABEL_DATA_ONLY
use SmartDato\FedEx\Enums\DeletionControl;   // DELETE_ALL_PACKAGES, DELETE_ONE_PACKAGE, LEGACY
use SmartDato\FedEx\Enums\ShipDocumentType;  // CERTIFICATE_OF_ORIGIN, COMMERCIAL_INVOICE, ETD_LABEL, ...
use SmartDato\FedEx\Enums\WorkflowName;      // ETDPreshipment, ETDPostshipment
use SmartDato\FedEx\Enums\CarrierCode;       // FDXE, FDXG

Error Handling

API errors are thrown as FedExApiException:

use SmartDato\FedEx\Exceptions\FedExApiException;

try {
    $response = $fedex->ship()->createShipment($request);
} catch (FedExApiException $e) {
    $e->getMessage();       // Error message from the API
    $e->getCode();          // HTTP status code
    $e->errorCode;          // FedEx error code (e.g., "NOT.AUTHORIZED.ERROR")
    $e->transactionId;      // Transaction ID for support reference
}

Testing

composer test             # Run tests
composer analyse          # Static analysis (PHPStan level 5)
composer format           # Code style (Laravel Pint)
composer test-coverage    # Tests with coverage report

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

smart-dato/fedex-rest-sdk 适用场景与选型建议

smart-dato/fedex-rest-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「SmartDato」 「fedex-rest-sdk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 smart-dato/fedex-rest-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 smart-dato/fedex-rest-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 smart-dato/fedex-rest-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10