PHP code example of pralhadstha / nepalcan-laravel

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


use OmniCargo\NepalCan\Laravel\Facades\NepalCan;

$order = NepalCan::shipments()->create([
    'receiver_name' => 'Ram Shrestha',
    'receiver_phone' => '9801234567',
    'receiver_address' => 'Kathmandu',
    'product_name' => 'Electronics',
    'cod_charge' => '1500',
    'quantity' => 1,
]);

echo $order->orderId;

// By order ID
$statuses = NepalCan::tracking()->getStatusHistory(12345);

// By tracking ID
$detail = NepalCan::tracking()->track('NCM-123456');
echo $detail->lastDeliveryStatus;

// Bulk status check
$bulk = NepalCan::tracking()->getBulkStatuses([12345, 67890]);

use OmniCargo\NepalCan\Services\RateService;

$rate = NepalCan::rates()->calculate('Kathmandu', 'Pokhara');
echo $rate->charge;

// Specify delivery type
$rate = NepalCan::rates()->calculate(
    'Kathmandu',
    'Pokhara',
    RateService::TYPE_D2B, // Door to Branch
);

$branches = NepalCan::branches()->list();

foreach ($branches as $branch) {
    echo "{$branch->name} - {$branch->district}";
}

use OmniCargo\NepalCan\Services\TicketService;

// Create a ticket
$ticket = NepalCan::tickets()->create(
    TicketService::TYPE_GENERAL,
    'Need help with order #12345',
);

// Request COD transfer
$ticket = NepalCan::tickets()->createCodTransfer(
    bankName: 'Nepal Bank',
    accountName: 'Ram Shrestha',
    accountNumber: '1234567890',
);

// Close a ticket
NepalCan::tickets()->close($ticket->ticketId);

$result = NepalCan::staff()->list(search: 'ram', page: 1, pageSize: 10);

foreach ($result['results'] as $staff) {
    echo "{$staff->name} - {$staff->email}";
}

use OmniCargo\NepalCan\Client;

class ShippingController extends Controller
{
    public function __construct(private readonly Client $client)
    {
    }

    public function show(int $orderId)
    {
        $order = $this->client->shipments->find($orderId);
        $history = $this->client->tracking->getStatusHistory($orderId);

        return view('shipping.show', compact('order', 'history'));
    }
}

// app/Http/Middleware/VerifyCsrfToken.php
protected $except = [
    'nepalcan/webhook',
];

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'nepalcan/webhook',
    ]);
})

use OmniCargo\NepalCan\Laravel\Events\DeliveryCompleted;
use OmniCargo\NepalCan\Laravel\Events\NepalCanWebhookReceived;

// Listen for a specific event
Event::listen(DeliveryCompleted::class, function (DeliveryCompleted $event) {
    $orderId = $event->webhook->orderId;
    // Update your order status, notify customer, etc.
});

// Listen for ALL webhook events
Event::listen(NepalCanWebhookReceived::class, function (NepalCanWebhookReceived $event) {
    Log::info("NCM webhook: {$event->webhook->event}", [
        'order_id' => $event->webhook->orderId,
        'status' => $event->webhook->status,
    ]);
});
bash
php artisan vendor:publish --tag=nepalcan-config