PHP code example of steadfast-courier / steadfast-laravel-package

1. Go to this page and download the library: Download steadfast-courier/steadfast-laravel-package library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

steadfast-courier / steadfast-laravel-package example snippets


use SteadFast\SteadFastCourierLaravelPackage\Facades\SteadfastCourier;

// Book a parcel
$response = SteadfastCourier::placeOrder([
    'invoice' => 'INV-1001',
    'recipient_name' => 'Badrul Hasan Sajib',
    'recipient_phone' => '01309055966',
    'recipient_address' => 'Dhanmondi, Zigatola, Dhaka',
    'cod_amount' => 3010,
]);

$consignmentId = $response['consignment']['consignment_id'];

// Check on it later
$status = SteadfastCourier::checkDeliveryStatusByConsignmentId($consignmentId);

echo $status['delivery_status']; // "in_review"

use SteadFast\SteadFastCourierLaravelPackage\Facades\SteadfastCourier;

$response = SteadfastCourier::placeOrder([
    // Required
    'invoice' => 'INV-1001',                         // your own reference, must be unique
    'recipient_name' => 'Badrul Hasan Sajib',
    'recipient_phone' => '01309055966',
    'recipient_address' => 'Dhanmondi, Zigatola, Dhaka',
    'cod_amount' => 3010,                            // 0 when nothing is collected

    // Optional
    'recipient_email' => '[email protected]',
    'alternative_phone' => '01722743076',
    'item_description' => 'iPhone 16 Pro Max 512GB',
    'note' => 'Call before delivery.',
]);

$response = SteadfastCourier::bulkCreateOrders([
    [
        'invoice' => 'INV-1001',
        'recipient_name' => 'Badrul Hasan Sajib',
        'recipient_phone' => '01309055966',
        'recipient_address' => 'Dhanmondi, Zigatola, Dhaka',
        'cod_amount' => 3010,
    ],
    [
        'invoice' => 'INV-1002',
        'recipient_name' => 'Jane Smith',
        'recipient_phone' => '01711111111',
        'recipient_address' => 'House 44, Road 2/A, Dhanmondi, Dhaka 1209',
        'cod_amount' => 1500,
    ],
]);

foreach ($response['data'] as $consignment) {
    if ($consignment['status'] === 'error') {
        // This one order was rejected; the rest may have succeeded.
        continue;
    }

    // $consignment['consignment_id'], $consignment['tracking_code']
}

use SteadFast\SteadFastCourierLaravelPackage\SteadfastCourier as Client;

foreach (array_chunk($orders, Client::MAX_BULK_ORDERS) as $chunk) {
    SteadfastCourier::bulkCreateOrders($chunk);
}

SteadfastCourier::checkDeliveryStatusByConsignmentId(131822154);
SteadfastCourier::checkDeliveryStatusByInvoiceId('INV-1001');
SteadfastCourier::checkDeliveryStatusByTrackingCode('7DB724A65D4');

use SteadFast\SteadFastCourierLaravelPackage\Enums\DeliveryStatus;

$status = DeliveryStatus::fromResponse(
    SteadfastCourier::checkDeliveryStatusByConsignmentId($consignmentId)
);

if ($status?->isFinal()) {
    // Settled and the balance is updated. Stop polling.
}

if ($status?->isAwaitingApproval()) {
    // Outcome recorded, waiting on a Steadfast admin. The balance has not moved.
}

if ($status?->needsSupport()) {
    // Steadfast cannot classify this parcel. A human needs to look at it.
}

echo $status?->label(); // "Partially delivered and balance updated"

DeliveryStatus::fromResponse(SteadfastCourier::placeOrder($order));              // InReview
DeliveryStatus::fromResponse(SteadfastCourier::checkDeliveryStatusByInvoiceId($i)); // Delivered
DeliveryStatus::fromResponse($webhookPayload);                                   // Cancelled

$response = SteadfastCourier::getTrackingsByInvoice('INV-1001');

foreach ($response['tracking'] as $event) {
    echo $event['text']; // "Consignment created by Sender(API)."
}

$response = SteadfastCourier::createReturnRequest([
    'consignment_id' => 160757328,
    'reason' => 'Received a damaged or broken item',
]);

$all = SteadfastCourier::getReturnRequests();
$one = SteadfastCourier::getReturnRequest(1);

$response = SteadfastCourier::createPickupRequest([
    'address_id' => 5,
    'police_station_id' => 92,
    'address' => 'Dhanmondi, Zigatola 2/a',
    'contact_number' => '01722743076',
    'note' => 'Ten small parcels, ready after 3pm.',
    'estim_qty' => 12,
]);

$response = SteadfastCourier::getCurrentBalance();

$all = SteadfastCourier::getPayments();
$one = SteadfastCourier::getPayment('SFC-23618228');

$response = SteadfastCourier::getPoliceStations();

foreach ($response['data'] as $district) {
    echo $district['name']; // "Bagerhat"

    foreach ($district['policestations'] as $station) {
        echo "{$station['id']}: {$station['name']}";
    }
}

$response = SteadfastCourier::fraudCheck('01309055966');

$response = SteadfastCourier::withConfig(
    $tenant->steadfast_api_key,
    $tenant->steadfast_secret_key,
    'https://portal.packzy.com/api/v1', // optional, falls back to config
)->getCurrentBalance();

$tenantA = SteadfastCourier::withConfig($a->api_key, $a->secret_key);
$tenantB = SteadfastCourier::withConfig($b->api_key, $b->secret_key);

$tenantA->placeOrder($order); // always tenant A
$tenantB->placeOrder($order); // always tenant B
SteadfastCourier::getCurrentBalance(); // still the credentials from config

$response = SteadfastCourier::placeOrder($order);

if ($response['status'] !== 200) {
    Log::warning('Steadfast rejected the order', ['message' => $response['message']]);
}

use Illuminate\Http\Client\ConnectionException;
use SteadFast\SteadFastCourierLaravelPackage\Exceptions\InvalidRequestException;
use SteadFast\SteadFastCourierLaravelPackage\Exceptions\SteadfastCourierException;

try {
    $response = SteadfastCourier::getCurrentBalance();
} catch (InvalidRequestException $e) {
    // A bug in the calling code: bad identifier, oversized batch, missing reference.
    report($e);
} catch (SteadfastCourierException $e) {
    Log::error('Unreadable response from Steadfast', [
        'status' => $e->status(),
        'body' => $e->response()?->body(),
    ]);
} catch (ConnectionException $e) {
    Log::error('Steadfast unreachable', ['message' => $e->getMessage()]);
}

try {
    SteadfastCourier::placeOrder($order);
} catch (ConnectionException $e) {
    // Do not simply retry. Find out whether it was created first.
    $existing = SteadfastCourier::checkDeliveryStatusByInvoiceId($order['invoice']);
}

Route::post('/steadfast-webhook', [SteadFastWebhookController::class, 'handle']);

use Illuminate\Http\Request;

class SteadFastWebhookController extends Controller
{
    public function handle(Request $request)
    {
        $expected = config('steadfast.webhook_bearer_token');

        // Never authenticate against an unset token. If $expected were empty, the
        // comparison below would collapse to 'Bearer ', which any caller can send.
        if (blank($expected)) {
            return response()->json(['error' => 'Webhook token is not configured'], 500);
        }

        $token = $request->header('Authorization');

        // hash_equals compares in constant time, so the token cannot be recovered
        // by measuring how long the comparison takes.
        if (! is_string($token) || ! hash_equals('Bearer '.$expected, $token)) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        $payload = $request->validate([
            'consignment_id' => ['

$status = DeliveryStatus::tryFrom($payload['status']);

if ($status?->isFinal()) {
    $order->markSettled($status);
}

// 1.x
foreach (SteadfastCourier::bulkCreateOrders($orders) as $consignment) { }

// 2.0
foreach (SteadfastCourier::bulkCreateOrders($orders)['data'] as $consignment) { }
bash
php artisan vendor:publish --tag="steadfast-config"
bash
php artisan vendor:publish --tag="steadfast-config"
bash
php artisan vendor:publish --tag="steadfast-config" --force