PHP code example of pralhadstha / nepal-can-php-sdk
1. Go to this page and download the library: Download pralhadstha/nepal-can-php-sdk 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/ */
pralhadstha / nepal-can-php-sdk example snippets
use OmniCargo\NepalCan\Client;
use OmniCargo\NepalCan\Config;
// Sandbox environment (default)
$client = new Client('your-api-token');
// Production environment
$client = new Client('your-api-token', Config::BASE_URL_PRODUCTION);
// Get comments for an order
$comments = $client->shipments->getComments(134);
foreach ($comments as $comment) {
echo $comment->comments;
echo $comment->addedBy;
}
// Get last 25 bulk comments
$comments = $client->shipments->getBulkComments();
// Add a comment
$client->shipments->addComment(1234567, 'Test comment from api');
// Get status history for an order
$statuses = $client->tracking->getStatusHistory(134);
foreach ($statuses as $status) {
echo $status->status;
echo $status->addedTime;
}
// Track by tracking ID
$tracking = $client->tracking->track('8D634706B3394C3');
echo $tracking->trackId;
echo $tracking->lastDeliveryStatus;
echo $tracking->receiver;
echo $tracking->destination;
foreach ($tracking->statusHistory as $status) {
echo $status; // "Pickup Order Created - 2026-03-23 04:43 AM"
}
// Bulk status check
$result = $client->tracking->getBulkStatuses([4041, 3855, 4032]);
// $result['result'] => ['4041' => 'Pickup Order Created', ...]
// $result['errors'] => [4042, ...] (invalid order IDs)
// Return an order
$client->shipments->returnOrder(4041, 'Customer refused the delivery');
// Create exchange order
$response = $client->shipments->createExchange(4041);
// $response['cust_order'] => new delivery order ID
// $response['ven_order'] => return order ID
// Redirect an order
$client->shipments->redirect(4041, [
'name' => 'Jane Doe',
'phone' => '9800000000',
'address' => 'New Address',
]);
use OmniCargo\NepalCan\Services\TicketService;
// Create a general ticket
$ticket = $client->tickets->create(TicketService::TYPE_GENERAL, 'Please arrange delivery at the earliest');
echo $ticket->ticketId;
// Create COD transfer ticket
$ticket = $client->tickets->createCodTransfer('Nepal Bank Limited', 'John Doe', '1234567890');
// Close a ticket
$client->tickets->close(123);
use OmniCargo\NepalCan\Webhooks\EventDispatcher;
use OmniCargo\NepalCan\Webhooks\WebhookEvent;
use OmniCargo\NepalCan\Webhooks\WebhookHandlerInterface;
use OmniCargo\NepalCan\Resources\Webhook;
// Create a handler
class DeliveryCompletedHandler implements WebhookHandlerInterface
{
public function handle(Webhook $webhook): void
{
// Update order status in your system
}
}
// Register handlers and dispatch
$dispatcher = new EventDispatcher();
$dispatcher
->subscribe(WebhookEvent::DELIVERY_COMPLETED, new DeliveryCompletedHandler())
->subscribe(WebhookEvent::ORDER_DISPATCHED, new OrderDispatchedHandler());
$webhook = $client->webhooks->parse($payload);
$dispatcher->dispatch($webhook);
class DeliveryCompletedHandler implements WebhookHandlerInterface
{
public function handle(Webhook $webhook): void
{
// Create a unique key from the webhook data
$idempotencyKey = $webhook->orderId . ':' . $webhook->event . ':' . $webhook->timestamp;
// Skip if already processed
if (ProcessedWebhook::where('idempotency_key', $idempotencyKey)->exists()) {
return;
}
// Process the webhook
Order::where('ncm_order_id', $webhook->orderId)
->update(['status' => $webhook->status]);
// Mark as processed
ProcessedWebhook::create(['idempotency_key' => $idempotencyKey]);
}
}
use OmniCargo\NepalCan\Exceptions\ApiException;
use OmniCargo\NepalCan\Exceptions\AuthenticationException;
use OmniCargo\NepalCan\Exceptions\ValidationException;
use OmniCargo\NepalCan\Exceptions\NotFoundException;
try {
$order = $client->shipments->find(99999);
} catch (AuthenticationException $e) {
// Invalid or missing API token (401)
} catch (ValidationException $e) {
// Invalid parameters (400)
$errors = $e->getErrors();
} catch (NotFoundException $e) {
// Order not found (404)
} catch (ApiException $e) {
// Other API errors
$statusCode = $e->getStatusCode();
$errorBody = $e->getErrorBody();
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.