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

1. Go to this page and download the library: Download steadfast-courier/steadfast-courier-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-courier-laravel-package example snippets


 "base_url" => env('STEADFAST_BASE_URL', 'https://portal.steadfast.com.bd/api/v1'),
 "api_key" => env('STEADFAST_API_KEY', 'your-api-key'),
 "secret_key" => env('STEADFAST_SECRET_KEY', 'your-secret-key'),
 "webhook_bearer_token" => env('STEADFAST_BEARER_TOKEN', 'your-generated-token'),

$orderData = [
    'invoice' => '123456',
    'recipient_name' => 'John Doe',
    'recipient_phone' => '01234567890',
    'recipient_address' => 'Fla# A1,House# 17/1, Road# 3/A, Dhanmondi,Dhaka-1209',
    'cod_amount' => 1000,
    'note' => 'Handle with care'
];

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

$ordersData =
[
    [
        'invoice' => '123456',
        'recipient_name' => 'John Doe',
        'recipient_phone' => '01234567890',
        'recipient_address' => '123 Main St',
        'cod_amount' => 1000,
        'note' => 'Handle with care'
    ],[
        'invoice' => '789012',
        'recipient_name' => 'Jane Smith',
        'recipient_phone' => '09876543210',
        'recipient_address' => '456 Elm St',
        'cod_amount' => 1500,
        'note' => 'Fragile'
    ]
];

$response = SteadfastCourier::bulkCreateOrders($ordersData);


// Way 1: Check Delivery Status using Consignment ID
$consignmentId = 123456;
$response1 = SteadfastCourier::checkDeliveryStatusByConsignmentId($consignmentId);

// Way 2: Check Delivery Status using Invoice ID
$invoice = "230822-1";
$response3 = SteadfastCourier::checkDeliveryStatusByInvoiceId($invoice);

// Way 3: Check Delivery Status using Tracking Code
$trackingCode = "B025A3FA";
$response2 = SteadfastCourier::checkDeliveryStatusByTrackingCode($trackingCode);


[
    pending,
    delivered_approval_pending,
    partial_delivered_approval_pending,
    cancelled_approval_pending,
    unknown_approval_pending,
    delivered,
    partial_delivered,
    cancelled,
    hold,
    in_review,
    unknown
]

$response = SteadfastCourier::getCurrentBalance();

Route::post('/staedfast-webhook', [SteadFastWebhookController::class, 'handleSteadFastWebhook']);

public function handleSteadFastWebhook(Request $request)
{
    $payload = $request->all();
    $token = $request->header('Authorization');

    // Check if Bearer token is valid
    if ($token !== 'Bearer '. config('steadfast-courier.webhook_bearer_token')) {
        return response()->json(['error' => 'Unauthorized'], 401);
    }

    try {
        $this->validatePayload($payload);
        $this->processPayload($payload);

        return response()->json(['status' => 'success'], 200);
    } catch (\Throwable $th) {
        return response()->json(['error' => $th->getMessage()], 400);
    }
    

    return response()->json(['message' => 'Webhook received'], 200);
}

private function validatePayload($payload)
{
    $properties = [
        'consignment_id',
        'invoice',
        'status',
        'cod_amount',
        'updated_at',
    ];
    $missingProperties = array_diff($properties, array_keys($payload));
    if ($missingProperties) {
        abort(400, 'Missing 
bash
php artisan vendor:publish --tag="steadfast-courier-config"
json
[
    {
        "invoice": "230822-1",
        "recipient_name": "John Doe",
        "recipient_address": "House 44, Road 2/A, Dhanmondi, Dhaka 1209",
        "recipient_phone": "0171111111",
        "cod_amount": "0.00",
        "note": null,
        "consignment_id": null,
        "tracking_code": null,
        "status": "error"
    },
]