PHP code example of devmapr / laravel-shipping

1. Go to this page and download the library: Download devmapr/laravel-shipping 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/ */

    

devmapr / laravel-shipping example snippets


use Planx\Shipping\Facades\Shipping;

// Resolve a driver by name
$shipping = Shipping::driver('tinex');

// Submit an order (driver-specific payload)
$result = $shipping->submit($order, [
    'parcel_size_id' => 3,
    'is_self_pickup' => true,
]);

$sizes = \Planx\Shipping\Drivers\TinexDriver::getSizeOptions();
// [1 => 'بسته سایز 1 (10×15×10)', 2 => 'بسته سایز 2 (15×20×10)', ...]



namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Planx\Shipping\Drivers\SnappBoxDriver;

class SnappBoxWebhookController extends Controller
{
    public function __construct(
        private readonly SnappBoxDriver $driver,
    ) {}

    public function __invoke(Request $request, string $webhookType): JsonResponse
    {
        $payload = $request->json()->all();

        if ( ! is_array($payload)) {
            return response()->json(['success' => false, 'message' => 'Invalid JSON'], 400);
        }

        try {
            $result = $this->driver->handleWebhook($webhookType, $payload);
            return response()->json($result);
        } catch (\Throwable $e) {
            return response()->json(['success' => false, 'message' => 'Internal error'], 200);
        }
    }
}

// routes/api.php
Route::post('snappboxwebhooks/{webhookType}', SnappBoxWebhookController::class);

use Planx\Shipping\Contracts\ShippingDriver;

class MyCourierDriver implements ShippingDriver
{
    public function createParcel(array $payload): array { /* ... */ }
    public function updateParcel(int|string $id, array $payload): array { /* ... */ }
    public function getParcel(int|string $id): array { /* ... */ }
    public function deleteParcel(int|string $id): array { /* ... */ }
    public function getDays(): array { /* ... */ }
    public function getDaysIndex(): array { /* ... */ }
}

'drivers' => [
    'mycourier' => [
        'class' => \App\Shipping\MyCourierDriver::class,
        'config' => [
            'api_key' => env('MYCOURIER_API_KEY'),
        ],
    ],
],

Shipping::driver('mycourier')->createParcel([...]);
bash
php artisan vendor:publish --tag=shipping-config

src/
├── Contracts/
│   └── ShippingDriver.php          # Driver interface
├── Drivers/
│   ├── AlonomicDriver.php          # Alopeyk integration
│   ├── ForwardDriver.php           # Forward integration
│   ├── SnappBoxDriver.php          # SnappBox + webhooks
│   └── TinexDriver.php             # Next One (Tinex) integration
├── Facades/
│   └── Shipping.php                # Facade for ShippingManager
├── Providers/
│   └── ShippingServiceProvider.php # Auto-discovery service provider
└── ShippingManager.php             # Driver resolution & management