<?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);
}
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"
$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']);
}
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) { }